Updated license to GPL 2+
[jackctlmmc.git] / main.c
blob5dbe26e9046da8ad727957ce6d0374e6f385bdbe
1 /* -*- Mode: C ; c-basic-offset: 3 -*- */
2 /*
3 * Control JACK transport using MMC (MIDI)
5 * Copyright (c) 2006,2007,2008 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (c) 2008 Alex Montgomery <apmontgomery@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free Software
10 * Foundation; version 2 or later of the License.
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <signal.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
26 #include "common.h"
28 void printMMCMessage(const char* message, ...)
30 va_list args;
31 va_start(args, message);
33 vprintf(message, args);
35 va_end(args);
38 /* the signal handler */
39 void signalled(int signal)
41 g_quit = 1;
44 // foward declares
45 void process_command_line(int argc, char *argv[], MidiSettings* settings);
46 void cleanup();
48 int main(int argc, char *argv[])
50 int ret = 0;
52 MidiSettings settings;
54 // frame rate (frames per second) of the MMC controlling device
55 settings.frameRate = 30; // command line equivalent: -f 30
57 // jitter tolerance in milliseconds
58 settings.jitterTolerance = 50; //command line: -t 50
60 settings.deviceID = 0x7f; // command line -d 7f
62 settings.verbose = 0; // pass -v on the command line to turn this on
64 // read the jitter tolerance, verbose setting, and device frame rate from command line
65 process_command_line(argc, argv, &settings);
67 printMMCMessage("%s starting with a jitter tolerance of %d milliseconds and a device frame rate of %d per second listening for device %x.\n",
68 argv[0], settings.jitterTolerance, settings.frameRate, settings.deviceID);
70 // setup our signal handler signalled() above, so we can exit cleanly
71 signal(SIGINT, signalled);
73 ret = init_alsa_sequencer("jackctlmmc");
74 if (ret < 0)
76 printMMCMessage("couldn't initialize alsa sequencer, the show won't go on...\n");
77 cleanup();
78 exit(0);
81 if (init_jack("jackctlmmc") < 0)
83 printMMCMessage("couldn't connect to jack server. Either it's not running or the client name is already taken\n");
84 cleanup();
85 exit(0);
88 #if LASH_SUPPORT
89 init_lash(argc, argv);
90 #endif // LASH_SUPPORT
92 #if JACK_MIDI_SUPPORT
93 if (!init_jack_midi(&settings))
94 printMMCMessage("could not initialize JACK MIDI, alsa port may still work...\n");
95 #endif
97 activate_jack();
99 while (!g_quit)
101 uint8_t* midiData = get_midi_input();
102 if (midiData)
103 handle_midi(midiData, &settings);
106 // so we shall quit, eh? ok, cleanup time. otherwise jack would probably produce an xrun on shutdown
107 cleanup();
109 if (settings.verbose)
110 printMMCMessage("exited normally\n");
112 return 0;
116 void process_command_line(int argc, char *argv[], // IN parameters
117 MidiSettings* midiSettings) // OUT parameters
119 int parameter;
120 long value;
121 while ((parameter = getopt(argc, argv, "f:t:d:v")) != -1)
123 switch (parameter)
125 case 'v':
126 midiSettings->verbose = 1;
127 break;
128 case 'f':
129 value = strtol(optarg, 0, 10);
130 midiSettings->frameRate = (uint32_t)value;
131 break;
132 case 't':
133 value = strtol(optarg, 0, 10);
134 midiSettings->jitterTolerance = (uint32_t)value;
135 break;
136 case 'd':
137 value = strtol(optarg, 0, 16);
138 if (value >= 0 && value <= 255)
139 midiSettings->deviceID = (uint8_t)value;
140 break;
141 case '?':
142 printMMCMessage("usage: %s -f <device_frames_per_second> -t <jitter_tolerance_in_ms> -d <device_id_in_hexadecimal>\n", argv[0]);
143 exit(1);
144 break;
149 void cleanup()
151 printMMCMessage("cleaning up...\n");
152 cleanup_globals();