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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. sem_init(&work, PTHREAD_PROCESS_PRIVATE, 0);
  26. }
  27. WavEngine::~WavEngine()
  28. {
  29. Stop();
  30. sem_destroy(&work);
  31. destroyFile();
  32. }
  33. bool WavEngine::openAudio()
  34. {
  35. return file && file->good();
  36. }
  37. bool WavEngine::Start()
  38. {
  39. if(pThread)
  40. return true;
  41. pThread = new pthread_t;
  42. pthread_attr_t attr;
  43. pthread_attr_init(&attr);
  44. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  45. pthread_create(pThread, &attr, _AudioThread, this);
  46. return true;
  47. }
  48. void WavEngine::Stop()
  49. {
  50. if(!pThread)
  51. return;
  52. pthread_t *tmp = pThread;
  53. pThread = NULL;
  54. sem_post(&work);
  55. pthread_join(*tmp, NULL);
  56. delete pThread;
  57. }
  58. void WavEngine::push(Stereo<float *> smps, size_t len)
  59. {
  60. if(!pThread)
  61. return;
  62. //copy the input [overflow when needed]
  63. for(size_t i = 0; i < len; ++i) {
  64. buffer.push(*smps.l++);
  65. buffer.push(*smps.r++);
  66. }
  67. sem_post(&work);
  68. }
  69. void WavEngine::newFile(WavFile *_file)
  70. {
  71. //ensure system is clean
  72. destroyFile();
  73. file = _file;
  74. //check state
  75. if(!file->good())
  76. cerr
  77. << "ERROR: WavEngine handed bad file output WavEngine::newFile()"
  78. << endl;
  79. }
  80. void WavEngine::destroyFile()
  81. {
  82. if(file)
  83. delete file;
  84. file = NULL;
  85. }
  86. void *WavEngine::_AudioThread(void *arg)
  87. {
  88. return (static_cast<WavEngine *>(arg))->AudioThread();
  89. }
  90. void *WavEngine::AudioThread()
  91. {
  92. short *recordbuf_16bit = new short[2 * synth->buffersize];
  93. while(!sem_wait(&work) && pThread) {
  94. for(int i = 0; i < synth->buffersize; ++i) {
  95. float left = 0.0f, right = 0.0f;
  96. buffer.pop(left);
  97. buffer.pop(right);
  98. recordbuf_16bit[2 * i] = limit((int)(left * 32767.0f),
  99. -32768,
  100. 32767);
  101. recordbuf_16bit[2 * i + 1] = limit((int)(right * 32767.0f),
  102. -32768,
  103. 32767);
  104. }
  105. file->writeStereoSamples(synth->buffersize, recordbuf_16bit);
  106. }
  107. delete[] recordbuf_16bit;
  108. return NULL;
  109. }