3 * Author: Jindrich Kovar
9 #define PRINT_LOADING_ERROR(FILENAME) cerr << "Error: loading file " << FILENAME << " failed!" << endl;
13 // String for exceptions
16 // Position of the listener.
17 ListenerPos
[0]=0.0; //{ 0.0, 0.0, 0.0 }
21 // Velocity of the listener.
22 ListenerVel
[0]=0.0; //{ 0.0, 0.0, 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 }
34 // Default value (nothing is playing)
35 iActuallyPlayingMusic
=MUSIC_NOTHING
;
36 iActuallyPlayingSound
=SOUND_NOTHING
;
38 // Initialize OpenAL and clear the error bit
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
);
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
);
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
)
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
);
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
);
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
);
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;
131 alDeleteBuffers(NUM_BUFFERS
, Buffers
);
132 alDeleteSources(NUM_SOURCES
, Sources
);
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
;