// BASPYRC.H
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

// Started by JL, July 1996
// Sound player and recorder
//
#if !defined(__SDPYRC_H__)
#define __SDPYRC_H__

#if !defined(__BASNDDA_H__)
#include <basndda.h>
#endif

#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif

#if !defined(__S32FILE_H__)
#include <s32file.h>
#endif

//
// class CSoundSampler
//

class RDevSound;

class MSoundSamplerObserver
	{
public:
	IMPORT_C virtual void PlayComplete(TInt aStatus);
	IMPORT_C virtual void RecordComplete(TInt aStatus);
	IMPORT_C virtual void SeekComplete(TInt aStatus);
	};

class CSoundSampler : public CTimer
    {
public:
	IMPORT_C static CSoundSampler* NewL(MSoundSamplerObserver& anObserver);
	IMPORT_C static CSoundSampler* NewL(MSoundSamplerObserver& anObserver,const CStreamStore& aStore,TStreamId aStreamId);
	IMPORT_C static CSoundSampler* NewL(MSoundSamplerObserver& anObserver,RFile& aFile,TInt aStartPos);
	IMPORT_C ~CSoundSampler();
//
	inline TBool IsIdle() const;
//
	IMPORT_C void UseSeekIndexL();
	IMPORT_C TTimeIntervalMicroSeconds Tell() const;
	IMPORT_C void Beginning();
	IMPORT_C void End();
	IMPORT_C void Seek(const TTimeIntervalMicroSeconds& aTime);
	IMPORT_C void TruncateL();
	IMPORT_C void PlayL(RDevSound& aDev);
	IMPORT_C void RecordL(RDevSound& aDev);
	inline void Stop();
//
	IMPORT_C TUid Compression() const;
	IMPORT_C TInt Repeat() const;
	IMPORT_C TInt Volume() const;
	IMPORT_C TTimeIntervalMicroSeconds32 TrailingSilence() const;
	IMPORT_C TTimeIntervalMicroSeconds SamplePlayLength() const;
	IMPORT_C TTimeIntervalMicroSeconds TotalPlayLength() const;
//
	IMPORT_C void SetCompressionL(TUid aCompression);
	IMPORT_C void SetRepeat(TInt aRepeat);
	IMPORT_C void SetVolume(TInt aVolume);
	IMPORT_C void SetTrailingSilence(TTimeIntervalMicroSeconds32 aMicroSecond);
	IMPORT_C void CommitHeaderL() const;
//
	IMPORT_C void DetachL() const;
	IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
	IMPORT_C void SaveL(RFile& aFile,TBool aUseNewFile=EFalse);
private:
	enum TState {EIdle,EIndex,ESeek,ERecord,EPlay,ESilence,ERepeat};
private:
    IMPORT_C CSoundSampler(MSoundSamplerObserver& anObserver,CSoundData* aData);
	IMPORT_C void RunL();
	IMPORT_C void DoCancel();
	static CSoundSampler* NewL(MSoundSamplerObserver& aObserver,CSoundData* aData);
//
	void DoRunL();
	void Complete(TInt aStatus);
	void SeekNowL(TInt aPos);
	void StartPlayL(TInt aPos);
	void DoPlayL();
	void Queue(TState aState);
	void StopPlaying(TInt aError);
	void StopRecording(TInt aError);
	void StartIndexing();
	void StopIndexing();
	void CheckIdle() const;
//
	inline TBool IsPlaying() const;
	inline CSoundData& Data() const;
	inline RDevSound& SoundDevice() const;
private:
	TState iState;
	RDevSound* iDev;
	CSoundData* iData;
    TInt iPos;
	TInt iRepeat;
	MSoundSamplerObserver* iObserver;
	TBuf8<KSoundDataChunkLength> iBuf;
	};

inline TBool CSoundSampler::IsIdle() const
	{return iState<=EIndex;}
inline void CSoundSampler::Stop()
	{Cancel();}

class MSoundPlayerObserver
	{
public:
	IMPORT_C virtual void PlayComplete(TInt aStatus);
	};

enum TSoundType {ESoundAlgorithmic,ESoundSample};

class CSoundAlarmPlayer;
class CSoundPlayer : public CBase, public MSoundSamplerObserver
    {
public:
	IMPORT_C static CSoundPlayer* NewL(MSoundPlayerObserver& anObserver,RDevSound& aDev);
	IMPORT_C ~CSoundPlayer();
//
	IMPORT_C void PlaySoundL(const TDesC& aName,RFs& aFs);
	IMPORT_C void PlayFileL(const TDesC& aName,RFs& aFs);
	IMPORT_C void PlayAlarm(const TDesC& aName);
	IMPORT_C void PlayAlarm(TInt aAlarmIndex);
	inline void PlayFallBackAlarm();
	IMPORT_C void PlayTone(TInt aFrequency,TTimeIntervalMicroSeconds32 aDuration,TInt aAmplitude);
	IMPORT_C void Stop();
	IMPORT_C TBool IsPlaying() const;
	inline TSoundType SoundType() const;
private:
    IMPORT_C CSoundPlayer(MSoundPlayerObserver& anObserver);
	IMPORT_C void PlayComplete(TInt aStatus);
	void DeletePlayer();
private:
	CSoundAlarmPlayer* iAlarm;
	CSoundSampler* iPlayer;
	CStreamStore* iStore;
	TSoundType iType;
	MSoundPlayerObserver* iObserver;
//
	friend CSoundAlarmPlayer;
	};

inline void CSoundPlayer::PlayFallBackAlarm()
	{PlayAlarm(-1);}
inline TSoundType CSoundPlayer::SoundType() const
	{return iType;}

#endif

