Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

audio.cpp

Go to the documentation of this file.
00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/environment/audio.hpp> 00019 #include <ptf/core/ptfcore.hpp> 00020 #include <stdexcept> 00021 00025 Audio::Audio() { 00026 // initialize audio system 00027 // open 44.1KHz, signed 16bit, system byte order, stereo audio, using 1024 byte chunks 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 // allocate 4 mixing channels 00036 Mix_AllocateChannels(4); 00037 // set all channels to half volume 00038 Mix_Volume(-1, MIX_MAX_VOLUME/2); 00039 } 00040 00044 Audio::~Audio() { 00045 // delete samples 00046 for (int i = 0; i < (int)samples.size(); i++) delete samples[i]; 00047 // close audio 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 // play desired sample once using first free channel 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 }

Generated on Fri Dec 17 14:54:23 2004 for Psychological Test Framework by doxygen 1.3.8