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: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fCS001029E5fE2dE5fAudioE5fInputE5fE52outingE5fAPIX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxCS001029E20E2dE20AudioE20InputE20E52outingE20APIE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZaudioQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZmultimediaQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxaudioX qfnZuserE5ftagQSxepcm16X qfnZuserE5ftagQSxmultimediaX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx4E2E0000X