1 /*
2  * OpenAL Source Play Example
3  *
4  * Copyright (c) 2017 by Chris Robinson <chris.kcat@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /* This file contains an example for playing a sound buffer. */
26 
27 #include <assert.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #include "sndfile.h"
34 
35 #include "AL/al.h"
36 #include "AL/alext.h"
37 
38 #include "common/alhelpers.h"
39 
40 
41 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
42  * returns the new buffer ID.
43  */
LoadSound(const char * filename)44 static ALuint LoadSound(const char *filename)
45 {
46     ALenum err, format;
47     ALuint buffer;
48     SNDFILE *sndfile;
49     SF_INFO sfinfo;
50     short *membuf;
51     sf_count_t num_frames;
52     ALsizei num_bytes;
53 
54     /* Open the audio file and check that it's usable. */
55     sndfile = sf_open(filename, SFM_READ, &sfinfo);
56     if(!sndfile)
57     {
58         fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
59         return 0;
60     }
61     if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
62     {
63         fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
64         sf_close(sndfile);
65         return 0;
66     }
67 
68     /* Get the sound format, and figure out the OpenAL format */
69     format = AL_NONE;
70     if(sfinfo.channels == 1)
71         format = AL_FORMAT_MONO16;
72     else if(sfinfo.channels == 2)
73         format = AL_FORMAT_STEREO16;
74     else if(sfinfo.channels == 3)
75     {
76         if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
77             format = AL_FORMAT_BFORMAT2D_16;
78     }
79     else if(sfinfo.channels == 4)
80     {
81         if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
82             format = AL_FORMAT_BFORMAT3D_16;
83     }
84     if(!format)
85     {
86         fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
87         sf_close(sndfile);
88         return 0;
89     }
90 
91     /* Decode the whole audio file to a buffer. */
92     membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
93 
94     num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
95     if(num_frames < 1)
96     {
97         free(membuf);
98         sf_close(sndfile);
99         fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
100         return 0;
101     }
102     num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
103 
104     /* Buffer the audio data into a new buffer object, then free the data and
105      * close the file.
106      */
107     buffer = 0;
108     alGenBuffers(1, &buffer);
109     alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
110 
111     free(membuf);
112     sf_close(sndfile);
113 
114     /* Check if an error occured, and clean up if so. */
115     err = alGetError();
116     if(err != AL_NO_ERROR)
117     {
118         fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
119         if(buffer && alIsBuffer(buffer))
120             alDeleteBuffers(1, &buffer);
121         return 0;
122     }
123 
124     return buffer;
125 }
126 
127 
main(int argc,char ** argv)128 int main(int argc, char **argv)
129 {
130     ALuint source, buffer;
131     ALfloat offset;
132     ALenum state;
133 
134     /* Print out usage if no arguments were specified */
135     if(argc < 2)
136     {
137         fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
138         return 1;
139     }
140 
141     /* Initialize OpenAL. */
142     argv++; argc--;
143     if(InitAL(&argv, &argc) != 0)
144         return 1;
145 
146     /* Load the sound into a buffer. */
147     buffer = LoadSound(argv[0]);
148     if(!buffer)
149     {
150         CloseAL();
151         return 1;
152     }
153 
154     /* Create the source to play the sound with. */
155     source = 0;
156     alGenSources(1, &source);
157     alSourcei(source, AL_BUFFER, (ALint)buffer);
158     assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
159 
160     /* Play the sound until it finishes. */
161     alSourcePlay(source);
162     do {
163         al_nssleep(10000000);
164         alGetSourcei(source, AL_SOURCE_STATE, &state);
165 
166         /* Get the source offset. */
167         alGetSourcef(source, AL_SEC_OFFSET, &offset);
168         printf("\rOffset: %f  ", offset);
169         fflush(stdout);
170     } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
171     printf("\n");
172 
173     /* All done. Delete resources, and close down OpenAL. */
174     alDeleteSources(1, &source);
175     alDeleteBuffers(1, &buffer);
176 
177     CloseAL();
178 
179     return 0;
180 }
181