forked from meh2481/openal-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenal-example.cpp
84 lines (67 loc) · 2.03 KB
/
openal-example.cpp
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
/*
* OpenAL example
*
* Copyright(C) Florian Fainelli <[email protected]>
*/
/*
* Modifications by Mark Hutcheson 2013
*
* Modifications public domain if above copyright notice doesn't apply (Shoot, I won't pretend to know how copyright works)
*
*/
#include "openal-example.h"
int main(int argc, char **argv)
{
ALboolean enumeration;
const ALCchar *devices;
const ALCchar *defaultDeviceName;
int ret;
char *bufferData;
ALCdevice *device;
ALuint *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 };
ALCenum error;
ALint source_state;
defaultDeviceName = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
device = alcOpenDevice(defaultDeviceName);
if (!device) {
std::cerr << "unable to open default device" << std::endl;
return -1;
}
std::cout << "Device: " << alcGetString(device, ALC_DEVICE_SPECIFIER) << std::endl;
alGetError();
context = alcCreateContext(device, NULL);
if (!alcMakeContextCurrent(context)) {
std::cerr << "failed to make default context" << std::endl;
return -1;
}
/* set orientation */
alListener3f(AL_POSITION, 0, 0, 1.0f);
alListener3f(AL_VELOCITY, 0, 0, 0);
alListenerfv(AL_ORIENTATION, listenerOri);
alGenSources((ALuint)1, &source);
alSourcef(source, AL_PITCH, 1);
alSourcef(source, AL_GAIN, 1);
alSource3f(source, AL_POSITION, 0, 0, 0);
alSource3f(source, AL_VELOCITY, 0, 0, 0);
alSourcei(source, AL_LOOPING, AL_FALSE);
loadWavFile("hai.wav", &buffer, &size, &freq, &format); //Call our own function to load a WAV file, since alut is deprecated
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
while (source_state == AL_PLAYING) {
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
}
/* exit context */
alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
device = alcGetContextsDevice(context);
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);
return 0;
}