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.

WavEngine.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (C) 2006 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License
  6. as published by the Free Software Foundation.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License (version 2) for more details.
  11. You should have received a copy of the GNU General Public License (version 2)
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include "WavEngine.h"
  16. #include <cstdio>
  17. #include <iostream>
  18. #include <cstdlib>
  19. #include "../Misc/WavFile.h"
  20. #include "../Misc/Util.h"
  21. using namespace std;
  22. WavEngine::WavEngine()
  23. :AudioOut(), file(NULL), buffer(synth->samplerate * 4), pThread(NULL)
  24. {
  25. work.init(PTHREAD_PROCESS_PRIVATE, 0);
  26. }
  27. WavEngine::~WavEngine()
  28. {
  29. Stop();
  30. destroyFile();
  31. }
  32. bool WavEngine::openAudio()
  33. {
  34. return file && file->good();
  35. }
  36. bool WavEngine::Start()
  37. {
  38. if(pThread)
  39. return true;
  40. pThread = new pthread_t;
  41. pthread_attr_t attr;
  42. pthread_attr_init(&attr);
  43. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  44. pthread_create(pThread, &attr, _AudioThread, this);
  45. return true;
  46. }
  47. void WavEngine::Stop()
  48. {
  49. if(!pThread)
  50. return;
  51. pthread_t *tmp = pThread;
  52. pThread = NULL;
  53. work.post();
  54. pthread_join(*tmp, NULL);
  55. delete pThread;
  56. }
  57. void WavEngine::push(Stereo<float *> smps, size_t len)
  58. {
  59. if(!pThread)
  60. return;
  61. //copy the input [overflow when needed]
  62. for(size_t i = 0; i < len; ++i) {
  63. buffer.push(*smps.l++);
  64. buffer.push(*smps.r++);
  65. }
  66. work.post();
  67. }
  68. void WavEngine::newFile(WavFile *_file)
  69. {
  70. //ensure system is clean
  71. destroyFile();
  72. file = _file;
  73. //check state
  74. if(!file->good())
  75. cerr
  76. << "ERROR: WavEngine handed bad file output WavEngine::newFile()"
  77. << endl;
  78. }
  79. void WavEngine::destroyFile()
  80. {
  81. if(file)
  82. delete file;
  83. file = NULL;
  84. }
  85. void *WavEngine::_AudioThread(void *arg)
  86. {
  87. return (static_cast<WavEngine *>(arg))->AudioThread();
  88. }
  89. void *WavEngine::AudioThread()
  90. {
  91. short *recordbuf_16bit = new short[2 * synth->buffersize];
  92. while(!work.wait() && pThread) {
  93. for(int i = 0; i < synth->buffersize; ++i) {
  94. float left = 0.0f, right = 0.0f;
  95. buffer.pop(left);
  96. buffer.pop(right);
  97. recordbuf_16bit[2 * i] = limit((int)(left * 32767.0f),
  98. -32768,
  99. 32767);
  100. recordbuf_16bit[2 * i + 1] = limit((int)(right * 32767.0f),
  101. -32768,
  102. 32767);
  103. }
  104. file->writeStereoSamples(synth->buffersize, recordbuf_16bit);
  105. }
  106. delete[] recordbuf_16bit;
  107. return NULL;
  108. }