Skip to content

Commit 962a3e7

Browse files
Merge pull request #2384 from andoriyaprashant/branch10
Nature Sound Generator Script Added
2 parents c77e642 + 971a717 commit 962a3e7

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

Nature Sound Generator/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Nature Sounds Generator
2+
3+
The Nature Sounds Generator is a Python script that generates random, realistic nature sounds, such as raindrops, rustling leaves, and chirping birds. The script utilizes the `PyAudio` library to play the generated sounds, creating an immersive audio experience.
4+
5+
## Requirements
6+
7+
- Python 3.x
8+
- PyAudio library
9+
10+
You can install the required library using the following command:
11+
12+
``` bash
13+
pip install pyaudio
14+
```
15+
16+
## Features
17+
18+
1. Sound Variation: Introduces slight variations in frequency and duration for each sound, making the generated sounds more natural and less repetitive.
19+
20+
2. Multiple Channels: Plays different sounds simultaneously by utilizing multiple channels of PyAudio, creating a more immersive audio experience.
21+
22+
3. Volume Control: Implements volume control for each sound, allowing you to adjust the loudness of individual nature sounds.
23+
24+
4. Random Delays: Adds random delays between the sounds to create a more realistic environment.
25+
26+
5. User Input: Allows the user to choose specific sounds they want to hear or select a combination of sounds to play.
27+
28+
## Usage
29+
30+
1. Clone this repository to your local machine.
31+
32+
2. Navigate to the project folder:
33+
34+
``` bash
35+
cd nature-sounds-generator
36+
```
37+
38+
3. Run the script:
39+
40+
``` bash
41+
nature_sounds_generator.py
42+
```
43+
44+
4. Follow the instructions on the console to select the sounds you want to hear. You can enter specific sound names (e.g., "raindrops") or choose to hear all sounds by typing "all."
45+
46+
5. Enjoy the random nature sounds with sound variations, multiple channels, and random delays!
47+
48+
## Customization
49+
50+
You can customize the behavior of the Nature Sounds Generator by modifying the following variables in the `nature_sounds_generator.py` file:
51+
52+
- `num_channels`: Number of simultaneous sound channels to play.
53+
- `channel_volume`: Volume for each channel (adjust as needed).
54+
- Sound Characteristics: Modify the `sounds` dictionary in the script to change the characteristics of each sound. You can adjust the minimum and maximum frequency and duration for each sound.
55+
56+
## Note
57+
58+
This script generates simple tones to represent nature sounds and may not sound entirely realistic. For more realistic results, more advanced audio synthesis techniques or pre-recorded audio samples are required.
59+
60+
61+
62+
63+
64+
65+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pyaudio
2+
numpy
3+
random

0 commit comments

Comments
 (0)