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