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

Reading from an IStream to a Buffer (Audio)

本文关键字:音频 缓冲区 IStream 读取      更新时间:2023-10-16

我基本上试图将音频流的内容读取到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);

我做错了什么?我快疯了。谢谢,旧金山

SAPI写入流后,流的位置在末尾,因此IStream::Read将其" Read "输出设置为零字节。

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