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