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.

PaEngine.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. PaEngine.cpp - Audio output for PortAudio
  4. Copyright (C) 2002 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. */
  11. #include "PaEngine.h"
  12. #include <iostream>
  13. using namespace std;
  14. namespace zyncarla {
  15. PaEngine::PaEngine(const SYNTH_T &synth)
  16. :AudioOut(synth), stream(NULL)
  17. {
  18. name = "PA";
  19. }
  20. PaEngine::~PaEngine()
  21. {
  22. Stop();
  23. }
  24. bool PaEngine::Start()
  25. {
  26. if(getAudioEn())
  27. return true;
  28. Pa_Initialize();
  29. PaStreamParameters outputParameters;
  30. outputParameters.device = Pa_GetDefaultOutputDevice();
  31. if(outputParameters.device == paNoDevice) {
  32. cerr << "Error: No default output device." << endl;
  33. Pa_Terminate();
  34. return false;
  35. }
  36. outputParameters.channelCount = 2; /* stereo output */
  37. outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
  38. outputParameters.suggestedLatency =
  39. Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
  40. outputParameters.hostApiSpecificStreamInfo = NULL;
  41. Pa_OpenStream(&stream,
  42. NULL,
  43. &outputParameters,
  44. synth.samplerate,
  45. synth.buffersize,
  46. 0,
  47. PAprocess,
  48. (void *) this);
  49. Pa_StartStream(stream);
  50. return true;
  51. }
  52. void PaEngine::setAudioEn(bool nval)
  53. {
  54. if(nval)
  55. Start();
  56. else
  57. Stop();
  58. }
  59. bool PaEngine::getAudioEn() const
  60. {
  61. return stream;
  62. }
  63. int PaEngine::PAprocess(const void *inputBuffer,
  64. void *outputBuffer,
  65. unsigned long framesPerBuffer,
  66. const PaStreamCallbackTimeInfo *outTime,
  67. PaStreamCallbackFlags flags,
  68. void *userData)
  69. {
  70. (void) inputBuffer;
  71. (void) outTime;
  72. (void) flags;
  73. return static_cast<PaEngine *>(userData)->process((float *) outputBuffer,
  74. framesPerBuffer);
  75. }
  76. int PaEngine::process(float *out, unsigned long framesPerBuffer)
  77. {
  78. const Stereo<float *> smp = getNext();
  79. for(unsigned i = 0; i < framesPerBuffer; ++i) {
  80. *out++ = smp.l[i];
  81. *out++ = smp.r[i];
  82. }
  83. return 0;
  84. }
  85. void PaEngine::Stop()
  86. {
  87. if(!getAudioEn())
  88. return;
  89. Pa_StopStream(stream);
  90. Pa_CloseStream(stream);
  91. stream = NULL;
  92. Pa_Terminate();
  93. }
  94. }