AudioStream.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 <iostream>
00025 using namespace std;
00026 
00027 #include <bass.h>
00028 #include <cassert>
00029 
00030 #include "AudioStream.h"
00031 
00032 #define MAX_SPEAKER_NUM 30
00033 
00035 
00036 AudioStream::AudioStream (const char* filename, uint32 speakers, bool mono, bool looped)
00037 {
00038         assert(filename);
00039 
00040         uint32 BASS_Speakers = AssignSpeakers(speakers);
00041         uint32 flags =  (looped ? BASS_SAMPLE_LOOP : 0) |
00042                                         (mono ? BASS_SAMPLE_MONO : 0) |
00043                                         BASS_Speakers;
00044 
00045         handle = BASS_StreamCreateFile(false, filename, 0, 0, flags);
00046 
00047         assert(BASS_ErrorGetCode() == BASS_OK);
00048         size = BASS_ChannelGetLength(handle);
00049         BASS_ChannelGetInfo(handle, &info);
00050         BASS_ChannelGetAttributes(handle, (unsigned long*)&freq, (unsigned long*)&vol, &pan);
00051         assert(BASS_ErrorGetCode() == BASS_OK);
00052 }
00053 
00055 
00056 AudioStream::~AudioStream (void)
00057 {
00058         BASS_StreamFree(handle);
00059 }
00060 
00062 
00063 uint32 AudioStream::AssignSpeakers (uint32 speakers)
00064 {
00065         uint32 BASS_Speakers = 0;
00066         uint8 i;
00067 
00068         switch(speakers & 0xFFFFFFFF) {
00069                 case 0x0:
00070                         BASS_Speakers = BASS_SPEAKER_FRONT;
00071                         break;
00072                 case 0x40000000:
00073                         BASS_Speakers = BASS_SPEAKER_REAR;
00074                         break;
00075                 case 0x80000000:
00076                         BASS_Speakers = BASS_SPEAKER_CENLFE;
00077                         break;
00078                 case 0xC0000000:
00079                         BASS_Speakers = BASS_SPEAKER_REAR2;
00080                         break;
00081                 default:
00082                         for(i = 1; i <= MAX_SPEAKER_NUM; ++i) {
00083                                 if(speakers & SPEAKER(i)) {
00084                                         BASS_Speakers = (BASS_SPEAKER_N((i+1)/2) |
00085                                                                         (i%2 ? BASS_SPEAKER_LEFT : BASS_SPEAKER_RIGHT));
00086                                         break;
00087                                 }
00088                         }
00089         }
00090         return BASS_Speakers;
00091 }
00092 
00094 
00095 void AudioStream::Play (bool restart) {
00096 
00097         BASS_ChannelPlay(handle, restart);
00098         assert(BASS_ErrorGetCode() == BASS_OK);
00099 }
00100 
00102 
00103 void AudioStream::Pause (void) {
00104 
00105         BASS_ChannelPause(handle);
00106 }
00107 
00109 
00110 void AudioStream::Stop (void) {
00111 
00112         BASS_ChannelStop(handle);
00113 }
00114 
00116 
00117 uint64 AudioStream::GetSize (void)
00118 {
00119         return size;
00120 }
00121 
00123 
00124 int AudioStream::GetLength (void)
00125 {
00126         return int(BASS_ChannelGetLength(handle));
00127 }
00128 
00130 
00131 float AudioStream::GetTimeRemaining (void)
00132 {
00133         return (GetLength() - GetTimeElapsed());
00134 }
00135 
00137 
00138 float AudioStream::GetTimeElapsed (void)
00139 {
00140         return BASS_ChannelBytes2Seconds(handle, BASS_ChannelGetPosition(handle));
00141 }
00142 
00144 
00145 void AudioStream::SetPosition (float sec)
00146 {
00147         uint16 len = uint16(BASS_ChannelGetLength(handle));
00148         if(sec > 0 && sec < len)
00149                 BASS_ChannelSetPosition(handle, BASS_ChannelSeconds2Bytes(handle, sec));
00150 }
00151 
00153 
00154 void AudioStream::SetSpekears (uint32 speakers)
00155 {
00156         uint32 BASS_Speakers = AssignSpeakers(speakers);
00157         info.flags &= ~0x3F000000; // clear all speaker flags.
00158         info.flags |= BASS_Speakers;
00159         BASS_ChannelSetFlags(handle, info.flags);
00160 }
00161 
00163 
00164 void AudioStream::SetFrequency (uint32 freq)
00165 {
00166         if(BASS_ChannelSetAttributes(handle, freq, -1, -101))
00167                 this->freq = freq;
00168 }
00169 
00171 
00172 uint32 AudioStream::GetFrequency (void)
00173 {
00174         return freq;
00175 }
00176 
00178 
00179 void AudioStream::SetVolume (uint16 vol)
00180 {
00181         if(BASS_ChannelSetAttributes(handle, -1, vol, -101))
00182                 this->vol = vol;
00183 }
00184 
00186 
00187 uint16 AudioStream::GetVolume (void)
00188 {
00189         return vol;
00190 }
00191 
00193 
00194 void AudioStream::SetPanning (sint8 pan)
00195 {
00196         if(BASS_ChannelSetAttributes(handle, -1, -1, pan))
00197                 this->pan = pan;
00198 }
00199 
00201 
00202 sint8 AudioStream::GetPanning (void)
00203 {
00204         return pan;
00205 }
00206 
00208 
00209 void AudioStream::PrintChannelAttributes (void)
00210 {
00211         cerr << "============= Audio Stream: " << handle << " =========" << endl;
00212         cerr << "Frequency: " << freq << endl << "Volume: " << vol << endl << "Panning:" << pan << endl;
00213         cerr << "freq: " << info.freq << endl << "Channels:" << info.chans << endl;
00214         cerr << "length: " << BASS_ChannelGetLength(handle) << endl << "size:" << size << endl;
00215 }
00216 

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