CAudio: Imbecilic function name PlaySound2() changed to PlayEffect(). (Also StopSound...
[mines3d.git] / sound / CAudio.cpp
blobaf61ce39f0fb653a186a6798923a8cd52611067f
1 /*
2 * File: CAudio.cpp
3 * Author: Jindrich Kovar
4 */
5 #include "CAudio.h"
7 #define BUFSIZE 100
9 #define PRINT_LOADING_ERROR(FILENAME) cerr << "Error: loading file " << FILENAME << " failed!" << endl;
11 CAudio::CAudio()
13 // String for exceptions
14 char str[BUFSIZE];
16 // Position of the listener.
17 ListenerPos[0]=0.0; //{ 0.0, 0.0, 0.0 }
18 ListenerPos[1]=0.0;
19 ListenerPos[2]=0.0;
21 // Velocity of the listener.
22 ListenerVel[0]=0.0; //{ 0.0, 0.0, 0.0 }
23 ListenerVel[1]=0.0;
24 ListenerVel[2]=0.0;
26 // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
27 ListenerOri[0]=0.0;// { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }
28 ListenerOri[1]=0.0;
29 ListenerOri[2]=-1.0;
30 ListenerOri[3]=0.0;
31 ListenerOri[4]=1.0;
32 ListenerOri[5]=0.0;
34 // Default value (nothing is playing)
35 iActuallyPlayingMusic=MUSIC_NOTHING;
36 iActuallyPlayingSound=SOUND_NOTHING;
38 // Initialize OpenAL and clear the error bit
39 alutInit(NULL, 0);
40 alGetError();
42 // Error check
43 int error;
44 if((error= alGetError() ) != AL_NO_ERROR)
46 snprintf(str, BUFSIZE, "Cannot initialize OpenAl. (code %d)",error); str[BUFSIZE]='\0';
47 throw CAudioException(str);
51 // Load wav data into buffers
52 alGenBuffers(NUM_BUFFERS, Buffers);
54 // Error check
55 if((error= alGetError() ) != AL_NO_ERROR)
57 snprintf(str, BUFSIZE, "Cannot generate buffers for sounds.(code %d)",error); str[BUFSIZE]='\0';
58 throw CAudioException(str);
61 // Load music files
62 if ( (Buffers[MUSIC_SLOW] = alutCreateBufferFromFile(MUSIC_SLOW_SOURCE))==0 )
63 PRINT_LOADING_ERROR(MUSIC_SLOW_SOURCE)
65 if ( (Buffers[MUSIC_FAST] = alutCreateBufferFromFile(MUSIC_FAST_SOURCE))==0 )
66 PRINT_LOADING_ERROR(MUSIC_FAST_SOURCE)
68 if ( (Buffers[SOUND_BUTTON_SUCCESS] = alutCreateBufferFromFile(SOUND_BUTTON_SUCCESS_SOURCE))==0 )
69 PRINT_LOADING_ERROR(SOUND_BUTTON_SUCCESS_SOURCE)
71 if ( (Buffers[SOUND_BUTTON_HOVER] = alutCreateBufferFromFile(SOUND_BUTTON_HOVER_SOURCE))==0 )
72 PRINT_LOADING_ERROR(SOUND_BUTTON_HOVER_SOURCE)
74 if ( (Buffers[SOUND_GAME_OVER] = alutCreateBufferFromFile(SOUND_GAME_OVER_SOURCE))==0 )
75 PRINT_LOADING_ERROR(SOUND_GAME_OVER_SOURCE)
77 if ( (Buffers[SOUND_GAME_VICTORY] = alutCreateBufferFromFile(SOUND_GAME_VICTORY_SOURCE))==0 )
78 PRINT_LOADING_ERROR(SOUND_GAME_VICTORY_SOURCE)
80 // Error check
81 if((error= alGetError() ) != AL_NO_ERROR)
83 snprintf(str, BUFSIZE, "Error loading sound files.(code %d)",error); str[BUFSIZE]='\0';
84 throw CAudioException(str);
87 // Bind buffers into audio sources.
88 alGenSources(NUM_SOURCES, Sources);
90 // Error check
91 if((error= alGetError() ) != AL_NO_ERROR)
93 snprintf(str, BUFSIZE, "Cannot generate sources for sounds.(code %d)",error); str[BUFSIZE]='\0';
94 throw CAudioException(str);
97 for (int i=0;i<NUM_SOURCES;i++)
99 alSourcei (Sources[i], AL_BUFFER, Buffers[i] );
100 alSourcef (Sources[i], AL_PITCH, 1.0f );
101 alSourcef (Sources[i], AL_GAIN, 1.0f );
102 alSourcefv(Sources[i], AL_POSITION, SourcesPos[i]);
103 alSourcefv(Sources[i], AL_VELOCITY, SourcesVel[i]);
104 // Only music files are in loop
105 if (i<NUM_MUSICS) alSourcei (Sources[i], AL_LOOPING, AL_TRUE);
106 else alSourcei (Sources[i], AL_LOOPING, AL_FALSE );
109 // Error check
110 if((error= alGetError() ) != AL_NO_ERROR)
112 snprintf(str, BUFSIZE, "Cannot assign values to sources.(code %d)",error); str[BUFSIZE]='\0';
113 throw CAudioException(str);
116 alListenerfv(AL_POSITION, ListenerPos);
117 alListenerfv(AL_VELOCITY, ListenerVel);
118 alListenerfv(AL_ORIENTATION, ListenerOri);
120 // Error check
121 if((error= alGetError() ) != AL_NO_ERROR)
123 snprintf(str, BUFSIZE, "Cannot assign values to listener.(code %d)",error); str[BUFSIZE]='\0';
124 throw CAudioException(str);
127 bSoundEnabled = true;
129 CAudio::~CAudio()
131 alDeleteBuffers(NUM_BUFFERS, Buffers);
132 alDeleteSources(NUM_SOURCES, Sources);
133 alutExit();
136 Function plays music by ID. It stops
137 actually playing music if any.
138 @param[in] ID = number of music source to play
140 void CAudio::PlayMusic(int ID)
142 if(ID == MUSIC_NOTHING) return StopMusic();
143 if ((ID>-1)&&(ID<NUM_MUSICS))
145 if (iActuallyPlayingMusic!=MUSIC_NOTHING) StopMusic();
146 alSourcePlay(Sources[ID]);
147 iActuallyPlayingMusic=ID;
151 Function stops actually playing music.
153 void CAudio::StopMusic(void)
155 if (iActuallyPlayingMusic!=MUSIC_NOTHING)
157 alSourceStop(Sources[iActuallyPlayingMusic]);
159 iActuallyPlayingMusic=MUSIC_NOTHING;
162 Function plays sound by ID.
163 @param[in] ID = number of sound source to play
165 void CAudio::PlayEffect(int ID)
167 if(!bSoundEnabled) return;
168 if ((ID>=NUM_MUSICS)&&(ID<NUM_SOURCES))
170 alSourcePlay(Sources[ID]);
171 iActuallyPlayingSound=ID;
175 Function stops actually playing sound or attempts to stop last played.
177 void CAudio::StopEffect(void)
179 if ( iActuallyPlayingSound!=SOUND_NOTHING)
181 alSourceStop(Sources[iActuallyPlayingSound]);
183 iActuallyPlayingSound=SOUND_NOTHING;