annotate EXAMPLES/playstream.c @ 44:56855942fdc6

Split off background thread (threads only) termination and creation into separate functions from Interruption handling to make it easier to avoid race condition bug with Apple rdar://10081775 w.r.t. setting the OpenAL context. New APIs to suspend and resume update threads. Interruption handling calls these automatically. Also partially regressed simplification with regards to getting the current playing state in the main update loop.
author Eric Wing <ewing@anscamobile.com>
date Tue, 13 Sep 2011 18:03:21 -0700
parents ee50db043251
children b1e13d5688d1
rev   line source
4
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
1 #include "ALmixer.h"
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
2 #include <stdio.h>
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
3 #include <stdlib.h>
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
4
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
5 #ifdef ALMIXER_COMPILE_WITHOUT_SDL
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
6 #if defined(_WIN32)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
7 #define WIN32_LEAN_AND_MEAN
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
8 #include <windows.h>
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
9 #else
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
10 #include <unistd.h>
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
11 #endif
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
12
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
13 static void Internal_Delay(ALuint milliseconds_delay)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
14 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
15 #if defined(_WIN32)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
16 Sleep(milliseconds_delay);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
17 #else
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
18 usleep(milliseconds_delay);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
19 #endif
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
20 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
21 #else
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
22 #include "SDL.h"
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
23 #define Internal_Delay SDL_Delay
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
24 #endif
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
25 #define MAX_SOURCES 16
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
26
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
27 ALboolean g_PlayingAudio[MAX_SOURCES];
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
28
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
29 void Internal_SoundFinished_CallbackIntercept(ALint which_channel, ALuint al_source, ALmixer_Data* almixer_data, ALboolean finished_naturally, void* user_data)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
30 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
31 fprintf(stderr, "Channel %d finished\n", which_channel);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
32 g_PlayingAudio[which_channel] = AL_FALSE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
33 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
34
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
35 int main(int argc, char* argv[])
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
36 {
7
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
37 ALint i;
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
38 ALboolean still_playing = AL_TRUE;
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
39
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
40 ALmixer_Data* audio_data[MAX_SOURCES];
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
41
4
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
42 if(argc < 1)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
43 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
44 printf("Pass a sound file (or files) as a parameter\n");
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
45 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
46 else if(argc-1 > MAX_SOURCES)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
47 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
48 printf("Maximum supported files is %d\n", MAX_SOURCES);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
49 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
50 ALmixer_Init(ALMIXER_DEFAULT_FREQUENCY, ALMIXER_DEFAULT_NUM_SOURCES, ALMIXER_DEFAULT_REFRESH);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
51
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
52 for(i=1; i<argc; i++)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
53 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
54 if(!(audio_data[i-1]=ALmixer_LoadStream( argv[i], ALMIXER_DEFAULT_BUFFERSIZE, ALMIXER_DEFAULT_QUEUE_BUFFERS, ALMIXER_DEFAULT_STARTUP_BUFFERS, AL_FALSE) ))
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
55 /*
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
56 if(!(audio_data[i-1]=ALmixer_LoadStream( argv[i], 4096, 2, 1, AL_FALSE) ))
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
57 if(!(audio_data[i-1]=ALmixer_LoadStream( argv[i], 4096, 5 * 4, 2, AL_FALSE) ))
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
58 */
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
59 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
60 printf("%s. Quiting program.\n", ALmixer_GetError());
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
61 exit(0);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
62 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
63 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
64
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
65 ALmixer_SetPlaybackFinishedCallback(Internal_SoundFinished_CallbackIntercept, NULL);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
66
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
67 for(i=1; i<argc; i++)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
68 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
69 g_PlayingAudio[i-1] = AL_TRUE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
70 ALmixer_PlayChannel(i-1, audio_data[i-1], 0);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
71 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
72
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
73 while(still_playing)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
74 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
75 still_playing = AL_FALSE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
76 for(i=1; i<argc; i++)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
77 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
78 still_playing |= g_PlayingAudio[i-1];
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
79 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
80 ALmixer_Update();
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
81 Internal_Delay(10);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
82 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
83
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
84 for(i=1; i<argc; i++)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
85 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
86 ALmixer_FreeData(audio_data[i-1]);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
87 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
88
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
89 ALmixer_Quit();
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
90
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
91 return 0;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
92 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
93