123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- Copyright (C) 2016 Tobias Platen
-
- This file is part of OREMO2.
- OREMO2 is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
- #include "jackconnector.h"
- #include <jack/midiport.h>
- #include <stdio.h>
- #include <QDebug>
- #include <QMessageBox>
- #include <QIcon>
- int JackConnector::readMidiData(char *buffer, int maxlength)
- {
- int count = jack_ringbuffer_read_space(this->_midi_rb);
- if(count>0)
- {
- if(count>maxlength) count=maxlength;
- jack_ringbuffer_read(this->_midi_rb,buffer,count);
- }
- return count;
- }
- int JackConnector::process(jack_nframes_t nframes, void *arg)
- {
- JackConnector* conn = (JackConnector*) arg;
- // midi input handing
- void* port_buf = jack_port_get_buffer( conn->_midi_port, nframes);
- jack_nframes_t event_count = jack_midi_get_event_count(port_buf);
- if(event_count > 0)
- {
- for(jack_nframes_t i=0; i<event_count; i++)
- {
- jack_midi_event_t in_event;
- jack_midi_event_get(&in_event, port_buf, i);
- //FIXME: sysex support
- jack_ringbuffer_write(conn->_midi_rb,(char*)in_event.buffer,in_event.size);
- }
- emit conn->midiInput();
- }
- sample_t* read_samp = (sample_t*)jack_port_get_buffer(conn->_read_port,nframes);
- sample_t* write_samp = (sample_t*)jack_port_get_buffer(conn->_write_port,nframes);
- memset(write_samp,0,nframes*sizeof(sample_t));
- if(conn->_recording)
- {
- for(uint i=0;i<nframes;i++)
- {
- double r = (rand() % 1000)/25000.0;
- write_samp[i] = read_samp[i] + r;
- }
- jack_ringbuffer_write(conn->_read_rb,(char*)read_samp,sizeof(sample_t)*nframes);
- emit conn->ready();
- }
- if(conn->_playback)
- {
- if(jack_ringbuffer_read_space(conn->_write_rb)>=sizeof(sample_t)*nframes)
- {
- jack_ringbuffer_read(conn->_write_rb,(char*)write_samp,sizeof(sample_t)*nframes);
- }
- emit conn->ready();
- }
- return 0;
- }
- JackConnector::JackConnector(QObject *parent) :
- QObject(parent)
- {
- _client = jack_client_open("OREMO2",JackNoStartServer,NULL);
- if(_client==NULL) {
- QMessageBox msgbox;
- msgbox.setText("jack is not running");
- msgbox.exec();
- exit(1);
- }
- int buffer_size=jack_get_buffer_size(_client);
- _read_port = jack_port_register(_client,"mike",JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,buffer_size);
- _write_port = jack_port_register(_client,"phones",JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,buffer_size);
- _write_rb = jack_ringbuffer_create (sizeof(sample_t) * buffer_size * 4);
- _read_rb = jack_ringbuffer_create (sizeof(sample_t) * buffer_size * 4);
- _midi_rb = jack_ringbuffer_create(4096);
- _midi_port = jack_port_register(_client,"midi_in",JACK_DEFAULT_MIDI_TYPE,JackPortIsInput,0);
- jack_set_process_callback(_client,process,this);
- jack_activate(_client);
- _recording = false;
- _playback = false;
- }
- int JackConnector::readData(void* framebuf,uint bytes_per_frame)
- {
- if(_recording==false) return false;
- if(jack_ringbuffer_read_space (_read_rb) >= bytes_per_frame)
- {
- jack_ringbuffer_read (_read_rb, (char*)framebuf, bytes_per_frame);
- return true;
- }
- return false;
- }
- int JackConnector::writeData(void *framebuf, uint bytes_per_frame)
- {
- if(_playback==false) return false;
- if(jack_ringbuffer_write_space(_write_rb)>=bytes_per_frame)
- {
- jack_ringbuffer_write(_write_rb,(char*)framebuf,bytes_per_frame);
- return true;
- }
- return false;
- }
- int JackConnector::sampleRate()
- {
- return jack_get_sample_rate(_client);
- }
|