Note
Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.
Microsoft Speech Platform
ISpGrammarCompiler::CompileStream
ISpGrammarCompiler::CompileStream loads an XML-format grammar file and converts it to a grammar in the Speech Platform's binary format. It compiles the grammar file pointed to by pSource stream and writes the output to the pDest stream.
<pre IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml"> <strong>HRESULT CompileStream(</strong> <strong> IStream</strong> *<em>pSource</em>, <strong> IStream</strong> *<em>pDest</em>, <strong> IStream</strong> *<em>pHeader</em>, <strong> IUnknown</strong> *<em>pReserved</em>, <strong>ISpErrorLog</strong> *<em>pErrorLog</em>, <strong> DWORD</strong> <em>dwFlags</em> <strong>);</strong> </pre>
Parameters
- pSource
Pointer to the source of the XML grammar file. - pDest
Pointer to the destination stream for the binary grammar. - pHeader
Pointer to the stream to write the C/C++ header information. - pReserved
Reserved. Do not use. - pErrorLog
Pointer to the error log receiving the messages. - dwFlags
[in] Not currently used. Must be zero.
Return Values
Value | Description |
---|---|
S_OK | Function completed successfully. |
E_INVALIDARG | One of the parameters is bad or invalid. |
FAILED (hr) | Appropriate error message. |
Remarks
Applications that do not need to modify a grammar at run time, or that want to increase speech recognition performance of their application should load the compiled binary form statically (not dynamically). If loading the grammar compiler at application runtime, note that the Speech Platform must allow for modification and validation of complicated state/transition graphs.
Example
The following example uses CompileStream to convert an XML grammar (sample.xml) to a binary grammar (sample.cfg) for consumption by the Speech Platform.
CComPtr<CSpFileStream> cpInStream;
if (SUCCEEDED(hr))
{
hr = OpenFile(pszInFileName, GENERIC_READ, OPEN_EXISTING, L"sample.xml", &cpInStream;);
}
if(SUCCEEDED(hr))
{
if (SUCCEEDED(hr))
{
CComPtr<ISpGrammarCompiler> cpCompiler2;
hr = cpCompiler2.CoCreateInstance(CLSID_SpW3CGrammarCompiler);
if (SUCCEEDED(hr))
{
cpOutStream.Release();
hr = OpenFile(pszOutFileName, GENERIC_WRITE, CREATE_ALWAYS, NULL, &cpOutStream;);
if (SUCCEEDED(hr))
{
CError errorlog(pszInFileName, L"W3C XML");
hr = cpCompiler2->CompileStream(cpInStream, cpOutStream, NULL, NULL, &errorlog;, 0);
if (SUCCEEDED(hr))
{
fwprintf(stderr, L"[W3C XML] Compilation successful!\n");
}
else
{
fwprintf(stderr, L"[W3C XML] Compilation Failed!\n");
}
}
}
else
{
fwprintf(stderr, L"[W3C XML] Error creating CLSID_SpW3CGrammarCompiler\n");
}
}
}
}