gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:20:15 25 4
gpt4 key购买 nike

我基本上是在尝试将音频流的内容读入 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 会将其“读取”输出设置为零字节。

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

关于c++ - 从 IStream 读取到缓冲区(音频),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com