Guest User

Untitled

a guest
Apr 15th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import numpy as np
  2. import simpleaudio as sa
  3.  
  4. SAMPLE_RATE = 8000
  5.  
  6.  
  7. def get_sound(note, duration, volume=0.5):
  8. time_vector = np.linspace(0, duration, int(duration * SAMPLE_RATE), False)
  9. # frequency = 440 * 2 ** ((note - 49) / 12) # 440Hz is key 49 on a piano
  10. frequency = 220 * 2 ** (note / 48)
  11. # Generate audio samples
  12. audio = np.sin(frequency * time_vector * 2 * np.pi)
  13. # normalize to 16-bit range
  14. audio *= volume * 32767 / np.max(np.abs(audio))
  15.  
  16. # Fade out the end
  17. release_time = 0.05 # Seconds
  18. if duration >= release_time:
  19. release_samples = int(np.ceil(release_time * SAMPLE_RATE))
  20. fade_curve = np.linspace(volume, 0.0, num=release_samples)
  21. audio[-release_samples:] *= fade_curve
  22.  
  23. # convert to 16-bit data
  24. audio = audio.astype(np.int16)
  25. return audio
  26.  
  27.  
  28. ##scale = [get_sound(note, 0.5) for note in [49, 51, 52, 54, 56, 57, 59, 61]]
  29. ##for note in scale:
  30. ## wave_object = sa.WaveObject(note, 1, 2, SAMPLE_RATE)
  31. ## play_object = wave_object.play()
  32. ## play_object.wait_done()
  33.  
  34. blips = [get_sound(i, 0.1) for i in range(48, 92, 1)]
  35.  
  36. for blip in blips:
  37. wave_object = sa.WaveObject(blip, 1, 2, SAMPLE_RATE)
  38. play_object = wave_object.play()
  39. play_object.wait_done()
  40.  
  41. blips = [get_sound(i, 0.1) for i in range(48, 92, 1)]
  42.  
  43. for blip in blips[::-1]:
  44. wave_object = sa.WaveObject(blip, 1, 2, SAMPLE_RATE)
  45. play_object = wave_object.play()
  46. play_object.wait_done()
Advertisement
Add Comment
Please, Sign In to add comment