qt/MainForm.cpp: Unneeded variable removed (take it easy ;-) - furthermore it made...
[mines3d.git] / sound / CAudio.cpp
blobca1dcad2e59bd62907924c1897004260a44ccc57
1 /*
2 * File: CAudio.cpp
3 * Author: Jindrich Kovar
4 */
5 #include "CAudio.h"
6 CAudio::CAudio()
7 {
8 // String for exceptions
9 char str[100];
11 // Position of the listener.
12 ListenerPos[0]=0.0; //{ 0.0, 0.0, 0.0 }
13 ListenerPos[1]=0.0;
14 ListenerPos[2]=0.0;
16 // Velocity of the listener.
17 ListenerVel[0]=0.0; //{ 0.0, 0.0, 0.0 }
18 ListenerVel[1]=0.0;
19 ListenerVel[2]=0.0;
21 // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
22 ListenerOri[0]=0.0;// { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }
23 ListenerOri[1]=0.0;
24 ListenerOri[2]=-1.0;
25 ListenerOri[3]=0.0;
26 ListenerOri[4]=1.0;
27 ListenerOri[5]=0.0;
29 // Default value (nothing is playing)
30 iActuallyPlayingMusic=NOTHING_PLAYING;
31 iActuallyPlayingSound=NOTHING_PLAYING;
33 // Initialize OpenAL and clear the error bit
34 alutInit(NULL, 0);
35 alGetError();
37 // Error check
38 if(alGetError() != AL_NO_ERROR)
40 sprintf(str, "Cannot initialize OpenAl.");
41 throw CAudioException(str);
45 // Variables to load into
46 ALenum format;
47 ALsizei size;
48 ALvoid* data;
49 ALsizei freq;
50 ALboolean loop;
52 // Load wav data into buffers
53 alGenBuffers(NUM_BUFFERS, Buffers);
55 // Error check
56 if(alGetError() != AL_NO_ERROR)
58 sprintf(str, "Cannot generate buffers for sounds.");
59 throw CAudioException(str);
62 // Load music files
63 alutLoadWAVFile(MUSIC_SLOW_SOURCE, &format, &data, &size, &freq, &loop);
64 alBufferData(Buffers[MUSIC_SLOW], format, data, size, freq);
65 alutUnloadWAV(format, data, size, freq);
67 alutLoadWAVFile(MUSIC_FAST_SOURCE, &format, &data, &size, &freq, &loop);
68 alBufferData(Buffers[MUSIC_FAST], format, data, size, freq);
69 alutUnloadWAV(format, data, size, freq);
71 // Load sound files
72 alutLoadWAVFile(SOUND_BUTTON_SUCCESS_SOURCE, &format, &data, &size, &freq, &loop);
73 alBufferData(Buffers[SOUND_BUTTON_SUCCESS], format, data, size, freq);
74 alutUnloadWAV(format, data, size, freq);
76 alutLoadWAVFile(SOUND_BUTTON_HOVER_SOURCE, &format, &data, &size, &freq, &loop);
77 alBufferData(Buffers[SOUND_BUTTON_HOVER], format, data, size, freq);
78 alutUnloadWAV(format, data, size, freq);
80 alutLoadWAVFile(SOUND_GAME_OVER_SOURCE, &format, &data, &size, &freq, &loop);
81 alBufferData(Buffers[SOUND_GAME_OVER], format, data, size, freq);
82 alutUnloadWAV(format, data, size, freq);
84 alutLoadWAVFile(SOUND_GAME_VICTORY_SOURCE, &format, &data, &size, &freq, &loop);
85 alBufferData(Buffers[SOUND_GAME_VICTORY], format, data, size, freq);
86 alutUnloadWAV(format, data, size, freq);
88 // Ignore error by loading files
89 alGetError();
91 /* // Error check
92 if(alGetError() != AL_NO_ERROR)
94 sprintf(str, "Cannot load all sound files.");
95 throw CAudioException(str);
98 // Bind buffers into audio sources.
99 alGenSources(NUM_SOURCES, Sources);
101 // Error check
102 if(alGetError() != AL_NO_ERROR)
104 sprintf(str, "Cannot generate sources for sounds.");
105 throw CAudioException(str);
108 for (int i=0;i<NUM_SOURCES;i++)
110 alSourcei (Sources[i], AL_BUFFER, Buffers[i] );
111 alSourcef (Sources[i], AL_PITCH, 1.0f );
112 alSourcef (Sources[i], AL_GAIN, 1.0f );
113 alSourcefv(Sources[i], AL_POSITION, SourcesPos[i]);
114 alSourcefv(Sources[i], AL_VELOCITY, SourcesVel[i]);
115 // Only music files are in loop
116 if (i<NUM_MUSICS) alSourcei (Sources[i], AL_LOOPING, AL_TRUE);
117 else alSourcei (Sources[i], AL_LOOPING, AL_FALSE );
120 // Error check
121 if(alGetError() != AL_NO_ERROR)
123 sprintf(str, "Cannot assign values to sources.");
124 throw CAudioException(str);
127 alListenerfv(AL_POSITION, ListenerPos);
128 alListenerfv(AL_VELOCITY, ListenerVel);
129 alListenerfv(AL_ORIENTATION, ListenerOri);
131 // Error check
132 if(alGetError() != AL_NO_ERROR)
134 sprintf(str, "Cannot assign values to listener.");
135 throw CAudioException(str);
138 CAudio::~CAudio()
140 alDeleteBuffers(NUM_BUFFERS, Buffers);
141 alDeleteSources(NUM_SOURCES, Sources);
142 alutExit();
145 Function plays music by ID. It stops
146 actually playing music if any.
147 @param[in] ID = number of music source to play
149 void CAudio::PlayMusic(int ID)
151 if ((ID>-1)&&(ID<NUM_MUSICS))
153 if (iActuallyPlayingMusic!=NOTHING_PLAYING) StopMusic();
154 alSourcePlay(Sources[ID]);
155 iActuallyPlayingMusic=ID;
159 Function stops actually playing music.
161 void CAudio::StopMusic(void)
163 if (iActuallyPlayingMusic!=NOTHING_PLAYING)
165 alSourceStop(Sources[iActuallyPlayingMusic]);
167 iActuallyPlayingMusic=NOTHING_PLAYING;
170 Function plays sound by ID.
171 @param[in] ID = number of sound source to play
173 void CAudio::PlaySound(int ID)
175 if ((ID>=NUM_MUSICS)&&(ID<NUM_SOURCES))
177 alSourcePlay(Sources[ID]);
178 iActuallyPlayingSound=ID;
182 Function stops actually playing sound or attempts to stop last played.
184 void CAudio::StopSound(void)
186 if ( iActuallyPlayingSound!=NOTHING_PLAYING)
188 alSourceStop(Sources[iActuallyPlayingSound]);
190 iActuallyPlayingSound=NOTHING_PLAYING;