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


   /* 
    * 
============================================================================ 
    *  Name     : SoundMixerContainer.cpp 
    *  Part of  : SoundMixer 
    *  Created  : 03/01/2003 by Forum Nokia 
    *  Description: 
    *     This is the project specification file for SoundMixer. 
    * 
    *  Version  : 1.0.0 
    *  Copyright: Forum Nokia 
    * 
============================================================================ 
    */ 
 
 
#include "SoundMixerContainer.h" 
#include  
#include "CSndMixer.h" 
#include "CWavLoader.h" 
 
void CSoundMixerContainer::ConstructL(const TRect& aRect) 
    { 
 
	// load sound samples 
	CWavLoader* wavLoader = CWavLoader::NewLC(); 
 
	iMusicSample = wavLoader->LoadL(_L("music2.wav") ); 
	iEffectSample = wavLoader->LoadL( _L("effect.wav") ); 
	iEffectSample2 = wavLoader->LoadL( _L("effect2.wav") ); 
 
	CleanupStack::PopAndDestroy( wavLoader ); 
 
	// create sound mixer 
	iSndMixer = CSndMixer::NewL(); 
 
	// make iMusicSample repeat itself 
	iMusicSample.iRepEnd = iMusicSample.iLength; 
 
	// and play music on channel 0 at 16KHz with volume 256 ( max ) 
	iSndMixer->Play( iMusicSample, 0, 16000, 256 ); 
 
    CreateWindowL(); 
 
	// create periodic timer to move blocks 
    iTimer = CPeriodic::NewL( EPriorityNormal ); 
 
	// initial positions and speeds for blocks 
	TInt i; 
	for( i=0; i<8; i++ ) 
		{ 
		iSpeed[ i ] = TPoint( ( i & 3 ) -1, ( ( i+2 ) & 3 ) -1 ); 
		iPosition[ i ] = TPoint( 10 + i * 10, 100 ); 
		} 
 
    SetRect( aRect ); 
    ActivateL(); 
 
	// make this class observe changes is foreground events 
	// to stop blocks and sound when application is inactive 
	CEikonEnv::Static()->AddForegroundObserverL( *this ); 
    } 
 
CSoundMixerContainer::~CSoundMixerContainer() 
    { 
	iTimer->Cancel(); 
	delete iTimer; 
 
	delete iSndMixer; 
	delete iMusicSample.iData; 
	delete iEffectSample.iData; 
	delete iEffectSample2.iData; 
    } 
 
TKeyResponse CSoundMixerContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType ) 
	{ 
	// here's the place for getting key events 
 
	// set volume control to joystick's left / right 
	if( aType == EEventKeyDown ) 
		{ 
		if( aKeyEvent.iScanCode == EStdKeyLeftArrow ) 
			{ 
			VolumeDown(); 
			} 
		if( aKeyEvent.iScanCode == EStdKeyRightArrow ) 
			{ 
			VolumeUp(); 
			} 
		} 
	return EKeyWasConsumed; 
	} 
 
 
TInt CSoundMixerContainer::TimerCallBack( TAny* aPtr ) 
	{ 
	return ((CSoundMixerContainer*)aPtr)->DoTimer(); 
	} 
 
TInt CSoundMixerContainer::DoTimer() 
	{ 
	// here's everything moving and sounding 
 
	// get gc to draw with 
	CWindowGc& gc = SystemGc(); 
 
	gc.Activate( Window() ); 
	gc.Clear(); 
	gc.SetPenStyle(CGraphicsContext::ENullPen); 
    gc.SetBrushColor( TRgb( 255,0,0 ) ); 
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush ); 
 
 
	for( TInt i=0; i<8; i++ ) 
		{ 
		TBool edgeCollide = EFalse;	// flag for boxes colliding against walls 
		TBool boxCollide = EFalse;	// flag for boxes colliding each other 
 
		TPoint& pos = iPosition[ i ]; 
		TPoint& spd = iSpeed[ i ]; 
 
		// move blocks 
		pos += spd; 
 
		TRect rect = Rect(); 
 
		// check box collide against walls 
		if( ( pos.iX+8 >= rect.iBr.iX ) || ( pos.iX <= rect.iTl.iX ) ) 
			{ 
			spd.iX = -spd.iX; 
			pos += spd; 
			edgeCollide = ETrue; 
			} 
 
		if( ( pos.iY+8 >= rect.iBr.iY ) || ( pos.iY <= rect.iTl.iY ) ) 
			{ 
			spd.iY = -spd.iY; 
			pos += spd; 
			edgeCollide = ETrue; 
			} 
 
 
		// check box collide against each other 
		TRect boxRect( pos, TSize( 8,8 ) ); 
		for( TInt j=0; j<8; j++ ) 
			{ 
			TRect boxRect2( iPosition[ j ], TSize( 8,8 ) ); 
			if( ( i != j ) && ( boxRect.Intersects( boxRect2 ) ) ) 
				{ 
				spd.iY = -spd.iY; 
				spd.iX = -spd.iX; 
				boxCollide = ETrue; 
				} 
			} 
 
		// play sounds if collide 
		if( edgeCollide ) 
			{ 
			iSndMixer->Play( iEffectSample, 1 + i, 8000 + i * 1000, 32 ); 
			} 
 
		if( boxCollide ) 
			{ 
			iSndMixer->Play( iEffectSample2, 1 + i, 8000 + i * 1000, 32 ); 
			} 
 
 
		// draw current box 
		gc.DrawRect( boxRect ); 
 
		} 
 
	gc.Deactivate(); 
	return ETrue; 
	} 
 
 
void CSoundMixerContainer::HandleGainingForeground() 
	{ 
	// application gets focused 
	// this function is called first time when application starts 
	// that's why there's no need to start timer and sound in constructor 
 
	iTimer->Start( 10000, 10000, TCallBack( TimerCallBack, this ) ); 
	iSndMixer->Resume(); 
	} 
 
void CSoundMixerContainer::HandleLosingForeground() 
	{ 
	// application loses focus 
	// so stop moving blocks and playing sound 
 
	iTimer->Cancel(); 
	iSndMixer->Pause(); 
	} 
 
 
void CSoundMixerContainer::StartMixer() 
	{ 
	iSndMixer->Resume(); 
	} 
 
void CSoundMixerContainer::StopMixer() 
	{ 
	iSndMixer->Pause(); 
	} 
 
void CSoundMixerContainer::VolumeUp() 
	{ 
	TInt volume = iSndMixer->Volume(); 
	volume += 20; 
	if( volume > 255 ) volume = 255; 
	iSndMixer->SetVolume( volume ); 
	} 
 
void CSoundMixerContainer::VolumeDown() 
	{ 
	TInt volume = iSndMixer->Volume(); 
	volume -= 20; 
	if( volume < 0 ) volume = 0; 
	iSndMixer->SetVolume( volume ); 
	}