AudioDevice.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of Micole Architecture
00003  *
00004  * Copyright (C) 2007 Micole partners
00005  *
00006  * Micole Architecture is free software: you can redistribute it 
00007  * and/or modify it under the terms of the GNU Lesser General 
00008  * Public License as published by the Free Software Foundation, 
00009  * either version 3 of the License, or (at your option) any 
00010  * later version.
00011  *
00012  * Micole Architecture is distributed in the hope that it will be 
00013  * useful, * but WITHOUT ANY WARRANTY; without even the implied 
00014  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00015  * PURPOSE.  See the GNU Lesser General Public License for more 
00016  * details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Micole Architecture.  If not, see
00020  * <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #include "stdafx.h"
00024 #include "AudioDevice.h"
00025 
00026 #define MAXVOL 10000
00027 
00029 
00030 uint8 AudioDevice::nextDevice = 1;
00031 uint8 AudioDevice::activeDevice = 1;
00032 
00034 
00035 AudioDevice::AudioDevice (std::string id) {
00036 
00037         do {
00038                 currDevice = nextDevice++;
00039                 BASS_Init(currDevice, 44100, BASS_DEVICE_SPEAKERS, 0, NULL);
00040         } while (BASS_ErrorGetCode() == BASS_ERROR_ALREADY);
00041 
00042         if(BASS_ErrorGetCode() == BASS_OK)
00043         {
00044                 this->id = id;
00045                 BASS_SetDevice(currDevice);
00046                 activeDevice = currDevice;
00047                 BASS_GetInfo(&info);
00048                 vol = uint16(BASS_GetVolume());
00049         }
00050         else
00051                 currDevice = 0;
00052 }
00053 
00055 
00056 AudioDevice::~AudioDevice (void)
00057 {
00058         AudioStreamMap::const_iterator iter;
00059 
00060         if(currDevice != activeDevice)
00061         {
00062                 BASS_SetDevice(currDevice);
00063                 activeDevice = currDevice;
00064         }
00065 
00066         for(iter = audioStreamMap.begin(); iter != audioStreamMap.end(); ++iter)
00067                 delete (*iter).second;
00068         audioStreamMap.clear();
00069 
00070         BASS_Free();
00071         nextDevice = currDevice;
00072 }
00073 
00075 
00076 AudioStream* AudioDevice::CreateStream (std::string id, const char* filename, uint32 speakers, bool mono, bool looped)
00077 {
00078         AudioStream* audioStream;
00079         AudioStreamMap::const_iterator iter;
00080 
00081         if(currDevice != activeDevice)
00082         {
00083                 BASS_SetDevice(currDevice);
00084                 activeDevice = currDevice;
00085         }
00086 
00087         if(!(audioStream = new AudioStream(filename, speakers, mono, looped)))
00088                 return (AudioStream*)0;
00089 
00090         if((iter = audioStreamMap.find(id)) == audioStreamMap.end())
00091                 audioStreamMap[id] = audioStream;
00092         else
00093         {
00094                 delete (*iter).second;
00095                 audioStreamMap[id] = audioStream;
00096         }
00097         return audioStream;
00098 }
00099 
00101 
00102 AudioStream* AudioDevice::GetStream (std::string id)
00103 {
00104         AudioStreamMap::const_iterator iter;
00105 
00106         if((iter = audioStreamMap.find(id)) != audioStreamMap.end())
00107                 return (*iter).second;
00108         else
00109                 return (AudioStream*)0;
00110 }
00111 
00113 
00114 void AudioDevice::RemoveStream (std::string id)
00115 {
00116         AudioStreamMap::iterator iter;
00117 
00118         if((iter = audioStreamMap.find(id)) != audioStreamMap.end())
00119         {
00120                 delete (*iter).second;
00121                 audioStreamMap.erase(iter);
00122         }
00123 }
00124 
00126 
00127 void AudioDevice::SetVolume (uint16 volume)
00128 {
00129         if(currDevice != activeDevice)
00130         {
00131                 BASS_SetDevice(currDevice);
00132                 activeDevice = currDevice;
00133         }
00134 
00135         if(BASS_SetVolume(volume > MAXVOL ? MAXVOL : volume))
00136                 vol = volume;
00137 }
00138 
00140 
00141 uint16 AudioDevice::GetVolume (void)
00142 {
00143         return vol;
00144 }
00145 
00147 
00148 std::string     AudioDevice::GetDeviceDescription (void)
00149 {
00150         return std::string(BASS_GetDeviceDescription(currDevice));
00151 }
00152 
00154 
00155 uint8 AudioDevice::GetDeviceID (void)
00156 {
00157         return currDevice;
00158 }
00159 
00161 
00162 void AudioDevice::Start (void)
00163 {
00164         if(currDevice != activeDevice)
00165         {
00166                 BASS_SetDevice(currDevice);
00167                 activeDevice = currDevice;
00168         }
00169 
00170         BASS_Start();
00171 }
00172 
00174 
00175 void AudioDevice::Pause (void)
00176 {
00177         if(currDevice != activeDevice)
00178         {
00179                 BASS_SetDevice(currDevice);
00180                 activeDevice = currDevice;
00181         }
00182 
00183         BASS_Pause();
00184 }
00185 
00187 
00188 void AudioDevice::Stop (void)
00189 {
00190         if(currDevice != activeDevice)
00191         {
00192                 BASS_SetDevice(currDevice);
00193                 activeDevice = currDevice;
00194         }
00195 
00196         BASS_Stop();
00197 }
00198         
00200 
00201 uint8 AudioDevice::GetSpeakersNum (void)
00202 {
00203         return uint8(info.speakers);
00204 }
00205 

Generated on Tue Oct 16 17:10:42 2007 for Micole by  doxygen 1.4.7