【问题标题】:Reading from an IStream to a Buffer (Audio)从 IStream 读取到缓冲区(音频)
【发布时间】:2013-03-13 03:52:13
【问题描述】:

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

我做错了什么?这让我发疯。 谢谢, -弗朗西斯科

【问题讨论】:

    标签: c++ audio buffer sapi istream


    【解决方案1】:

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

    在从流中读取之前,调用 IStream::Seek(zero, STREAM_SEEK_SET, NULL) 就可以读取数据了。

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 2011-10-31
      • 2014-11-14
      • 1970-01-01
      • 2011-11-02
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多