forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnature_sounds_generator.py
73 lines (55 loc) · 2.34 KB
/
nature_sounds_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import random
import time
import numpy as np
import pyaudio
# Dictionary to hold sound characteristics
sounds = {
"raindrops": {"min_freq": 100, "max_freq": 300, "min_duration": 0.1, "max_duration": 0.5},
"rustling_leaves": {"min_freq": 500, "max_freq": 1500, "min_duration": 0.2, "max_duration": 0.7},
"chirping_birds": {"min_freq": 1000, "max_freq": 5000, "min_duration": 0.2, "max_duration": 0.4}
}
def play_sound(frequency, duration, volume=1.0):
sample_rate = 44100
t = np.linspace(0, duration, int(sample_rate * duration), False)
audio_data = np.sin(2 * np.pi * frequency * t)
audio_data *= volume
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=sample_rate,
output=True)
stream.write(audio_data.tobytes())
stream.stop_stream()
stream.close()
p.terminate()
def main():
num_channels = 3 # Number of simultaneous channels
channel_volume = 0.5 # Volume for each channel (adjust as needed)
while True:
user_input = input("Enter sound(s) you want to hear (comma-separated) "
"or 'all' for all sounds (e.g., 'raindrops,chirping_birds'): ")
if user_input.lower() == 'all':
selected_sounds = list(sounds.keys())
else:
selected_sounds = [sound.strip()
for sound in user_input.split(",")]
random.shuffle(selected_sounds) # Randomize sound order
for _ in range(num_channels):
if not selected_sounds:
break
sound_choice = selected_sounds.pop()
sound_params = sounds[sound_choice]
# Add slight variations to frequency and duration
frequency = random.uniform(
sound_params["min_freq"], sound_params["max_freq"])
duration = random.uniform(
sound_params["min_duration"], sound_params["max_duration"])
# Volume variation for each channel
volume = channel_volume + random.uniform(-0.2, 0.2)
volume = max(0.0, min(1.0, volume))
print(f"Playing {sound_choice}...")
play_sound(frequency, duration, volume)
# Random delay between sounds
time.sleep(random.uniform(1, 4))
if __name__ == "__main__":
main()