annotate EXAMPLES/playsound.c @ 4:26aec5629f68

Added example programs.
author Eric Wing <ewing . public |-at-| gmail . com>
date Wed, 27 Oct 2010 21:41:31 -0700
parents
children ee50db043251
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 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
36 if(argc < 1)
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
37 {
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
38 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
39 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
40 else if(argc-1 > MAX_SOURCES)
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("Maximum supported files is %d\n", MAX_SOURCES);
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 size_t i;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
45 ALboolean still_playing = AL_TRUE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
46
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
47 ALmixer_Data* audio_data[MAX_SOURCES];
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
48 ALmixer_Init(22050, 0, 0);
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 for(i=1; i<argc; i++)
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 if(!(audio_data[i-1]=ALmixer_LoadAll( argv[i], AL_TRUE) ))
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 printf("%s. Quiting program.\n", ALmixer_GetError());
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
55 exit(0);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
56 }
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 ALmixer_SetPlaybackFinishedCallback(Internal_SoundFinished_CallbackIntercept, NULL);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
60
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
61 for(i=1; i<argc; i++)
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 g_PlayingAudio[i-1] = AL_TRUE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
64 ALmixer_PlayChannel(i-1, audio_data[i-1], 0);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
65 }
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 while(still_playing)
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 still_playing = AL_FALSE;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
70 for(i=1; i<argc; i++)
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 still_playing |= g_PlayingAudio[i-1];
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
73 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
74 ALmixer_Update();
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
75 Internal_Delay(10);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
76 }
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 for(i=1; i<argc; i++)
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_FreeData(audio_data[i-1]);
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
81 }
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 ALmixer_Quit();
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
84
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
85 return 0;
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
86 }
26aec5629f68 Added example programs.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
87