3 * Author: Jindrich Kovar
8 // String for exceptions
11 // Position of the listener.
12 ListenerPos
[0]=0.0; //{ 0.0, 0.0, 0.0 }
16 // Velocity of the listener.
17 ListenerVel
[0]=0.0; //{ 0.0, 0.0, 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 }
29 // Default value (nothing is playing)
30 iActuallyPlayingMusic
=NOTHING_PLAYING
;
31 iActuallyPlayingSound
=NOTHING_PLAYING
;
33 // Initialize OpenAL and clear the error bit
38 if(alGetError() != AL_NO_ERROR
)
40 sprintf(str
, "Cannot initialize OpenAl.");
41 throw CAudioException(str
);
45 // Variables to load into
52 // Load wav data into buffers
53 alGenBuffers(NUM_BUFFERS
, Buffers
);
56 if(alGetError() != AL_NO_ERROR
)
58 sprintf(str
, "Cannot generate buffers for sounds.");
59 throw CAudioException(str
);
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
);
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
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
);
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
);
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
);
132 if(alGetError() != AL_NO_ERROR
)
134 sprintf(str
, "Cannot assign values to listener.");
135 throw CAudioException(str
);
140 alDeleteBuffers(NUM_BUFFERS
, Buffers
);
141 alDeleteSources(NUM_SOURCES
, Sources
);
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
;