annotate libao2/ao_openal.c @ 26896:4559d4a7c3cb

Check for ALTIVEC_H instead of __APPLE_CC__ to decide which AltiVec vector declaration syntax to use. Checking for HAVE_ALTIVEC_VECTOR_BRACES would be better, but this variant is more likely to be mergeable upstream.
author diego
date Fri, 30 May 2008 12:09:06 +0000
parents 74db93fee74f
children d97a607821f1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
1 /*
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
2 * ao_openal.c - OpenAL audio output driver for MPlayer
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
3 *
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
4 * This driver is under the same license as MPlayer.
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
5 * (http://www.mplayerhq.hu)
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
6 *
23734
acfe034e5386 ISO8859-1 --> UTF-8
diego
parents: 21570
diff changeset
7 * Copyleft 2006 by Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de)
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
8 */
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
9
21556
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
10 #include "config.h"
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
11
17644
87c049a38c36 include stdlib.h and stdio.h, they tend to be useful :-)
reimar
parents: 17633
diff changeset
12 #include <stdlib.h>
87c049a38c36 include stdlib.h and stdio.h, they tend to be useful :-)
reimar
parents: 17633
diff changeset
13 #include <stdio.h>
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
14 #include <inttypes.h>
21556
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
15 #ifdef OPENAL_AL_H
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
16 #include <OpenAL/alc.h>
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
17 #include <OpenAL/al.h>
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
18 #else
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
19 #include <AL/alc.h>
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
20 #include <AL/al.h>
21556
283bff578e1c Support OpenAL headers in OpenAL/ instead of AL/ and OpenAL on MacOSX in general.
reimar
parents: 21550
diff changeset
21 #endif
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
22
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
23 #include "mp_msg.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
24 #include "help_mp.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
25
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
26 #include "audio_out.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
27 #include "audio_out_internal.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
28 #include "libaf/af_format.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
29 #include "osdep/timer.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
30 #include "subopt-helper.h"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
31
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
32 static ao_info_t info =
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
33 {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
34 "OpenAL audio output",
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
35 "openal",
23734
acfe034e5386 ISO8859-1 --> UTF-8
diego
parents: 21570
diff changeset
36 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
37 ""
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
38 };
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
39
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
40 LIBAO_EXTERN(openal)
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
41
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
42 #define MAX_CHANS 6
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
43 #define NUM_BUF 128
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
44 #define CHUNK_SIZE 512
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
45 static ALuint buffers[MAX_CHANS][NUM_BUF];
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
46 static ALuint sources[MAX_CHANS];
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
47
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
48 static int cur_buf[MAX_CHANS];
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
49 static int unqueue_buf[MAX_CHANS];
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
50 static int16_t *tmpbuf;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
51
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
52
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
53 static int control(int cmd, void *arg) {
21570
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
54 switch (cmd) {
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
55 case AOCONTROL_GET_VOLUME:
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
56 case AOCONTROL_SET_VOLUME: {
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
57 ALfloat volume;
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
58 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
59 if (cmd == AOCONTROL_SET_VOLUME) {
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
60 volume = (vol->left + vol->right) / 200.0;
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
61 alListenerf(AL_GAIN, volume);
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
62 }
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
63 alGetListenerf(AL_GAIN, &volume);
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
64 vol->left = vol->right = volume * 100;
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
65 return CONTROL_TRUE;
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
66 }
a29ae9b13a50 OpenAL volume control
reimar
parents: 21556
diff changeset
67 }
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
68 return CONTROL_UNKNOWN;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
69 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
70
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
71 /**
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
72 * \brief print suboption usage help
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
73 */
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
74 static void print_help(void) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
75 mp_msg(MSGT_AO, MSGL_FATAL,
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
76 "\n-ao openal commandline help:\n"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
77 "Example: mplayer -ao openal\n"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
78 "\nOptions:\n"
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
79 );
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
80 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
81
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
82 static int init(int rate, int channels, int format, int flags) {
21527
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
83 float position[3] = {0, 0, 0};
21530
b755687aa485 Improve OpenAL speaker positions a bit.
reimar
parents: 21527
diff changeset
84 float direction[6] = {0, 0, 1, 0, -1, 0};
21527
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
85 float sppos[6][3] = {
21530
b755687aa485 Improve OpenAL speaker positions a bit.
reimar
parents: 21527
diff changeset
86 {-1, 0, 0.5}, {1, 0, 0.5},
b755687aa485 Improve OpenAL speaker positions a bit.
reimar
parents: 21527
diff changeset
87 {-1, 0, -1}, {1, 0, -1},
b755687aa485 Improve OpenAL speaker positions a bit.
reimar
parents: 21527
diff changeset
88 {0, 0, 1}, {0, 0, 0.1},
21527
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
89 };
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
90 ALCdevice *dev = NULL;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
91 ALCcontext *ctx = NULL;
21549
4a6d3fa76982 Set frequency and correct frequency getting
reimar
parents: 21530
diff changeset
92 ALCint freq = 0;
4a6d3fa76982 Set frequency and correct frequency getting
reimar
parents: 21530
diff changeset
93 ALCint attribs[] = {ALC_FREQUENCY, rate, 0, 0};
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
94 int i;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
95 opt_t subopts[] = {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
96 {NULL}
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
97 };
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
98 if (subopt_parse(ao_subdevice, subopts) != 0) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
99 print_help();
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
100 return 0;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
101 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
102 if (channels > MAX_CHANS) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
103 mp_msg(MSGT_AO, MSGL_FATAL, "[OpenAL] Invalid number of channels: %i\n", channels);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
104 goto err_out;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
105 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
106 dev = alcOpenDevice(NULL);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
107 if (!dev) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
108 mp_msg(MSGT_AO, MSGL_FATAL, "[OpenAL] could not open device\n");
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
109 goto err_out;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
110 }
21550
9d9e0f1ef0e9 Fix a compiler warning
reimar
parents: 21549
diff changeset
111 ctx = alcCreateContext(dev, attribs);
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
112 alcMakeContextCurrent(ctx);
21527
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
113 alListenerfv(AL_POSITION, position);
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
114 alListenerfv(AL_ORIENTATION, direction);
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
115 alGenSources(channels, sources);
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
116 for (i = 0; i < channels; i++) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
117 cur_buf[i] = 0;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
118 unqueue_buf[i] = 0;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
119 alGenBuffers(NUM_BUF, buffers[i]);
21527
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
120 alSourcefv(sources[i], AL_POSITION, sppos[i]);
9b228834e07b Half-properly set up OpenAL position stuff
reimar
parents: 18885
diff changeset
121 alSource3f(sources[i], AL_VELOCITY, 0, 0, 0);
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
122 }
21530
b755687aa485 Improve OpenAL speaker positions a bit.
reimar
parents: 21527
diff changeset
123 if (channels == 1)
b755687aa485 Improve OpenAL speaker positions a bit.
reimar
parents: 21527
diff changeset
124 alSource3f(sources[0], AL_POSITION, 0, 0, 1);
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
125 ao_data.channels = channels;
21549
4a6d3fa76982 Set frequency and correct frequency getting
reimar
parents: 21530
diff changeset
126 alcGetIntegerv(dev, ALC_FREQUENCY, 1, &freq);
4a6d3fa76982 Set frequency and correct frequency getting
reimar
parents: 21530
diff changeset
127 if (alcGetError(dev) == ALC_NO_ERROR && freq)
4a6d3fa76982 Set frequency and correct frequency getting
reimar
parents: 21530
diff changeset
128 rate = freq;
4a6d3fa76982 Set frequency and correct frequency getting
reimar
parents: 21530
diff changeset
129 ao_data.samplerate = rate;
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
130 ao_data.format = AF_FORMAT_S16_NE;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
131 ao_data.bps = channels * rate * 2;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
132 ao_data.buffersize = CHUNK_SIZE * NUM_BUF;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
133 ao_data.outburst = channels * CHUNK_SIZE;
18885
5c8acc972551 rm unnecesary casts from void* - part 4
reynaldo
parents: 17644
diff changeset
134 tmpbuf = malloc(CHUNK_SIZE);
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
135 return 1;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
136
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
137 err_out:
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
138 return 0;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
139 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
140
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
141 // close audio device
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
142 static void uninit(int immed) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
143 ALCcontext *ctx = alcGetCurrentContext();
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
144 ALCdevice *dev = alcGetContextsDevice(ctx);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
145 free(tmpbuf);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
146 if (!immed) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
147 ALint state;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
148 alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
149 while (state == AL_PLAYING) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
150 usec_sleep(10000);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
151 alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
152 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
153 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
154 reset();
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
155 alcMakeContextCurrent(NULL);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
156 alcDestroyContext(ctx);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
157 alcCloseDevice(dev);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
158 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
159
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
160 static void unqueue_buffers(void) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
161 ALint p;
25910
74db93fee74f Remove unused variable.
reimar
parents: 25909
diff changeset
162 int s;
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
163 for (s = 0; s < ao_data.channels; s++) {
25907
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
164 int till_wrap = NUM_BUF - unqueue_buf[s];
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
165 alGetSourcei(sources[s], AL_BUFFERS_PROCESSED, &p);
25907
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
166 if (p >= till_wrap) {
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
167 alSourceUnqueueBuffers(sources[s], till_wrap, &buffers[s][unqueue_buf[s]]);
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
168 unqueue_buf[s] = 0;
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
169 p -= till_wrap;
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
170 }
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
171 if (p) {
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
172 alSourceUnqueueBuffers(sources[s], p, &buffers[s][unqueue_buf[s]]);
bc8452f11147 Reduce number of UnqueueBuffer calls
reimar
parents: 25906
diff changeset
173 unqueue_buf[s] += p;
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
174 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
175 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
176 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
177
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
178 /**
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
179 * \brief stop playing and empty buffers (for seeking/pause)
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
180 */
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
181 static void reset(void) {
25906
ffea6350c511 alSourceRewindv seems to be broken in particular in Creatives Windows-Implementation,
reimar
parents: 23734
diff changeset
182 alSourceStopv(ao_data.channels, sources);
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
183 unqueue_buffers();
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
184 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
185
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
186 /**
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
187 * \brief stop playing, keep buffers (for pause)
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
188 */
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
189 static void audio_pause(void) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
190 alSourcePausev(ao_data.channels, sources);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
191 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
192
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
193 /**
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
194 * \brief resume playing, after audio_pause()
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
195 */
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
196 static void audio_resume(void) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
197 alSourcePlayv(ao_data.channels, sources);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
198 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
199
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
200 static int get_space(void) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
201 ALint queued;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
202 unqueue_buffers();
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
203 alGetSourcei(sources[0], AL_BUFFERS_QUEUED, &queued);
25909
f6a0f861f7e6 Fix get_space calculation to always leave some space, esp. for the currently playing buffer.
reimar
parents: 25908
diff changeset
204 queued = NUM_BUF - queued - 3;
f6a0f861f7e6 Fix get_space calculation to always leave some space, esp. for the currently playing buffer.
reimar
parents: 25908
diff changeset
205 if (queued < 0) return 0;
f6a0f861f7e6 Fix get_space calculation to always leave some space, esp. for the currently playing buffer.
reimar
parents: 25908
diff changeset
206 return queued * CHUNK_SIZE * ao_data.channels;
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
207 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
208
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
209 /**
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
210 * \brief write data into buffer and reset underrun flag
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
211 */
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
212 static int play(void *data, int len, int flags) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
213 ALint state;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
214 int i, j, k;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
215 int ch;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
216 int16_t *d = data;
25908
7cf1b1c1c44c Change code to also work with different outburst sizes
reimar
parents: 25907
diff changeset
217 len /= ao_data.channels * CHUNK_SIZE;
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
218 for (i = 0; i < len; i++) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
219 for (ch = 0; ch < ao_data.channels; ch++) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
220 for (j = 0, k = ch; j < CHUNK_SIZE / 2; j++, k += ao_data.channels)
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
221 tmpbuf[j] = d[k];
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
222 alBufferData(buffers[ch][cur_buf[ch]], AL_FORMAT_MONO16, tmpbuf,
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
223 CHUNK_SIZE, ao_data.samplerate);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
224 alSourceQueueBuffers(sources[ch], 1, &buffers[ch][cur_buf[ch]]);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
225 cur_buf[ch] = (cur_buf[ch] + 1) % NUM_BUF;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
226 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
227 d += ao_data.channels * CHUNK_SIZE / 2;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
228 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
229 alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
230 if (state != AL_PLAYING) // checked here in case of an underrun
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
231 alSourcePlayv(ao_data.channels, sources);
25908
7cf1b1c1c44c Change code to also work with different outburst sizes
reimar
parents: 25907
diff changeset
232 return len * ao_data.channels * CHUNK_SIZE;
17633
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
233 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
234
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
235 static float get_delay(void) {
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
236 ALint queued;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
237 unqueue_buffers();
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
238 alGetSourcei(sources[0], AL_BUFFERS_QUEUED, &queued);
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
239 return queued * CHUNK_SIZE / 2 / (float)ao_data.samplerate;
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
240 }
e0ef5688cce7 OpenAL audio support, actual output is mono-only (no positioning yet).
reimar
parents:
diff changeset
241