www.pudn.com > SoundMixer_Example_v1_0.zip > CSndMixer.cpp


   /* 
    * 
============================================================================ 
    *  Name     : CSndMixer.cpp 
    *  Part of  : SoundMixer 
    *  Created  : 03/01/2003 by Forum Nokia 
    *  Description: 
    *     This is the project specification file for SoundMixer. 
    *     Initial content was generated by Series 60 AppWizard. 
    * 
    *  Version  : 1.0.0 
    *  Copyright: Forum Nokia 
    * 
============================================================================ 
    */ 
 
#include "CSndMixer.h" 
#include "CMixerThread.h" 
#include  
 
CSndMixer* CSndMixer::NewL() 
	{ 
	CSndMixer* self = new( ELeave )CSndMixer(); 
	CleanupStack::PushL( self ); 
	self->ConstructL(); 
	CleanupStack::Pop( self ); 
	return self; 
	} 
 
void CSndMixer::ConstructL() 
	{ 
	iShared.iMainVolume = 256;	// 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; 
	} 
 
CSndMixer::CSndMixer() 
	{ 
 
	} 
 
CSndMixer::~CSndMixer() 
	{ 
	if( iPaused ) 
		{ 
		SendCmd( ECmdStartMixer ); 
		} 
	SendCmd( ECmdDestroyMixer ); 
	// 
	iShared.iAliveMutex.Wait(); 
	iShared.iAliveMutex.Close(); 
	iShared.iMutex.Close(); 
 
	} 
 
void CSndMixer::Pause() 
	{ 
	RDebug::Print( _L("CSndMixer::Pause") ); 
	// check if mutex is already in wait ( pause mode ) 
	if( iPaused ) 
		{ 
		return; 
		} 
 
	iPaused = ETrue; 
 
	SendCmd( ECmdStopMixer ); 
	} 
 
void CSndMixer::Resume() 
	{ 
	RDebug::Print( _L("CSndMixer::Resume") ); 
	if( !iPaused ) 
		{ 
		return; 
		} 
	iPaused = EFalse; 
 
	SendCmd( ECmdStartMixer ); 
	} 
 
void CSndMixer::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 CSndMixer::Stop( TInt aChannel ) 
	{ 
	iShared.iMutex.Wait(); 
	iShared.iPlayStarted[ aChannel ] = ETrue; 
	iShared.iSample[ aChannel ] = iEmptySample; 
	iShared.iMutex.Signal();	 
	} 
 
 
void CSndMixer::SetVolume( TInt aVolume ) 
	{ 
	iShared.iMutex.Wait(); 
	iShared.iMainVolume = aVolume; 
	iShared.iMutex.Signal();	 
	} 
 
 
TInt CSndMixer::Volume() 
	{ 
	return iShared.iMainVolume; 
	} 
 
 
void CSndMixer::SendCmd( TMixerCmd aCmd ) 
	{ 
	iShared.iMutex.Wait(); 
	iShared.iCmd = aCmd; 
	iShared.iMutex.Signal(); 
	iMixerThread.RaiseException( EExcUserInterrupt ); 
	}