jackaudio.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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__ 4
  19. #include "jackaudio.h"
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <QDebug>
  23. #include <QMessageBox>
  24. #include <QIcon>
  25. #include <sys/eventfd.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #include <jack/midiport.h>
  31. #include <iostream>
  32. int JackAudio::createEFD()
  33. {
  34. _eventfd = eventfd(0, 0);
  35. return _eventfd;
  36. }
  37. int JackAudio::readMidiData(char *buffer, int maxlength)
  38. {
  39. int count = jack_ringbuffer_read_space(this->_midi_rb);
  40. if(count>0)
  41. {
  42. if(count>maxlength) count=maxlength;
  43. jack_ringbuffer_read(this->_midi_rb,buffer,count);
  44. }
  45. return count;
  46. }
  47. int JackAudio::process(jack_nframes_t nframes, void *arg)
  48. {
  49. JackAudio* conn = (JackAudio*) arg;
  50. // midi input handing
  51. void* port_buf = jack_port_get_buffer( conn->_midi_port, nframes);
  52. jack_nframes_t event_count = jack_midi_get_event_count(port_buf);
  53. if(event_count > 0)
  54. {
  55. for(jack_nframes_t i=0; i<event_count; i++)
  56. {
  57. jack_midi_event_t in_event;
  58. //jack_position_t position;
  59. jack_midi_event_get(&in_event, port_buf, i);
  60. //FIXME: sysex support
  61. jack_ringbuffer_write(conn->_midi_rb,(char*)in_event.buffer,in_event.size);
  62. }
  63. }
  64. // audio output
  65. sample_t* write_samp = (sample_t*)jack_port_get_buffer(conn->_write_port,nframes);
  66. if(jack_ringbuffer_read_space(conn->_write_rb)>=sizeof(sample_t)*nframes)
  67. {
  68. jack_ringbuffer_read(conn->_write_rb,(char*)write_samp,sizeof(sample_t)*nframes);
  69. }
  70. else
  71. {
  72. for(unsigned int i=0;i<nframes;i++) write_samp[i]=0.0;//buffer underrun
  73. DEVLOG_DEBUG("xrun in JackAudio::process");
  74. }
  75. // transport control
  76. if (conn->_use_transport) {
  77. conn->_transport_state = jack_transport_query(conn->_client, NULL);
  78. if (conn->_transport_state == JackTransportStopped && conn->_previous_transport_state == JackTransportRolling) {
  79. conn->_state_changed = true;
  80. }
  81. if (conn->_transport_state == JackTransportStarting && conn->_previous_transport_state != JackTransportStarting) {
  82. conn->_state_changed = true;
  83. }
  84. if (conn->_transport_state == JackTransportRolling && conn->_previous_transport_state != JackTransportRolling) {
  85. conn->_state_changed = true;
  86. }
  87. conn->_previous_transport_state = conn->_transport_state;
  88. }
  89. if(conn->_transport_command==TRANSPORT_START) jack_transport_start(conn->_client);
  90. if(conn->_transport_command==TRANSPORT_STOP) jack_transport_stop(conn->_client);
  91. if(conn->_transport_command==TRANSPORT_ZERO) {
  92. jack_position_t pos;
  93. pos.valid=(jack_position_bits_t)0;
  94. pos.frame=0;
  95. jack_transport_stop(conn->_client);
  96. jack_transport_reposition(conn->_client,&pos);
  97. }
  98. if(conn->_transport_command==TRANSPORT_STARTPOS)
  99. {
  100. jack_position_t pos;
  101. pos.valid=(jack_position_bits_t)0;
  102. pos.frame=conn->_startpos*conn->sampleRate();
  103. jack_transport_stop(conn->_client);
  104. jack_transport_reposition(conn->_client,&pos);
  105. jack_transport_start(conn->_client);
  106. }
  107. conn->_transport_command = 0;
  108. //notify
  109. uint64_t u = 1;
  110. if(write(conn->_eventfd, &u, sizeof(uint64_t))!=8) return 1;
  111. // no error
  112. return 0;
  113. }
  114. int JackAudio::sync(jack_transport_state_t state, jack_position_t *pos, void *arg)
  115. {
  116. (void) state;
  117. //start tranport
  118. JackAudio* conn = (JackAudio*) arg;
  119. if(conn->_use_transport)
  120. {
  121. conn->_position = pos->frame;
  122. return 1;
  123. }
  124. else return 1;
  125. }
  126. JackAudio::JackAudio(bool autoconnect)
  127. {
  128. _client = jack_client_open("QTau",JackNoStartServer,NULL);
  129. if(_client==NULL) {
  130. QMessageBox msgbox;
  131. msgbox.setText("jack is not running");
  132. msgbox.exec();
  133. exit(1);
  134. }
  135. _buffer_size = jack_get_buffer_size(_client);
  136. //TODO configurable port names ???
  137. _write_port = jack_port_register(_client,"out",JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,_buffer_size);
  138. _write_rb = jack_ringbuffer_create (sizeof(sample_t) * 4096);
  139. _midi_rb = jack_ringbuffer_create(4096);
  140. _midi_port = jack_port_register(_client,"midi_in",JACK_DEFAULT_MIDI_TYPE,JackPortIsInput,0);
  141. jack_set_process_callback(_client,process,this);
  142. jack_set_sync_callback(_client,sync,this);
  143. jack_activate(_client);
  144. if(autoconnect)
  145. {
  146. jack_connect(_client,"QTau:out","system:playback_2");//FOR testing
  147. jack_connect(_client,"QTau:out","system:playback_1");//FOR testing
  148. }
  149. _transport_command = TRANSPORT_ZERO;
  150. _use_transport = true;
  151. _playback = false;
  152. }
  153. int JackAudio::sampleRate()
  154. {
  155. return jack_get_sample_rate(_client);
  156. }
  157. int JackAudio::writeData(void *framebuf, int bytes_per_frame)
  158. {
  159. if(jack_ringbuffer_write_space(_write_rb)>=(unsigned int)bytes_per_frame)
  160. {
  161. return jack_ringbuffer_write(_write_rb,(char*)framebuf,bytes_per_frame);
  162. }
  163. DEVLOG_DEBUG("xrun in JackAudio::writeData");//FIXME do not log while program startup
  164. return 0;//not data was written
  165. }
  166. float *JackAudio::allocateBuffer()
  167. {
  168. if(_buffer_size==0) return NULL;
  169. return new float[_buffer_size];
  170. }
  171. void JackAudio::shutdown()
  172. {
  173. jack_deactivate(_client);
  174. jack_client_close(_client);
  175. }