简体   繁体   English

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

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

I am basically trying to read the contents of an audio stream into a char * buffer to apply an FFT. 我基本上是试图将音频流的内容读入char *缓冲区以应用FFT。 I am using SAPI to simplify the transformations I will need to make after applying my FFT. 我正在使用SAPI简化应用FFT后需要进行的转换。

I have an IStream buffer I allocated on HGLOBAL 我在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);

I then wrote wav contents to this stream 然后我将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);

I can verify that the stream contains the wav contents by speaking the stream: 我可以通过说出流来验证流中是否包含wav内容:

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

However, when I try and get the buffer for the stream, I read only garbage data (all '=' in buffer). 但是,当我尝试获取流的缓冲区时,我仅读取垃圾数据(缓冲区中的所有“ =”)。

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);

What am I doing wrong? 我究竟做错了什么? This is driving me nuts. 这让我发疯。 Thanks, -Francisco 谢谢,-Francisco

After SAPI writes the stream, the stream position is at the end, so IStream::Read will set its "read" output to zero bytes. 在SAPI写入流之后,流位置在末尾,因此IStream :: Read将其“读取”输出设置为零字节。

Before reading from the stream, call IStream::Seek(zero, STREAM_SEEK_SET, NULL) and you will be able to read the data. 从流中读取之前,请调用IStream :: Seek(zero,STREAM_SEEK_SET,NULL),您将能够读取数据。

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

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