audioengine.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "PluginInterfaces.h"
  21. #include "audio/jackaudio.h"
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <sys/eventfd.h>
  26. #include <unistd.h>
  27. #include "../Controller.h"
  28. AudioEngine::AudioEngine(JackAudio* jack, QObject* parent) : QThread(parent) {
  29. _jack = jack;
  30. _jack_buffer = _jack->allocateBuffer();
  31. _jack_buffer_size = _jack->getBufferSize();
  32. _eventfd = _jack->createEFD();
  33. _pos = 0;
  34. start();
  35. }
  36. void AudioEngine::shutdown() {
  37. _running = false;
  38. uint64_t u = 2;
  39. int ret = write(_eventfd, &u, sizeof(uint64_t));
  40. if (ret == 8) usleep(100);
  41. }
  42. void AudioEngine::run() {
  43. while (_running) {
  44. uint64_t u;
  45. int count = read(_eventfd, &u, sizeof(uint64_t));
  46. if (u == 1 && count == 8) {
  47. memset(_jack_buffer, 0, _jack_buffer_size * sizeof(float));
  48. bool isRolling;
  49. if (_useJackTransport)
  50. isRolling = _jack->isRolling();
  51. else
  52. isRolling = _localTransportRolling;
  53. if (isRolling) {
  54. // if(_scheduledSynth) {
  55. // _scheduledSynth->readData(_jack_buffer,_jack_buffer_size);
  56. //}
  57. _outp->readData(_jack_buffer, _jack_buffer_size);
  58. _pos += _jack_buffer_size;
  59. if (_useJackTransport) _lastJackTransportPos = _pos;
  60. }
  61. if (_preview) _preview->readData(_jack_buffer, _jack_buffer_size);
  62. _jack->writeData(_jack_buffer, _jack_buffer_size * sizeof(float));
  63. }
  64. }
  65. }