1 | #include <iostream> |
---|
2 | #include <string> |
---|
3 | #include "AudioObject.h" |
---|
4 | |
---|
5 | namespace audio |
---|
6 | { |
---|
7 | AudioObject::AudioObject(std::string audioFile) |
---|
8 | { |
---|
9 | audioFile_ = audioFile; |
---|
10 | SourcePos[0]=0; |
---|
11 | SourcePos[1]=0; |
---|
12 | SourcePos[2]=0; |
---|
13 | |
---|
14 | SourceVel[0]=0; |
---|
15 | SourceVel[1]=0; |
---|
16 | SourceVel[2]=0; |
---|
17 | |
---|
18 | ListenerPos[0]=0; |
---|
19 | ListenerPos[1]=0; |
---|
20 | ListenerPos[2]=0; |
---|
21 | |
---|
22 | ListenerVel[0]=0; |
---|
23 | ListenerVel[1]=0; |
---|
24 | ListenerVel[2]=0; |
---|
25 | |
---|
26 | ListenerOri[0]=0; |
---|
27 | ListenerOri[1]=0; |
---|
28 | ListenerOri[2]=-1; |
---|
29 | ListenerOri[3]=0; |
---|
30 | ListenerOri[4]=1; |
---|
31 | ListenerOri[5]=0; |
---|
32 | |
---|
33 | |
---|
34 | // Initialize OpenAL and clear the error bit. |
---|
35 | |
---|
36 | alutInit(NULL, 0); |
---|
37 | alGetError(); |
---|
38 | |
---|
39 | // Load the wav data. |
---|
40 | |
---|
41 | if(LoadALData() == AL_FALSE) |
---|
42 | { |
---|
43 | printf("Error loading sound data."); |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | SetListenerValues(); |
---|
48 | std::cout << "Play sone ambient background sound"; |
---|
49 | } |
---|
50 | |
---|
51 | AudioObject::~AudioObject() |
---|
52 | { |
---|
53 | KillALData(); |
---|
54 | } |
---|
55 | |
---|
56 | ALboolean AudioObject::LoadALData() |
---|
57 | { |
---|
58 | ALenum format; |
---|
59 | ALsizei size; |
---|
60 | ALvoid* data; |
---|
61 | ALsizei freq; |
---|
62 | ALboolean loop; |
---|
63 | |
---|
64 | |
---|
65 | alGenBuffers(1, &Buffer); |
---|
66 | |
---|
67 | if(alGetError() != AL_NO_ERROR) |
---|
68 | return AL_FALSE; |
---|
69 | |
---|
70 | alutLoadWAVFile((ALbyte*)audioFile_.c_str(), &format, &data, &size, &freq, &loop); |
---|
71 | alBufferData(Buffer, format, data, size, freq); |
---|
72 | alutUnloadWAV(format, data, size, freq); |
---|
73 | |
---|
74 | alGenSources(1, &Source); |
---|
75 | |
---|
76 | if(alGetError() != AL_NO_ERROR) |
---|
77 | return AL_FALSE; |
---|
78 | |
---|
79 | alSourcei (Source, AL_BUFFER, Buffer ); |
---|
80 | alSourcef (Source, AL_PITCH, 1.0 ); |
---|
81 | alSourcef (Source, AL_GAIN, 1.0 ); |
---|
82 | alSourcefv(Source, AL_POSITION, SourcePos); |
---|
83 | alSourcefv(Source, AL_VELOCITY, SourceVel); |
---|
84 | alSourcei (Source, AL_LOOPING, loop ); |
---|
85 | |
---|
86 | if(alGetError() == AL_NO_ERROR) |
---|
87 | return AL_TRUE; |
---|
88 | |
---|
89 | |
---|
90 | return AL_FALSE; |
---|
91 | } |
---|
92 | |
---|
93 | void AudioObject::SetListenerValues() |
---|
94 | { |
---|
95 | alListenerfv(AL_POSITION, ListenerPos); |
---|
96 | alListenerfv(AL_VELOCITY, ListenerVel); |
---|
97 | alListenerfv(AL_ORIENTATION, ListenerOri); |
---|
98 | } |
---|
99 | |
---|
100 | void AudioObject::KillALData() |
---|
101 | { |
---|
102 | alDeleteBuffers(1, &Buffer); |
---|
103 | alDeleteSources(1, &Source); |
---|
104 | alutExit(); |
---|
105 | } |
---|
106 | |
---|
107 | void AudioObject::play() |
---|
108 | { |
---|
109 | alSourcePlay(Source); |
---|
110 | |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|