2013-03-13 66 views
1

我基本上试图将音频流的内容读入char *缓冲区以应用FFT。 我正在使用SAPI来简化应用我的FFT后需要做的转换。从一个IStream读取到一个缓冲区(音频)

我有我的HGLOBAL分配

// Address of IStream* pointer variable that receives the interface pointer to the new stream object. 
CComPtr<IStream> pMemStream; 
// A memory handle allocated by the GlobalAlloc function, or if NULL a new handle is to be allocated instead. 
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(pMemStream)); 
// Create stream object that uses an HGLOBAL memory handle to store the stream contents. 
hr = ::CreateStreamOnHGlobal(hGlobal, TRUE, &pMemStream); 

然后我写WAV内容此流

// Variable to receive ISpStream pointer 
CComPtr<ISpStream> cpSpStream; 
hr = cpSpStream.CoCreateInstance(CLSID_SpStream); 
hr = cpSpStream->SetBaseStream(pMemStream, SPDFID_WaveFormatEx, defaultStreamFormat.WaveFormatExPtr()); 
hr = cpVoice->SetOutput(cpSpStream, TRUE); 
hr = cpVoice->Speak(L"This is a phrase.", SPF_DEFAULT, NULL); 

我可以确认的是,流通过说出流包含WAV内容的IStream的缓冲区:

// Speak the modified stream. 
cpVoice->SetOutput(NULL, FALSE); 
cpVoice->SpeakStream(cpSpStream, SPF_DEFAULT, NULL); 

但是,当我尝试获取流的缓冲区时,我只读ga垃圾数据(全部'='在缓冲区中)。

IStream *pIstream; 
cpSpStream->GetBaseStream(&pIstream); 
STATSTG stats; 
pIstream->Stat(&stats, STATFLAG_NONAME); 
ULONG sSize = stats.cbSize.QuadPart; 
ULONG bytesRead; 
char *pBuffer = new char[sSize]; 
pIstream->Read(pBuffer, sSize, &bytesRead); 

我在做什么错?这让我疯狂。 谢谢, -Francisco

回答

2

SAPI写入流后,流位置在最后,所以IStream :: Read将其“读取”输出设置为零字节。

从流中读取之前,调用IStream :: Seek(零,STREAM_SEEK_SET,NULL),您将能够读取数据。