Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import simpleaudio as sa
- SAMPLE_RATE = 8000
- def get_sound(note, duration, volume=0.5):
- time_vector = np.linspace(0, duration, int(duration * SAMPLE_RATE), False)
- # frequency = 440 * 2 ** ((note - 49) / 12) # 440Hz is key 49 on a piano
- frequency = 220 * 2 ** (note / 48)
- # Generate audio samples
- audio = np.sin(frequency * time_vector * 2 * np.pi)
- # normalize to 16-bit range
- audio *= volume * 32767 / np.max(np.abs(audio))
- # Fade out the end
- release_time = 0.05 # Seconds
- if duration >= release_time:
- release_samples = int(np.ceil(release_time * SAMPLE_RATE))
- fade_curve = np.linspace(volume, 0.0, num=release_samples)
- audio[-release_samples:] *= fade_curve
- # convert to 16-bit data
- audio = audio.astype(np.int16)
- return audio
- ##scale = [get_sound(note, 0.5) for note in [49, 51, 52, 54, 56, 57, 59, 61]]
- ##for note in scale:
- ## wave_object = sa.WaveObject(note, 1, 2, SAMPLE_RATE)
- ## play_object = wave_object.play()
- ## play_object.wait_done()
- blips = [get_sound(i, 0.1) for i in range(48, 92, 1)]
- for blip in blips:
- wave_object = sa.WaveObject(blip, 1, 2, SAMPLE_RATE)
- play_object = wave_object.play()
- play_object.wait_done()
- blips = [get_sound(i, 0.1) for i in range(48, 92, 1)]
- for blip in blips[::-1]:
- wave_object = sa.WaveObject(blip, 1, 2, SAMPLE_RATE)
- play_object = wave_object.play()
- play_object.wait_done()
Advertisement
Add Comment
Please, Sign In to add comment