Improve gain/hf/lf packing when processing voice updates
[openal-soft.git] / examples / alplay.c
blob09ad96b4c8e4a88cc917e0fe66ba374734cbafb3
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 <stdio.h>
28 #include <assert.h>
30 #include "SDL_sound.h"
31 #include "SDL_audio.h"
32 #include "SDL_stdinc.h"
34 #include "AL/al.h"
36 #include "common/alhelpers.h"
39 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
40 * returns the new buffer ID.
42 static ALuint LoadSound(const char *filename)
44 Sound_Sample *sample;
45 ALenum err, format;
46 ALuint buffer;
47 Uint32 slen;
49 /* Open the audio file */
50 sample = Sound_NewSampleFromFile(filename, NULL, 65536);
51 if(!sample)
53 fprintf(stderr, "Could not open audio in %s\n", filename);
54 return 0;
57 /* Get the sound format, and figure out the OpenAL format */
58 if(sample->actual.channels == 1)
60 if(sample->actual.format == AUDIO_U8)
61 format = AL_FORMAT_MONO8;
62 else if(sample->actual.format == AUDIO_S16SYS)
63 format = AL_FORMAT_MONO16;
64 else
66 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
67 Sound_FreeSample(sample);
68 return 0;
71 else if(sample->actual.channels == 2)
73 if(sample->actual.format == AUDIO_U8)
74 format = AL_FORMAT_STEREO8;
75 else if(sample->actual.format == AUDIO_S16SYS)
76 format = AL_FORMAT_STEREO16;
77 else
79 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
80 Sound_FreeSample(sample);
81 return 0;
84 else
86 fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
87 Sound_FreeSample(sample);
88 return 0;
91 /* Decode the whole audio stream to a buffer. */
92 slen = Sound_DecodeAll(sample);
93 if(!sample->buffer || slen == 0)
95 fprintf(stderr, "Failed to read audio from %s\n", filename);
96 Sound_FreeSample(sample);
97 return 0;
100 /* Buffer the audio data into a new buffer object, then free the data and
101 * close the file. */
102 buffer = 0;
103 alGenBuffers(1, &buffer);
104 alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
105 Sound_FreeSample(sample);
107 /* Check if an error occured, and clean up if so. */
108 err = alGetError();
109 if(err != AL_NO_ERROR)
111 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
112 if(buffer && alIsBuffer(buffer))
113 alDeleteBuffers(1, &buffer);
114 return 0;
117 return buffer;
121 int main(int argc, char **argv)
123 ALuint source, buffer;
124 ALfloat offset;
125 ALenum state;
127 /* Print out usage if no arguments were specified */
128 if(argc < 2)
130 fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
131 return 1;
134 /* Initialize OpenAL. */
135 argv++; argc--;
136 if(InitAL(&argv, &argc) != 0)
137 return 1;
139 /* Initialize SDL_sound. */
140 Sound_Init();
142 /* Load the sound into a buffer. */
143 buffer = LoadSound(argv[0]);
144 if(!buffer)
146 Sound_Quit();
147 CloseAL();
148 return 1;
151 /* Create the source to play the sound with. */
152 source = 0;
153 alGenSources(1, &source);
154 alSourcei(source, AL_BUFFER, (ALint)buffer);
155 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
157 /* Play the sound until it finishes. */
158 alSourcePlay(source);
159 do {
160 al_nssleep(10000000);
161 alGetSourcei(source, AL_SOURCE_STATE, &state);
163 /* Get the source offset. */
164 alGetSourcef(source, AL_SEC_OFFSET, &offset);
165 printf("\rOffset: %f ", offset);
166 fflush(stdout);
167 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
168 printf("\n");
170 /* All done. Delete resources, and close down SDL_sound and OpenAL. */
171 alDeleteSources(1, &source);
172 alDeleteBuffers(1, &buffer);
174 Sound_Quit();
175 CloseAL();
177 return 0;