00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
#include <ptf/environment/audio.hpp>
00019
#include <ptf/core/ptfcore.hpp>
00020
#include <stdexcept>
00021
00025 Audio::Audio() {
00026
00027
00028
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1) {
00029
PtfCore::getSingleton().
getMessageLog() <<
ERROR(
"Audio system failed to initialize.\n");
00030
throw std::runtime_error(
"Audio system failed to initialize.");
00031 }
00032
else {
00033
PtfCore::getSingleton().
getMessageLog() <<
MESSAGE(
"Audio system initialized.\n");
00034 }
00035
00036 Mix_AllocateChannels(4);
00037
00038 Mix_Volume(-1, MIX_MAX_VOLUME/2);
00039 }
00040
00044 Audio::~Audio() {
00045
00046
for (
int i = 0; i < (
int)samples.size(); i++)
delete samples[i];
00047
00048 Mix_CloseAudio();
00049 }
00050
00057 int Audio::addSample(
char* fname) {
00058 Mix_Chunk* sample;
00059 sample = Mix_LoadWAV(fname);
00060
if(sample == 0) {
00061
PtfCore::getSingleton().
getMessageLog() <<
"ERROR: Failed to load an audio sample ("
00062 << fname <<
").\n";
00063
throw std::runtime_error(
"Failed to load an audio sample.");
00064 }
00065
else {
00066 samples.push_back(sample);
00067
return (
int)samples.size();
00068 }
00069 }
00070
00076 void Audio::playSample(
int index) {
00077
if (index >= (
int)samples.size() || index < 0) {
00078
PtfCore::getSingleton().
getMessageLog()
00079 <<
"ERROR: Sample doesn't exist (number " << index <<
").\n";
00080
throw std::runtime_error(
"Sample doesn't exist.");
00081 }
00082
00083
if (Mix_PlayChannel(-1, samples[index], 0) < 0) {
00084
PtfCore::getSingleton().
getMessageLog() <<
"ERROR: Failed to play an audio sample. \n";
00085
throw std::runtime_error(
"Failed to play an audio sample.");
00086 }
00087 }