annotate EXAMPLES/playsound.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 eca6f008fad0
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
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
4 #ifdef ALMIXER_COMPILE_WITHOUT_SDL
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
5 #if defined(_WIN32)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
6 #define WIN32_LEAN_AND_MEAN
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
7 #include <windows.h>
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
8 #else
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
9 #include <unistd.h>
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
10 #endif
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
11
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
12 static void Internal_Delay(ALuint milliseconds_delay)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
13 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
14 #if defined(_WIN32)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
15 Sleep(milliseconds_delay);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
16 #else
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
17 usleep(milliseconds_delay);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
18 #endif
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
19 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
20 #else
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
21 #include "SDL.h"
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
22 #define Internal_Delay SDL_Delay
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
23 #endif
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
24 #define MAX_SOURCES 16
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
25
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
26 ALboolean g_PlayingAudio[MAX_SOURCES];
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
27
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
28 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
29 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
30 fprintf(stderr, "Channel %d finished\n", which_channel);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
31 g_PlayingAudio[which_channel] = AL_FALSE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
32 }
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 int main(int argc, char* argv[])
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
35 {
7
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
36 ALint i;
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
37 ALboolean still_playing = AL_TRUE;
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
38
ee50db043251 Cross-platform fixes.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4
diff changeset
39 ALmixer_Data* audio_data[MAX_SOURCES];
4
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
40 if(argc < 1)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
41 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
42 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
43 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
44 else if(argc-1 > MAX_SOURCES)
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 printf("Maximum supported files is %d\n", 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
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
49 ALmixer_Init(22050, 0, 0);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
50
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
51 for(i=1; i<argc; i++)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
52 {
11
eca6f008fad0 Shouldn't be enabling data callbacks for the example since they aren't used.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7
diff changeset
53 if(!(audio_data[i-1]=ALmixer_LoadAll( argv[i], AL_FALSE) ))
4
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
54 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
55 printf("%s. Quiting program.\n", ALmixer_GetError());
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
56 exit(0);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
57 }
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 ALmixer_SetPlaybackFinishedCallback(Internal_SoundFinished_CallbackIntercept, NULL);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
61
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
62 for(i=1; i<argc; i++)
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 g_PlayingAudio[i-1] = AL_TRUE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
65 ALmixer_PlayChannel(i-1, audio_data[i-1], 0);
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
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
68 while(still_playing)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
69 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
70 still_playing = AL_FALSE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
71 for(i=1; i<argc; i++)
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 still_playing |= g_PlayingAudio[i-1];
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 ALmixer_Update();
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
76 Internal_Delay(10);
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
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
79 for(i=1; i<argc; i++)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
80 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
81 ALmixer_FreeData(audio_data[i-1]);
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 ALmixer_Quit();
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 return 0;
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