Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PyRtAudioTest.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import rtaudio as rt
  2. from math import cos
  3. import struct
  4. class audio_generator:
  5. def __init__(self):
  6. self.idx = -1
  7. self.freq = 440.
  8. def __call__(self):
  9. self.idx += 1
  10. if self.idx%48000 == 0:
  11. self.freq *= 2**(1/12.)
  12. return 0.5*cos(2.*3.1416*self.freq*self.idx/48000.)
  13. class callback:
  14. def __init__(self, gen):
  15. self.gen = gen
  16. self.i = 0
  17. def __call__(self,playback, capture):
  18. [struct.pack_into("f", playback, 4*o, self.gen()) for o in xrange(256)]
  19. self.i = self.i + 256
  20. if self.i > 48000*10:
  21. print '.'
  22. return 1
  23. dac = rt.RtAudio()
  24. n = dac.getDeviceCount()
  25. print 'Number of devices available: ', n
  26. for i in range(n):
  27. try:
  28. print dac.getDeviceInfo(i)
  29. except rt.RtError as e:
  30. print e
  31. print 'Default output device: ', dac.getDefaultOutputDevice()
  32. print 'Default input device: ', dac.getDefaultInputDevice()
  33. print 'is stream open: ', dac.isStreamOpen()
  34. print 'is stream running: ', dac.isStreamRunning()
  35. oParams = {'deviceId': 1, 'nChannels': 1, 'firstChannel': 0}
  36. iParams = {'deviceId': 1, 'nChannels': 1, 'firstChannel': 0}
  37. try:
  38. dac.openStream(oParams,oParams,48000,256,callback(audio_generator()) )
  39. except rt.RtError as e:
  40. print e
  41. else:
  42. dac.startStream()
  43. import time
  44. print 'latency: ', dac.getStreamLatency()
  45. while (dac.isStreamRunning()):
  46. time.sleep(0.1)
  47. print dac.getStreamTime()
  48. dac.stopStream()
  49. dac.abortStream()
  50. dac.closeStream()