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
25 // globals used only here in common.c
26 snd_seq_t
* g_seq_ptr
= NULL
;
27 jack_client_t
* g_jack_client
= NULL
;
28 struct pollfd
* g_pollDescriptor
= NULL
;
29 int g_numDescriptors
= 0;
32 lash_client_t
* g_lashc
= NULL
;
34 void init_lash(int argc
, char* argv
[])
37 g_lashc
= lash_init( lash_extract_args(&argc
, &argv
), "jackctlmmc", 0, LASH_PROTOCOL_VERSION
);
41 printMMCMessage("Failed to connect to LASH. Session management will not occur.\n");
44 // register JACK and ALSA clients with lash
45 lash_alsa_client_id(g_lashc
, snd_seq_client_id(g_seq_ptr
));
46 lash_jack_client_name(g_lashc
, "jackctlmmc");
49 void process_lash_event(lash_event_t
* event_ptr
)
51 enum LASH_Event_Type type
;
54 type
= lash_event_get_type(event_ptr
);
55 str
= lash_event_get_string(event_ptr
);
60 printMMCMessage("LASH_Quit received.\n");
65 case LASH_Restore_File
:
66 case LASH_Save_Data_Set
:
68 printMMCMessage("LASH Event. Type = %u, string = \"%s\"\n",
70 (str
== NULL
)?"":str
);
74 void process_lash_config(lash_config_t
* config_ptr
)
80 key
= lash_config_get_key(config_ptr
);
81 data
= lash_config_get_value(config_ptr
);
82 data_size
= lash_config_get_value_size(config_ptr
);
84 printMMCMessage("LASH Config. Key = \"%s\"\n", key
);
86 #endif // LASH_SUPPORT
90 jack_port_t
* g_jackMidiIn
= NULL
;
92 int jack_process_cb(jack_nframes_t numFrames
, void* cbContext
)
94 uint8_t* inBuffer
= 0;
95 int numMidiEvents
= 0;
96 jack_midi_event_t currEvent
;
98 MidiSettings
* settings
= (MidiSettings
*) cbContext
;
100 inBuffer
= jack_port_get_buffer(g_jackMidiIn
, numFrames
);
101 numMidiEvents
= jack_midi_get_event_count(inBuffer
);
103 for (midiIndex
= 0; midiIndex
< numMidiEvents
; ++midiIndex
)
105 jack_midi_event_get(&currEvent
, inBuffer
, midiIndex
);
106 handle_midi(currEvent
.buffer
, settings
);
112 int init_jack_midi(MidiSettings
* settings
)
116 errorCode
= jack_set_process_callback(g_jack_client
, &jack_process_cb
, settings
);
117 g_jackMidiIn
= jack_port_register(g_jack_client
, "in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
| JackPortIsTerminal
, 1024);
118 return (errorCode
== 0 && g_jackMidiIn
!= 0);
120 #endif // JACK_MIDI_SUPPORT
122 int init_alsa_sequencer(const char* appName
)
124 snd_seq_port_info_t
* seq_port_info
= NULL
;
127 /* ALSA sequencer initialisation */
128 ret
= snd_seq_open( &g_seq_ptr
, "default", SND_SEQ_OPEN_INPUT
, 0);
132 // setup alsa sequencer port
133 snd_seq_port_info_alloca(&seq_port_info
);
134 snd_seq_port_info_set_capability(seq_port_info
, SND_SEQ_PORT_CAP_WRITE
| SND_SEQ_PORT_CAP_SUBS_WRITE
);
135 snd_seq_port_info_set_type( seq_port_info
, SND_SEQ_PORT_TYPE_APPLICATION
);
136 snd_seq_port_info_set_midi_channels(seq_port_info
, 16);
137 snd_seq_port_info_set_port_specified(seq_port_info
, 1);
138 snd_seq_port_info_set_name(seq_port_info
, "midi in");
139 snd_seq_port_info_set_port(seq_port_info
, 0);
141 ret
= snd_seq_create_port(g_seq_ptr
, seq_port_info
);
144 printMMCMessage("Error with alsa sequencer initialization, %s\n", snd_strerror(ret
));
145 else // all is well, register the sequencer with whatever name was passed in
147 snd_seq_set_client_name(g_seq_ptr
, appName
);
149 g_numDescriptors
= snd_seq_poll_descriptors_count(g_seq_ptr
, POLLIN
);
150 g_pollDescriptor
= (struct pollfd
*)malloc(g_numDescriptors
* sizeof(struct pollfd
));
151 snd_seq_poll_descriptors(g_seq_ptr
, g_pollDescriptor
, g_numDescriptors
, POLLIN
);
157 int init_jack(const char* appName
)
159 jack_status_t status
;
160 g_jack_client
= jack_client_open(appName
, JackNoStartServer
, &status
);
162 if (!g_jack_client
) // something went wrong
163 return -1; // todo: check status to see exactly what happened
170 /* tell jack that we are ready to do our thing */
171 return jack_activate(g_jack_client
);
174 void handle_midi(uint8_t* midiBuff
, MidiSettings
* settings
)
176 if (midiBuff
[0] == 0xF0 && midiBuff
[1] == 0x7F && midiBuff
[3] == 0x06)
178 const uint8_t messageDeviceID
= midiBuff
[2];
179 if (messageDeviceID
== settings
->deviceID
)
181 const char mmcCommand
= midiBuff
[4];
185 if (settings
->verbose
)
186 printMMCMessage("MMC Stop -> JACK transport stop\n");
187 jack_transport_stop(g_jack_client
);
190 case 3: /* deferred play */
191 if (settings
->verbose
)
192 printMMCMessage("MMC Play -> JACK transport start\n");
193 jack_transport_start(g_jack_client
);
196 if (settings
->verbose
)
197 printMMCMessage("MMC Fast Forward -> Ignored\n");
200 if (settings
->verbose
)
201 printMMCMessage("MMC Play -> JACK transport locate to 0\n");
202 jack_transport_locate(g_jack_client
, 0);
205 if (settings
->verbose
)
206 printMMCMessage("MMC Record Strobe (Punch In) -> Ignored\n");
209 if (settings
->verbose
)
210 printMMCMessage("MMC Record Exit (Punch out) -> Ignored\n");
213 if (settings
->verbose
)
214 printMMCMessage("MMC Record Ready (Record Pause) -> Ignored\n");
217 if (settings
->verbose
)
218 printMMCMessage("MMC Pause -> JACK transport stop\n");
219 jack_transport_stop(g_jack_client
);
221 case 0x44: /* goto */
222 if (midiBuff
[5] == 0x06 && midiBuff
[6] == 0x01)
224 // some devices call hour 0 "60". Mask off the upper bits
225 uint8_t hour
= (midiBuff
[7] & 0x1f);
226 uint8_t minute
= midiBuff
[8];
227 uint8_t second
= midiBuff
[9];
228 uint8_t frame
= midiBuff
[10];
229 uint8_t subframe
= midiBuff
[11]; // percentage of a frame: 0 - 99
230 jack_position_t jack_pos
;
232 // get Jack's current framerate and position
233 jack_transport_query(g_jack_client
, &jack_pos
);
234 uint32_t jack_frame_rate
= jack_pos
.frame_rate
;
235 uint32_t jack_time_ms
= (jack_pos
.frame
* 1000) / jack_frame_rate
;
236 uint32_t device_time_ms
= ((subframe
* 10) / settings
->frameRate
+ // subframe == 1/100th of a frame
237 (frame
* 1000) / settings
->frameRate
+
239 (minute
* 60 * 1000) +
240 (hour
* 60 * 60 * 1000));
242 // difference in milliseconds from JACK's reported transport to the MIDI goto's time
243 uint32_t jitter
= (device_time_ms
> jack_time_ms
? (device_time_ms
- jack_time_ms
) : (jack_time_ms
- device_time_ms
));
245 if (settings
->verbose
)
246 printMMCMessage("MMC goto hour: %d, min: %d, sec: %d, frame: %d, subframe: %d\n",
253 // check if the JACK clock is far enough away from the MMC time to care
254 if (jitter
> settings
->jitterTolerance
)
255 { // it is, change it.
256 jack_pos
.valid
= 0; // only the frame number will be valid since that's all we're changing
258 // need a placeholder so that we can keep precision while not letting the integer overflow
259 uint64_t placeholder
= (uint64_t)device_time_ms
* jack_frame_rate
/ 1000;
260 jack_pos
.frame
= placeholder
;
262 jack_transport_reposition(g_jack_client
, &jack_pos
);
264 else if (settings
->verbose
)
265 printMMCMessage("New position is within jitter range, ignoring. (%d ms)\n", jitter
);
268 case 0x48: /* step */
271 jack_position_t jack_pos
;
280 if (settings
->verbose
)
281 printMMCMessage("MMC Step %d\n", step
);
283 // get Jack's current framerate and position
284 jack_transport_query(g_jack_client
, &jack_pos
);
286 jack_pos
.valid
= 0; // only the frame number will be valid since that's all we're changing
288 jack_pos
.frame
+= (int64_t)step
* (int64_t)jack_pos
.frame_rate
/ (int64_t)10;
290 jack_transport_reposition(g_jack_client
, &jack_pos
);
294 if (settings
->verbose
)
295 printMMCMessage("unsupported MMC command: 0x%x\n", mmcCommand
);
298 else if (settings
->verbose
)
300 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",
308 uint8_t* get_midi_input ()
310 snd_seq_event_t
* seq_event_ptr
= NULL
;
313 /* Process LASH events */
315 lash_event_t
* lash_event_ptr
= NULL
;
316 lash_config_t
* lash_config_ptr
= NULL
;
317 while ((lash_event_ptr
= lash_get_event(g_lashc
)) != NULL
)
319 process_lash_event(lash_event_ptr
);
320 lash_event_destroy(lash_event_ptr
);
323 /* Process LASH configs */
324 while ((lash_config_ptr
= lash_get_config(g_lashc
)) != NULL
)
326 process_lash_config(lash_config_ptr
);
327 lash_config_destroy(lash_config_ptr
);
332 if (poll(g_pollDescriptor
, g_numDescriptors
, 250) > 0 && snd_seq_event_input(g_seq_ptr
, &seq_event_ptr
) >= 0 && seq_event_ptr
->type
== SND_SEQ_EVENT_SYSEX
)
333 return ((uint8_t *)seq_event_ptr
->data
.ext
.ptr
);
338 void cleanup_globals()
344 #if JACK_MIDI_SUPPORT
345 jack_port_unregister(g_jack_client
, g_jackMidiIn
);
347 jack_deactivate(g_jack_client
);
348 jack_client_close(g_jack_client
);
353 ret
= snd_seq_close(g_seq_ptr
);
356 printMMCMessage("Cannot close sequncer, %s\n", snd_strerror(ret
));
360 if (g_pollDescriptor
)
361 free(g_pollDescriptor
);