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