Pregunta

I am basically trying to read the contents of an audio stream into a char * buffer to apply an FFT. I am using SAPI to simplify the transformations I will need to make after applying my FFT.

I have an IStream buffer I allocated on HGLOBAL

// 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

// 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:

// 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

¿Fue útil?

Solución

After SAPI writes the stream, the stream position is at the end, so IStream::Read will set its "read" output to zero bytes.

Before reading from the stream, call IStream::Seek(zero, STREAM_SEEK_SET, NULL) and you will be able to read the data.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top