×
Namespaces

Variants
Actions

Tone player helper class for Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This code example provides a Symbian C++ helper class for playing simple sounds (or tone sequences).

Article Metadata
Article
Keywords: CMdaAudioToneUtility
Created: lbuczkow (05 Jun 2007)
Last edited: hamishwillee (30 May 2013)

Using the CSoundEngine

iSoundEngine = CSoundEngine::NewL();
 
// ...
 
TTone tone;
 
iSoundEngine->Reset();
tone.duration = 100;
tone.pitch = 440; // frequency in Hz
iSoundEngine->Append(tone);
tone.pitch = 660; // frequency in Hz
iSoundEngine->Append(tone);
tone.pitch = 880; // frequency in Hz
iSoundEngine->Append(tone);
iSoundEngine->Play();

Header files

///////////////////////////////
// SoundEngine.h
///////////////////////////////
 
/* Leszek Buczkowski, Ingraal 2006 */
 
#ifndef __SOUND_ENGINE_H__
#define __SOUND_ENGINE_H__
 
#include <e32base.h>
#include <MdaAudioTonePlayer.h>
#include "Delay.h"
 
class TTone
{
public:
TInt pitch;
TInt duration;
};
 
class CSoundEngine : public MMdaAudioToneObserver, MDelayObserver
{
public:
static CSoundEngine* NewL();
virtual ~CSoundEngine();
 
void Reset();
void Append(TTone& aTone);
void Play();
void Stop();
 
private:
CSoundEngine();
void ConstructL();
 
void MatoPrepareComplete(TInt aError);
void MatoPlayComplete(TInt aError);
void DelayDoneL();
 
 
private:
CMdaAudioToneUtility* iMdaAudioToneUtility;
RArray<TTone> iTones;
TInt iIndex;
CDelay* iDelay;
};
 
#endif


///////////////////////////////
// Delay.h
///////////////////////////////
 
/* Leszek Buczkowski, Ingraal 2006 */
 
#ifndef __DELAY_H__
#define __DELAY_H__
 
#include <e32base.h>
 
class MDelayObserver
{
public:
virtual void DelayDoneL() = 0;
};
 
class CDelay : public CActive
{
public:
static CDelay* NewL(MDelayObserver &aObserver);
~CDelay();
 
void Delay(TTimeIntervalMicroSeconds32 aPeriod);
 
private:
CDelay(MDelayObserver &aObserver);
void ConstructL();
void RunL();
void DoCancel();
 
private:
RTimer iTimer;
TTimeIntervalMicroSeconds32 iPeriod;
MDelayObserver &iObserver;
};
 
#endif

Source code

///////////////////////////////
// SoundEngine.cpp
///////////////////////////////
 
/* Leszek Buczkowski, Ingraal 2006 */
 
#include "SoundEngine.h"
 
CSoundEngine* CSoundEngine::NewL()
{
CSoundEngine* self=new(ELeave) CSoundEngine();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
CSoundEngine::CSoundEngine() : iIndex(0)
{
}
 
void CSoundEngine::ConstructL()
{
iMdaAudioToneUtility = CMdaAudioToneUtility::NewL(*this);
iDelay = CDelay::NewL(*this);
}
 
CSoundEngine::~CSoundEngine()
{
delete iDelay;
delete iMdaAudioToneUtility;
}
 
void CSoundEngine::MatoPrepareComplete(TInt /*aError*/)
{
iMdaAudioToneUtility->SetVolume(iMdaAudioToneUtility->MaxVolume());
}
 
void CSoundEngine::MatoPlayComplete(TInt /*aError*/)
{
if (iIndex < iTones.Count())
{
 
if (iTones[iIndex].pitch > 0)
{
TTimeIntervalMicroSeconds interval(1000 * iTones[iIndex].duration);
iMdaAudioToneUtility->PrepareToPlayTone(iTones[iIndex].pitch, interval);
iIndex++;
iMdaAudioToneUtility->Play();
}
else
{
iDelay->Delay(1000 * iTones[iIndex].duration);
iIndex++;
}
}
}
 
void CSoundEngine::DelayDoneL()
{
MatoPlayComplete(0);
}
 
void CSoundEngine::Reset()
{
Stop();
iTones.Reset();
}
 
void CSoundEngine::Append(TTone& aTone)
{
iTones.Append(aTone);
}
 
void CSoundEngine::Play()
{
Stop();
MatoPlayComplete(0);
}
 
void CSoundEngine::Stop()
{
iDelay->Cancel();
iMdaAudioToneUtility->CancelPlay();
iIndex = 0;
}
///////////////////////////////
// Delay.cpp
///////////////////////////////
 
/* Leszek Buczkowski, Ingraal 2006 */
 
#include "Delay.h"
 
CDelay* CDelay::NewL(MDelayObserver &aObserver)
{
CDelay* self=new(ELeave) CDelay(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
CDelay::CDelay(MDelayObserver &aObserver): CActive(0), iPeriod(0), iObserver(aObserver)
{
CActiveScheduler::Add(this);
}
 
void CDelay::ConstructL()
{
User::LeaveIfError(iTimer.CreateLocal());
}
 
CDelay::~CDelay()
{
Cancel();
iTimer.Close();
}
 
void CDelay::Delay(TTimeIntervalMicroSeconds32 aPeriod)
{
iTimer.After(iStatus, aPeriod);
SetActive();
 
 
void CDelay::RunL()
{
iObserver.DelayDoneL();
}
 
void CDelay::DoCancel()
{
iTimer.Cancel();
}
This page was last modified on 30 May 2013, at 07:36.
63 page views in the last 30 days.
×