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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. PaEngine::PaEngine(const SYNTH_T &synth)
  15. :AudioOut(synth), stream(NULL)
  16. {
  17. name = "PA";
  18. }
  19. PaEngine::~PaEngine()
  20. {
  21. Stop();
  22. }
  23. bool PaEngine::Start()
  24. {
  25. if(getAudioEn())
  26. return true;
  27. Pa_Initialize();
  28. PaStreamParameters outputParameters;
  29. outputParameters.device = Pa_GetDefaultOutputDevice();
  30. if(outputParameters.device == paNoDevice) {
  31. cerr << "Error: No default output device." << endl;
  32. Pa_Terminate();
  33. return false;
  34. }
  35. outputParameters.channelCount = 2; /* stereo output */
  36. outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
  37. outputParameters.suggestedLatency =
  38. Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
  39. outputParameters.hostApiSpecificStreamInfo = NULL;
  40. Pa_OpenStream(&stream,
  41. NULL,
  42. &outputParameters,
  43. synth.samplerate,
  44. synth.buffersize,
  45. 0,
  46. PAprocess,
  47. (void *) this);
  48. Pa_StartStream(stream);
  49. return true;
  50. }
  51. void PaEngine::setAudioEn(bool nval)
  52. {
  53. if(nval)
  54. Start();
  55. else
  56. Stop();
  57. }
  58. bool PaEngine::getAudioEn() const
  59. {
  60. return stream;
  61. }
  62. int PaEngine::PAprocess(const void *inputBuffer,
  63. void *outputBuffer,
  64. unsigned long framesPerBuffer,
  65. const PaStreamCallbackTimeInfo *outTime,
  66. PaStreamCallbackFlags flags,
  67. void *userData)
  68. {
  69. (void) inputBuffer;
  70. (void) outTime;
  71. (void) flags;
  72. return static_cast<PaEngine *>(userData)->process((float *) outputBuffer,
  73. framesPerBuffer);
  74. }
  75. int PaEngine::process(float *out, unsigned long framesPerBuffer)
  76. {
  77. const Stereo<float *> smp = getNext();
  78. for(unsigned i = 0; i < framesPerBuffer; ++i) {
  79. *out++ = smp.l[i];
  80. *out++ = smp.r[i];
  81. }
  82. return 0;
  83. }
  84. void PaEngine::Stop()
  85. {
  86. if(!getAudioEn())
  87. return;
  88. Pa_StopStream(stream);
  89. Pa_CloseStream(stream);
  90. stream = NULL;
  91. Pa_Terminate();
  92. }