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.

NulEngine.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. OSSaudiooutput.C - Audio output for Open Sound System
  4. Copyright (C) 2002-2005 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 "NulEngine.h"
  18. #include "../globals.h"
  19. #include <unistd.h>
  20. #include <iostream>
  21. using namespace std;
  22. NulEngine::NulEngine(const SYNTH_T &synth_)
  23. :AudioOut(synth_), pThread(NULL)
  24. {
  25. name = "NULL";
  26. playing_until.tv_sec = 0;
  27. playing_until.tv_usec = 0;
  28. }
  29. void *NulEngine::_AudioThread(void *arg)
  30. {
  31. return (static_cast<NulEngine *>(arg))->AudioThread();
  32. }
  33. void *NulEngine::AudioThread()
  34. {
  35. while(pThread) {
  36. getNext();
  37. struct timeval now;
  38. int remaining = 0;
  39. gettimeofday(&now, NULL);
  40. if((playing_until.tv_usec == 0) && (playing_until.tv_sec == 0)) {
  41. playing_until.tv_usec = now.tv_usec;
  42. playing_until.tv_sec = now.tv_sec;
  43. }
  44. else {
  45. remaining = (playing_until.tv_usec - now.tv_usec)
  46. + (playing_until.tv_sec - now.tv_sec) * 1000000;
  47. if(remaining > 10000) //Don't sleep() less than 10ms.
  48. //This will add latency...
  49. usleep(remaining - 10000);
  50. if(remaining < 0)
  51. cerr << "WARNING - too late" << endl;
  52. }
  53. playing_until.tv_usec += synth.buffersize * 1000000
  54. / synth.samplerate;
  55. if(remaining < 0)
  56. playing_until.tv_usec -= remaining;
  57. playing_until.tv_sec += playing_until.tv_usec / 1000000;
  58. playing_until.tv_usec %= 1000000;
  59. }
  60. return NULL;
  61. }
  62. NulEngine::~NulEngine()
  63. {}
  64. bool NulEngine::Start()
  65. {
  66. setAudioEn(true);
  67. return getAudioEn();
  68. }
  69. void NulEngine::Stop()
  70. {
  71. setAudioEn(false);
  72. }
  73. void NulEngine::setAudioEn(bool nval)
  74. {
  75. if(nval) {
  76. if(!getAudioEn()) {
  77. pthread_t *thread = new pthread_t;
  78. pthread_attr_t attr;
  79. pthread_attr_init(&attr);
  80. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  81. pThread = thread;
  82. pthread_create(pThread, &attr, _AudioThread, this);
  83. }
  84. }
  85. else
  86. if(getAudioEn()) {
  87. pthread_t *thread = pThread;
  88. pThread = NULL;
  89. pthread_join(*thread, NULL);
  90. delete thread;
  91. }
  92. }
  93. bool NulEngine::getAudioEn() const
  94. {
  95. return pThread;
  96. }