annotate decoders/midi.c @ 138:70baf29577d8

Added default sample format.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 15 Oct 2001 18:04:49 +0000
parents 803ba4dabbce
children 1df5c106504e
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
138
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
129 /*
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
130 * 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
131 * TiMidity does all the conversions we need.
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
132 */
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
133 if (sample->desired.rate == 0)
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
134 {
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
135 sample->actual.channels = 2;
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
136 sample->actual.rate = 44100;
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
137 sample->actual.format = AUDIO_S16SYS;
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
138 } /* if */
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
139 else
70baf29577d8 Added default sample format.
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
140 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
141 sample->flags = SOUND_SAMPLEFLAG_NONE;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 if (pipe(fd1) < 0 || pipe(fd2) < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 Sound_SetError("MIDI: Pipe error.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 pid = fork();
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 if (pid < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 Sound_SetError("MIDI: Fork error.");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 if (pid == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 /* child */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 char arg_freq[10]; /* !!! FIXME: Large enough? */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 char arg_format[10];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 char *arg_list[] =
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 { "timidity", "-s", NULL, NULL, "-o", "-", "-", NULL };
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 arg_list[2] = arg_freq;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 arg_list[3] = arg_format;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 close(fd1[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 close(fd2[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 /* !!! 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
171
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 if (fd1[0] != STDIN_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 close(fd1[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 if (fd2[1] != STDOUT_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 close(fd2[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 } /* if */
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 /* Construct command-line arguments matching the audio format */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 sprintf(arg_freq, "%d", sample->actual.rate);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 sprintf(arg_format, "-Or%c%c%cl",
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 (sample->actual.format & 0x0008) ? '8' : '1',
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 (sample->actual.format & 0x8000) ? 's' : 'u',
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 (sample->actual.channels == 1) ? 'M' : 'S');
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 if ((sample->actual.format & 0x0010) &&
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 (sample->actual.format & 0x7fff) != (AUDIO_S16SYS & 0x7fff))
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 strcat(arg_format, "x");
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 if (execvp("timidity", arg_list) < 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 return(0);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 SNDDBG(("MIDI: Is this line ever reached?"));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 return(1);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 } /* if */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 /* parent */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 close(fd1[0]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 close(fd2[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 /* Copy the entire file to the child process */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 for (;;)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 retval = SDL_RWread(internal->rw, buf, 1, BUFSIZ);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 if (retval == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 break;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 if (write(fd1[1], buf, retval) != retval)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 {
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 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
217 return(0);
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 } /* for */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 close(fd1[1]);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 /* !!! 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
224
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 m = (midi_t *) malloc(sizeof(midi_t));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 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
227 internal->decoder_private = (void *) m;
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 /* 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
230 m->fd_audio = fd2[0];
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 SNDDBG(("MIDI: Accepting data stream.\n"));
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 return(1); /* we'll handle this data. */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 } /* MIDI_open */
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 static void MIDI_close(Sound_Sample *sample)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 {
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
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 close(((midi_t *) internal->decoder_private)->fd_audio);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 free(internal->decoder_private);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 } /* MIDI_close */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244
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 static Uint32 MIDI_read(Sound_Sample *sample)
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 ssize_t retval;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 midi_t *m = (midi_t *) internal->decoder_private;
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 /*
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 * 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
254 * directly into the internal buffer.
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 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
257
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 /* Make sure the read went smoothly... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 if (retval == 0)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260 sample->flags |= SOUND_SAMPLEFLAG_EOF;
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 else if (retval == -1)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265 /* (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
266 else if (retval < internal->buffer_size)
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269 return(retval);
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 } /* MIDI_read */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 #endif /* SOUND_SUPPORTS_MIDI */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274 /* end of midi.c ... */
803ba4dabbce Initial add, from Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275