audioengine.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. #define __devloglevel__ 5
  19. #include "audio/audioengine.h"
  20. #include "audio/jackaudio.h"
  21. #include "PluginInterfaces.h"
  22. #include <sys/eventfd.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include "../Controller.h"
  28. AudioEngine::AudioEngine(JackAudio* jack,QObject *parent) :
  29. QThread(parent)
  30. {
  31. _jack = jack;
  32. _jack_buffer = _jack->allocateBuffer();
  33. _jack_buffer_size = _jack->getBufferSize();
  34. _eventfd = _jack->createEFD();
  35. _pos=0;
  36. start();
  37. }
  38. void AudioEngine::shutdown()
  39. {
  40. _running = false;
  41. uint64_t u=2;
  42. int ret = write(_eventfd, &u, sizeof(uint64_t));
  43. if(ret==8) usleep(100);
  44. }
  45. void AudioEngine::run()
  46. {
  47. while(_running)
  48. {
  49. uint64_t u;
  50. int count = read(_eventfd, &u, sizeof(uint64_t));
  51. if(u==1 && count==8)
  52. {
  53. memset(_jack_buffer,0,_jack_buffer_size*sizeof(float));
  54. bool isRolling;
  55. if(_useJackTransport) isRolling=_jack->isRolling();
  56. else isRolling=_localTransportRolling;
  57. if(isRolling)
  58. {
  59. //if(_scheduledSynth) {
  60. // _scheduledSynth->readData(_jack_buffer,_jack_buffer_size);
  61. //}
  62. _outp->readData(_jack_buffer,_jack_buffer_size);
  63. _pos += _jack_buffer_size;
  64. if(_useJackTransport) _lastJackTransportPos = _pos;
  65. }
  66. if(_preview) _preview->readData(_jack_buffer,_jack_buffer_size);
  67. _jack->writeData(_jack_buffer,_jack_buffer_size*sizeof(float));
  68. }
  69. }
  70. }