This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
openal-example.c
202 lines (171 loc) · 4.6 KB
/
openal-example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* OpenAL example
*
* Copyright(C) Florian Fainelli <f.fainelli@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <stdbool.h>
#include <AL/al.h>
#include <AL/alc.h>
#ifdef LIBAUDIO
#include <audio/wave.h>
#define BACKEND "libaudio"
#else
#include <AL/alut.h>
#define BACKEND "alut"
#endif
static void list_audio_devices(const ALCchar *devices)
{
const ALCchar *device = devices, *next = devices + 1;
size_t len = 0;
fprintf(stdout, "Devices list:\n");
fprintf(stdout, "----------\n");
while (device && *device != '\0' && next && *next != '\0') {
fprintf(stdout, "%s\n", device);
len = strlen(device);
device += (len + 1);
next += (len + 2);
}
fprintf(stdout, "----------\n");
}
#define TEST_ERROR(_msg) \
error = alGetError(); \
if (error != AL_NO_ERROR) { \
fprintf(stderr, _msg "\n"); \
return -1; \
}
static inline ALenum to_al_format(short channels, short samples)
{
bool stereo = (channels > 1);
switch (samples) {
case 16:
if (stereo)
return AL_FORMAT_STEREO16;
else
return AL_FORMAT_MONO16;
case 8:
if (stereo)
return AL_FORMAT_STEREO8;
else
return AL_FORMAT_MONO8;
default:
return -1;
}
}
int main(int argc, char **argv)
{
ALboolean enumeration;
const ALCchar *devices;
const ALCchar *defaultDeviceName = argv[1];
int ret;
#ifdef LIBAUDIO
WaveInfo *wave;
#endif
char *bufferData;
ALCdevice *device;
ALvoid *data;
ALCcontext *context;
ALsizei size, freq;
ALenum format;
ALuint buffer, source;
ALfloat listenerOri[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
ALboolean loop = AL_FALSE;
ALCenum error;
ALint source_state;
fprintf(stdout, "Using " BACKEND " as audio backend\n");
enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT");
if (enumeration == AL_FALSE)
fprintf(stderr, "enumeration extension not available\n");
list_audio_devices(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
if (!defaultDeviceName)
defaultDeviceName = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
device = alcOpenDevice(defaultDeviceName);
if (!device) {
fprintf(stderr, "unable to open default device\n");
return -1;
}
fprintf(stdout, "Device: %s\n", alcGetString(device, ALC_DEVICE_SPECIFIER));
alGetError();
context = alcCreateContext(device, NULL);
if (!alcMakeContextCurrent(context)) {
fprintf(stderr, "failed to make default context\n");
return -1;
}
TEST_ERROR("make default context");
/* set orientation */
alListener3f(AL_POSITION, 0, 0, 1.0f);
TEST_ERROR("listener position");
alListener3f(AL_VELOCITY, 0, 0, 0);
TEST_ERROR("listener velocity");
alListenerfv(AL_ORIENTATION, listenerOri);
TEST_ERROR("listener orientation");
alGenSources((ALuint)1, &source);
TEST_ERROR("source generation");
alSourcef(source, AL_PITCH, 1);
TEST_ERROR("source pitch");
alSourcef(source, AL_GAIN, 1);
TEST_ERROR("source gain");
alSource3f(source, AL_POSITION, 0, 0, 0);
TEST_ERROR("source position");
alSource3f(source, AL_VELOCITY, 0, 0, 0);
TEST_ERROR("source velocity");
alSourcei(source, AL_LOOPING, AL_FALSE);
TEST_ERROR("source looping");
alGenBuffers(1, &buffer);
TEST_ERROR("buffer generation");
#ifdef LIBAUDIO
/* load data */
wave = WaveOpenFileForReading("test.wav");
if (!wave) {
fprintf(stderr, "failed to read wave file\n");
return -1;
}
ret = WaveSeekFile(0, wave);
if (ret) {
fprintf(stderr, "failed to seek wave file\n");
return -1;
}
bufferData = malloc(wave->dataSize);
if (!bufferData) {
perror("malloc");
return -1;
}
ret = WaveReadFile(bufferData, wave->dataSize, wave);
if (ret != wave->dataSize) {
fprintf(stderr, "short read: %d, want: %d\n", ret, wave->dataSize);
return -1;
}
alBufferData(buffer, to_al_format(wave->channels, wave->bitsPerSample),
bufferData, wave->dataSize, wave->sampleRate);
TEST_ERROR("failed to load buffer data");
#else
alutLoadWAVFile("test.wav", &format, &data, &size, &freq, &loop);
TEST_ERROR("loading wav file");
alBufferData(buffer, format, data, size, freq);
TEST_ERROR("buffer copy");
#endif
alSourcei(source, AL_BUFFER, buffer);
TEST_ERROR("buffer binding");
alSourcePlay(source);
TEST_ERROR("source playing");
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
TEST_ERROR("source state get");
while (source_state == AL_PLAYING) {
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
TEST_ERROR("source state get");
}
/* exit context */
alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
device = alcGetContextsDevice(context);
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);
return 0;
}