Creating a Voice Menu

When you create a voice menu, you initialize your application grammar to let the speech recognition engine know what words your application handles for each device context.

The code examples used in this section are part of a complete sample application, SRapp.dsp, included in the SDK. SRapp.dsp demonstrates how to use speech recognition to input user commands into an application. It contains the C++ file App.cpp, which contains the code for setting up a voice menu for an application, and Appsink.cpp, which programs a message sink. Like all Auto PC applications, SRapp.dsp creates a forms manager and forms using GetFormsManager and CreateForms.

    To create a voice menu

  1. Call CreateVMenu with your application name and the menu name.
  2. Add words to your applications’s menu in one of the following ways:
  3. Call put_VoiceMenu to associate the voice menu with a form in your application.
  4. Be sure to handle the WM_SPCH_RECOG message in the HandleMessage attached to your form or WM_COMMAND messages in the form’s event sink (IASEventSink::ReceiveMsg).

If you want to access other interfaces on the voice command object, call IAPCSpeech::QuerySpeechInterface to get the IVoiceCmd, IVCmdAttributes, and IVCmdUserWord interfaces.

The following code example shows how to create a voice menu from a resouce file using CreateColorsVoiceMenu.

HRESULT
CSRApp::CreateColorsVoiceMenu()
{
    HRESULT hr;

    if(!IsColorWordsTrained()) return E_FAIL;

    VCMDNAME vnColors;
    memset(&vnColors, NULL, sizeof(VCMDNAME));
    lstrcpy(vnColors.szApplication, L"SR App");
    lstrcpy(vnColors.szState, L"SR App - Colors");

// Creates the color menu
    hr = m_pSpeech->CreateVMenu(
        NULL,
        &vnColors,
        m_hInst,
        0,
        NULL,
        APCSPCH_VM_USEEXISTING,
        &m_pVMColorsForm);
    if(FAILED(hr))
        return E_FAIL;

// Adds names of colors to the menu from a resource file
    hr = m_pSpeech->AddVMenuCommandsFromResource(
        m_pVMColorsForm,
        m_hInst,
        MAKEINTRESOURCE(IDVM_COLORS),
        0,
        APCSPCH_VM_USEEXISTING);
    if(FAILED(hr))
        return E_FAIL;

    hr = m_pSpeech->AddVMenuCommandsFromResource(
        m_pVMColorsForm,
        m_hInst,
        MAKEINTRESOURCE(IDVM_COLORS_END),
        0,
        APCSPCH_VM_USEEXISTING);
    if(FAILED(hr))
        return E_FAIL;

The following code example shows how to associate a voice menu with a form.

    hr = m_pColorsForm->put_VoiceMenu(m_pVMColorsForm);
    if(FAILED(hr))
        return E_FAIL;

    return NOERROR;

The following code example shows the message handler for SRapp.dsp, contained in Appsink.cpp.

STDMETHODIMP CAppMessageSink::HandleMessage(IDispatch* pdispControl,
LONG uMsg, LONG wParam, LONG lParam)
{
    switch (uMsg)
    {
    default:
        break;

    case WM_SPCH_RECOG: 
        switch (lParam)
        {
        default:
            break;

        case IDVMI_BLACK:
            g_pApp->m_pColorsForm->put_BackColor(BLACK);
            break;
        case IDVMI_RED:
            g_pApp->m_pColorsForm->put_BackColor(RED);
            break;
        case IDVMI_GREEN:
            g_pApp->m_pColorsForm->put_BackColor(GREEN);
            break;
...
        case IDVMI_WHITE:
            g_pApp->m_pColorsForm->put_BackColor(WHITE);
            break;

        case IDVMI_ZERO:
        case IDVMI_ONE:
...
        case IDVMI_NINE:
            g_pApp->AddNumber(lParam - IDVMI_NUMBERS_BASE);
            break;

        case IDVMI_NUMBERS_END:
            g_pApp->m_pActiveForms->SetFocus(g_pApp->m_pMainForm);
            break;

        case IDVMI_COLORS_END:
            g_pApp->m_pActiveForms->SetFocus(g_pApp->m_pMainForm);
            break;

        }
        break;
    }

    return S_FALSE;
}