skip420

sound.py

Jul 23rd, 2021
1,072
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #sudo apt install python-pyaudio
  2. #low_HIGH_FREQUENCY
  3. #_#FreeNode_@shark
  4.  
  5. import pyaudio
  6. import numpy as np
  7.  
  8. p = pyaudio.PyAudio()
  9.  
  10. volume = 0.5    # range [0.0, 1.0]
  11. fs = 100000       # sampling rate, Hz, must be integer
  12. duration = 50.0   # in seconds, may be float
  13. f = 20        # sine frequency, Hz, may be float
  14.  
  15. # generate samples, note conversion to float32 array
  16. samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
  17.  
  18. # for paFloat32 sample values must be in range [-1.0, 1.0]
  19. stream = p.open(format=pyaudio.paFloat32,
  20.                 channels=1,
  21.                 rate=fs,
  22.                 output=True)
  23.  
  24. # play. May repeat with different volume values (if done interactively)
  25. stream.write(volume*samples)
  26.  
  27. stream.stop_stream()
  28. stream.close()
  29.  
  30. p.terminate()
  31.  
RAW Paste Data