Mercurial > SDL_sound_CoreAudio
annotate playsound/playsound_simple.c @ 528:61a317171ef0 stable-1.0
Merge r536:537 from trunk: Speex bogus data check.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 11 Apr 2008 19:55:10 +0000 |
parents | 50bb9a6cebfe |
children | 2e8907ff98e9 |
rev | line source |
---|---|
492
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
1 /* |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
2 * SDL_sound -- An abstract sound format decoding API. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
3 * Copyright (C) 2001 Ryan C. Gordon. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
4 * |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
5 * This library is free software; you can redistribute it and/or |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
6 * modify it under the terms of the GNU Lesser General Public |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
7 * License as published by the Free Software Foundation; either |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
8 * version 2.1 of the License, or (at your option) any later version. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
9 * |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
10 * This library is distributed in the hope that it will be useful, |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
13 * Lesser General Public License for more details. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
14 * |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU Lesser General Public |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
16 * License along with this library; if not, write to the Free Software |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
18 */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
19 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
20 /** |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
21 * This is just a simple "decode sound, play it through SDL" example. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
22 * The much more complex, fancy, and robust code is playsound.c. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
23 * |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
24 * Please see the file COPYING in the source's root directory. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
25 * |
525
50bb9a6cebfe
Updated my email address.
Ryan C. Gordon <icculus@icculus.org>
parents:
507
diff
changeset
|
26 * This file written by Ryan C. Gordon. (icculus@icculus.org) |
492
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
27 */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
28 |
505
a9676c2865f9
Backport from devbranch: gcc4 cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents:
492
diff
changeset
|
29 #include <stdio.h> |
a9676c2865f9
Backport from devbranch: gcc4 cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents:
492
diff
changeset
|
30 #include <stdlib.h> |
507
7f11a82e918e
Whoops, didn't actually fix the gcc4 warnings (backport from devbranch).
Ryan C. Gordon <icculus@icculus.org>
parents:
505
diff
changeset
|
31 #include <string.h> |
505
a9676c2865f9
Backport from devbranch: gcc4 cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents:
492
diff
changeset
|
32 |
492
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
33 #include "SDL.h" |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
34 #include "SDL_sound.h" |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
35 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
36 /* global decoding state. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
37 typedef struct |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
38 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
39 Sound_Sample *sample; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
40 SDL_AudioSpec devformat; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
41 Uint8 *decoded_ptr; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
42 Uint32 decoded_bytes; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
43 } PlaysoundAudioCallbackData; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
44 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
45 /* |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
46 * This variable is flipped to non-zero when the audio callback has |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
47 * finished playing the whole file. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
48 */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
49 static volatile int global_done_flag = 0; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
50 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
51 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
52 /* |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
53 * The audio callback. SDL calls this frequently to feed the audio device. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
54 * We decode the audio file being played in here in small chunks and feed |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
55 * the device as necessary. Other solutions may want to predecode more |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
56 * (or all) of the file, since this needs to run fast and frequently, |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
57 * but since we're only sitting here and waiting for the file to play, |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
58 * the only real requirement is that we can decode a given audio file |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
59 * faster than realtime, which isn't really a problem with any modern format |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
60 * on even pretty old hardware at this point. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
61 */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
62 static void audio_callback(void *userdata, Uint8 *stream, int len) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
63 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
64 PlaysoundAudioCallbackData *data = (PlaysoundAudioCallbackData *) userdata; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
65 Sound_Sample *sample = data->sample; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
66 int bw = 0; /* bytes written to stream this time through the callback */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
67 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
68 while (bw < len) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
69 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
70 int cpysize; /* bytes to copy on this iteration of the loop. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
71 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
72 if (data->decoded_bytes == 0) /* need more data! */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
73 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
74 /* if there wasn't previously an error or EOF, read more. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
75 if ( ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) && |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
76 ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) ) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
77 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
78 data->decoded_bytes = Sound_Decode(sample); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
79 data->decoded_ptr = sample->buffer; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
80 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
81 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
82 if (data->decoded_bytes == 0) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
83 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
84 /* ...there isn't any more data to read! */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
85 memset(stream + bw, '\0', len - bw); /* write silence. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
86 global_done_flag = 1; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
87 return; /* we're done playback, one way or another. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
88 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
89 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
90 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
91 /* we have data decoded and ready to write to the device... */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
92 cpysize = len - bw; /* len - bw == amount device still wants. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
93 if (cpysize > data->decoded_bytes) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
94 cpysize = data->decoded_bytes; /* clamp to what we have left. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
95 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
96 /* if it's 0, next iteration will decode more or decide we're done. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
97 if (cpysize > 0) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
98 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
99 /* write this iteration's data to the device. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
100 memcpy(stream + bw, (Uint8 *) data->decoded_ptr, cpysize); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
101 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
102 /* update state for next iteration or callback */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
103 bw += cpysize; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
104 data->decoded_ptr += cpysize; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
105 data->decoded_bytes -= cpysize; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
106 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
107 } /* while */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
108 } /* audio_callback */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
109 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
110 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
111 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
112 static void playOneSoundFile(const char *fname) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
113 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
114 PlaysoundAudioCallbackData data; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
115 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
116 memset(&data, '\0', sizeof (PlaysoundAudioCallbackData)); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
117 data.sample = Sound_NewSampleFromFile(fname, NULL, 65536); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
118 if (data.sample == NULL) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
119 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
120 fprintf(stderr, "Couldn't load '%s': %s.\n", fname, Sound_GetError()); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
121 return; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
122 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
123 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
124 /* |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
125 * Open device in format of the the sound to be played. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
126 * We open and close the device for each sound file, so that SDL |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
127 * handles the data conversion to hardware format; this is the |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
128 * easy way out, but isn't practical for most apps. Usually you'll |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
129 * want to pick one format for all the data or one format for the |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
130 * audio device and convert the data when needed. This is a more |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
131 * complex issue than I can describe in a source code comment, though. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
132 */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
133 data.devformat.freq = data.sample->actual.rate; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
134 data.devformat.format = data.sample->actual.format; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
135 data.devformat.channels = data.sample->actual.channels; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
136 data.devformat.samples = 4096; /* I just picked a largish number here. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
137 data.devformat.callback = audio_callback; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
138 data.devformat.userdata = &data; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
139 if (SDL_OpenAudio(&data.devformat, NULL) < 0) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
140 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
141 fprintf(stderr, "Couldn't open audio device: %s.\n", SDL_GetError()); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
142 Sound_FreeSample(data.sample); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
143 return; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
144 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
145 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
146 printf("Now playing [%s]...\n", fname); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
147 SDL_PauseAudio(0); /* SDL audio device is "paused" right after opening. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
148 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
149 global_done_flag = 0; /* the audio callback will flip this flag. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
150 while (!global_done_flag) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
151 SDL_Delay(10); /* just wait for the audio callback to finish. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
152 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
153 /* at this point, we've played the entire audio file. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
154 SDL_PauseAudio(1); /* so stop the device. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
155 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
156 /* |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
157 * Sleep two buffers' worth of audio before closing, in order |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
158 * to allow the playback to finish. This isn't always enough; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
159 * perhaps SDL needs a way to explicitly wait for device drain? |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
160 * Most apps don't have this issue, since they aren't explicitly |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
161 * closing the device as soon as a sound file is done playback. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
162 * As an alternative for this app, you could also change the callback |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
163 * to write silence for a call or two before flipping global_done_flag. |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
164 */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
165 SDL_Delay(2 * 1000 * data.devformat.samples / data.devformat.freq); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
166 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
167 /* if there was an error, tell the user. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
168 if (data.sample->flags & SOUND_SAMPLEFLAG_ERROR) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
169 fprintf(stderr, "Error decoding file: %s\n", Sound_GetError()); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
170 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
171 Sound_FreeSample(data.sample); /* clean up SDL_Sound resources... */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
172 SDL_CloseAudio(); /* will reopen with next file's format. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
173 } /* playOneSoundFile */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
174 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
175 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
176 int main(int argc, char **argv) |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
177 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
178 int i; |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
179 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
180 if (!Sound_Init()) /* this calls SDL_Init(SDL_INIT_AUDIO) ... */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
181 { |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
182 fprintf(stderr, "Sound_Init() failed: %s.\n", Sound_GetError()); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
183 SDL_Quit(); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
184 return(42); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
185 } /* if */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
186 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
187 for (i = 1; i < argc; i++) /* each arg is an audio file to play. */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
188 playOneSoundFile(argv[i]); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
189 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
190 /* Shutdown the libraries... */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
191 Sound_Quit(); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
192 SDL_Quit(); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
193 return(0); |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
194 } /* main */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
195 |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
196 /* end of playsound-simple.c ... */ |
1b91006ebfef
Added playsound_simple.c, which is a better example
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
197 |