Add examples for Visual C++ MFC (#208)

This commit is contained in:
Fangjun Kuang
2023-07-12 13:37:28 +08:00
committed by GitHub
parent 5cd72ba3aa
commit c818b82e13
18 changed files with 1487 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by StreamingSpeechRecognition.rc
//
#define IDD_STREAMINGSPEECHRECOGNITION_DIALOG 102
#define IDR_MAINFRAME 128
#define IDC_EDIT1 1000
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 130
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

View File

@@ -0,0 +1,85 @@
// StreamingSpeechRecognition.cpp : Defines the class behaviors for the
// application.
//
#include "StreamingSpeechRecognition.h"
#include "StreamingSpeechRecognitionDlg.h"
#include "framework.h"
#include "pch.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CStreamingSpeechRecognitionApp
BEGIN_MESSAGE_MAP(CStreamingSpeechRecognitionApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CStreamingSpeechRecognitionApp construction
CStreamingSpeechRecognitionApp::CStreamingSpeechRecognitionApp() {
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CStreamingSpeechRecognitionApp object
CStreamingSpeechRecognitionApp theApp;
// CStreamingSpeechRecognitionApp initialization
BOOL CStreamingSpeechRecognitionApp::InitInstance() {
CWinApp::InitInstance();
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Activate "Windows Native" visual manager for enabling themes in MFC
// controls
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CStreamingSpeechRecognitionDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK) {
// TODO: Place code here to handle when the dialog is
// dismissed with OK
} else if (nResponse == IDCANCEL) {
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} else if (nResponse == -1) {
TRACE(traceAppMsg, 0,
"Warning: dialog creation failed, so application is terminating "
"unexpectedly.\n");
TRACE(traceAppMsg, 0,
"Warning: if you are using MFC controls on the dialog, you cannot "
"#define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
}
// Delete the shell manager created above.
if (pShellManager != nullptr) {
delete pShellManager;
}
#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
ControlBarCleanUp();
#endif
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}

View File

@@ -0,0 +1,31 @@
// StreamingSpeechRecognition.h : main header file for the PROJECT_NAME
// application
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'pch.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
// CStreamingSpeechRecognitionApp:
// See StreamingSpeechRecognition.cpp for the implementation of this class
//
class CStreamingSpeechRecognitionApp : public CWinApp {
public:
CStreamingSpeechRecognitionApp();
// Overrides
public:
virtual BOOL InitInstance();
// Implementation
DECLARE_MESSAGE_MAP()
};
extern CStreamingSpeechRecognitionApp theApp;

View File

@@ -0,0 +1,219 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{A79C2604-C33D-497C-9770-D34E118B77FE}</ProjectGuid>
<Keyword>MFCProj</Keyword>
<RootNamespace>StreamingSpeechRecognition</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="sherpa-onnx-deps.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="sherpa-onnx-deps.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="sherpa-onnx-deps.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="sherpa-onnx-deps.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
</Link>
<Midl>
<MkTypLibCompatible>false</MkTypLibCompatible>
<ValidateAllParameters>true</ValidateAllParameters>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</Midl>
<ResourceCompile>
<Culture>0x0409</Culture>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
</Link>
<Midl>
<MkTypLibCompatible>false</MkTypLibCompatible>
<ValidateAllParameters>true</ValidateAllParameters>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</Midl>
<ResourceCompile>
<Culture>0x0409</Culture>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
<Midl>
<MkTypLibCompatible>false</MkTypLibCompatible>
<ValidateAllParameters>true</ValidateAllParameters>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</Midl>
<ResourceCompile>
<Culture>0x0409</Culture>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
<Midl>
<MkTypLibCompatible>false</MkTypLibCompatible>
<ValidateAllParameters>true</ValidateAllParameters>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</Midl>
<ResourceCompile>
<Culture>0x0409</Culture>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="StreamingSpeechRecognition.h" />
<ClInclude Include="StreamingSpeechRecognitionDlg.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="StreamingSpeechRecognition.cpp" />
<ClCompile Include="StreamingSpeechRecognitionDlg.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="StreamingSpeechRecognition.rc" />
</ItemGroup>
<ItemGroup>
<None Include="res\StreamingSpeechRecognition.rc2" />
</ItemGroup>
<ItemGroup>
<Image Include="res\StreamingSpeechRecognition.ico" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="StreamingSpeechRecognition.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="StreamingSpeechRecognitionDlg.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="framework.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="StreamingSpeechRecognition.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="StreamingSpeechRecognitionDlg.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="StreamingSpeechRecognition.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<None Include="res\StreamingSpeechRecognition.rc2">
<Filter>Resource Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Image Include="res\StreamingSpeechRecognition.ico">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,431 @@
// StreamingSpeechRecognitionDlg.cpp : implementation file
//
#include "StreamingSpeechRecognitionDlg.h"
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "StreamingSpeechRecognition.h"
#include "afxdialogex.h"
#include "framework.h"
#include "pch.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Microphone::Microphone() {
PaError err = Pa_Initialize();
if (err != paNoError) {
fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
exit(-2);
}
}
Microphone::~Microphone() {
PaError err = Pa_Terminate();
if (err != paNoError) {
fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
exit(-2);
}
}
// CStreamingSpeechRecognitionDlg dialog
CStreamingSpeechRecognitionDlg::CStreamingSpeechRecognitionDlg(
CWnd *pParent /*=nullptr*/)
: CDialogEx(IDD_STREAMINGSPEECHRECOGNITION_DIALOG, pParent) {
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CStreamingSpeechRecognitionDlg::~CStreamingSpeechRecognitionDlg() {
if (recognizer_) {
DestroyOnlineRecognizer(recognizer_);
recognizer_ = nullptr;
}
}
void CStreamingSpeechRecognitionDlg::DoDataExchange(CDataExchange *pDX) {
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDOK, my_btn_);
DDX_Control(pDX, IDC_EDIT1, my_text_);
}
BEGIN_MESSAGE_MAP(CStreamingSpeechRecognitionDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CStreamingSpeechRecognitionDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// CStreamingSpeechRecognitionDlg message handlers
BOOL CStreamingSpeechRecognitionDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetWindowText(_T("Real-time speech recogntion with Next-gen Kaldi"));
InitMicrophone();
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CStreamingSpeechRecognitionDlg::OnPaint() {
if (IsIconic()) {
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()),
0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
} else {
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user
// drags
// the minimized window.
HCURSOR CStreamingSpeechRecognitionDlg::OnQueryDragIcon() {
return static_cast<HCURSOR>(m_hIcon);
}
static int32_t RecordCallback(const void *input_buffer,
void * /*output_buffer*/,
unsigned long frames_per_buffer, // NOLINT
const PaStreamCallbackTimeInfo * /*time_info*/,
PaStreamCallbackFlags /*status_flags*/,
void *user_data) {
auto dlg = reinterpret_cast<CStreamingSpeechRecognitionDlg *>(user_data);
auto stream = dlg->stream_;
if (stream) {
AcceptWaveform(stream, 16000, reinterpret_cast<const float *>(input_buffer),
frames_per_buffer);
}
return dlg->started_ ? paContinue : paComplete;
}
void CStreamingSpeechRecognitionDlg::OnBnClickedOk() {
if (!recognizer_) {
AppendLineToMultilineEditCtrl("Creating recognizer...");
InitRecognizer();
if (!recognizer_) {
// failed to create the recognizer
return;
}
AppendLineToMultilineEditCtrl("Recognizer created!");
}
if (!started_) {
started_ = true;
if (stream_) {
DestroyOnlineStream(stream_);
stream_ = nullptr;
}
stream_ = CreateOnlineStream(recognizer_);
PaStreamParameters param;
param.device = Pa_GetDefaultInputDevice();
const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device);
param.channelCount = 1;
param.sampleFormat = paFloat32;
param.suggestedLatency = info->defaultLowInputLatency;
param.hostApiSpecificStreamInfo = nullptr;
float sample_rate = 16000;
pa_stream_ = nullptr;
PaError err =
Pa_OpenStream(&pa_stream_, &param, nullptr, /* &outputParameters, */
sample_rate,
0, // frames per buffer
paClipOff, // we won't output out of range samples
// so don't bother clipping them
RecordCallback, this);
if (err != paNoError) {
AppendLineToMultilineEditCtrl(std::string("PortAudio error: ") +
Pa_GetErrorText(err));
my_btn_.EnableWindow(FALSE);
return;
}
err = Pa_StartStream(pa_stream_);
if (err != paNoError) {
AppendLineToMultilineEditCtrl(std::string("PortAudio error: ") +
Pa_GetErrorText(err));
my_btn_.EnableWindow(FALSE);
return;
}
AppendLineToMultilineEditCtrl("Started! Please speak");
my_btn_.SetWindowText(_T("Stop"));
thread_ = new RecognizerThread(this);
thread_->CreateThread(CREATE_SUSPENDED);
thread_->m_bAutoDelete = false; // Let me delete it.
thread_->ResumeThread();
} else {
started_ = false;
Pa_Sleep(200); // sleep for 200ms
if (pa_stream_) {
PaError err = Pa_CloseStream(pa_stream_);
if (err != paNoError) {
AppendLineToMultilineEditCtrl(std::string("PortAudio error: ") +
Pa_GetErrorText(err));
my_btn_.EnableWindow(FALSE);
return;
}
}
pa_stream_ = nullptr;
WaitForSingleObject(thread_->m_hThread, INFINITE);
delete thread_;
thread_ = nullptr;
// AfxMessageBox("stopped", MB_OK);
my_btn_.SetWindowText(_T("Start"));
AppendLineToMultilineEditCtrl("Stopped");
}
}
void CStreamingSpeechRecognitionDlg::InitMicrophone() {
int default_device = Pa_GetDefaultInputDevice();
int device_count = Pa_GetDeviceCount();
if (default_device == paNoDevice) {
// CString str;
// str.Format(_T("No default input device found!"));
// AfxMessageBox(str, MB_OK | MB_ICONSTOP);
// exit(-1);
AppendLineToMultilineEditCtrl("No default input device found!");
my_btn_.EnableWindow(FALSE);
}
AppendLineToMultilineEditCtrl(std::string("Selected device ") +
Pa_GetDeviceInfo(default_device)->name);
}
bool CStreamingSpeechRecognitionDlg::Exists(const std::string &filename) {
std::ifstream is(filename);
return is.good();
}
void CStreamingSpeechRecognitionDlg::InitRecognizer() {
std::string encoder = "./encoder.onnx";
std::string decoder = "./decoder.onnx";
std::string joiner = "./joiner.onnx";
std::string tokens = "./tokens.txt";
#if 1
std::string prefix =
"C:/Users/fangjun/source/repos/MFCApplication2/x64/Release";
encoder = prefix + encoder;
decoder = prefix + decoder;
joiner = prefix + joiner;
tokens = prefix + tokens;
#endif
bool is_ok = true;
if (!Exists(encoder)) {
std::string msg = encoder + " does not exist!";
AppendLineToMultilineEditCtrl(msg);
is_ok = false;
}
if (!Exists(decoder)) {
std::string msg = decoder + " does not exist!";
AppendLineToMultilineEditCtrl(msg);
is_ok = false;
}
if (!Exists(joiner)) {
std::string msg = joiner + " does not exist!";
AppendLineToMultilineEditCtrl(msg);
is_ok = false;
}
if (!Exists(tokens)) {
std::string msg = tokens + " does not exist!";
AppendLineToMultilineEditCtrl(msg);
is_ok = false;
}
if (!is_ok) {
my_btn_.EnableWindow(FALSE);
std::string msg =
"\r\nPlease go to "
"https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html "
"\r\n";
msg += "to download an offline model.";
msg +=
" You need to rename them to encoder.onnx, decoder.onnx, and "
"joiner.onnx correspoondingly";
AppendLineToMultilineEditCtrl(msg);
return;
}
SherpaOnnxOnlineRecognizerConfig config;
config.model_config.debug = 0;
config.model_config.num_threads = 2;
config.model_config.provider = "cpu";
config.decoding_method = "greedy_search";
config.max_active_paths = 4;
config.feat_config.sample_rate = 16000;
config.feat_config.feature_dim = 80;
config.enable_endpoint = 1;
config.rule1_min_trailing_silence = 1.2f;
config.rule2_min_trailing_silence = 0.8f;
config.rule3_min_utterance_length = 300.0f;
config.model_config.tokens = tokens.c_str();
config.model_config.encoder = encoder.c_str();
config.model_config.decoder = decoder.c_str();
config.model_config.joiner = joiner.c_str();
recognizer_ = CreateOnlineRecognizer(&config);
}
// see
// https://stackoverflow.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring
std::wstring Utf8ToUtf16(const std::string &utf8) {
std::vector<unsigned long> unicode;
size_t i = 0;
while (i < utf8.size()) {
unsigned long uni;
size_t todo;
bool error = false;
unsigned char ch = utf8[i++];
if (ch <= 0x7F) {
uni = ch;
todo = 0;
} else if (ch <= 0xBF) {
throw std::logic_error("not a UTF-8 string");
} else if (ch <= 0xDF) {
uni = ch & 0x1F;
todo = 1;
} else if (ch <= 0xEF) {
uni = ch & 0x0F;
todo = 2;
} else if (ch <= 0xF7) {
uni = ch & 0x07;
todo = 3;
} else {
throw std::logic_error("not a UTF-8 string");
}
for (size_t j = 0; j < todo; ++j) {
if (i == utf8.size()) throw std::logic_error("not a UTF-8 string");
unsigned char ch = utf8[i++];
if (ch < 0x80 || ch > 0xBF) throw std::logic_error("not a UTF-8 string");
uni <<= 6;
uni += ch & 0x3F;
}
if (uni >= 0xD800 && uni <= 0xDFFF)
throw std::logic_error("not a UTF-8 string");
if (uni > 0x10FFFF) throw std::logic_error("not a UTF-8 string");
unicode.push_back(uni);
}
std::wstring utf16;
for (size_t i = 0; i < unicode.size(); ++i) {
unsigned long uni = unicode[i];
if (uni <= 0xFFFF) {
utf16 += (wchar_t)uni;
} else {
uni -= 0x10000;
utf16 += (wchar_t)((uni >> 10) + 0xD800);
utf16 += (wchar_t)((uni & 0x3FF) + 0xDC00);
}
}
return utf16;
}
void CStreamingSpeechRecognitionDlg::AppendTextToEditCtrl(
const std::string &s) {
// get the initial text length
int nLength = my_text_.GetWindowTextLength();
// put the selection at the end of text
my_text_.SetSel(nLength, nLength);
// replace the selection
CString str;
str.Format(_T("%s"), s.c_str());
std::wstring wstr = Utf8ToUtf16(s);
// my_text_.ReplaceSel(wstr.c_str());
my_text_.ReplaceSel(wstr.c_str());
}
void CStreamingSpeechRecognitionDlg::AppendLineToMultilineEditCtrl(
const std::string &s) {
AppendTextToEditCtrl("\r\n" + s);
}
static std::string Cat(const std::vector<std::string> &results,
const std::string &s) {
std::ostringstream os;
std::string sep;
int i = 0;
for (i = 0; i != results.size(); ++i) {
os << sep << i << ": " << results[i];
sep = "\r\n";
}
if (!s.empty()) {
os << sep << i << ": " << s;
}
return os.str();
}
int CStreamingSpeechRecognitionDlg::RunThread() {
std::vector<std::string> results;
std::string last_text;
while (started_) {
while (IsOnlineStreamReady(recognizer_, stream_)) {
DecodeOnlineStream(recognizer_, stream_);
}
auto r = GetOnlineStreamResult(recognizer_, stream_);
std::string text = r->text;
DestroyOnlineRecognizerResult(r);
if (!text.empty() && last_text != text) {
// CString str;
// str.Format(_T("%s"), Cat(results, text).c_str());
auto str = Utf8ToUtf16(Cat(results, text).c_str());
my_text_.SetWindowText(str.c_str());
last_text = text;
}
int is_endpoint = IsEndpoint(recognizer_, stream_);
if (is_endpoint) {
Reset(recognizer_, stream_);
if (!text.empty()) {
results.push_back(std::move(text));
}
}
Pa_Sleep(100); // sleep for 100ms
}
return 0;
}

View File

@@ -0,0 +1,80 @@
// StreamingSpeechRecognitionDlg.h : header file
//
#pragma once
#include <string>
#include "portaudio.h"
#include "sherpa-onnx/c-api/c-api.h"
class Microphone {
public:
Microphone();
~Microphone();
};
class RecognizerThread;
// CStreamingSpeechRecognitionDlg dialog
class CStreamingSpeechRecognitionDlg : public CDialogEx {
// Construction
public:
CStreamingSpeechRecognitionDlg(
CWnd *pParent = nullptr); // standard constructor
~CStreamingSpeechRecognitionDlg();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_STREAMINGSPEECHRECOGNITION_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
Microphone mic_;
SherpaOnnxOnlineRecognizer *recognizer_ = nullptr;
PaStream *pa_stream_ = nullptr;
RecognizerThread *thread_;
CButton my_btn_;
CEdit my_text_;
public:
bool started_ = false;
SherpaOnnxOnlineStream *stream_ = nullptr;
public:
int RunThread();
afx_msg void OnBnClickedOk();
private:
void AppendTextToEditCtrl(const std::string &s);
void AppendLineToMultilineEditCtrl(const std::string &s);
void InitMicrophone();
bool Exists(const std::string &filename);
void InitRecognizer();
};
class RecognizerThread : public CWinThread {
public:
RecognizerThread(CStreamingSpeechRecognitionDlg *dlg) : dlg_(dlg) {}
virtual BOOL InitInstance() { return TRUE; }
virtual int Run() { return dlg_->RunThread(); }
private:
CStreamingSpeechRecognitionDlg *dlg_;
};

View File

@@ -0,0 +1,26 @@
#pragma once
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif
#include "targetver.h"
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be
// explicit
// turns off MFC's hiding of some common and often safely ignored warning
// messages
#define _AFX_ALL_WARNINGS
#include <afxext.h> // MFC extensions
#include <afxwin.h> // MFC core and standard components
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <afxcontrolbars.h> // MFC support for ribbons and control bars

View File

@@ -0,0 +1,6 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for
// compilation to succeed.

View File

@@ -0,0 +1,15 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for
// future builds. This also affects IntelliSense performance, including code
// completion and many code browsing features. However, files listed here are
// ALL re-compiled if any one of them is updated between builds. Do not add
// files here that you will be updating frequently as this negates the
// performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#endif // PCH_H

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<SherpaOnnxBuildDirectory>..\..\build</SherpaOnnxBuildDirectory>
<SherpaOnnxInstallDirectory>..\..\build\install</SherpaOnnxInstallDirectory>
<SherpaOnnxLibraries>sherpa-onnx-portaudio.lib;sherpa-onnx-c-api.lib;sherpa-onnx-core.lib</SherpaOnnxLibraries>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>
$(SherpaOnnxBuildDirectory)\_deps\portaudio-src\include;$(SherpaOnnxInstallDirectory)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(SherpaOnnxInstallDirectory)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>$(SherpaOnnxLibraries)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

View File

@@ -0,0 +1,9 @@
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform,
// include WinSDKVer.h and set the _WIN32_WINNT macro to the platform you wish
// to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>