merge new MTDM code from Fons' latest release.
[jack2.git] / common / JackMidiDriver.cpp
blobe0638d716a2629786dc1585038c5cb693a901013
1 /*
2 Copyright (C) 2009 Grame.
4 This program 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 2 of the License, or
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 (at your option) any later version.
12 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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackSystemDeps.h"
21 #include "JackMidiDriver.h"
22 #include "JackTime.h"
23 #include "JackError.h"
24 #include "JackEngineControl.h"
25 #include "JackPort.h"
26 #include "JackGraphManager.h"
27 #include "JackException.h"
28 #include <assert.h>
30 using namespace std;
32 namespace Jack
35 JackMidiDriver::JackMidiDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
36 : JackDriver(name, alias, engine, table)
39 JackMidiDriver::~JackMidiDriver()
42 int JackMidiDriver::Open(bool capturing,
43 bool playing,
44 int inchannels,
45 int outchannels,
46 bool monitor,
47 const char* capture_driver_name,
48 const char* playback_driver_name,
49 jack_nframes_t capture_latency,
50 jack_nframes_t playback_latency)
52 fCaptureChannels = inchannels;
53 fPlaybackChannels = outchannels;
54 return JackDriver::Open(capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
57 int JackMidiDriver::Attach()
59 JackPort* port;
60 jack_port_id_t port_index;
61 char name[REAL_JACK_PORT_NAME_SIZE];
62 char alias[REAL_JACK_PORT_NAME_SIZE];
63 int i;
65 jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
67 for (i = 0; i < fCaptureChannels; i++) {
68 snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
69 snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
70 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
71 jack_error("driver: cannot register port for %s", name);
72 return -1;
74 port = fGraphManager->GetPort(port_index);
75 port->SetAlias(alias);
76 fCapturePortList[i] = port_index;
77 jack_log("JackMidiDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
80 for (i = 0; i < fPlaybackChannels; i++) {
81 snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
82 snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
83 if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
84 jack_error("driver: cannot register port for %s", name);
85 return -1;
87 port = fGraphManager->GetPort(port_index);
88 port->SetAlias(alias);
89 fPlaybackPortList[i] = port_index;
90 jack_log("JackMidiDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
93 UpdateLatencies();
94 return 0;
97 int JackMidiDriver::Detach()
99 int i;
100 jack_log("JackMidiDriver::Detach");
102 for (i = 0; i < fCaptureChannels; i++) {
103 fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
106 for (i = 0; i < fPlaybackChannels; i++) {
107 fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
110 return 0;
113 void JackMidiDriver::UpdateLatencies()
115 jack_latency_range_t range;
117 for (int i = 0; i < fCaptureChannels; i++) {
118 range.max = range.min = fEngineControl->fBufferSize;
119 fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
122 for (int i = 0; i < fPlaybackChannels; i++) {
123 if (! fEngineControl->fSyncMode) {
124 range.max = range.min = fEngineControl->fBufferSize * 2;
126 fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
130 int JackMidiDriver::SetBufferSize(jack_nframes_t buffer_size)
132 UpdateLatencies();
133 return 0;
136 int JackMidiDriver::ProcessReadSync()
138 int res = 0;
140 // Read input buffers for the current cycle
141 if (Read() < 0) {
142 jack_error("JackMidiDriver::ProcessReadSync: read error");
143 res = -1;
146 if (ResumeRefNum() < 0) {
147 jack_error("JackMidiDriver::ProcessReadSync: ResumeRefNum error");
148 res = -1;
151 return res;
154 int JackMidiDriver::ProcessWriteSync()
156 int res = 0;
158 if (SuspendRefNum() < 0) {
159 jack_error("JackMidiDriver::ProcessWriteSync: SuspendRefNum error");
160 res = -1;
163 // Write output buffers from the current cycle
164 if (Write() < 0) {
165 jack_error("JackMidiDriver::ProcessWriteSync: write error");
166 res = -1;
169 return res;
172 int JackMidiDriver::ProcessReadAsync()
174 int res = 0;
176 // Read input buffers for the current cycle
177 if (Read() < 0) {
178 jack_error("JackMidiDriver::ProcessReadAsync: read error");
179 res = -1;
182 // Write output buffers from the previous cycle
183 if (Write() < 0) {
184 jack_error("JackMidiDriver::ProcessReadAsync: write error");
185 res = -1;
188 if (ResumeRefNum() < 0) {
189 jack_error("JackMidiDriver::ProcessReadAsync: ResumeRefNum error");
190 res = -1;
193 return res;
196 int JackMidiDriver::ProcessWriteAsync()
198 return 0;
201 JackMidiBuffer* JackMidiDriver::GetInputBuffer(int port_index)
203 assert(fCapturePortList[port_index]);
204 return (JackMidiBuffer*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
207 JackMidiBuffer* JackMidiDriver::GetOutputBuffer(int port_index)
209 assert(fPlaybackPortList[port_index]);
210 return (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
213 } // end of namespace