Limit convolution processing to the output ambisonic order
[openal-soft.git] / examples / alplay.c
blob70314d47875f1e92ee611adb434e886b539c5567
1 /*
2 * OpenAL Source Play Example
4 * Copyright (c) 2017 by Chris Robinson <chris.kcat@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 /* This file contains an example for playing a sound buffer. */
27 #include <assert.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
33 #include "sndfile.h"
35 #include "AL/al.h"
37 #include "common/alhelpers.h"
40 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
41 * returns the new buffer ID.
43 static ALuint LoadSound(const char *filename)
45 ALenum err, format;
46 ALuint buffer;
47 SNDFILE *sndfile;
48 SF_INFO sfinfo;
49 short *membuf;
50 sf_count_t num_frames;
51 ALsizei num_bytes;
53 /* Open the audio file and check that it's usable. */
54 sndfile = sf_open(filename, SFM_READ, &sfinfo);
55 if(!sndfile)
57 fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
58 return 0;
60 if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
62 fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
63 sf_close(sndfile);
64 return 0;
67 /* Get the sound format, and figure out the OpenAL format */
68 if(sfinfo.channels == 1)
69 format = AL_FORMAT_MONO16;
70 else if(sfinfo.channels == 2)
71 format = AL_FORMAT_STEREO16;
72 else
74 fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
75 sf_close(sndfile);
76 return 0;
79 /* Decode the whole audio file to a buffer. */
80 membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
82 num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
83 if(num_frames < 1)
85 free(membuf);
86 sf_close(sndfile);
87 fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
88 return 0;
90 num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
92 /* Buffer the audio data into a new buffer object, then free the data and
93 * close the file.
95 buffer = 0;
96 alGenBuffers(1, &buffer);
97 alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
99 free(membuf);
100 sf_close(sndfile);
102 /* Check if an error occured, and clean up if so. */
103 err = alGetError();
104 if(err != AL_NO_ERROR)
106 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
107 if(buffer && alIsBuffer(buffer))
108 alDeleteBuffers(1, &buffer);
109 return 0;
112 return buffer;
116 int main(int argc, char **argv)
118 ALuint source, buffer;
119 ALfloat offset;
120 ALenum state;
122 /* Print out usage if no arguments were specified */
123 if(argc < 2)
125 fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
126 return 1;
129 /* Initialize OpenAL. */
130 argv++; argc--;
131 if(InitAL(&argv, &argc) != 0)
132 return 1;
134 /* Load the sound into a buffer. */
135 buffer = LoadSound(argv[0]);
136 if(!buffer)
138 CloseAL();
139 return 1;
142 /* Create the source to play the sound with. */
143 source = 0;
144 alGenSources(1, &source);
145 alSourcei(source, AL_BUFFER, (ALint)buffer);
146 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
148 /* Play the sound until it finishes. */
149 alSourcePlay(source);
150 do {
151 al_nssleep(10000000);
152 alGetSourcei(source, AL_SOURCE_STATE, &state);
154 /* Get the source offset. */
155 alGetSourcef(source, AL_SEC_OFFSET, &offset);
156 printf("\rOffset: %f ", offset);
157 fflush(stdout);
158 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
159 printf("\n");
161 /* All done. Delete resources, and close down OpenAL. */
162 alDeleteSources(1, &source);
163 alDeleteBuffers(1, &buffer);
165 CloseAL();
167 return 0;