从IStream读取到缓冲区(音频)

时间:2013-03-13 03:52:13

标签: c++ audio buffer sapi istream

我基本上试图将音频流的内容读入char *缓冲区以应用FFT。 我正在使用SAPI来简化应用FFT后需要进行的转换。

我有一个在HGLOBAL上分配的IStream缓冲区

// 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内容:

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

然而,当我尝试获取流的缓冲区时,我只读取垃圾数据(缓冲区中的所有'=')。

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

1 个答案:

答案 0 :(得分:2)

在SAPI写入流后,流位置在末尾,因此IStream :: Read会将其“读取”输出设置为零字节。

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