1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <AL/alut.h>
4 
5 /*
6  * This program loads and plays a variety of files, basically an automated
7  * version of examples/playfile.c.
8  */
9 
10 static void
playFile(const char * fileName)11 playFile (const char *fileName)
12 {
13   ALuint buffer;
14   ALuint source;
15   ALenum error;
16   ALint status;
17 
18   /* Create an AL buffer from the given sound file. */
19   buffer = alutCreateBufferFromFile (fileName);
20   if (buffer == AL_NONE)
21     {
22       error = alutGetError ();
23       fprintf (stderr, "Error loading file: '%s'\n",
24                alutGetErrorString (error));
25       alutExit ();
26       exit (EXIT_FAILURE);
27     }
28 
29   /* Generate a single source, attach the buffer to it and start playing. */
30   alGenSources (1, &source);
31   alSourcei (source, AL_BUFFER, buffer);
32   alSourcePlay (source);
33 
34   /* Normally nothing should go wrong above, but one never knows... */
35   error = alGetError ();
36   if (error != ALUT_ERROR_NO_ERROR)
37     {
38       fprintf (stderr, "%s\n", alGetString (error));
39       alutExit ();
40       exit (EXIT_FAILURE);
41     }
42 
43   /* Check every 0.1 seconds if the sound is still playing. */
44   do
45     {
46       alutSleep (0.1f);
47       alGetSourcei (source, AL_SOURCE_STATE, &status);
48     }
49   while (status == AL_PLAYING);
50 }
51 
52 int
main(int argc,char ** argv)53 main (int argc, char **argv)
54 {
55   /* Initialise ALUT and eat any ALUT-specific commandline flags. */
56   if (!alutInit (&argc, argv))
57     {
58       ALenum error = alutGetError ();
59       fprintf (stderr, "%s\n", alutGetErrorString (error));
60       exit (EXIT_FAILURE);
61     }
62 
63   /* If everything is OK, play the sound files and exit when finished. */
64   playFile ("file1.wav");
65   playFile ("file2.au");
66   playFile ("file3.raw");
67 
68   if (!alutExit ())
69     {
70       ALenum error = alutGetError ();
71       fprintf (stderr, "%s\n", alutGetErrorString (error));
72       exit (EXIT_FAILURE);
73     }
74   return EXIT_SUCCESS;
75 }
76