jackaudio.h 2.9 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. #ifndef JACKCONNECTOR_H
  19. #define JACKCONNECTOR_H
  20. #include <jack/jack.h>
  21. #include <jack/ringbuffer.h>
  22. #include "Utils.h"
  23. typedef jack_default_audio_sample_t sample_t;
  24. #define TRANSPORT_NONE 0
  25. #define TRANSPORT_START 1
  26. #define TRANSPORT_STOP 2
  27. #define TRANSPORT_ZERO 3
  28. #define TRANSPORT_STARTPOS 4
  29. class JackAudio {
  30. jack_client_t* _client = nullptr;
  31. jack_port_t* _write_port = nullptr;
  32. jack_ringbuffer_t* _write_rb = nullptr;
  33. jack_ringbuffer_t* _control_rb = nullptr;
  34. jack_port_t* _midi_port = nullptr;
  35. jack_ringbuffer_t* _midi_rb = nullptr;
  36. bool _playback = false;
  37. jack_transport_state_t _transport_state;
  38. jack_transport_state_t _previous_transport_state;
  39. volatile int _transport_command = 0;
  40. int _buffer_size = 0;
  41. bool _use_transport = 0;
  42. bool _state_changed = 0;
  43. int _position = 0;
  44. int _eventfd = 0;
  45. float _startpos = 0;
  46. static int process(jack_nframes_t nframes, void* arg);
  47. static int sync(jack_transport_state_t state, jack_position_t* pos,
  48. void* arg);
  49. public:
  50. explicit JackAudio(bool autoconnect);
  51. void shutdown();
  52. int sampleRate();
  53. int writeData(void* famebuf, int bytes_per_frame);
  54. void changeTranportState(int command) { _transport_command = command; }
  55. bool isRolling() { return _transport_state == JackTransportRolling; }
  56. void setUseTransport(bool use_transport) { _use_transport = use_transport; }
  57. int getBufferSize() { return _buffer_size; }
  58. float* allocateBuffer();
  59. bool stateChanged() {
  60. bool retval = _state_changed;
  61. _state_changed = false;
  62. return retval;
  63. }
  64. int positionChange() {
  65. int retval = _position;
  66. _position = -1;
  67. return retval;
  68. }
  69. jack_transport_state_t transportState() { return _transport_state; }
  70. int createEFD();
  71. int getSamplerate() { return jack_get_sample_rate(_client); }
  72. int readMidiData(char* buffer, int maxlength);
  73. void transportStartPos(float startpos) {
  74. _startpos = startpos;
  75. _transport_command = TRANSPORT_STARTPOS;
  76. }
  77. };
  78. #endif // JACKCONNECTOR_H