Merge branch 'master' of git://repo.or.cz/jackctlmmc
[jackctlmmc.git] / common.c
blobd2046792703c7351a3110cfe2b115075d484e09c
1 /*
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
20 #include "common.h"
22 // common globals
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
27 #if LASH_SUPPORT
28 lash_client_t* g_lashc = NULL;
30 void init_lash(int argc, char* argv[])
32 lash_event_t * lash_event_ptr = NULL;
34 /* LASH setup */
35 g_lashc = lash_init( lash_extract_args(&argc, &argv), "jackctlmmc", 0, LASH_PROTOCOL_VERSION);
37 if (g_lashc == NULL)
39 printMMCMessage("Failed to connect to LASH. Session management will not occur.\n");
41 else
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;
56 const char * str;
58 type = lash_event_get_type(event_ptr);
59 str = lash_event_get_string(event_ptr);
61 switch (type)
63 case LASH_Quit:
64 printMMCMessage("LASH_Quit received.\n");
65 g_lashc = NULL;
66 g_quit = 1;
67 break;
68 case LASH_Save_File:
69 case LASH_Restore_File:
70 case LASH_Save_Data_Set:
71 default:
72 printMMCMessage("LASH Event. Type = %u, string = \"%s\"\n",
73 (unsigned int)type,
74 (str == NULL)?"":str);
78 void process_lash_config(lash_config_t * config_ptr)
80 const char * key;
81 const void * data;
82 size_t data_size;
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;
95 int ret = 0;
97 /* ALSA sequencer initialisation */
98 ret = snd_seq_open( &g_seq_ptr, "default", SND_SEQ_OPEN_INPUT, 0);
99 if (ret < 0)
100 return ret;
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);
113 if (ret < 0)
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);
118 return ret;
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
129 return 0;
132 int activate_jack()
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);
146 while(!g_quit)
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];
159 switch (mmcCommand)
161 case 1: /* stop */
162 if (verbose)
163 printMMCMessage("MMC Stop -> JACK transport stop\n");
164 jack_transport_stop(g_jack_client);
165 break;
166 case 2: /* play */
167 case 3: /* deferred play */
168 if (verbose)
169 printMMCMessage("MMC Play -> JACK transport start\n");
170 jack_transport_start(g_jack_client);
171 break;
172 case 4:
173 if (verbose)
174 printf("MMC Fast Forward -> Ignored\n");
175 break;
176 case 5: /* rewind */
177 if (verbose)
178 printMMCMessage("MMC Play -> JACK transport locate to 0\n");
179 jack_transport_locate(g_jack_client, 0);
180 break;
181 case 6:
182 if (verbose)
183 printf("MMC Record Strobe (Punch In) -> Ignored\n");
184 break;
185 case 7:
186 if (verbose)
187 printf("MMC Record Exit (Punch out) -> Ignored\n");
188 break;
189 case 8:
190 if (verbose)
191 printf("MMC Record Ready (Record Pause) -> Ignored\n");
192 break;
193 case 9: /* pause */
194 if (verbose)
195 printf("MMC Pause -> JACK transport stop\n");
196 jack_transport_stop(g_jack_client);
197 break;
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 +
216 (second * 1000) +
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));
223 if (verbose)
224 printMMCMessage("MMC locate to hour: %d, minute: %d, second: %d, frame: %d, subframe: %d\n",
225 hour,
226 minute,
227 second,
228 frame,
229 subframe);
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);
242 if (verbose)
243 printMMCMessage("Difference between JACK time and MMC destination time in ms: %d. New position is outside of jitter range -> repositioning JACK.\n", jitter);
245 else if (verbose)
247 if (verbose)
248 printMMCMessage("Difference between JACK time and MMC destination time in ms: %d. New position is within jitter range -> ignoring.\n", jitter);
252 break;
253 case 0x48: /* step */
255 int step;
256 jack_position_t jack_pos;
258 step = ((uint8_t*)seq_event_ptr->data.ext.ptr)[6];
259 if (step > 0x40)
261 step &= 0x3F;
262 step = -step;
265 if (verbose)
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);
277 break;
278 default:
279 if (verbose)
280 printMMCMessage("unsupported MMC command: 0x%x\n", mmcCommand);
283 else if (verbose)
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",
286 messageDeviceID,
287 deviceID);
293 #if LASH_SUPPORT
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);
311 #endif
315 void cleanup_globals()
317 int ret = 0;
319 if (g_seq_ptr)
321 ret = snd_seq_close(g_seq_ptr);
322 if (ret < 0)
324 printMMCMessage("Cannot close sequncer, %s\n", snd_strerror(ret));
328 if (g_jack_client)
330 jack_deactivate(g_jack_client);
331 jack_client_close(g_jack_client);