Jack2 1.9.6

JackMidiDriver.cpp

00001 /*
00002 Copyright (C) 2009 Grame.
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 This program is distributed in the hope that it will be useful,
00008 but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 (at your option) any later version.
00011 
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include "JackSystemDeps.h"
00021 #include "JackMidiDriver.h"
00022 #include "JackTime.h"
00023 #include "JackError.h"
00024 #include "JackEngineControl.h"
00025 #include "JackPort.h"
00026 #include "JackGraphManager.h"
00027 #include "JackException.h"
00028 #include <assert.h>
00029 
00030 namespace Jack
00031 {
00032 
00033 JackMidiDriver::JackMidiDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
00034         : JackDriver(name, alias, engine, table),
00035         fCaptureChannels(0),
00036         fPlaybackChannels(0)
00037 {
00038     for (int i = 0; i < DRIVER_PORT_NUM; i++) {
00039         fRingBuffer[i] = NULL;
00040     }
00041 }
00042 
00043 JackMidiDriver::~JackMidiDriver()
00044 {
00045     for (int i = 0; i < fCaptureChannels; i++) {
00046         if (fRingBuffer[i])
00047             jack_ringbuffer_free(fRingBuffer[i]);
00048     }
00049 }
00050 
00051 int JackMidiDriver::Open(bool capturing,
00052                         bool playing,
00053                         int inchannels,
00054                         int outchannels,
00055                         bool monitor,
00056                         const char* capture_driver_name,
00057                         const char* playback_driver_name,
00058                         jack_nframes_t capture_latency,
00059                         jack_nframes_t playback_latency)
00060 {
00061     fCaptureChannels = inchannels;
00062     fPlaybackChannels = outchannels;
00063     
00064     for (int i = 0; i < fCaptureChannels; i++) {
00065         fRingBuffer[i] = jack_ringbuffer_create(sizeof(float) * BUFFER_SIZE_MAX);
00066     }
00067   
00068     return JackDriver::Open(capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
00069 }
00070 
00071 int JackMidiDriver::Attach()
00072 {
00073     JackPort* port;
00074     jack_port_id_t port_index;
00075     char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
00076     char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
00077     int i;
00078 
00079     jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
00080 
00081     for (i = 0; i < fCaptureChannels; i++) {
00082         snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
00083         snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
00084         if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
00085             jack_error("driver: cannot register port for %s", name);
00086             return -1;
00087         }
00088         port = fGraphManager->GetPort(port_index);
00089         port->SetAlias(alias);
00090         fCapturePortList[i] = port_index;
00091         jack_log("JackMidiDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
00092     }
00093 
00094     for (i = 0; i < fPlaybackChannels; i++) {
00095         snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
00096         snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
00097         if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
00098             jack_error("driver: cannot register port for %s", name);
00099             return -1;
00100         }
00101         port = fGraphManager->GetPort(port_index);
00102         port->SetAlias(alias);
00103         fPlaybackPortList[i] = port_index;
00104         jack_log("JackMidiDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
00105     }
00106 
00107     return 0;
00108 }
00109 
00110 int JackMidiDriver::Detach()
00111 {
00112     int i;
00113     jack_log("JackMidiDriver::Detach");
00114 
00115     for (i = 0; i < fCaptureChannels; i++) {
00116         fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
00117     }
00118 
00119     for (i = 0; i < fPlaybackChannels; i++) {
00120         fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
00121     }
00122 
00123     return 0;
00124 }
00125 
00126 int JackMidiDriver::Read()
00127 {
00128     return 0;
00129 }
00130 
00131 int JackMidiDriver::Write()
00132 {
00133     return 0;
00134 }
00135 
00136 int JackMidiDriver::ProcessNull()
00137 {
00138     return 0;
00139 }
00140 
00141 int JackMidiDriver::Process()
00142 {
00143     // Read input buffers for the current cycle
00144     if (Read() < 0) { 
00145         jack_error("JackMidiDriver::Process: read error, skip cycle");
00146         return 0;   // Skip cycle, but continue processing...
00147     }
00148     
00149     fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00150     if (fEngineControl->fSyncMode) {
00151         if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0) {
00152             jack_error("JackFreewheelDriver::ProcessSync SuspendRefNum error");
00153             return -1;
00154         }
00155     }
00156     
00157     // Write output buffers for the current cycle
00158     if (Write() < 0) { 
00159         jack_error("JackMidiDriver::Process: write error, skip cycle");
00160         return 0;   // Skip cycle, but continue processing...
00161     }
00162     
00163     return 0;
00164 }
00165 
00166 JackMidiBuffer* JackMidiDriver::GetInputBuffer(int port_index)
00167 {
00168     assert(fCapturePortList[port_index]);
00169     return (JackMidiBuffer*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
00170 }
00171 
00172 JackMidiBuffer* JackMidiDriver::GetOutputBuffer(int port_index)
00173 {
00174     assert(fPlaybackPortList[port_index]);
00175     return (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
00176 }
00177 
00178 } // end of namespace