2 * OpenAL Audio Stream Example
4 * Copyright (c) 2011 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains a relatively simple streaming audio player. */
32 #include "SDL_sound.h"
33 #include "SDL_audio.h"
34 #include "SDL_stdinc.h"
38 #include "common/alhelpers.h"
41 #ifndef SDL_AUDIO_MASK_BITSIZE
42 #define SDL_AUDIO_MASK_BITSIZE (0xFF)
44 #ifndef SDL_AUDIO_BITSIZE
45 #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
48 /* Define the number of buffers and buffer size (in milliseconds) to use. 4
49 * buffers with 200ms each gives a nice per-chunk size, and lets the queue last
50 * for almost one second. */
52 #define BUFFER_TIME_MS 200
54 typedef struct StreamPlayer
{
55 /* These are the buffers and source to play out through OpenAL with */
56 ALuint buffers
[NUM_BUFFERS
];
59 /* Handle for the audio file */
62 /* The format of the output stream */
67 static StreamPlayer
*NewPlayer(void);
68 static void DeletePlayer(StreamPlayer
*player
);
69 static int OpenPlayerFile(StreamPlayer
*player
, const char *filename
);
70 static void ClosePlayerFile(StreamPlayer
*player
);
71 static int StartPlayer(StreamPlayer
*player
);
72 static int UpdatePlayer(StreamPlayer
*player
);
74 /* Creates a new player object, and allocates the needed OpenAL source and
75 * buffer objects. Error checking is simplified for the purposes of this
76 * example, and will cause an abort if needed. */
77 static StreamPlayer
*NewPlayer(void)
81 player
= calloc(1, sizeof(*player
));
82 assert(player
!= NULL
);
84 /* Generate the buffers and source */
85 alGenBuffers(NUM_BUFFERS
, player
->buffers
);
86 assert(alGetError() == AL_NO_ERROR
&& "Could not create buffers");
88 alGenSources(1, &player
->source
);
89 assert(alGetError() == AL_NO_ERROR
&& "Could not create source");
91 /* Set parameters so mono sources play out the front-center speaker and
92 * won't distance attenuate. */
93 alSource3i(player
->source
, AL_POSITION
, 0, 0, -1);
94 alSourcei(player
->source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
95 alSourcei(player
->source
, AL_ROLLOFF_FACTOR
, 0);
96 assert(alGetError() == AL_NO_ERROR
&& "Could not set source parameters");
101 /* Destroys a player object, deleting the source and buffers. No error handling
102 * since these calls shouldn't fail with a properly-made player object. */
103 static void DeletePlayer(StreamPlayer
*player
)
105 ClosePlayerFile(player
);
107 alDeleteSources(1, &player
->source
);
108 alDeleteBuffers(NUM_BUFFERS
, player
->buffers
);
109 if(alGetError() != AL_NO_ERROR
)
110 fprintf(stderr
, "Failed to delete object IDs\n");
112 memset(player
, 0, sizeof(*player
));
117 /* Opens the first audio stream of the named file. If a file is already open,
118 * it will be closed first. */
119 static int OpenPlayerFile(StreamPlayer
*player
, const char *filename
)
123 ClosePlayerFile(player
);
125 /* Open the file and get the first stream from it */
126 player
->sample
= Sound_NewSampleFromFile(filename
, NULL
, 0);
129 fprintf(stderr
, "Could not open audio in %s\n", filename
);
133 /* Get the stream format, and figure out the OpenAL format */
134 if(player
->sample
->actual
.channels
== 1)
136 if(player
->sample
->actual
.format
== AUDIO_U8
)
137 player
->format
= AL_FORMAT_MONO8
;
138 else if(player
->sample
->actual
.format
== AUDIO_S16SYS
)
139 player
->format
= AL_FORMAT_MONO16
;
142 fprintf(stderr
, "Unsupported sample format: 0x%04x\n", player
->sample
->actual
.format
);
146 else if(player
->sample
->actual
.channels
== 2)
148 if(player
->sample
->actual
.format
== AUDIO_U8
)
149 player
->format
= AL_FORMAT_STEREO8
;
150 else if(player
->sample
->actual
.format
== AUDIO_S16SYS
)
151 player
->format
= AL_FORMAT_STEREO16
;
154 fprintf(stderr
, "Unsupported sample format: 0x%04x\n", player
->sample
->actual
.format
);
160 fprintf(stderr
, "Unsupported channel count: %d\n", player
->sample
->actual
.channels
);
163 player
->srate
= (ALsizei
)player
->sample
->actual
.rate
;
165 frame_size
= player
->sample
->actual
.channels
*
166 SDL_AUDIO_BITSIZE(player
->sample
->actual
.format
) / 8;
168 /* Set the buffer size, given the desired millisecond length. */
169 Sound_SetBufferSize(player
->sample
, (Uint32
)((Uint64
)player
->srate
*BUFFER_TIME_MS
/1000) *
176 Sound_FreeSample(player
->sample
);
177 player
->sample
= NULL
;
182 /* Closes the audio file stream */
183 static void ClosePlayerFile(StreamPlayer
*player
)
186 Sound_FreeSample(player
->sample
);
187 player
->sample
= NULL
;
191 /* Prebuffers some audio from the file, and starts playing the source */
192 static int StartPlayer(StreamPlayer
*player
)
196 /* Rewind the source position and clear the buffer queue */
197 alSourceRewind(player
->source
);
198 alSourcei(player
->source
, AL_BUFFER
, 0);
200 /* Fill the buffer queue */
201 for(i
= 0;i
< NUM_BUFFERS
;i
++)
203 /* Get some data to give it to the buffer */
204 Uint32 slen
= Sound_Decode(player
->sample
);
207 alBufferData(player
->buffers
[i
], player
->format
, player
->sample
->buffer
, (ALsizei
)slen
,
210 if(alGetError() != AL_NO_ERROR
)
212 fprintf(stderr
, "Error buffering for playback\n");
216 /* Now queue and start playback! */
217 alSourceQueueBuffers(player
->source
, i
, player
->buffers
);
218 alSourcePlay(player
->source
);
219 if(alGetError() != AL_NO_ERROR
)
221 fprintf(stderr
, "Error starting playback\n");
228 static int UpdatePlayer(StreamPlayer
*player
)
230 ALint processed
, state
;
232 /* Get relevant source info */
233 alGetSourcei(player
->source
, AL_SOURCE_STATE
, &state
);
234 alGetSourcei(player
->source
, AL_BUFFERS_PROCESSED
, &processed
);
235 if(alGetError() != AL_NO_ERROR
)
237 fprintf(stderr
, "Error checking source state\n");
241 /* Unqueue and handle each processed buffer */
247 alSourceUnqueueBuffers(player
->source
, 1, &bufid
);
250 if((player
->sample
->flags
&(SOUND_SAMPLEFLAG_EOF
|SOUND_SAMPLEFLAG_ERROR
)))
253 /* Read the next chunk of data, refill the buffer, and queue it
254 * back on the source */
255 slen
= Sound_Decode(player
->sample
);
258 alBufferData(bufid
, player
->format
, player
->sample
->buffer
, (ALsizei
)slen
,
260 alSourceQueueBuffers(player
->source
, 1, &bufid
);
262 if(alGetError() != AL_NO_ERROR
)
264 fprintf(stderr
, "Error buffering data\n");
269 /* Make sure the source hasn't underrun */
270 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
274 /* If no buffers are queued, playback is finished */
275 alGetSourcei(player
->source
, AL_BUFFERS_QUEUED
, &queued
);
279 alSourcePlay(player
->source
);
280 if(alGetError() != AL_NO_ERROR
)
282 fprintf(stderr
, "Error restarting playback\n");
291 int main(int argc
, char **argv
)
293 StreamPlayer
*player
;
296 /* Print out usage if no arguments were specified */
299 fprintf(stderr
, "Usage: %s [-device <name>] <filenames...>\n", argv
[0]);
304 if(InitAL(&argv
, &argc
) != 0)
309 player
= NewPlayer();
311 /* Play each file listed on the command line */
312 for(i
= 0;i
< argc
;i
++)
314 const char *namepart
;
316 if(!OpenPlayerFile(player
, argv
[i
]))
319 /* Get the name portion, without the path, for display. */
320 namepart
= strrchr(argv
[i
], '/');
321 if(namepart
|| (namepart
=strrchr(argv
[i
], '\\')))
326 printf("Playing: %s (%s, %dhz)\n", namepart
, FormatName(player
->format
), player
->srate
);
329 if(!StartPlayer(player
))
331 ClosePlayerFile(player
);
335 while(UpdatePlayer(player
))
336 al_nssleep(10000000);
338 /* All done with this file. Close it and go to the next */
339 ClosePlayerFile(player
);
343 /* All files done. Delete the player, and close down SDL_sound and OpenAL */
344 DeletePlayer(player
);