annotate decoders/midi.c @ 108:803ba4dabbce

Initial add, from Torbj�rn Andersson.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 03 Oct 2001 20:17:13 +0000
parents
children 70baf29577d8
rev   line source
108
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * MIDI decoder for SDL_sound, using TiMidity.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This is a rough, proof-of-concept implementation. A number of things need
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * to be fixed or at the very least looked at:
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 * - I have only tested this with TiMidity++. Does TiMidity use the same
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 * command-line options?
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 * - No attempt is made to ensure that the input really is MIDI. Eek. Does
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 * anyone here know how to recognize MIDI?
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 * - Error handling is a joke.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 * - Did I make any stupid errors in the process communication?
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 * - TiMidity++ spews a number of messages (to stderr?) which should be
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 * silenced eventually.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 * - This has to be the least portable decoder that has ever been written.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 * - Etc.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 * Oh, and the whole thing should be rewritten as a generic "send data to
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 * external decoder" thing so this version should either be scrapped or
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 * cannibalized for ideas. :-)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 * Please see the file LICENSE in the source's root directory.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 *
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 * This file written by Torbjörn Andersson. (d91tan@Update.UU.SE)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 #if HAVE_CONFIG_H
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 # include <config.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 #endif
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 #ifdef SOUND_SUPPORTS_MIDI
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 #include <stdio.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 #include <stdlib.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 #include <string.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 #include <assert.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 #include <unistd.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 #include <signal.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 #include <sys/types.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 #include <sys/wait.h>
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 #include "SDL_sound.h"
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 #define __SDL_SOUND_INTERNAL__
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 #include "SDL_sound_internal.h"
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 static int MIDI_init(void);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 static void MIDI_quit(void);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 static int MIDI_open(Sound_Sample *sample, const char *ext);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 static void MIDI_close(Sound_Sample *sample);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 static Uint32 MIDI_read(Sound_Sample *sample);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 const Sound_DecoderFunctions __Sound_DecoderFunctions_MIDI =
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 "MIDI",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 "MIDI music through the TiMidity MIDI to WAVE converter",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 "Torbjörn Andersson <d91tan@Update.UU.SE>",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 "http://www.goice.co.jp/member/mo/timidity/"
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 },
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 MIDI_init, /* init() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 MIDI_quit, /* quit() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 MIDI_open, /* open() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 MIDI_close, /* close() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 MIDI_read /* read() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 };
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 /* this is what we store in our internal->decoder_private field... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 typedef struct
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 int fd_audio;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 } midi_t;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 static void sig_pipe(int signo)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 signal(signo, sig_pipe);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 } /* sig_pipe */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 static int MIDI_init(void)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 if (signal(SIGPIPE, sig_pipe) == SIG_ERR)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 Sound_SetError("MIDI: Could not set up SIGPIPE signal handler.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 return(1);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 } /* MIDI_init */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 static void MIDI_quit(void)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 /* it's a no-op. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 } /* MIDI_quit */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 static int MIDI_open(Sound_Sample *sample, const char *ext)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 SDL_RWops *rw = internal->rw;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 midi_t *m;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 Uint8 buf[BUFSIZ];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 ssize_t retval;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 int fd1[2];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 int fd2[2];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 pid_t pid;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 /* TiMidity does all the conversions we need. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 memcpy(&sample->actual, &sample->desired, sizeof (Sound_AudioInfo));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 sample->flags = SOUND_SAMPLEFLAG_NONE;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 if (pipe(fd1) < 0 || pipe(fd2) < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 Sound_SetError("MIDI: Pipe error.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 pid = fork();
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 if (pid < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 Sound_SetError("MIDI: Fork error.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 if (pid == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 /* child */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 char arg_freq[10]; /* !!! FIXME: Large enough? */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 char arg_format[10];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 char *arg_list[] =
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 { "timidity", "-s", NULL, NULL, "-o", "-", "-", NULL };
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 arg_list[2] = arg_freq;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 arg_list[3] = arg_format;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 close(fd1[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 close(fd2[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 /* !!! FIXME: Errors should be propagated back to the parent */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 if (fd1[0] != STDIN_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 close(fd1[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 if (fd2[1] != STDOUT_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 close(fd2[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 /* Construct command-line arguments matching the audio format */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 sprintf(arg_freq, "%d", sample->actual.rate);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 sprintf(arg_format, "-Or%c%c%cl",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 (sample->actual.format & 0x0008) ? '8' : '1',
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 (sample->actual.format & 0x8000) ? 's' : 'u',
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 (sample->actual.channels == 1) ? 'M' : 'S');
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 if ((sample->actual.format & 0x0010) &&
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 (sample->actual.format & 0x7fff) != (AUDIO_S16SYS & 0x7fff))
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 strcat(arg_format, "x");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 if (execvp("timidity", arg_list) < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 SNDDBG(("MIDI: Is this line ever reached?"));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 return(1);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 /* parent */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 close(fd1[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 close(fd2[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 /* Copy the entire file to the child process */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 for (;;)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 retval = SDL_RWread(internal->rw, buf, 1, BUFSIZ);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 if (retval == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 break;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 if (write(fd1[1], buf, retval) != retval)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 Sound_SetError("MIDI: Could not send data to child process.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 }
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 } /* for */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 close(fd1[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 /* !!! FIXME: We still need some way of recognizing valid MIDI data */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 m = (midi_t *) malloc(sizeof(midi_t));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, 0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 internal->decoder_private = (void *) m;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 /* This is where we'll read the converted audio data. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 m->fd_audio = fd2[0];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 SNDDBG(("MIDI: Accepting data stream.\n"));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 return(1); /* we'll handle this data. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 } /* MIDI_open */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 static void MIDI_close(Sound_Sample *sample)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 close(((midi_t *) internal->decoder_private)->fd_audio);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 free(internal->decoder_private);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 } /* MIDI_close */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 static Uint32 MIDI_read(Sound_Sample *sample)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 ssize_t retval;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 midi_t *m = (midi_t *) internal->decoder_private;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 /*
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 * We don't actually do any decoding, so we read the converted data
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 * directly into the internal buffer.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 retval = read(m->fd_audio, internal->buffer, internal->buffer_size);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 /* Make sure the read went smoothly... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 if (retval == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 sample->flags |= SOUND_SAMPLEFLAG_EOF;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 else if (retval == -1)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 /* (next call this EAGAIN may turn into an EOF or error.) */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 else if (retval < internal->buffer_size)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 return(retval);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260 } /* MIDI_read */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262 #endif /* SOUND_SUPPORTS_MIDI */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 /* end of midi.c ... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265