www.pudn.com > Tetris.rar > CSoundMixer.cpp


#include "CSoundMixer.h" 
#include "CMixerThread.h" 
#include  
 
 
CSoundMixer* CSoundMixer::NewL() 
{ 
	CSoundMixer* self = new( ELeave )CSoundMixer(); 
	CleanupStack::PushL( self ); 
	self->ConstructL(); 
	CleanupStack::Pop( self ); 
	return self; 
} 
 
void CSoundMixer::ConstructL() 
{ 
	iShared.iMainVolume = 64;	// default main volume 
 
	User::LeaveIfError( iMixerThread.Create( _L("Mixer"), 
											 CMixerThread::ThreadFunction, 
											 KDefaultStackSize, 
											 NULL, 
											 NULL) ); 
 
	// Give all possible priority to audio 
	iMixerThread.SetPriority( EPriorityRealTime ); 
	RThread().SetPriority( EPriorityAbsoluteLow ); 
	 
	User::LeaveIfError( iShared.iAliveMutex.CreateLocal( 1 ) ); 
	User::LeaveIfError( iShared.iMutex.CreateLocal() ); 
	iMixerThread.SetInitialParameter( &iShared ); 
	iMixerThread.Resume(); 
	iPaused = ETrue; 
} 
 
CSoundMixer::CSoundMixer() 
{ 
} 
 
CSoundMixer::~CSoundMixer() 
{ 
	if( iPaused ) 
		SendCmd( ECmdStartMixer );			// maybe there's bug here 
 
	SendCmd( ECmdDestroyMixer ); 
	// 
	iShared.iAliveMutex.Wait(); 
	iShared.iAliveMutex.Close(); 
	iShared.iMutex.Close(); 
 
} 
 
void CSoundMixer::Pause() 
{ 
	//RDebug::Print( _L("CSndMixer::Pause") ); 
	// check if mutex is already in wait ( pause mode ) 
	if( iPaused ) 
	{ 
		return; 
	} 
 
	iPaused = ETrue; 
 
	SendCmd( ECmdStopMixer ); 
} 
 
void CSoundMixer::Resume() 
{ 
	//RDebug::Print( _L("CSoundMixer::Resume") ); 
	if( !iPaused ) 
	{ 
		return; 
	} 
	iPaused = EFalse; 
 
	SendCmd( ECmdStartMixer ); 
} 
 
void CSoundMixer::Play( const TSample& aSample, TInt aChannel, TInt aFrequency, TInt aVolume ) 
{ 
	iShared.iMutex.Wait(); 
	iShared.iPlayStarted[ aChannel ] = ETrue; 
	iShared.iSample[ aChannel ] = aSample; 
	iShared.iFrequency[ aChannel ] = aFrequency; 
	iShared.iVolume[ aChannel ] = aVolume; 
	iShared.iMutex.Signal(); 
} 
 
 
void CSoundMixer::Stop( TInt aChannel ) 
{ 
	iShared.iMutex.Wait(); 
	iShared.iPlayStarted[ aChannel ] = ETrue; 
	iShared.iSample[ aChannel ] = iEmptySample; 
	iShared.iMutex.Signal();	 
} 
 
 
void CSoundMixer::SetVolume( TInt aVolume ) 
{ 
	iShared.iMutex.Wait(); 
	iShared.iMainVolume = aVolume; 
	iShared.iMutex.Signal();	 
} 
 
 
TInt CSoundMixer::Volume() 
{ 
	return iShared.iMainVolume; 
} 
 
 
void CSoundMixer::SendCmd( TMixerCmd aCmd ) 
{ 
	iShared.iMutex.Wait(); 
	iShared.iCmd = aCmd; 
	iShared.iMutex.Signal(); 
	iMixerThread.RaiseException( EExcUserInterrupt ); 
}