CAudio: DO NOT PRINT to stderr in case of throwing an exception! And check the buffer...
[mines3d.git] / sound / CAudio.cpp
blob9cc6268b9b03aa3df8d5ae23c204723b83802cc4
1 /*
2 * File: CAudio.cpp
3 * Author: Jindrich Kovar
4 */
5 #include "CAudio.h"
7 #define BUFSIZE 100
9 CAudio::CAudio()
11 // String for exceptions
12 char str[BUFSIZE];
14 // Position of the listener.
15 ListenerPos[0]=0.0; //{ 0.0, 0.0, 0.0 }
16 ListenerPos[1]=0.0;
17 ListenerPos[2]=0.0;
19 // Velocity of the listener.
20 ListenerVel[0]=0.0; //{ 0.0, 0.0, 0.0 }
21 ListenerVel[1]=0.0;
22 ListenerVel[2]=0.0;
24 // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
25 ListenerOri[0]=0.0;// { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }
26 ListenerOri[1]=0.0;
27 ListenerOri[2]=-1.0;
28 ListenerOri[3]=0.0;
29 ListenerOri[4]=1.0;
30 ListenerOri[5]=0.0;
32 // Default value (nothing is playing)
33 iActuallyPlayingMusic=MUSIC_NOTHING;
34 iActuallyPlayingSound=SOUND_NOTHING;
36 // Initialize OpenAL and clear the error bit
37 alutInit(NULL, 0);
38 alGetError();
40 // Error check
41 int error;
42 if((error= alGetError() ) != AL_NO_ERROR)
44 snprintf(str, BUFSIZE, "Cannot initialize OpenAl. (code %d)",error); str[BUFSIZE]='\0';
45 throw CAudioException(str);
49 // Load wav data into buffers
50 alGenBuffers(NUM_BUFFERS, Buffers);
52 // Error check
53 if((error= alGetError() ) != AL_NO_ERROR)
55 snprintf(str, BUFSIZE, "Cannot generate buffers for sounds.(code %d)",error); str[BUFSIZE]='\0';
56 throw CAudioException(str);
59 // Load music files
60 if ( (Buffers[MUSIC_SLOW] = alutCreateBufferFromFile(MUSIC_SLOW_SOURCE))==0 )
62 cerr<<"Error: loading file "<<MUSIC_SLOW_SOURCE<<" failed"<<endl;
65 if ( (Buffers[MUSIC_FAST] = alutCreateBufferFromFile(MUSIC_FAST_SOURCE))==0 )
67 cerr<<"Error: loading file "<<MUSIC_FAST_SOURCE<<" failed"<<endl;
70 if ( (Buffers[SOUND_BUTTON_SUCCESS] = alutCreateBufferFromFile(SOUND_BUTTON_SUCCESS_SOURCE))==0 )
72 cerr<<"Error: loading file "<<SOUND_BUTTON_SUCCESS_SOURCE<<" failed"<<endl;
75 if ( (Buffers[SOUND_BUTTON_HOVER] = alutCreateBufferFromFile(SOUND_BUTTON_HOVER_SOURCE))==0 )
77 cerr<<"Error: loading file "<<SOUND_BUTTON_HOVER_SOURCE<<" failed"<<endl;
80 if ( (Buffers[SOUND_GAME_OVER] = alutCreateBufferFromFile(SOUND_GAME_OVER_SOURCE))==0 )
82 cerr<<"Error: loading file "<<SOUND_GAME_OVER_SOURCE<<" failed"<<endl;
85 if ( (Buffers[SOUND_GAME_VICTORY] = alutCreateBufferFromFile(SOUND_GAME_VICTORY_SOURCE))==0 )
87 cerr<<"Error: loading file "<<SOUND_GAME_VICTORY_SOURCE<<" failed"<<endl;
90 // Error check
91 if((error= alGetError() ) != AL_NO_ERROR)
93 snprintf(str, BUFSIZE, "Error loading sound files.(code %d)",error); str[BUFSIZE]='\0';
94 throw CAudioException(str);
97 // Bind buffers into audio sources.
98 alGenSources(NUM_SOURCES, Sources);
100 // Error check
101 if((error= alGetError() ) != AL_NO_ERROR)
103 snprintf(str, BUFSIZE, "Cannot generate sources for sounds.(code %d)",error); str[BUFSIZE]='\0';
104 throw CAudioException(str);
107 for (int i=0;i<NUM_SOURCES;i++)
109 alSourcei (Sources[i], AL_BUFFER, Buffers[i] );
110 alSourcef (Sources[i], AL_PITCH, 1.0f );
111 alSourcef (Sources[i], AL_GAIN, 1.0f );
112 alSourcefv(Sources[i], AL_POSITION, SourcesPos[i]);
113 alSourcefv(Sources[i], AL_VELOCITY, SourcesVel[i]);
114 // Only music files are in loop
115 if (i<NUM_MUSICS) alSourcei (Sources[i], AL_LOOPING, AL_TRUE);
116 else alSourcei (Sources[i], AL_LOOPING, AL_FALSE );
119 // Error check
120 if((error= alGetError() ) != AL_NO_ERROR)
122 snprintf(str, BUFSIZE, "Cannot assign values to sources.(code %d)",error); str[BUFSIZE]='\0';
123 throw CAudioException(str);
126 alListenerfv(AL_POSITION, ListenerPos);
127 alListenerfv(AL_VELOCITY, ListenerVel);
128 alListenerfv(AL_ORIENTATION, ListenerOri);
130 // Error check
131 if((error= alGetError() ) != AL_NO_ERROR)
133 snprintf(str, BUFSIZE, "Cannot assign values to listener.(code %d)",error); str[BUFSIZE]='\0';
134 throw CAudioException(str);
137 bSoundEnabled = true;
139 CAudio::~CAudio()
141 alDeleteBuffers(NUM_BUFFERS, Buffers);
142 alDeleteSources(NUM_SOURCES, Sources);
143 alutExit();
146 Function plays music by ID. It stops
147 actually playing music if any.
148 @param[in] ID = number of music source to play
150 void CAudio::PlayMusic(int ID)
152 if(ID == MUSIC_NOTHING) return StopMusic();
153 if ((ID>-1)&&(ID<NUM_MUSICS))
155 if (iActuallyPlayingMusic!=MUSIC_NOTHING) StopMusic();
156 alSourcePlay(Sources[ID]);
157 iActuallyPlayingMusic=ID;
161 Function stops actually playing music.
163 void CAudio::StopMusic(void)
165 if (iActuallyPlayingMusic!=MUSIC_NOTHING)
167 alSourceStop(Sources[iActuallyPlayingMusic]);
169 iActuallyPlayingMusic=MUSIC_NOTHING;
172 Function plays sound by ID.
173 @param[in] ID = number of sound source to play
175 void CAudio::PlaySound2(int ID)
177 if(!bSoundEnabled) return;
178 if ((ID>=NUM_MUSICS)&&(ID<NUM_SOURCES))
180 alSourcePlay(Sources[ID]);
181 iActuallyPlayingSound=ID;
185 Function stops actually playing sound or attempts to stop last played.
187 void CAudio::StopSound(void)
189 if ( iActuallyPlayingSound!=SOUND_NOTHING)
191 alSourceStop(Sources[iActuallyPlayingSound]);
193 iActuallyPlayingSound=SOUND_NOTHING;