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.

72 lines
1.6KB

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