annotate decoders/midi.c @ 164:77482005beb6

Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust stream handling; uses metadata check to see if stream is valid instead of magic number.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 19 Nov 2001 16:39:42 +0000
parents 1df5c106504e
children 47cc2de2ae36
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
149
1df5c106504e Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
72 static const char *extensions_midi[] = { "MIDI", NULL };
108
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 const Sound_DecoderFunctions __Sound_DecoderFunctions_MIDI =
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 {
149
1df5c106504e Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
76 extensions_midi,
108
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 "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
78 "Torbjörn Andersson <d91tan@Update.UU.SE>",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 "http://www.goice.co.jp/member/mo/timidity/"
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 MIDI_init, /* init() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 MIDI_quit, /* quit() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 MIDI_open, /* open() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 MIDI_close, /* close() method */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 MIDI_read /* read() method */
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 /* 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
90 typedef struct
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 int fd_audio;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 } midi_t;
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 static void sig_pipe(int signo)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 signal(signo, sig_pipe);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 } /* sig_pipe */
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 static int MIDI_init(void)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 if (signal(SIGPIPE, sig_pipe) == SIG_ERR)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 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
107 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 return(1);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 } /* MIDI_init */
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 static void MIDI_quit(void)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 /* it's a no-op. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 } /* MIDI_quit */
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 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
120 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 SDL_RWops *rw = internal->rw;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 midi_t *m;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 Uint8 buf[BUFSIZ];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 ssize_t retval;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 int fd1[2];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 int fd2[2];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 pid_t pid;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129
138
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
130 /*
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
131 * Use the desired format if available, or pick sensible defaults.
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
132 * TiMidity does all the conversions we need.
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
133 */
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
134 if (sample->desired.rate == 0)
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
135 {
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
136 sample->actual.channels = 2;
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
137 sample->actual.rate = 44100;
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
138 sample->actual.format = AUDIO_S16SYS;
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
139 } /* if */
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
140 else
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
141 memcpy(&sample->actual, &sample->desired, sizeof (Sound_AudioInfo));
108
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 sample->flags = SOUND_SAMPLEFLAG_NONE;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 if (pipe(fd1) < 0 || pipe(fd2) < 0)
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 Sound_SetError("MIDI: Pipe error.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 pid = fork();
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 if (pid < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 Sound_SetError("MIDI: Fork error.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 } /* if */
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 if (pid == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 /* child */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 char arg_freq[10]; /* !!! FIXME: Large enough? */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 char arg_format[10];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 char *arg_list[] =
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 { "timidity", "-s", NULL, NULL, "-o", "-", "-", NULL };
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 arg_list[2] = arg_freq;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 arg_list[3] = arg_format;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 close(fd1[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 close(fd2[0]);
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 /* !!! 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
172
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 if (fd1[0] != STDIN_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 close(fd1[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 if (fd2[1] != STDOUT_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 close(fd2[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 /* Construct command-line arguments matching the audio format */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 sprintf(arg_freq, "%d", sample->actual.rate);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 sprintf(arg_format, "-Or%c%c%cl",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 (sample->actual.format & 0x0008) ? '8' : '1',
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 (sample->actual.format & 0x8000) ? 's' : 'u',
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 (sample->actual.channels == 1) ? 'M' : 'S');
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 if ((sample->actual.format & 0x0010) &&
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 (sample->actual.format & 0x7fff) != (AUDIO_S16SYS & 0x7fff))
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 strcat(arg_format, "x");
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 if (execvp("timidity", arg_list) < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 return(0);
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 SNDDBG(("MIDI: Is this line ever reached?"));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 return(1);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 } /* if */
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 /* parent */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 close(fd1[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 close(fd2[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 /* Copy the entire file to the child process */
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 retval = SDL_RWread(internal->rw, buf, 1, BUFSIZ);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 if (retval == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 break;
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 if (write(fd1[1], buf, retval) != retval)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 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
218 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 }
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 } /* for */
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 close(fd1[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 /* !!! 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
225
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 m = (midi_t *) malloc(sizeof(midi_t));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 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
228 internal->decoder_private = (void *) m;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 /* 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
231 m->fd_audio = fd2[0];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 SNDDBG(("MIDI: Accepting data stream.\n"));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 return(1); /* we'll handle this data. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 } /* MIDI_open */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236
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 static void MIDI_close(Sound_Sample *sample)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
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 close(((midi_t *) internal->decoder_private)->fd_audio);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 free(internal->decoder_private);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 } /* MIDI_close */
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 static Uint32 MIDI_read(Sound_Sample *sample)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 ssize_t retval;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 midi_t *m = (midi_t *) internal->decoder_private;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 /*
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 * 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
255 * directly into the internal buffer.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 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
258
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 /* Make sure the read went smoothly... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260 if (retval == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 sample->flags |= SOUND_SAMPLEFLAG_EOF;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 else if (retval == -1)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266 /* (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
267 else if (retval < internal->buffer_size)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 return(retval);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 } /* MIDI_read */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273 #endif /* SOUND_SUPPORTS_MIDI */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 /* end of midi.c ... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276