jackconnector.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (C) 2016 Tobias Platen
  3. This file is part of OREMO2.
  4. OREMO2 is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "jackconnector.h"
  16. #include <jack/midiport.h>
  17. #include <stdio.h>
  18. #include <QDebug>
  19. #include <QMessageBox>
  20. #include <QIcon>
  21. int JackConnector::readMidiData(char *buffer, int maxlength)
  22. {
  23. int count = jack_ringbuffer_read_space(this->_midi_rb);
  24. if(count>0)
  25. {
  26. if(count>maxlength) count=maxlength;
  27. jack_ringbuffer_read(this->_midi_rb,buffer,count);
  28. }
  29. return count;
  30. }
  31. int JackConnector::process(jack_nframes_t nframes, void *arg)
  32. {
  33. JackConnector* conn = (JackConnector*) arg;
  34. // midi input handing
  35. void* port_buf = jack_port_get_buffer( conn->_midi_port, nframes);
  36. jack_nframes_t event_count = jack_midi_get_event_count(port_buf);
  37. if(event_count > 0)
  38. {
  39. for(jack_nframes_t i=0; i<event_count; i++)
  40. {
  41. jack_midi_event_t in_event;
  42. jack_midi_event_get(&in_event, port_buf, i);
  43. //FIXME: sysex support
  44. jack_ringbuffer_write(conn->_midi_rb,(char*)in_event.buffer,in_event.size);
  45. }
  46. emit conn->midiInput();
  47. }
  48. sample_t* read_samp = (sample_t*)jack_port_get_buffer(conn->_read_port,nframes);
  49. sample_t* write_samp = (sample_t*)jack_port_get_buffer(conn->_write_port,nframes);
  50. memset(write_samp,0,nframes*sizeof(sample_t));
  51. if(conn->_recording)
  52. {
  53. for(uint i=0;i<nframes;i++)
  54. {
  55. double r = (rand() % 1000)/25000.0;
  56. write_samp[i] = read_samp[i] + r;
  57. }
  58. jack_ringbuffer_write(conn->_read_rb,(char*)read_samp,sizeof(sample_t)*nframes);
  59. emit conn->ready();
  60. }
  61. if(conn->_playback)
  62. {
  63. if(jack_ringbuffer_read_space(conn->_write_rb)>=sizeof(sample_t)*nframes)
  64. {
  65. jack_ringbuffer_read(conn->_write_rb,(char*)write_samp,sizeof(sample_t)*nframes);
  66. }
  67. emit conn->ready();
  68. }
  69. return 0;
  70. }
  71. JackConnector::JackConnector(QObject *parent) :
  72. QObject(parent)
  73. {
  74. _client = jack_client_open("OREMO2",JackNoStartServer,NULL);
  75. if(_client==NULL) {
  76. QMessageBox msgbox;
  77. msgbox.setText("jack is not running");
  78. msgbox.exec();
  79. exit(1);
  80. }
  81. int buffer_size=jack_get_buffer_size(_client);
  82. _read_port = jack_port_register(_client,"mike",JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,buffer_size);
  83. _write_port = jack_port_register(_client,"phones",JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,buffer_size);
  84. _write_rb = jack_ringbuffer_create (sizeof(sample_t) * buffer_size * 4);
  85. _read_rb = jack_ringbuffer_create (sizeof(sample_t) * buffer_size * 4);
  86. _midi_rb = jack_ringbuffer_create(4096);
  87. _midi_port = jack_port_register(_client,"midi_in",JACK_DEFAULT_MIDI_TYPE,JackPortIsInput,0);
  88. jack_set_process_callback(_client,process,this);
  89. jack_activate(_client);
  90. _recording = false;
  91. _playback = false;
  92. }
  93. int JackConnector::readData(void* framebuf,uint bytes_per_frame)
  94. {
  95. if(_recording==false) return false;
  96. if(jack_ringbuffer_read_space (_read_rb) >= bytes_per_frame)
  97. {
  98. jack_ringbuffer_read (_read_rb, (char*)framebuf, bytes_per_frame);
  99. return true;
  100. }
  101. return false;
  102. }
  103. int JackConnector::writeData(void *framebuf, uint bytes_per_frame)
  104. {
  105. if(_playback==false) return false;
  106. if(jack_ringbuffer_write_space(_write_rb)>=bytes_per_frame)
  107. {
  108. jack_ringbuffer_write(_write_rb,(char*)framebuf,bytes_per_frame);
  109. return true;
  110. }
  111. return false;
  112. }
  113. int JackConnector::sampleRate()
  114. {
  115. return jack_get_sample_rate(_client);
  116. }