1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <AL/alut.h> |
---|
4 | |
---|
5 | /* |
---|
6 | * This program loads and plays a variety of files from memory, |
---|
7 | * basically a modified version of test_suite/test_fileloader.c. |
---|
8 | */ |
---|
9 | |
---|
10 | static ALbyte fileBuffer[100000]; |
---|
11 | |
---|
12 | static void |
---|
13 | playFile (const char *fileName) |
---|
14 | { |
---|
15 | FILE *fileDescriptor; |
---|
16 | size_t fileLength; |
---|
17 | ALuint buffer; |
---|
18 | ALuint source; |
---|
19 | ALenum error; |
---|
20 | ALint status; |
---|
21 | |
---|
22 | /* Load the sound file into memory. */ |
---|
23 | fileDescriptor = fopen (fileName, "rb"); |
---|
24 | if (fileDescriptor == NULL) |
---|
25 | { |
---|
26 | fprintf (stderr, "Error opening file %s\n", fileName); |
---|
27 | alutExit (); |
---|
28 | exit (EXIT_FAILURE); |
---|
29 | } |
---|
30 | |
---|
31 | fileLength = fread (fileBuffer, 1, sizeof (fileBuffer), fileDescriptor); |
---|
32 | if (ferror (fileDescriptor)) |
---|
33 | { |
---|
34 | fprintf (stderr, "Error reading file %s\n", fileName); |
---|
35 | alutExit (); |
---|
36 | exit (EXIT_FAILURE); |
---|
37 | } |
---|
38 | |
---|
39 | fclose (fileDescriptor); |
---|
40 | |
---|
41 | /* Create an AL buffer from the given sound file. */ |
---|
42 | buffer = alutCreateBufferFromFileImage (fileBuffer, fileLength); |
---|
43 | if (buffer == AL_NONE) |
---|
44 | { |
---|
45 | error = alutGetError (); |
---|
46 | fprintf (stderr, "Error creating buffer from file image: '%s'\n", |
---|
47 | alutGetErrorString (error)); |
---|
48 | alutExit (); |
---|
49 | exit (EXIT_FAILURE); |
---|
50 | } |
---|
51 | |
---|
52 | /* Generate a single source, attach the buffer to it and start playing. */ |
---|
53 | alGenSources (1, &source); |
---|
54 | alSourcei (source, AL_BUFFER, buffer); |
---|
55 | alSourcePlay (source); |
---|
56 | |
---|
57 | /* Normally nothing should go wrong above, but one never knows... */ |
---|
58 | error = alGetError (); |
---|
59 | if (error != ALUT_ERROR_NO_ERROR) |
---|
60 | { |
---|
61 | fprintf (stderr, "%s\n", alGetString (error)); |
---|
62 | alutExit (); |
---|
63 | exit (EXIT_FAILURE); |
---|
64 | } |
---|
65 | |
---|
66 | /* Check every 0.1 seconds if the sound is still playing. */ |
---|
67 | do |
---|
68 | { |
---|
69 | alutSleep (0.1f); |
---|
70 | alGetSourcei (source, AL_SOURCE_STATE, &status); |
---|
71 | } |
---|
72 | while (status == AL_PLAYING); |
---|
73 | } |
---|
74 | |
---|
75 | int |
---|
76 | main (int argc, char **argv) |
---|
77 | { |
---|
78 | /* Initialise ALUT and eat any ALUT-specific commandline flags. */ |
---|
79 | if (!alutInit (&argc, argv)) |
---|
80 | { |
---|
81 | ALenum error = alutGetError (); |
---|
82 | fprintf (stderr, "%s\n", alutGetErrorString (error)); |
---|
83 | exit (EXIT_FAILURE); |
---|
84 | } |
---|
85 | |
---|
86 | /* If everything is OK, play the sound files and exit when finished. */ |
---|
87 | playFile ("file1.wav"); |
---|
88 | playFile ("file2.au"); |
---|
89 | /* Note that we can not play raw sound files from memory because the |
---|
90 | format can't be guessed without a file name. */ |
---|
91 | |
---|
92 | if (!alutExit ()) |
---|
93 | { |
---|
94 | ALenum error = alutGetError (); |
---|
95 | fprintf (stderr, "%s\n", alutGetErrorString (error)); |
---|
96 | exit (EXIT_FAILURE); |
---|
97 | } |
---|
98 | return EXIT_SUCCESS; |
---|
99 | } |
---|