AudioPlayer.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 )
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