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.

119 lines
3.0KB

  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 modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "PaEngine.h"
  18. #include <iostream>
  19. using namespace std;
  20. PaEngine::PaEngine()
  21. :stream(NULL)
  22. {
  23. name = "PA";
  24. }
  25. PaEngine::~PaEngine()
  26. {
  27. Stop();
  28. }
  29. bool PaEngine::Start()
  30. {
  31. if(getAudioEn())
  32. return true;
  33. Pa_Initialize();
  34. PaStreamParameters outputParameters;
  35. outputParameters.device = Pa_GetDefaultOutputDevice();
  36. if(outputParameters.device == paNoDevice) {
  37. cerr << "Error: No default output device." << endl;
  38. Pa_Terminate();
  39. return false;
  40. }
  41. outputParameters.channelCount = 2; /* stereo output */
  42. outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
  43. outputParameters.suggestedLatency =
  44. Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
  45. outputParameters.hostApiSpecificStreamInfo = NULL;
  46. Pa_OpenStream(&stream,
  47. NULL,
  48. &outputParameters,
  49. synth->samplerate,
  50. synth->buffersize,
  51. 0,
  52. PAprocess,
  53. (void *) this);
  54. Pa_StartStream(stream);
  55. return true;
  56. }
  57. void PaEngine::setAudioEn(bool nval)
  58. {
  59. if(nval)
  60. Start();
  61. else
  62. Stop();
  63. }
  64. bool PaEngine::getAudioEn() const
  65. {
  66. return stream;
  67. }
  68. int PaEngine::PAprocess(const void *inputBuffer,
  69. void *outputBuffer,
  70. unsigned long framesPerBuffer,
  71. const PaStreamCallbackTimeInfo *outTime,
  72. PaStreamCallbackFlags flags,
  73. void *userData)
  74. {
  75. (void) inputBuffer;
  76. (void) outTime;
  77. (void) flags;
  78. return static_cast<PaEngine *>(userData)->process((float *) outputBuffer,
  79. framesPerBuffer);
  80. }
  81. int PaEngine::process(float *out, unsigned long framesPerBuffer)
  82. {
  83. const Stereo<float *> smp = getNext();
  84. for(unsigned i = 0; i < framesPerBuffer; ++i) {
  85. *out++ = smp.l[i];
  86. *out++ = smp.r[i];
  87. }
  88. return 0;
  89. }
  90. void PaEngine::Stop()
  91. {
  92. if(!getAudioEn())
  93. return;
  94. Pa_StopStream(stream);
  95. Pa_CloseStream(stream);
  96. stream = NULL;
  97. Pa_Terminate();
  98. }