Debian Codegraph @

src/libout123/modules/openal.c

raw
1 /*
2 openal.c: audio output on OpenAL
3
4 copyright 1995-2016 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Taihei Monma
7 */
8
9 /* Need usleep(). */
10 #define _DEFAULT_SOURCE
11 #define _BSD_SOURCE
12
13 #include "../out123_int.h"
14
15 #ifdef OPENAL_SUBDIR_OPENAL
16 #include <OpenAL/al.h>
17 #include <OpenAL/alc.h>
18 #elif defined(OPENAL_SUBDIR_AL)
19 #include <AL/al.h>
20 #include <AL/alc.h>
21 #else
22 #include <al.h>
23 #include <alc.h>
24 #endif
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include "../../common/debug.h"
29
30 #define NUM_BUFFERS 16
31
32 #ifndef AL_FORMAT_MONO_FLOAT32
33 #define AL_FORMAT_MONO_FLOAT32 0x10010
34 #endif
35 #ifndef AL_FORMAT_STEREO_FLOAT32
36 #define AL_FORMAT_STEREO_FLOAT32 0x10011
37 #endif
38
39 typedef struct
40 {
41 ALCdevice *device;
42 ALCcontext *context;
43 ALuint source, buffer;
44 ALenum format;
45 ALsizei rate;
46 } mpg123_openal_t;
47
48
49 static int open_openal(out123_handle *ao)
50 {
51 mpg123_openal_t* al = (mpg123_openal_t*)ao->userptr;
52
53 al->device = alcOpenDevice(NULL);
54 al->context = alcCreateContext(al->device, NULL);
55 alcMakeContextCurrent(al->context);
56 alGenSources(1, &al->source);
57
58 al->rate = ao->rate;
59 if(ao->format == MPG123_ENC_SIGNED_16 && ao->channels == 2) al->format = AL_FORMAT_STEREO16;
60 else if(ao->format == MPG123_ENC_SIGNED_16 && ao->channels == 1) al->format = AL_FORMAT_MONO16;
61 else if(ao->format == MPG123_ENC_UNSIGNED_8 && ao->channels == 2) al->format = AL_FORMAT_STEREO8;
62 else if(ao->format == MPG123_ENC_UNSIGNED_8 && ao->channels == 1) al->format = AL_FORMAT_MONO8;
63 else if(ao->format == MPG123_ENC_FLOAT_32 && ao->channels == 2) al->format = AL_FORMAT_STEREO_FLOAT32;
64 else if(ao->format == MPG123_ENC_FLOAT_32 && ao->channels == 1) al->format = AL_FORMAT_MONO_FLOAT32;
65
66 return 0;
67 }
68
69 static int get_formats_openal(out123_handle *ao)
70 {
71 return MPG123_ENC_SIGNED_16|MPG123_ENC_UNSIGNED_8|((alIsExtensionPresent((ALchar*)"AL_EXT_float32") == AL_TRUE) ? MPG123_ENC_FLOAT_32 : 0);
72 }
73
74 static int write_openal(out123_handle *ao, unsigned char *buf, int len)
75 {
76 ALint state, n;
77 mpg123_openal_t* al = (mpg123_openal_t*)ao->userptr;
78
79 alGetSourcei(al->source, AL_BUFFERS_QUEUED, &n);
80 if(n < NUM_BUFFERS)
81 {
82 alGenBuffers(1, &al->buffer);
83 }
84 else
85 {
86 alGetSourcei(al->source, AL_SOURCE_STATE, &state);
87 if(state != AL_PLAYING)
88 {
89 alSourcePlay(al->source);
90 }
91 while(alGetSourcei(al->source, AL_BUFFERS_PROCESSED, &n), n == 0)
92 {
93 usleep(10000);
94 }
95 alSourceUnqueueBuffers(al->source, 1, &al->buffer);
96 }
97
98 alBufferData(al->buffer, al->format, buf, len, al->rate);
99 alSourceQueueBuffers(al->source, 1, &al->buffer);
100
101 return len;
102 }
103
104 static int close_openal(out123_handle *ao)
105 {
106 ALint state, n;
107 mpg123_openal_t* al = (mpg123_openal_t*)ao->userptr;
108
109 if (al)
110 {
111 /* wait until all buffers are consumed */
112 while(alGetSourcei(al->source, AL_SOURCE_STATE, &state), state == AL_PLAYING)
113 {
114 usleep(10000);
115 }
116 /* free all processed buffers */
117 while(alGetSourcei(al->source, AL_BUFFERS_PROCESSED, &n), n > 0)
118 {
119 alSourceUnqueueBuffers(al->source, 1, &al->buffer);
120 alDeleteBuffers(1, &al->buffer);
121 }
122 alDeleteSources(1, &al->source);
123 alcMakeContextCurrent(NULL);
124 alcDestroyContext(al->context);
125 alcCloseDevice(al->device);
126 }
127
128 return 0;
129 }
130
131 static void flush_openal(out123_handle *ao)
132 {
133 ALint n;
134 mpg123_openal_t* al = (mpg123_openal_t*)ao->userptr;
135
136 if (al)
137 {
138 /* stop playing and flush all buffers */
139 alSourceStop(al->source);
140 while(alGetSourcei(al->source, AL_BUFFERS_PROCESSED, &n), n > 0)
141 {
142 alSourceUnqueueBuffers(al->source, 1, &al->buffer);
143 alDeleteBuffers(1, &al->buffer);
144 }
145 }
146 }
147
148 static void deinit_openal(out123_handle* ao)
149 {
150 /* Free up memory */
151 if(ao->userptr)
152 {
153 free( ao->userptr );
154 ao->userptr = NULL;
155 }
156 }
157
158 static int init_openal(out123_handle* ao)
159 {
160 if (ao==NULL) return -1;
161
162 /* Set callbacks */
163 ao->open = open_openal;
164 ao->flush = flush_openal;
165 ao->write = write_openal;
166 ao->get_formats = get_formats_openal;
167 ao->close = close_openal;
168 ao->deinit = deinit_openal;
169
170 /* Allocate memory for data structure */
171 ao->userptr = malloc( sizeof( mpg123_openal_t ) );
172 if(ao->userptr==NULL)
173 {
174 if(!AOQUIET)
175 error("failed to malloc memory for 'mpg123_openal_t'");
176 return -1;
177 }
178 memset( ao->userptr, 0, sizeof(mpg123_openal_t) );
179
180 /* Success */
181 return 0;
182 }
183
184
185 /*
186 Module information data structure
187 */
188 mpg123_module_t mpg123_output_module_info = {
189 /* api_version */ MPG123_MODULE_API_VERSION,
190 /* name */ "openal",
191 /* description */ "Output audio using OpenAL.",
192 /* revision */ "$Rev:$",
193 /* handle */ NULL,
194
195 /* init_output */ init_openal,
196 };
197
198