PortAudioSink.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "PortAudioSink.h"
  2. PortAudioSink::PortAudioSink() {
  3. Pa_Initialize();
  4. this->setParams(44100, 2, 16);
  5. }
  6. bool PortAudioSink::setParams(uint32_t sampleRate, uint8_t channelCount,
  7. uint8_t bitDepth) {
  8. if (stream) {
  9. Pa_StopStream(stream);
  10. }
  11. PaStreamParameters outputParameters;
  12. outputParameters.device = Pa_GetDefaultOutputDevice();
  13. if (outputParameters.device == paNoDevice) {
  14. printf("PortAudio: Default audio device not found!\n");
  15. // exit(0);
  16. }
  17. printf("PortAudio: Default audio device not found!\n");
  18. outputParameters.channelCount = channelCount;
  19. switch (bitDepth) {
  20. case 32:
  21. outputParameters.sampleFormat = paInt32;
  22. break;
  23. case 24:
  24. outputParameters.sampleFormat = paInt24;
  25. break;
  26. case 16:
  27. outputParameters.sampleFormat = paInt16;
  28. break;
  29. case 8:
  30. outputParameters.sampleFormat = paInt8;
  31. break;
  32. default:
  33. outputParameters.sampleFormat = paInt16;
  34. break;
  35. }
  36. outputParameters.suggestedLatency = 0.050;
  37. outputParameters.hostApiSpecificStreamInfo = NULL;
  38. PaError err = Pa_OpenStream(&stream, NULL, &outputParameters, sampleRate,
  39. 4096 / (channelCount * bitDepth / 8), paClipOff,
  40. NULL, // blocking api
  41. NULL);
  42. Pa_StartStream(stream);
  43. return !err;
  44. }
  45. PortAudioSink::~PortAudioSink() {
  46. Pa_StopStream(stream);
  47. Pa_Terminate();
  48. }
  49. void PortAudioSink::feedPCMFrames(const uint8_t* buffer, size_t bytes) {
  50. Pa_WriteStream(stream, buffer, bytes / 4);
  51. }