Skip to content

Commit 5cb94e3

Browse files
Merge pull request #2501 from avinashkranjan/deepsource-transform-aad10dbf
format code with autopep8
2 parents eecee7f + 9e9d5aa commit 5cb94e3

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

Nature Sound Generator/nature_sounds_generator.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,64 @@
1010
"chirping_birds": {"min_freq": 1000, "max_freq": 5000, "min_duration": 0.2, "max_duration": 0.4}
1111
}
1212

13+
1314
def play_sound(frequency, duration, volume=1.0):
1415
sample_rate = 44100
1516
t = np.linspace(0, duration, int(sample_rate * duration), False)
1617
audio_data = np.sin(2 * np.pi * frequency * t)
1718
audio_data *= volume
18-
19+
1920
p = pyaudio.PyAudio()
2021
stream = p.open(format=pyaudio.paFloat32,
2122
channels=1,
2223
rate=sample_rate,
2324
output=True)
24-
25+
2526
stream.write(audio_data.tobytes())
2627
stream.stop_stream()
2728
stream.close()
2829
p.terminate()
2930

31+
3032
def main():
3133
num_channels = 3 # Number of simultaneous channels
3234
channel_volume = 0.5 # Volume for each channel (adjust as needed)
33-
35+
3436
while True:
3537
user_input = input("Enter sound(s) you want to hear (comma-separated) "
3638
"or 'all' for all sounds (e.g., 'raindrops,chirping_birds'): ")
37-
39+
3840
if user_input.lower() == 'all':
3941
selected_sounds = list(sounds.keys())
4042
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+
4346
random.shuffle(selected_sounds) # Randomize sound order
44-
47+
4548
for _ in range(num_channels):
4649
if not selected_sounds:
4750
break
48-
51+
4952
sound_choice = selected_sounds.pop()
5053
sound_params = sounds[sound_choice]
51-
54+
5255
# 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+
5661
# Volume variation for each channel
5762
volume = channel_volume + random.uniform(-0.2, 0.2)
5863
volume = max(0.0, min(1.0, volume))
59-
64+
6065
print(f"Playing {sound_choice}...")
6166
play_sound(frequency, duration, volume)
62-
67+
6368
# Random delay between sounds
6469
time.sleep(random.uniform(1, 4))
6570

71+
6672
if __name__ == "__main__":
6773
main()

0 commit comments

Comments
 (0)