Jack2  1.9.9
JackMidiDriver.cpp
1 /*
2 Copyright (C) 2009 Grame.
3 
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.
11 
12 GNU General Public License for more details.
13 
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.
17 
18 */
19 
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>
29 
30 using namespace std;
31 
32 namespace Jack
33 {
34 
35 JackMidiDriver::JackMidiDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
36  : JackDriver(name, alias, engine, table)
37 {}
38 
39 JackMidiDriver::~JackMidiDriver()
40 {}
41 
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)
51 {
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);
55 }
56 
57 int JackMidiDriver::Attach()
58 {
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;
64 
65  jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
66 
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;
73  }
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);
78  }
79 
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;
86  }
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);
91  }
92 
93  UpdateLatencies();
94  return 0;
95 }
96 
97 int JackMidiDriver::Detach()
98 {
99  int i;
100  jack_log("JackMidiDriver::Detach");
101 
102  for (i = 0; i < fCaptureChannels; i++) {
103  fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
104  }
105 
106  for (i = 0; i < fPlaybackChannels; i++) {
107  fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
108  }
109 
110  return 0;
111 }
112 
113 void JackMidiDriver::UpdateLatencies()
114 {
115  jack_latency_range_t range;
116 
117  for (int i = 0; i < fCaptureChannels; i++) {
118  range.max = range.min = fEngineControl->fBufferSize;
119  fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
120  }
121 
122  for (int i = 0; i < fPlaybackChannels; i++) {
123  if (! fEngineControl->fSyncMode) {
124  range.max = range.min = fEngineControl->fBufferSize * 2;
125  }
126  fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
127  }
128 }
129 
130 int JackMidiDriver::SetBufferSize(jack_nframes_t buffer_size)
131 {
132  UpdateLatencies();
133  return 0;
134 }
135 
136 int JackMidiDriver::ProcessReadSync()
137 {
138  int res = 0;
139 
140  // Read input buffers for the current cycle
141  if (Read() < 0) {
142  jack_error("JackMidiDriver::ProcessReadSync: read error");
143  res = -1;
144  }
145 
146  if (ResumeRefNum() < 0) {
147  jack_error("JackMidiDriver::ProcessReadSync: ResumeRefNum error");
148  res = -1;
149  }
150 
151  return res;
152 }
153 
154 int JackMidiDriver::ProcessWriteSync()
155 {
156  int res = 0;
157 
158  if (SuspendRefNum() < 0) {
159  jack_error("JackMidiDriver::ProcessWriteSync: SuspendRefNum error");
160  res = -1;
161  }
162 
163  // Write output buffers from the current cycle
164  if (Write() < 0) {
165  jack_error("JackMidiDriver::ProcessWriteSync: write error");
166  res = -1;
167  }
168 
169  return res;
170 }
171 
172 int JackMidiDriver::ProcessReadAsync()
173 {
174  int res = 0;
175 
176  // Read input buffers for the current cycle
177  if (Read() < 0) {
178  jack_error("JackMidiDriver::ProcessReadAsync: read error");
179  res = -1;
180  }
181 
182  // Write output buffers from the previous cycle
183  if (Write() < 0) {
184  jack_error("JackMidiDriver::ProcessReadAsync: write error");
185  res = -1;
186  }
187 
188  if (ResumeRefNum() < 0) {
189  jack_error("JackMidiDriver::ProcessReadAsync: ResumeRefNum error");
190  res = -1;
191  }
192 
193  return res;
194 }
195 
196 int JackMidiDriver::ProcessWriteAsync()
197 {
198  return 0;
199 }
200 
201 JackMidiBuffer* JackMidiDriver::GetInputBuffer(int port_index)
202 {
203  assert(fCapturePortList[port_index]);
204  return (JackMidiBuffer*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
205 }
206 
207 JackMidiBuffer* JackMidiDriver::GetOutputBuffer(int port_index)
208 {
209  assert(fPlaybackPortList[port_index]);
210  return (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
211 }
212 
213 } // end of namespace
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
jack_nframes_t min
Definition: types.h:268
jack_nframes_t max
Definition: types.h:272
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107