You Are Here:

Community: Wiki

This page was last modified on 29 September 2009, at 13:15.

Recording audio with stream

From Forum Nokia Wiki

Reviewer Approved   

CStreamRecorder illustrates how to record audio PCM data into streams, using CMdaAudioInputStream. With this example audio is recorded as PCM data into byte buffer, if you want to use other encodings, you need to convert the PCM data into the desired format.

CMdaAudioInputStream requires the recorder class to implement MMdaAudioInputStreamCallback callback interface, from which the MaiscOpenComplete is called when the initialization for the input stream is completed, recording should never be started until this function has been called by the input stream.

As shown in the example implementation for the MaiscOpenComplete, the recording is started by calling ReadL-function of the input stream, this will record a small while which after MaiscBufferCopied is called with the recorded buffer, which in the example is then appended to the sound data and new sample recording is started by calling the ReadL-function again.


Stream_Record.cpp

#include <mda\common\audio.h>
#include <MdaAudioInputStream.h> // audio input stream
#include <MdaAudioOutputStream.h> // audio output stream
#include <BAUTILS.H>
#include "Stream_Record.h"
 
const TInt KStreamBufferSize = 320;
const TInt KStreamBufferCount = 2;
 
const TInt KMaxFileSize = 500000;
 
CStreamRecorder* CStreamRecorder::NewL()
{
CStreamRecorder* self = CStreamRecorder::NewLC();
CleanupStack::Pop(self);
return self;
}
 
CStreamRecorder* CStreamRecorder::NewLC()
{
CStreamRecorder* self = new (ELeave) CStreamRecorder();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
 
CStreamRecorder::CStreamRecorder()
:iInputStream(NULL),iStreamIdx(0)
{
}
 
CStreamRecorder::~CStreamRecorder()
{
if(iInputStream)
{
iInputStream->Stop();
delete iInputStream;
}
 
if(iSoundData)
{
delete iSoundData;
}
 
iStreamBuffer.ResetAndDestroy();
}
 
void CStreamRecorder::ConstructL()
{
iReady = EFalse;
iInputStream = CMdaAudioInputStream::NewL(*this);
 
TDes8* buffer;
for (TInt idx=0; idx<KStreamBufferCount; idx++)
{
buffer = new(ELeave) TBuf8<KStreamBufferSize>;
CleanupStack::PushL(buffer);
buffer->SetMax();
buffer->Zero();
 
User::LeaveIfError(iStreamBuffer.Append(buffer));
CleanupStack::Pop(buffer);
}
}
 
 
HBufC8* CStreamRecorder::GetAudioBuffer(void)
{
HBufC8* Tmp = iSoundData;
 
Stop();
 
iSoundData = NULL;
iReady = EFalse;
 
return Tmp;
}
 
void CStreamRecorder::Record()
{
iReady = EFalse;
iPlayError = KErrNone;
 
if(iSoundData)
{
delete iSoundData;
iSoundData = NULL;
}
 
iSoundData = HBufC8::NewL(KMaxFileSize);
 
if(iInputStream)
{
iInputStream->Stop();
delete iInputStream;
iInputStream = NULL;
}
 
iInputStream = CMdaAudioInputStream::NewL(*this);
iInputStream->Open(&iStreamSettings);
}
 
void CStreamRecorder::Stop()
{
iReady = EFalse;
 
if(iInputStream)
iInputStream->Stop();
 
// we can't delete iInputStream yet!
// it will get cleaned up on destruction,
// or when we start recording something new.
//delete iInputStream;
//iInputStream = NULL;
}
 
void CStreamRecorder::MaiscOpenComplete(TInt aError)
{
iPlayError = aError;
if (aError==KErrNone && iInputStream)
{
iReady = ETrue;
 
iStreamSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate8000Hz;
iStreamSettings.iChannels = TMdaAudioDataSettings::EChannelsMono;
 
iInputStream->SetAudioPropertiesL(iStreamSettings.iSampleRate,iStreamSettings.iChannels);
iInputStream->SetGain(iInputStream->MaxGain());
iInputStream->SetPriority(EPriorityNormal, EMdaPriorityPreferenceTime);
iStreamIdx=0;
iInputStream->ReadL(*iStreamBuffer[iStreamIdx]);
}
}
 
 
void CStreamRecorder::MaiscBufferCopied(TInt aError, const TDesC8& aBuffer)
{
iPlayError = aError;
if (aError==KErrNone && iInputStream)
{
if (&aBuffer==iStreamBuffer[iStreamBuffer.Count()-1])
iStreamIdx=0;
else
iStreamIdx++;
 
iInputStream->ReadL(*iStreamBuffer[iStreamIdx]);
if(aBuffer.Length())
{
TInt ll = aBuffer.Length() + iSoundData->Des().Length();
 
if(ll < KMaxFileSize)
{
iSoundData->Des().Append(aBuffer);
}
else
{
Stop();// avoid buffer over run
}
}
}
}
 
 
void CStreamRecorder::MaiscRecordComplete(TInt aError)
{
iPlayError = aError;
}

Stream_Record.h

#include <mda\common\audio.h>
#include <Mda\Client\Utility.h>
#include <Mda\Common\Resource.h>
#include <MdaAudioOutputStream.h>
#include <MdaAudioInputStream.h>
#include <mmf\server\MmfCodec.h>
#include <mmf\server\mmfdatabuffer.h>
 
 
 
class CStreamRecorder : public CBase, public MMdaAudioInputStreamCallback
{
public:
static CStreamRecorder* NewL();
static CStreamRecorder* NewLC();
~CStreamRecorder();
public:
void Record();
void Stop();
HBufC8* GetAudioBuffer(void);
TBool StreamReady(){ return iReady;};
private:
virtual void MaiscOpenComplete(TInt aError);
virtual void MaiscBufferCopied(TInt aError, const TDesC8& aBuffer);
virtual void MaiscRecordComplete(TInt aError);
private:
void ConstructL();
CStreamRecorder();
private:
CMdaAudioInputStream* iInputStream;
RPointerArray<TDes8> iStreamBuffer;
TMdaAudioDataSettings iStreamSettings;;
TInt iStreamIdx,iPlayError;
HBufC8* iSoundData;
TBool iReady;
};

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fE25E458E2587E25AAE25E455E25AE45E259AE25E454E25B9E2589CMdaAudioInputStreamE25E457E259AE2584bufferE25E455E25A4E25A7E25E455E25B0E258FX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZaudioQ qfnZtopicQUqfnTopicZmultimediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx4E2E0000X