2 * Control JACK transport using MMC (MIDI)
4 * Copyright (c) 2006,2007,2008,2010 Nedko Arnaudov <nedko@arnaudov.name>
5 * Copyright (c) 2008 Alex Montgomery <apmontgomery@gmail.com>
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free Software
9 * Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 int g_quit
= 0; // a flag which will be set by our signal handler when it's time to exit
24 snd_seq_t
* g_seq_ptr
= NULL
;
25 jack_client_t
* g_jack_client
= NULL
;
26 int g_isListening
= 0;
29 lash_client_t
* g_lashc
= NULL
;
31 void init_lash(int argc
, char* argv
[])
33 lash_event_t
* lash_event_ptr
= NULL
;
36 g_lashc
= lash_init( lash_extract_args(&argc
, &argv
), "jackctlmmc", 0, LASH_PROTOCOL_VERSION
);
40 printMMCMessage("Failed to connect to LASH. Session management will not occur.\n");
43 // register JACK and ALSA clients with lash
44 lash_alsa_client_id(g_lashc
, snd_seq_client_id(g_seq_ptr
));
45 lash_jack_client_name(g_lashc
, "jackctlmmc");
48 void process_lash_event(lash_event_t
* event_ptr
)
50 enum LASH_Event_Type type
;
53 type
= lash_event_get_type(event_ptr
);
54 str
= lash_event_get_string(event_ptr
);
59 printMMCMessage("LASH_Quit received.\n");
64 case LASH_Restore_File
:
65 case LASH_Save_Data_Set
:
67 printMMCMessage("LASH Event. Type = %u, string = \"%s\"\n",
69 (str
== NULL
)?"":str
);
73 void process_lash_config(lash_config_t
* config_ptr
)
79 key
= lash_config_get_key(config_ptr
);
80 data
= lash_config_get_value(config_ptr
);
81 data_size
= lash_config_get_value_size(config_ptr
);
83 printMMCMessage("LASH Config. Key = \"%s\"\n", key
);
85 #endif // LASH_SUPPORT
89 jack_port_t
* g_jackMidiIn
= NULL
;
91 int jack_process_cb(jack_nframes_t numFrames
, void* cbContext
)
95 uint8_t* inBuffer
= 0;
96 int numMidiEvents
= 0;
97 jack_midi_event_t currEvent
;
99 MidiSettings
* settings
= (MidiSettings
*) cbContext
;
101 inBuffer
= jack_port_get_buffer(g_jackMidiIn
, numFrames
);
102 numMidiEvents
= jack_midi_get_event_count(inBuffer
);
104 for (midiIndex
= 0; midiIndex
< numMidiEvents
; ++midiIndex
)
106 jack_midi_event_get(&currEvent
, inBuffer
, midiIndex
);
107 handle_midi(currEvent
.buffer
, settings
);
114 int init_jack_midi(MidiSettings
* settings
)
118 errorCode
= jack_set_process_callback(g_jack_client
, &jack_process_cb
, settings
);
119 g_jackMidiIn
= jack_port_register(g_jack_client
, "in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
| JackPortIsTerminal
, 1024);
120 return (errorCode
== 0 && g_jackMidiIn
!= 0);
122 #endif // JACK_MIDI_SUPPORT
124 int init_alsa_sequencer(const char* appName
)
126 snd_seq_port_info_t
* seq_port_info
= NULL
;
129 /* ALSA sequencer initialisation */
130 ret
= snd_seq_open( &g_seq_ptr
, "default", SND_SEQ_OPEN_INPUT
, 0);
134 // setup alsa sequencer port
135 snd_seq_port_info_alloca(&seq_port_info
);
136 snd_seq_port_info_set_capability(seq_port_info
, SND_SEQ_PORT_CAP_WRITE
| SND_SEQ_PORT_CAP_SUBS_WRITE
);
137 snd_seq_port_info_set_type( seq_port_info
, SND_SEQ_PORT_TYPE_APPLICATION
);
138 snd_seq_port_info_set_midi_channels(seq_port_info
, 16);
139 snd_seq_port_info_set_port_specified(seq_port_info
, 1);
140 snd_seq_port_info_set_name(seq_port_info
, "midi in");
141 snd_seq_port_info_set_port(seq_port_info
, 0);
143 ret
= snd_seq_create_port(g_seq_ptr
, seq_port_info
);
146 printMMCMessage("Error with alsa sequencer initialization, %s\n", snd_strerror(ret
));
147 else // all is well, register the sequencer with whatever name was passed in
148 snd_seq_set_client_name(g_seq_ptr
, appName
);
153 int init_jack(const char* appName
)
155 jack_status_t status
;
156 g_jack_client
= jack_client_open(appName
, JackNoStartServer
, &status
);
158 if (!g_jack_client
) // something went wrong
159 return -1; // todo: check status to see exactly what happened
166 /* tell jack that we are ready to do our thing */
167 return jack_activate(g_jack_client
);
170 void handle_midi(uint8_t* midiBuff
, MidiSettings
* settings
)
172 if (midiBuff
[0] == 0xF0 && midiBuff
[1] == 0x7F && midiBuff
[3] == 0x06)
174 const uint8_t messageDeviceID
= midiBuff
[2];
175 if (messageDeviceID
== settings
->deviceID
)
177 const char mmcCommand
= midiBuff
[4];
181 if (settings
->verbose
)
182 printMMCMessage("MMC Stop -> JACK transport stop\n");
183 jack_transport_stop(g_jack_client
);
186 case 3: /* deferred play */
187 if (settings
->verbose
)
188 printMMCMessage("MMC Play -> JACK transport start\n");
189 jack_transport_start(g_jack_client
);
192 if (settings
->verbose
)
193 printMMCMessage("MMC Fast Forward -> Ignored\n");
196 if (settings
->verbose
)
197 printMMCMessage("MMC Play -> JACK transport locate to 0\n");
198 jack_transport_locate(g_jack_client
, 0);
201 if (settings
->verbose
)
202 printMMCMessage("MMC Record Strobe (Punch In) -> Ignored\n");
205 if (settings
->verbose
)
206 printMMCMessage("MMC Record Exit (Punch out) -> Ignored\n");
209 if (settings
->verbose
)
210 printMMCMessage("MMC Record Ready (Record Pause) -> Ignored\n");
213 if (settings
->verbose
)
214 printMMCMessage("MMC Pause -> JACK transport stop\n");
215 jack_transport_stop(g_jack_client
);
217 case 0x44: /* goto */
218 if (midiBuff
[5] == 0x06 && midiBuff
[6] == 0x01)
220 // some devices call hour 0 "60". Mask off the upper bits
221 uint8_t hour
= (midiBuff
[7] & 0x1f);
222 uint8_t minute
= midiBuff
[8];
223 uint8_t second
= midiBuff
[9];
224 uint8_t frame
= midiBuff
[10];
225 uint8_t subframe
= midiBuff
[11]; // percentage of a frame: 0 - 99
226 jack_position_t jack_pos
;
228 // get Jack's current framerate and position
229 jack_transport_query(g_jack_client
, &jack_pos
);
230 uint32_t jack_frame_rate
= jack_pos
.frame_rate
;
231 uint32_t jack_time_ms
= (jack_pos
.frame
* 1000) / jack_frame_rate
;
232 uint32_t device_time_ms
= ((subframe
* 10) / settings
->frameRate
+ // subframe == 1/100th of a frame
233 (frame
* 1000) / settings
->frameRate
+
235 (minute
* 60 * 1000) +
236 (hour
* 60 * 60 * 1000));
238 // difference in milliseconds from JACK's reported transport to the MIDI goto's time
239 uint32_t jitter
= (device_time_ms
> jack_time_ms
? (device_time_ms
- jack_time_ms
) : (jack_time_ms
- device_time_ms
));
241 if (settings
->verbose
)
242 printMMCMessage("MMC locate to hour: %d, minute: %d, second: %d, frame: %d, subframe: %d\n",
249 // check if the JACK clock is far enough away from the MMC time to care
250 if (jitter
> settings
->jitterTolerance
)
251 { // it is, change it.
252 jack_pos
.valid
= 0; // only the frame number will be valid since that's all we're changing
254 // need a placeholder so that we can keep precision while not letting the integer overflow
255 uint64_t placeholder
= (uint64_t)device_time_ms
* jack_frame_rate
/ 1000;
256 jack_pos
.frame
= placeholder
;
258 jack_transport_reposition(g_jack_client
, &jack_pos
);
260 else if (settings
->verbose
)
261 printMMCMessage("New position is %d ms away and within jitter range, ignoring.\n", jitter
);
264 case 0x48: /* step */
267 jack_position_t jack_pos
;
276 if (settings
->verbose
)
277 printMMCMessage("MMC Step %d\n", step
);
279 // get Jack's current framerate and position
280 jack_transport_query(g_jack_client
, &jack_pos
);
282 jack_pos
.valid
= 0; // only the frame number will be valid since that's all we're changing
284 jack_pos
.frame
+= (int64_t)step
* (int64_t)jack_pos
.frame_rate
/ (int64_t)10;
286 jack_transport_reposition(g_jack_client
, &jack_pos
);
290 if (settings
->verbose
)
291 printMMCMessage("unsupported MMC command: 0x%x\n", mmcCommand
);
294 else if (settings
->verbose
)
296 printMMCMessage("MMC command received for device ID %x while listening for commands from device ID %x. Have you specified the correct device ID to listen to?\n",
304 void listen_loop (MidiSettings
* settings
)
306 snd_seq_event_t
* seq_event_ptr
= NULL
;
308 int npfd
= snd_seq_poll_descriptors_count(g_seq_ptr
, POLLIN
);
309 struct pollfd
* pfd
= (struct pollfd
*)alloca(npfd
* sizeof(struct pollfd
));
310 snd_seq_poll_descriptors(g_seq_ptr
, pfd
, npfd
, POLLIN
);
314 if (poll(pfd
, npfd
, 250) > 0 && snd_seq_event_input(g_seq_ptr
, &seq_event_ptr
) >= 0 && seq_event_ptr
->type
== SND_SEQ_EVENT_SYSEX
)
315 handle_midi((uint8_t *)seq_event_ptr
->data
.ext
.ptr
, settings
);
318 /* Process LASH events */
320 lash_event_t
* lash_event_ptr
= NULL
;
321 lash_config_t
* lash_config_ptr
= NULL
;
322 while ((lash_event_ptr
= lash_get_event(g_lashc
)) != NULL
)
324 process_lash_event(lash_event_ptr
);
325 lash_event_destroy(lash_event_ptr
);
328 /* Process LASH configs */
329 while ((lash_config_ptr
= lash_get_config(g_lashc
)) != NULL
)
331 process_lash_config(lash_config_ptr
);
332 lash_config_destroy(lash_config_ptr
);
339 void cleanup_globals()
345 ret
= snd_seq_close(g_seq_ptr
);
348 printMMCMessage("Cannot close sequncer, %s\n", snd_strerror(ret
));
354 jack_deactivate(g_jack_client
);
355 jack_client_close(g_jack_client
);