Advertisement
J_Bernon

faire un son avec python

Apr 22nd, 2020
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import pyaudio
  2. import numpy as np
  3.  
  4. p = pyaudio.PyAudio()
  5.  
  6. volume = 1     # range [0.0, 1.0]
  7. fs = 44100       # sampling rate, Hz, must be integer
  8. duration = 10   # in seconds, may be float
  9. f = 440      # sine frequency, Hz, may be float
  10.  
  11. # generate samples, note conversion to float32 array
  12. samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
  13.  
  14. # for paFloat32 sample values must be in range [-1.0, 1.0]
  15. stream = p.open(format=pyaudio.paFloat32,
  16.                 channels=1,
  17.                 rate=fs,
  18.                 output=True)
  19.  
  20. # play. May repeat with different volume values (if done interactively)
  21. stream.write(volume*samples)
  22.  
  23. stream.stop_stream()
  24. stream.close()
  25.  
  26. p.terminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement