Guest User

Untitled

a guest
Jul 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. def gen_tone(frequency, duration=5.0, volume=0.5, fs=44100):
  2. p = PyAudio()
  3.  
  4. # Sine wave
  5. # samples = (np.sin(2*np.pi*np.arange(fs*duration)*frequency/fs)).astype(np.float32)
  6.  
  7. # Sawtooth wave. Can also be done with signal.sawtooth.
  8. # samples = (np.modf(np.arange(fs*duration)*frequency/fs)[0]*2.0-1.0).astype(np.float32)
  9.  
  10. # DO NOT USE Triangle wave
  11. # t = np.linspace(0, duration, fs*duration)
  12. # samples = signal.sawtooth(2 * np.pi * t * frequency, width=0.5)
  13.  
  14. stream = p.open(format=paFloat32,
  15. channels=1,
  16. rate=fs,
  17. output=True)
  18.  
  19. stream.write(volume * samples)
  20.  
  21. stream.stop_stream()
  22. stream.close()
  23. p.terminate()
Add Comment
Please, Sign In to add comment