繁体   English   中英

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

[英]Reading from an IStream to a Buffer (Audio)

我基本上是试图将音频流的内容读入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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM