CLAM-Development  1.4.0
AudioPlayer.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "AudioPlayer.hxx"
23 
24 #include "Audio.hxx"
25 #include "AudioIO.hxx"
26 #include "AudioOut.hxx"
27 #include "AudioManager.hxx"
28 
29 #include "DataTypes.hxx"
30 
31 #include "Err.hxx"
32 
33 using namespace CLAM;
34 
35 AudioPlayer* AudioPlayer::sCurrentPlayer = NULL;
36 
38  : mAudioReference( audio ), mT0( t0 )
39 {
40  mCancel = false;
41  sCurrentPlayer = this;
42 
43  mRequestStop.Connect( slot );
44 
45  pthread_create( &mThread, 0, sPlayingThreadSafe, this );
46 }
47 
49 {
50  mCancel = true ;
51  pthread_join( mThread, 0 );
52  delete mAudioReference;
53 }
54 
55 void AudioPlayer::PlayingThreadSafe( )
56 {
57  TSize bufferSize=512;
58  AudioManager audioManager( (int)mAudioReference->GetSampleRate(), 4096 );
59 
60  AudioIOConfig mOutCfgL;
61  AudioIOConfig mOutCfgR;
62  AudioOut mOutputL;
63  AudioOut mOutputR;
64 
65  mOutCfgL.SetChannelID( 0 );
66  mOutCfgR.SetChannelID( 1 );
67 
68  mOutputL.Configure( mOutCfgL );
69  mOutputR.Configure( mOutCfgR );
70 
71  Audio tmpAudioBuffer;
72  tmpAudioBuffer.SetSize(bufferSize);
73  TSize dataSize = mAudioReference->GetSize();
75 
76  // first sample calculation
77  TIndex firstSample =
78  ((mT0*1000. - mAudioReference->GetBeginTime())/(mAudioReference->GetEndTime()-mAudioReference->GetBeginTime()))*((TTime)mAudioReference->GetSize()-1.);
79 
80  CLAM_ASSERT( firstSample >= 0, "Bad sample index!" );
81  CLAM_ASSERT( firstSample < mAudioReference->GetSize(), "Bad sample index!" );
82 
83  mOutputL.Start();
84  mOutputR.Start();
85  for( TIndex i=firstSample; i<dataSize && !mCancel; i+=bufferSize )
86  {
87  mAudioReference->GetAudioChunk( i, i + tmpAudioBuffer.GetSize(), tmpAudioBuffer, false );
88  mOutputR.Do( tmpAudioBuffer );
89  mOutputL.Do( tmpAudioBuffer );
90  }
91 
92  if( !mCancel ) // True if we finished to play all the Audio
93  mRequestStop.Emit( );
94 }
95 
96 void* AudioPlayer::sPlayingThreadSafe(void* ptr)
97 {
98  ((AudioPlayer*)ptr)->PlayingThreadSafe();
99 
100  return NULL;
101 }
102 
104 {
105  if( sCurrentPlayer )
106  {
107  delete sCurrentPlayer;
108  sCurrentPlayer = NULL;
109  }
110 }
111