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
=MUSIC_NOTHING
;
31 iActuallyPlayingSound
=SOUND_NOTHING
;
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 // Load wav data into buffers
46 alGenBuffers(NUM_BUFFERS
, Buffers
);
49 if(alGetError() != AL_NO_ERROR
)
51 sprintf(str
, "Cannot generate buffers for sounds.");
52 throw CAudioException(str
);
56 Buffers
[MUSIC_SLOW
] = alutCreateBufferFromFile(MUSIC_SLOW_SOURCE
);
57 cerr
<< Buffers
[MUSIC_SLOW
] << endl
;
59 Buffers
[MUSIC_FAST
] = alutCreateBufferFromFile(MUSIC_FAST_SOURCE
);
60 cerr
<< Buffers
[MUSIC_FAST
] << endl
;
62 Buffers
[SOUND_BUTTON_SUCCESS
] = alutCreateBufferFromFile(SOUND_BUTTON_SUCCESS_SOURCE
);
63 cerr
<< Buffers
[SOUND_BUTTON_SUCCESS
] << endl
;
65 Buffers
[SOUND_BUTTON_HOVER
] = alutCreateBufferFromFile(SOUND_BUTTON_HOVER_SOURCE
);
66 cerr
<< Buffers
[SOUND_BUTTON_HOVER
] << endl
;
68 Buffers
[SOUND_GAME_OVER
] = alutCreateBufferFromFile(SOUND_GAME_OVER_SOURCE
);
69 cerr
<< Buffers
[SOUND_GAME_OVER
] << endl
;
71 Buffers
[SOUND_GAME_VICTORY
] = alutCreateBufferFromFile(SOUND_GAME_VICTORY_SOURCE
);
72 cerr
<< Buffers
[SOUND_GAME_VICTORY
] << endl
;
74 // Ignore error by loading files
78 if(alGetError() != AL_NO_ERROR)
80 sprintf(str, "Cannot load all sound files.");
81 throw CAudioException(str);
84 // Bind buffers into audio sources.
85 alGenSources(NUM_SOURCES
, Sources
);
88 if(alGetError() != AL_NO_ERROR
)
90 sprintf(str
, "Cannot generate sources for sounds.");
91 throw CAudioException(str
);
94 for (int i
=0;i
<NUM_SOURCES
;i
++)
96 alSourcei (Sources
[i
], AL_BUFFER
, Buffers
[i
] );
97 alSourcef (Sources
[i
], AL_PITCH
, 1.0f
);
98 alSourcef (Sources
[i
], AL_GAIN
, 1.0f
);
99 alSourcefv(Sources
[i
], AL_POSITION
, SourcesPos
[i
]);
100 alSourcefv(Sources
[i
], AL_VELOCITY
, SourcesVel
[i
]);
101 // Only music files are in loop
102 if (i
<NUM_MUSICS
) alSourcei (Sources
[i
], AL_LOOPING
, AL_TRUE
);
103 else alSourcei (Sources
[i
], AL_LOOPING
, AL_FALSE
);
107 if(alGetError() != AL_NO_ERROR
)
109 sprintf(str
, "Cannot assign values to sources.");
110 throw CAudioException(str
);
113 alListenerfv(AL_POSITION
, ListenerPos
);
114 alListenerfv(AL_VELOCITY
, ListenerVel
);
115 alListenerfv(AL_ORIENTATION
, ListenerOri
);
118 if(alGetError() != AL_NO_ERROR
)
120 sprintf(str
, "Cannot assign values to listener.");
121 throw CAudioException(str
);
124 bSoundEnabled
= true;
128 alDeleteBuffers(NUM_BUFFERS
, Buffers
);
129 alDeleteSources(NUM_SOURCES
, Sources
);
133 Function plays music by ID. It stops
134 actually playing music if any.
135 @param[in] ID = number of music source to play
137 void CAudio::PlayMusic(int ID
)
139 if ((ID
>-1)&&(ID
<NUM_MUSICS
))
141 if (iActuallyPlayingMusic
!=MUSIC_NOTHING
) StopMusic();
142 alSourcePlay(Sources
[ID
]);
143 iActuallyPlayingMusic
=ID
;
147 Function stops actually playing music.
149 void CAudio::StopMusic(void)
151 if (iActuallyPlayingMusic
!=MUSIC_NOTHING
)
153 alSourceStop(Sources
[iActuallyPlayingMusic
]);
155 iActuallyPlayingMusic
=MUSIC_NOTHING
;
158 Function plays sound by ID.
159 @param[in] ID = number of sound source to play
161 void CAudio::PlaySound(int ID
)
163 if(!bSoundEnabled
) return;
164 if ((ID
>=NUM_MUSICS
)&&(ID
<NUM_SOURCES
))
166 alSourcePlay(Sources
[ID
]);
167 iActuallyPlayingSound
=ID
;
171 Function stops actually playing sound or attempts to stop last played.
173 void CAudio::StopSound(void)
175 if ( iActuallyPlayingSound
!=SOUND_NOTHING
)
177 alSourceStop(Sources
[iActuallyPlayingSound
]);
179 iActuallyPlayingSound
=SOUND_NOTHING
;