3 * Author: Jindrich Kovar
11 // String for exceptions
14 // Position of the listener.
15 ListenerPos
[0]=0.0; //{ 0.0, 0.0, 0.0 }
19 // Velocity of the listener.
20 ListenerVel
[0]=0.0; //{ 0.0, 0.0, 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 }
32 // Default value (nothing is playing)
33 iActuallyPlayingMusic
=MUSIC_NOTHING
;
34 iActuallyPlayingSound
=SOUND_NOTHING
;
36 // Initialize OpenAL and clear the error bit
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
);
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
);
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
;
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
);
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
);
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
);
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;
141 alDeleteBuffers(NUM_BUFFERS
, Buffers
);
142 alDeleteSources(NUM_SOURCES
, Sources
);
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
;