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 snd_seq_t
* g_seq_ptr
= NULL
;
24 jack_client_t
* g_jack_client
= NULL
;
25 int g_quit
= 0; // a flag which will be set by our signal handler when it's time to exit
28 lash_client_t
* g_lashc
= NULL
;
30 void init_lash(int argc
, char* argv
[])
32 lash_event_t
* lash_event_ptr
= NULL
;
35 g_lashc
= lash_init( lash_extract_args(&argc
, &argv
), "jackctlmmc", 0, LASH_PROTOCOL_VERSION
);
39 printMMCMessage("Failed to connect to LASH. Session management will not occur.\n");
43 lash_event_ptr
= lash_event_new_with_type(LASH_Client_Name
);
44 lash_event_set_string(lash_event_ptr
, "jackctlmmc");
45 lash_send_event(g_lashc
, lash_event_ptr
);
48 // register JACK and ALSA clients with lash
49 lash_alsa_client_id(g_lashc
, snd_seq_client_id(g_seq_ptr
));
50 lash_jack_client_name(g_lashc
, "jackctlmmc");
53 void process_lash_event(lash_event_t
* event_ptr
)
55 enum LASH_Event_Type type
;
58 type
= lash_event_get_type(event_ptr
);
59 str
= lash_event_get_string(event_ptr
);
64 printMMCMessage("LASH_Quit received.\n");
69 case LASH_Restore_File
:
70 case LASH_Save_Data_Set
:
72 printMMCMessage("LASH Event. Type = %u, string = \"%s\"\n",
74 (str
== NULL
)?"":str
);
78 void process_lash_config(lash_config_t
* config_ptr
)
84 key
= lash_config_get_key(config_ptr
);
85 data
= lash_config_get_value(config_ptr
);
86 data_size
= lash_config_get_value_size(config_ptr
);
88 printMMCMessage("LASH Config. Key = \"%s\"\n", key
);
90 #endif // LASH_SUPPORT
92 int init_alsa_sequencer(const char* appName
)
94 snd_seq_port_info_t
* seq_port_info
= NULL
;
97 /* ALSA sequencer initialisation */
98 ret
= snd_seq_open( &g_seq_ptr
, "default", SND_SEQ_OPEN_INPUT
, 0);
102 // setup alsa sequencer port
103 snd_seq_port_info_alloca(&seq_port_info
);
104 snd_seq_port_info_set_capability(seq_port_info
, SND_SEQ_PORT_CAP_WRITE
| SND_SEQ_PORT_CAP_SUBS_WRITE
);
105 snd_seq_port_info_set_type( seq_port_info
, SND_SEQ_PORT_TYPE_APPLICATION
);
106 snd_seq_port_info_set_midi_channels(seq_port_info
, 16);
107 snd_seq_port_info_set_port_specified(seq_port_info
, 1);
108 snd_seq_port_info_set_name(seq_port_info
, "midi in");
109 snd_seq_port_info_set_port(seq_port_info
, 0);
111 ret
= snd_seq_create_port(g_seq_ptr
, seq_port_info
);
114 printMMCMessage("Error with alsa sequencer initialization, %s\n", snd_strerror(ret
));
115 else // all is well, register the sequencer with whatever name was passed in
116 snd_seq_set_client_name(g_seq_ptr
, appName
);
121 int init_jack(const char* appName
)
123 jack_status_t status
;
124 g_jack_client
= jack_client_open(appName
, JackNoStartServer
, &status
);
126 if (!g_jack_client
) // something went wrong
127 return -1; // todo: check status to see exactly what happened
134 /* tell jack that we are ready to do our thing */
135 return jack_activate(g_jack_client
);
138 void listen_loop (uint32_t frameRate
, uint32_t jitterTolerance
, int verbose
, uint8_t deviceID
)
140 snd_seq_event_t
* seq_event_ptr
= NULL
;
142 int npfd
= snd_seq_poll_descriptors_count(g_seq_ptr
, POLLIN
);
143 struct pollfd
* pfd
= (struct pollfd
*)alloca(npfd
* sizeof(struct pollfd
));
144 snd_seq_poll_descriptors(g_seq_ptr
, pfd
, npfd
, POLLIN
);
148 if (poll(pfd
, npfd
, 250) > 0 && snd_seq_event_input(g_seq_ptr
, &seq_event_ptr
) >= 0)
150 if (seq_event_ptr
->type
== SND_SEQ_EVENT_SYSEX
&&
151 ((uint8_t *)seq_event_ptr
->data
.ext
.ptr
)[0] == 0xF0 &&
152 ((uint8_t *)seq_event_ptr
->data
.ext
.ptr
)[1] == 0x7F &&
153 ((uint8_t *)seq_event_ptr
->data
.ext
.ptr
)[3] == 0x06)
155 const uint8_t messageDeviceID
= ((uint8_t *)seq_event_ptr
->data
.ext
.ptr
)[2];
156 if (messageDeviceID
== deviceID
)
158 const char mmcCommand
= ((char *)seq_event_ptr
->data
.ext
.ptr
)[4];
163 printMMCMessage("MMC Stop -> JACK transport stop\n");
164 jack_transport_stop(g_jack_client
);
167 case 3: /* deferred play */
169 printMMCMessage("MMC Play -> JACK transport start\n");
170 jack_transport_start(g_jack_client
);
174 printf("MMC Fast Forward -> Ignored\n");
178 printMMCMessage("MMC Play -> JACK transport locate to 0\n");
179 jack_transport_locate(g_jack_client
, 0);
183 printf("MMC Record Strobe (Punch In) -> Ignored\n");
187 printf("MMC Record Exit (Punch out) -> Ignored\n");
191 printf("MMC Record Ready (Record Pause) -> Ignored\n");
195 printf("MMC Pause -> JACK transport stop\n");
196 jack_transport_stop(g_jack_client
);
198 case 0x44: /* goto */
199 if (((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[5] == 0x06 &&
200 ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[6] == 0x01)
202 // some devices call hour 0 "60". Mask off the upper bits
203 uint8_t hour
= ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[7] & 0x1f;
204 uint8_t minute
= ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[8];
205 uint8_t second
= ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[9];
206 uint8_t frame
= ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[10];
207 uint8_t subframe
= ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[11]; // percentage of a frame: 0 - 99
208 jack_position_t jack_pos
;
210 // get Jack's current framerate and position
211 jack_transport_query(g_jack_client
, &jack_pos
);
212 uint32_t jack_frame_rate
= jack_pos
.frame_rate
;
213 uint32_t jack_time_ms
= (jack_pos
.frame
* 1000) / jack_frame_rate
;
214 uint32_t device_time_ms
= ((subframe
* 10) / frameRate
+ // subframe == 1/100th of a frame
215 (frame
* 1000) / frameRate
+
217 (minute
* 60 * 1000) +
218 (hour
* 60 * 60 * 1000));
220 // difference in milliseconds from JACK's reported transport to the MIDI goto's time
221 uint32_t jitter
= (device_time_ms
> jack_time_ms
? (device_time_ms
- jack_time_ms
) : (jack_time_ms
- device_time_ms
));
224 printMMCMessage("MMC locate to hour: %d, minute: %d, second: %d, frame: %d, subframe: %d\n",
231 // check if the JACK clock is far enough away from the MMC time to care
232 if (jitter
> jitterTolerance
)
233 { // it is, change it.
234 jack_pos
.valid
= 0; // only the frame number will be valid since that's all we're changing
236 // need a placeholder so that we can keep precision while not letting the integer overflow
237 uint64_t placeholder
= (uint64_t)device_time_ms
* jack_frame_rate
/ 1000;
238 jack_pos
.frame
= placeholder
;
240 jack_transport_reposition(g_jack_client
, &jack_pos
);
243 printMMCMessage("Difference between JACK time and MMC destination time in ms: %d. New position is outside of jitter range -> repositioning JACK.\n", jitter
);
248 printMMCMessage("Difference between JACK time and MMC destination time in ms: %d. New position is within jitter range -> ignoring.\n", jitter
);
253 case 0x48: /* step */
256 jack_position_t jack_pos
;
258 step
= ((uint8_t*)seq_event_ptr
->data
.ext
.ptr
)[6];
266 printf("MMC Step %d\n", step
);
268 // get Jack's current framerate and position
269 jack_transport_query(g_jack_client
, &jack_pos
);
271 jack_pos
.valid
= 0; // only the frame number will be valid since that's all we're changing
273 jack_pos
.frame
+= (int64_t)step
* (int64_t)jack_pos
.frame_rate
/ (int64_t)10;
275 jack_transport_reposition(g_jack_client
, &jack_pos
);
280 printMMCMessage("unsupported MMC command: 0x%x\n", mmcCommand
);
285 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",
294 /* Process LASH events */
296 lash_event_t
* lash_event_ptr
= NULL
;
297 lash_config_t
* lash_config_ptr
= NULL
;
298 while ((lash_event_ptr
= lash_get_event(g_lashc
)) != NULL
)
300 process_lash_event(lash_event_ptr
);
301 lash_event_destroy(lash_event_ptr
);
304 /* Process LASH configs */
305 while ((lash_config_ptr
= lash_get_config(g_lashc
)) != NULL
)
307 process_lash_config(lash_config_ptr
);
308 lash_config_destroy(lash_config_ptr
);
315 void cleanup_globals()
321 ret
= snd_seq_close(g_seq_ptr
);
324 printMMCMessage("Cannot close sequncer, %s\n", snd_strerror(ret
));
330 jack_deactivate(g_jack_client
);
331 jack_client_close(g_jack_client
);