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