2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 server_c.c - TiMidity server written by Masanao Izumo <mo@goice.co.jp>
21 Mon Apr 5 1999: Initial created.
24 Launch TiMidity server: (example)
28 The protocol is based on OSS interface.
29 TiMidity server has 2 TCP/IP connection. They are control port and
32 ASCII text protocol like FTP control port. See command_help for
33 control protocol in this source code.
35 One way Binary stream data from client to server. The format of
36 stream is same as OSS sequencer stream.
40 Protocol specification to be documented.
45 #endif /* HAVE_CONFIG_H */
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
62 #ifdef HAVE_SYS_SOUNDCARD_H
63 #include <sys/soundcard.h>
65 #include "server_defs.h"
66 #endif /* HAVE_SYS_SOUNDCARD_H */
79 /* #define DEBUG_DUMP_SEQ 1 */
80 #define MIDI_COMMAND_PER_SEC 100
81 #define DEFAULT_LOW_TIMEAT 0.4
82 #define DEFAULT_HIGH_TIMEAT 0.6
83 #define DONT_STOP_AUDIO 1
84 #define DEFAULT_TIMEBASE 100 /* HZ? */
85 #define MAXTICKDIFF 150
86 #define SIG_TIMEOUT_SEC 3
89 static int cmd_help(int argc, char **argv);
90 static int cmd_open(int argc, char **argv);
91 static int cmd_close(int argc, char **argv);
92 static int cmd_timebase(int argc, char **argv);
93 static int cmd_reset(int argc, char **argv);
94 static int cmd_patch(int argc, char **argv);
95 static int cmd_quit(int argc, char **argv);
96 static int cmd_queue(int argc, char **argv);
97 static int cmd_maxqueue(int argc, char **argv);
98 static int cmd_time(int argc, char **argv);
99 static int cmd_nop(int argc, char **argv);
100 static int cmd_autoreduce(int argc, char **argv);
101 static int cmd_setbuf(int argc, char **argv);
107 int (* proc)(int argc, char **argv); /* argv[0] is command name
108 * argv[1..argc-1] is the arg.
109 * return: 0=success, -1=fatal-error,
110 * 1=connection-closed
115 "HELP\tDisplay this message",
119 "\tOpen data connection\n"
120 "\tlsb: The byte order of data stream is LSB\n"
121 "\tmsb: The byte order of data stream is MSB",
124 "CLOSE\tShutdown current data connection",
127 "TIMEBASE [timebase]\n\tSet time base",
130 "RESET\tInitialize all of MIDI status",
133 "PATCH {drumset|bank} <bank-no> <prog-no>\n\tLoad specified patch",
136 "QUIT\tClose control connection",
139 "QUEUE\tTiMidity tells the time of audio buffer queue in second",
143 "\tTiMidity tells the maxmum time of audio buffer queue in second",
146 "TIME\tTiMidity tells the current playing time in second",
152 "AUTOREDUCE {on|off} [msec]\n\tEnable/Disable auto voice reduction",
153 2, 3, cmd_autoreduce},
155 "SETBUF low hi\n\tSpecify low/hi sec of buffer queue",
158 {NULL, NULL, 0, 0, NULL} /* terminate */
163 Change the tempo. If the argument is omitted, TiMidity tells the\n\
165 KEYSHIFT <{+|-}offs>\n\
166 Change the base key. (0 to reset)\n\
168 Change the play speed. (0 to reset)\n\
170 Specify default MIDI system mode\n\
172 Specify Synth type emulation. (no argument to default)\n\
176 static int ctl_open(int using_stdin, int using_stdout);
177 static void ctl_close(void);
178 static int ctl_read(int32 *valp);
179 static int cmsg(int type, int verbosity_level, char *fmt, ...);
180 static void ctl_event(CtlEvent *e);
181 static void ctl_pass_playing_list(int n, char *args[]);
183 /**********************************/
184 /* export the interface functions */
186 #define ctl server_control_mode
190 "remote interface", 'r',
195 ctl_pass_playing_list,
202 struct fd_read_buffer
205 /* count: beginning of read pointer
206 * size: end of read pointer
207 * fd: file descripter for input
211 static int fdgets(char *buff, size_t buff_size, struct fd_read_buffer *p);
212 static int fdputs(char *s, int fd);
213 static uint32 data2long(uint8* data);
214 static uint16 data2short(uint8* data);
215 static int do_control_command_nonblock(void);
217 static struct fd_read_buffer control_fd_buffer;
218 static int data_fd = -1, control_fd = -1;
219 static int data_port, control_port;
220 static int is_lsb_data = 1;
221 static int curr_timebase = DEFAULT_TIMEBASE;
222 static int32 sample_correction;
223 static int32 sample_increment;
224 static int32 sample_correction;
225 static int32 sample_cum;
226 static int32 curr_event_samples, event_time_offset;
227 static int32 curr_tick, tick_offs;
228 static double start_time;
229 static int tmr_running;
231 static int is_system_prefix = 0;
232 static struct sockaddr_in control_client;
233 static double low_time_at = 0.3;
234 static double high_time_at = 0.5;
238 static int ctl_open(int using_stdin, int using_stdout)
241 ctl.flags &= ~(CTLF_LIST_RANDOM|CTLF_LIST_SORT);
249 static void ctl_close(void)
266 static int ctl_read(int32 *valp)
269 do_control_command_nonblock();
273 static int cmsg(int type, int verbosity_level, char *fmt, ...)
277 if((type==CMSG_TEXT || type==CMSG_INFO || type==CMSG_WARNING) &&
278 ctl.verbosity < verbosity_level)
285 vfprintf(outfp, fmt, ap);
293 static void ctl_event(CtlEvent *e)
297 static int pasv_open(int *port)
300 struct sockaddr_in server;
302 if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
308 memset(&server, 0, sizeof(server));
309 server.sin_port = htons(*port);
310 server.sin_family = AF_INET;
311 server.sin_addr.s_addr = htonl(INADDR_ANY);
316 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (caddr_t)&on, sizeof(on));
318 #endif /* SO_REUSEADDR */
320 ctl.cmsg(CMSG_INFO, VERB_DEBUG, "Bind TCP/IP port=%d", *port);
321 if(bind(sfd, (struct sockaddr *)&server, sizeof(server)) < 0)
329 int len = sizeof(server);
330 if(getsockname(sfd, (struct sockaddr *)&server, &len) < 0)
332 perror("getsockname");
336 *port = ntohs(server.sin_port);
339 /* Set it up to wait for connections. */
340 if(listen(sfd, 1) < 0)
350 static RETSIGTYPE sig_timeout(int sig)
352 signal(SIGALRM, sig_timeout); /* For SysV base */
356 static void doit(void);
357 static int send_status(int status, char *message, ...);
358 static void compute_sample_increment(void);
359 static void server_reset(void);
361 static void ctl_pass_playing_list(int n, char *args[])
367 fprintf(stderr, "Usage: timidity -ir control-port [data-port]\n");
372 signal(SIGPIPE, SIG_IGN); /* Handle broken pipe */
375 control_port = atoi(args[0]);
377 data_port = atoi(args[1]);
382 sock = pasv_open(&control_port);
386 opt_realtime_playing = 1; /* Enable loading patch while playing */
387 allocate_cache_size = 0; /* Don't use pre-calclated samples */
388 /* aq_set_soft_queue(-1.0, 0.0); */
390 signal(SIGALRM, sig_timeout);
392 play_mode->close_output();
397 addrlen = sizeof(control_client);
398 memset(&control_client, 0, addrlen);
401 if((control_fd = accept(sock,
402 (struct sockaddr *)&control_client,
414 if(play_mode->open_output() < 0)
416 ctl.cmsg(CMSG_FATAL, VERB_NORMAL,
417 "Couldn't open %s (`%c')",
418 play_mode->id_name, play_mode->id_character);
419 send_status(510, "Couldn't open %s (`%c')",
420 play_mode->id_name, play_mode->id_character);
430 ctl.cmsg(CMSG_INFO, VERB_NOISY, "Connected");
432 ctl.cmsg(CMSG_INFO, VERB_NOISY, "Connection closed");
434 play_mode->close_output();
436 if(control_fd != -1 && control_port)
447 free_global_mblock();
453 #define MAX_GETCMD_PARAMS 8
457 *-1: fatal error (will be close the connection)
459 static int control_getcmd(char **params, int *nparams)
461 static char buff[BUFSIZ];
465 n = fdgets(buff, sizeof(buff), &control_fd_buffer);
473 if((params[0] = strtok(buff, " \t\r\n\240")) == NULL)
476 while(params[*nparams] && *nparams < MAX_GETCMD_PARAMS)
477 params[++(*nparams)] = strtok(NULL," \t\r\n\240");
481 static int send_status(int status, char *message, ...)
486 va_start(ap, message);
487 sprintf(buff, "%03d ", status);
488 vsnprintf(buff + 4, sizeof(buff) - 5, message, ap);
491 if(write(control_fd, buff, strlen(buff)) == -1)
496 static void seq_play_event(MidiEvent *ev)
499 ev->time = curr_event_samples;
504 event_time_offset += play_mode->rate / MIDI_COMMAND_PER_SEC;
505 ev->time = curr_event_samples;
509 double past_time = get_current_calender_time() - start_time;
510 if(play_mode->flag & PF_PCM_STREAM)
511 past_time += high_time_at;
512 ev->time = (int32)(past_time * play_mode->rate);
515 ev->time += event_time_offset;
519 static void tmr_reset(void)
524 playmidi_tmr_reset();
525 curr_timebase = DEFAULT_TIMEBASE;
526 curr_tick = tick_offs = 0;
527 start_time = get_current_calender_time();
530 static void compute_sample_increment(void)
533 a = (double)current_play_tempo * (double)play_mode->rate
534 * (65536.0/500000.0) / (double)curr_timebase,
535 sample_correction = (int32)(a) & 0xFFFF;
536 sample_increment = (int32)(a) >> 16;
539 static void add_tick(int tick)
544 samples_to_do = sample_increment * tick;
545 sample_cum += sample_correction * tick;
546 if(sample_cum & 0xFFFF0000)
548 samples_to_do += ((sample_cum >> 16) & 0xFFFF);
549 sample_cum &= 0x0000FFFF;
551 curr_event_samples += samples_to_do;
557 static int tick2sample(int tick)
561 samples = sample_increment * tick;
562 cum = sample_correction * tick;
564 samples += ((sample_cum >> 16) & 0xFFFF);
568 int time2tick(double sec)
570 return (int)(sec * curr_timebase);
574 static void stop_playing(void)
587 static int do_control_command(void);
588 static int do_control_command_nonblock(void);
589 static int do_sequencer(void);
590 static void do_chn_voice(uint8 *);
591 static void do_chn_common(uint8 *);
592 static void do_timing(uint8 *);
593 static void do_sysex(uint8 *, int len);
594 static void do_extended(uint8 *);
595 static void do_timeout(void);
596 static void server_seq_sync(double tm);
598 static uint8 data_buffer[BUFSIZ];
599 static int data_buffer_len;
601 static void doit(void)
603 memset(&control_fd_buffer, 0, sizeof(control_fd_buffer));
604 control_fd_buffer.fd = control_fd;
606 send_status(220, "TiMidity++ v%s ready", timidity_version);
608 /* while(data_fd != -1 && control_fd != -1) */
609 while(control_fd != -1)
616 if(do_control_command())
624 FD_SET(control_fd, &fds);
625 FD_SET(data_fd, &fds);
626 if(control_fd > data_fd)
636 filled = aq_filled();
637 if(!tmr_running && filled <= 0)
641 wait_time = (double)filled / play_mode->rate
646 usec = (long)(wait_time * 1000000);
654 struct timeval timeout;
655 timeout.tv_sec = usec / 1000000;
656 timeout.tv_usec = usec % 1000000;
657 n = select(maxfd + 1, &fds, NULL, NULL, &timeout);
660 n = select(maxfd + 1, &fds, NULL, NULL, NULL);
670 if(ctl.verbosity >= VERB_DEBUG)
679 if(control_fd != -1 && FD_ISSET(control_fd, &fds))
681 if(do_control_command())
687 else if(data_fd != -1 && FD_ISSET(data_fd, &fds))
693 send_status(403, "Data connection is closed");
703 static void do_timeout(void)
707 if(data_fd == -1 || !IS_STREAM_TRACE)
710 fill_time = high_time_at - (double)aq_filled() / play_mode->rate;
715 add_tick(time2tick(fill_time));
719 event_time_offset += (int32)(fill_time *
721 ev.time = curr_event_samples + event_time_offset;
727 /* -1=error, 0=success, 1=connection-closed */
728 static int data_flush(int discard)
732 struct timeval timeout;
738 FD_SET(data_fd, &fds);
741 timeout.tv_usec = 100000;
744 if((n = select(data_fd + 1, &fds, NULL, NULL, &timeout)) < 0)
753 if((n = read(data_fd, buff, sizeof(buff))) < 0)
764 if((status = do_sequencer()) != 0)
771 static void server_seq_sync(double tm)
775 t = (double)aq_filled() / play_mode->rate;
777 usleep((unsigned long)((t - tm) * 1000000));
780 static void server_reset(void)
782 playmidi_stream_init();
783 if(free_instruments_afterwards)
786 do_sysex(NULL, 0); /* Initialize SysEx buffer */
787 low_time_at = DEFAULT_LOW_TIMEAT;
788 high_time_at = DEFAULT_HIGH_TIMEAT;
789 reduce_voice_threshold = 0; /* Disable auto reduction voice */
790 compute_sample_increment();
793 start_time = get_current_calender_time();
796 /* -1=error, 0=success, 1=connection-closed */
797 static int do_control_command(void)
800 char *params[MAX_GETCMD_PARAMS];
804 if((status = control_getcmd(params, &nparams)) == -1)
808 send_status(500, "Error");
812 if(nparams == 0 || *params == NULL || **params == '\0')
815 for(i = 0; cmd_table[i].cmd; i++)
816 if(strcasecmp(params[0], cmd_table[i].cmd) == 0)
818 if(nparams < cmd_table[i].minarg)
819 return send_status(501, "'%s': Arguments is too few",
821 if(nparams > cmd_table[i].maxarg)
822 return send_status(501, "'%s': Arguments is too many",
824 return cmd_table[i].proc(nparams, params);
826 return send_status(500, "'%s': command not understood.", params[0]);
829 static int cmd_help(int argc, char **argv)
833 if(send_status(200, "Help ok"))
836 for(i = 0; cmd_table[i].cmd; i++)
838 if(fdputs(cmd_table[i].help, control_fd))
840 if(fdputs("\n", control_fd))
843 return fdputs(".\n", control_fd);
846 static int cmd_open(int argc, char **argv)
849 struct sockaddr_in in;
854 return send_status(125, "Data connection is already opened");
856 if(strcasecmp(argv[1], "lsb") == 0)
858 else if(strcasecmp(argv[1], "msb") == 0)
861 return send_status(502, "OPEN: Invalid argument: %s", argv[1]);
864 if((sock = pasv_open(&port)) == -1)
865 return send_status(511, "Can't open data connection");
867 addrlen = sizeof(in);
868 memset(&in, 0, addrlen);
869 send_status(200, "%d is ready acceptable", port);
871 alarm(SIG_TIMEOUT_SEC);
872 data_fd = accept(sock, (struct sockaddr *)&in, &addrlen);
877 send_status(512, "Accept error");
883 if(control_client.sin_addr.s_addr != in.sin_addr.s_addr)
884 return send_status(513, "Security violation: Address mismatch");
886 send_status(200, "Ready data connection");
888 do_sysex(NULL, 0); /* Initialize SysEx buffer */
894 static int cmd_close(int argc, char **argv)
900 return send_status(302, "Data connection is closed");
902 return send_status(302, "Data connection is already closed");
905 static int cmd_queue(int argc, char **argv)
909 aq_add(NULL, 0); /* Update software queue */
910 if(!aq_fill_buffer_flag)
911 qsamples = aq_soft_filled() + aq_filled();
914 return send_status(200, "%f sec", (double)qsamples / play_mode->rate);
917 static int cmd_maxqueue(int argc, char **argv)
919 return send_status(200, "%f sec",
920 (double)aq_get_dev_queuesize() / play_mode->rate);
923 static int cmd_time(int argc, char **argv)
925 return send_status(200, "%f sec", (double)aq_samples());
928 static int cmd_quit(int argc, char **argv)
930 send_status(200, "Bye");
934 static int cmd_timebase(int argc, char **argv)
939 return send_status(200, "%d OK", curr_timebase);
945 if(i != curr_timebase)
948 compute_sample_increment();
949 tick_offs = curr_tick;
950 start_time = get_current_calender_time();
952 return send_status(200, "OK");
955 static int cmd_patch(int argc, char **argv)
959 if(strcasecmp(argv[1], "drumset") == 0)
961 else if(strcasecmp(argv[1], "bank") == 0)
964 return send_status(502, "PATCH: Invalid argument: %s", argv[1]);
966 bank = atoi(argv[2]);
967 prog = atoi(argv[3]);
968 if(bank < 0 || bank > 127 || prog < 0 || prog > 127)
969 return send_status(502, "PATCH: Invalid argument");
970 if(play_midi_load_instrument(dr, bank, prog) == NULL)
971 return send_status(514, "PATCH: Can't load the patch");
972 return send_status(200, "OK");
975 static int cmd_reset(int argc, char **argv)
982 if((status = data_flush(1)) != 0)
986 return send_status(200, "OK");
989 static int cmd_autoreduce(int argc, char **argv)
991 if(strcasecmp(argv[1], "on") == 0)
994 reduce_voice_threshold = atoi(argv[2]);
996 reduce_voice_threshold = -1;
998 else if(strcasecmp(argv[1], "off") == 0)
999 reduce_voice_threshold = 0;
1001 return send_status(502, "AUTOREDUCE: Invalid argument: %s",
1003 return send_status(200, "OK");
1006 static int cmd_setbuf(int argc, char **argv)
1008 low_time_at = atof(argv[1]);
1009 high_time_at = atof(argv[1]);
1010 return send_status(200, "OK");
1013 static int cmd_nop(int argc, char **argv)
1015 return send_status(200, "NOP OK");
1018 static int do_control_command_nonblock(void)
1020 struct timeval timeout;
1024 if(control_fd == -1)
1027 FD_SET(control_fd, &fds);
1029 timeout.tv_usec = 0;
1030 n = select(control_fd + 1, &fds, NULL, NULL, &timeout);
1031 if(n > 0 && FD_ISSET(control_fd, &fds))
1032 return do_control_command();
1036 static int fdgets(char *buff, size_t buff_size, struct fd_read_buffer *p)
1038 int n, len, count, size, fd;
1039 char *buff_endp = buff + buff_size - 1, *pbuff, *beg;
1044 else if(buff_size == 1) /* buff == buff_endp */
1059 if((n = read(fd, pbuff, BUFSIZ)) <= 0)
1064 p->count = p->size = 0;
1067 return -1; /* < 0 error */
1069 count = p->count = 0;
1072 *buff++ = pbuff[count++];
1073 } while(*(buff - 1) != '\n' && buff != buff_endp);
1079 static int fdputs(char *s, int fd)
1081 if(write(fd, s, strlen(s)) == -1)
1086 #ifdef LITTLE_ENDIAN
1087 static uint32 data2long(uint8* data)
1090 memcpy(&x, data, sizeof(x));
1095 static uint16 data2short(uint8* data)
1098 memcpy(&x, data, sizeof(x));
1104 static uint32 data2long(uint8* data)
1107 memcpy(&x, data, sizeof(x));
1112 static uint16 data2short(uint8* data)
1115 memcpy(&x, data, sizeof(x));
1122 static int do_sequencer(void)
1127 n = read(data_fd, data_buffer + data_buffer_len,
1128 sizeof(data_buffer) - data_buffer_len);
1136 #ifdef DEBUG_DUMP_SEQ
1139 for(i = 0; i < n; i++)
1140 printf("%02x", data_buffer[data_buffer_len + i]);
1143 #endif /* DEBUG_DUMP_SEQ */
1145 data_buffer_len += n;
1147 while(offset < data_buffer_len)
1150 cmd = data_buffer[offset];
1152 #define READ_NEEDBUF(x) if(offset + x > data_buffer_len) goto done;
1153 #define READ_ADVBUF(x) offset += x;
1158 do_chn_voice(data_buffer + offset);
1163 do_chn_common(data_buffer + offset);
1168 do_timing(data_buffer + offset);
1173 do_sysex(data_buffer + offset + 2, 6);
1177 if(is_system_prefix)
1180 do_sysex(data_buffer + offset + 1, 1);
1181 if(data_buffer[offset + 1] == 0xf7)
1182 is_system_prefix = 0; /* End SysEX */
1187 switch(data_buffer[offset + 1] & 0xf0)
1191 ev.channel = data_buffer[offset + 1] & 0x0f;
1192 ev.a = data_buffer[offset + 5] & 0x7f;
1193 ev.b = data_buffer[offset + 9] & 0x7f;
1195 ev.type = ME_NOTEON;
1197 ev.type = ME_NOTEOFF;
1198 seq_play_event(&ev);
1204 ev.type = ME_NOTEOFF;
1205 ev.channel = data_buffer[offset + 1] & 0x0f;
1206 ev.a = data_buffer[offset + 5] & 0x7f;
1207 ev.b = data_buffer[offset + 9] & 0x7f;
1208 seq_play_event(&ev);
1212 case MIDI_KEY_PRESSURE:
1214 ev.type = ME_KEYPRESSURE;
1215 ev.channel = data_buffer[offset + 1] & 0x0f;
1216 ev.a = data_buffer[offset + 5] & 0x7f;
1217 ev.b = data_buffer[offset + 9] & 0x7f;
1218 seq_play_event(&ev);
1222 case MIDI_CTL_CHANGE:
1224 if(convert_midi_control_change(data_buffer[offset + 1] & 0x0f,
1225 data_buffer[offset + 5],
1226 data_buffer[offset + 9],
1228 seq_play_event(&ev);
1232 case MIDI_PGM_CHANGE:
1234 ev.type = ME_PROGRAM;
1235 ev.channel = data_buffer[offset + 1] & 0x0f;
1236 ev.a = data_buffer[offset + 5] & 0x7f;
1238 seq_play_event(&ev);
1242 case MIDI_CHN_PRESSURE:
1244 ev.type = ME_CHANNEL_PRESSURE;
1245 ev.channel = data_buffer[offset + 1] & 0x0f;
1246 ev.a = data_buffer[offset + 5] & 0x7f;
1248 seq_play_event(&ev);
1252 case MIDI_PITCH_BEND:
1254 ev.type = ME_PITCHWHEEL;
1255 ev.channel = data_buffer[offset + 1] & 0x0f;
1256 ev.a = data_buffer[offset + 5] & 0x7f;
1257 ev.b = data_buffer[offset + 9] & 0x7f;
1258 seq_play_event(&ev);
1262 case MIDI_SYSTEM_PREFIX:
1264 do_sysex(data_buffer + offset + 1, 1);
1265 is_system_prefix = 1; /* Start SysEX */
1270 ctl.cmsg(CMSG_ERROR, VERB_NORMAL,
1271 "Undefined SEQ_MIDIPUTC 0x%02x",
1272 data_buffer[offset + 1]);
1273 send_status(402, "Undefined SEQ_MIDIPUTC 0x%02x",
1274 data_buffer[offset + 1]);
1280 /* WARNING: This data may be devided into some socket fragments. */
1281 offset = data_buffer_len;
1282 ctl.cmsg(CMSG_WARNING, VERB_NORMAL,
1283 "SEQ_FULLSIZE is received. This command is not safety.");
1288 do_extended(data_buffer + offset);
1300 ctl.cmsg(CMSG_ERROR, VERB_NORMAL,
1301 "Undefined data 0x%02x", data_buffer[offset - 1]);
1302 send_status(401, "Wrong data is recieved (seqcmd=0x%02x)",
1314 data_buffer_len -= offset;
1315 memmove(data_buffer, data_buffer + offset, data_buffer_len);
1321 static void do_chn_voice(uint8 *data)
1323 int type, chn, note, parm;
1327 chn = data[3] % MAX_CHANNELS;
1328 note = data[4] & 0x7f;
1329 parm = data[5] & 0x7f;
1339 ev.type = ME_NOTEON;
1340 seq_play_event(&ev);
1345 ev.type = ME_NOTEOFF;
1346 seq_play_event(&ev);
1348 case MIDI_KEY_PRESSURE:
1349 ev.type = ME_KEYPRESSURE;
1350 seq_play_event(&ev);
1355 static void do_chn_common(uint8 *data)
1357 int type, chn, p1, p2, w14;
1361 chn = data[3] % MAX_CHANNELS;
1362 p1 = data[4] & 0x7f;
1363 p2 = data[5] & 0x7f;
1364 w14 = data2short(data + 6) & 0x3fff;
1366 if(type == 0xff) /* Meta event */
1368 /* This event is special event for timidity. (not OSS compatible) */
1371 case 0x2f: /* End of midi */
1376 case 0x7f: /* Sequencer-Specific Meta-Event */
1379 case 0x01: /* MIDI Reset */
1381 ev.a = DEFAULT_SYSTEM_MODE;
1383 seq_play_event(&ev);
1386 case 0x02: { /* Used to sync. */
1387 double target_time, queue_time, sleep_time;
1390 aq_flush(0); /* wait until playout */
1391 send_status(301, "0 Sync OK");
1396 target_time = (double)w14 / curr_timebase;
1397 queue_time = (double)aq_filled() / play_mode->rate;
1398 sleep_time = queue_time - target_time;
1401 send_status(301, "%g Sync OK", sleep_time);
1402 usleep((unsigned long)(sleep_time * 1000000));
1405 send_status(301, "0 Sync OK");
1416 case MIDI_CTL_CHANGE:
1417 if(convert_midi_control_change(chn, p1, w14, &ev))
1418 seq_play_event(&ev);
1420 case MIDI_PGM_CHANGE:
1421 ev.type = ME_PROGRAM;
1424 seq_play_event(&ev);
1426 case MIDI_CHN_PRESSURE:
1427 ev.type = ME_CHANNEL_PRESSURE;
1430 seq_play_event(&ev);
1432 case MIDI_PITCH_BEND:
1433 ev.type = ME_PITCHWHEEL;
1435 ev.b = (w14>>7) & 0x7f;
1436 seq_play_event(&ev);
1442 static void do_timing(uint8 *data)
1446 val = data2long(data + 4);
1452 event_time_offset = (int32)(play_mode->rate * high_time_at);
1463 tick_offs = curr_tick;
1464 start_time = get_current_calender_time();
1469 #if 0 /* What should TMR_TEMPO work ? */
1474 current_play_tempo = 60 * 1000000 / val;
1475 compute_sample_increment();
1482 printf("## TMR_WAIT_ABS: %d %d %d %g\n",
1484 curr_tick - (time2tick(get_current_calender_time()
1485 - start_time) + tick_offs),
1487 (double)aq_filled() / play_mode->rate);
1503 /* printf("## TMR=0x%02x is not supported\n", data[1]); */
1509 static void do_sysex(uint8 *data, int n)
1511 static uint8 sysexbuf[BUFSIZ];
1513 static int fillflag;
1518 buflen = fillflag = 0;
1519 is_system_prefix = 0;
1523 for(i = 0; i < n; i++)
1525 /* SysEx := /\xf0([^\xf7]*\xf7)/ */
1536 if(buflen < sizeof(sysexbuf))
1537 sysexbuf[buflen++] = data[i];
1541 if(parse_sysex_event(sysexbuf, buflen, &ev))
1542 seq_play_event(&ev);
1550 static void do_extended(uint8 *data)
1555 value = data[5] | data[6] * 256;
1558 case SEQ_CONTROLLER:
1561 case CTRL_PITCH_BENDER: /* ?? */
1563 case CTRL_PITCH_BENDER_RANGE: /* ?? */
1564 ev.channel = data[3] % MAX_CHANNELS;
1569 ev.type = ME_NRPN_LSB;
1570 seq_play_event(&ev);
1573 ev.type = ME_NRPN_MSB;
1574 seq_play_event(&ev);
1577 ev.type = ME_DATA_ENTRY_MSB;
1578 ev.a = value / 100; /* ?? */
1579 seq_play_event(&ev);
1593 * interface_<id>_loader();
1595 ControlMode *interface_r_loader(void)