CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 #include "AudioPlayer.hxx" 00023 00024 #include "Audio.hxx" 00025 #include "AudioIO.hxx" 00026 #include "AudioOut.hxx" 00027 #include "AudioManager.hxx" 00028 00029 #include "DataTypes.hxx" 00030 00031 #include "Err.hxx" 00032 00033 using namespace CLAM; 00034 00035 AudioPlayer* AudioPlayer::sCurrentPlayer = NULL; 00036 00037 AudioPlayer::AudioPlayer( Audio* audio, SigSlot::Slotv0& slot, TTime t0 ) 00038 : mAudioReference( audio ), mT0( t0 ) 00039 { 00040 mCancel = false; 00041 sCurrentPlayer = this; 00042 00043 mRequestStop.Connect( slot ); 00044 00045 pthread_create( &mThread, 0, sPlayingThreadSafe, this ); 00046 } 00047 00048 AudioPlayer::~AudioPlayer( ) 00049 { 00050 mCancel = true ; 00051 pthread_join( mThread, 0 ); 00052 delete mAudioReference; 00053 } 00054 00055 void AudioPlayer::PlayingThreadSafe( ) 00056 { 00057 TSize bufferSize=512; 00058 AudioManager audioManager( (int)mAudioReference->GetSampleRate(), 4096 ); 00059 00060 AudioIOConfig mOutCfgL; 00061 AudioIOConfig mOutCfgR; 00062 AudioOut mOutputL; 00063 AudioOut mOutputR; 00064 00065 mOutCfgL.SetChannelID( 0 ); 00066 mOutCfgR.SetChannelID( 1 ); 00067 00068 mOutputL.Configure( mOutCfgL ); 00069 mOutputR.Configure( mOutCfgR ); 00070 00071 Audio tmpAudioBuffer; 00072 tmpAudioBuffer.SetSize(bufferSize); 00073 TSize dataSize = mAudioReference->GetSize(); 00074 AudioManager::Current().Start(); 00075 00076 // first sample calculation 00077 TIndex firstSample = 00078 ((mT0*1000. - mAudioReference->GetBeginTime())/(mAudioReference->GetEndTime()-mAudioReference->GetBeginTime()))*((TTime)mAudioReference->GetSize()-1.); 00079 00080 CLAM_ASSERT( firstSample >= 0, "Bad sample index!" ); 00081 CLAM_ASSERT( firstSample < mAudioReference->GetSize(), "Bad sample index!" ); 00082 00083 mOutputL.Start(); 00084 mOutputR.Start(); 00085 for( TIndex i=firstSample; i<dataSize && !mCancel; i+=bufferSize ) 00086 { 00087 mAudioReference->GetAudioChunk( i, i + tmpAudioBuffer.GetSize(), tmpAudioBuffer, false ); 00088 mOutputR.Do( tmpAudioBuffer ); 00089 mOutputL.Do( tmpAudioBuffer ); 00090 } 00091 00092 if( !mCancel ) // True if we finished to play all the Audio 00093 mRequestStop.Emit( ); 00094 } 00095 00096 void* AudioPlayer::sPlayingThreadSafe(void* ptr) 00097 { 00098 ((AudioPlayer*)ptr)->PlayingThreadSafe(); 00099 00100 return NULL; 00101 } 00102 00103 void AudioPlayer::StopFromGUIThread( ) 00104 { 00105 if( sCurrentPlayer ) 00106 { 00107 delete sCurrentPlayer; 00108 sCurrentPlayer = NULL; 00109 } 00110 } 00111