Fangjun Kuang
Committed by GitHub

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

  1 +# See https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
  2 +## Ignore Visual Studio temporary files, build results, and
  3 +## files generated by popular Visual Studio add-ons.
  4 +##
  5 +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
  6 +
  7 +# User-specific files
  8 +*.rsuser
  9 +*.suo
  10 +*.user
  11 +*.userosscache
  12 +*.sln.docstates
  13 +
  14 +# User-specific files (MonoDevelop/Xamarin Studio)
  15 +*.userprefs
  16 +
  17 +# Mono auto generated files
  18 +mono_crash.*
  19 +
  20 +# Build results
  21 +[Dd]ebug/
  22 +[Dd]ebugPublic/
  23 +[Rr]elease/
  24 +[Rr]eleases/
  25 +x64/
  26 +x86/
  27 +[Ww][Ii][Nn]32/
  28 +[Aa][Rr][Mm]/
  29 +[Aa][Rr][Mm]64/
  30 +bld/
  31 +[Bb]in/
  32 +[Oo]bj/
  33 +[Ll]og/
  34 +[Ll]ogs/
  35 +
  36 +# Visual Studio 2015/2017 cache/options directory
  37 +.vs/
  38 +# Uncomment if you have tasks that create the project's static files in wwwroot
  39 +#wwwroot/
  40 +
  41 +# Visual Studio 2017 auto generated files
  42 +Generated\ Files/
  43 +
  44 +# MSTest test Results
  45 +[Tt]est[Rr]esult*/
  46 +[Bb]uild[Ll]og.*
  47 +
  48 +# NUnit
  49 +*.VisualState.xml
  50 +TestResult.xml
  51 +nunit-*.xml
  52 +
  53 +# Build Results of an ATL Project
  54 +[Dd]ebugPS/
  55 +[Rr]eleasePS/
  56 +dlldata.c
  57 +
  58 +# Benchmark Results
  59 +BenchmarkDotNet.Artifacts/
  60 +
  61 +# .NET Core
  62 +project.lock.json
  63 +project.fragment.lock.json
  64 +artifacts/
  65 +
  66 +# ASP.NET Scaffolding
  67 +ScaffoldingReadMe.txt
  68 +
  69 +# StyleCop
  70 +StyleCopReport.xml
  71 +
  72 +# Files built by Visual Studio
  73 +*_i.c
  74 +*_p.c
  75 +*_h.h
  76 +*.ilk
  77 +*.meta
  78 +*.obj
  79 +*.iobj
  80 +*.pch
  81 +*.pdb
  82 +*.ipdb
  83 +*.pgc
  84 +*.pgd
  85 +*.rsp
  86 +*.sbr
  87 +*.tlb
  88 +*.tli
  89 +*.tlh
  90 +*.tmp
  91 +*.tmp_proj
  92 +*_wpftmp.csproj
  93 +*.log
  94 +*.tlog
  95 +*.vspscc
  96 +*.vssscc
  97 +.builds
  98 +*.pidb
  99 +*.svclog
  100 +*.scc
  101 +
  102 +# Chutzpah Test files
  103 +_Chutzpah*
  104 +
  105 +# Visual C++ cache files
  106 +ipch/
  107 +*.aps
  108 +*.ncb
  109 +*.opendb
  110 +*.opensdf
  111 +*.sdf
  112 +*.cachefile
  113 +*.VC.db
  114 +*.VC.VC.opendb
  115 +
  116 +# Visual Studio profiler
  117 +*.psess
  118 +*.vsp
  119 +*.vspx
  120 +*.sap
  121 +
  122 +# Visual Studio Trace Files
  123 +*.e2e
  124 +
  125 +# TFS 2012 Local Workspace
  126 +$tf/
  127 +
  128 +# Guidance Automation Toolkit
  129 +*.gpState
  130 +
  131 +# ReSharper is a .NET coding add-in
  132 +_ReSharper*/
  133 +*.[Rr]e[Ss]harper
  134 +*.DotSettings.user
  135 +
  136 +# TeamCity is a build add-in
  137 +_TeamCity*
  138 +
  139 +# DotCover is a Code Coverage Tool
  140 +*.dotCover
  141 +
  142 +# AxoCover is a Code Coverage Tool
  143 +.axoCover/*
  144 +!.axoCover/settings.json
  145 +
  146 +# Coverlet is a free, cross platform Code Coverage Tool
  147 +coverage*.json
  148 +coverage*.xml
  149 +coverage*.info
  150 +
  151 +# Visual Studio code coverage results
  152 +*.coverage
  153 +*.coveragexml
  154 +
  155 +# NCrunch
  156 +_NCrunch_*
  157 +.*crunch*.local.xml
  158 +nCrunchTemp_*
  159 +
  160 +# MightyMoose
  161 +*.mm.*
  162 +AutoTest.Net/
  163 +
  164 +# Web workbench (sass)
  165 +.sass-cache/
  166 +
  167 +# Installshield output folder
  168 +[Ee]xpress/
  169 +
  170 +# DocProject is a documentation generator add-in
  171 +DocProject/buildhelp/
  172 +DocProject/Help/*.HxT
  173 +DocProject/Help/*.HxC
  174 +DocProject/Help/*.hhc
  175 +DocProject/Help/*.hhk
  176 +DocProject/Help/*.hhp
  177 +DocProject/Help/Html2
  178 +DocProject/Help/html
  179 +
  180 +# Click-Once directory
  181 +publish/
  182 +
  183 +# Publish Web Output
  184 +*.[Pp]ublish.xml
  185 +*.azurePubxml
  186 +# Note: Comment the next line if you want to checkin your web deploy settings,
  187 +# but database connection strings (with potential passwords) will be unencrypted
  188 +*.pubxml
  189 +*.publishproj
  190 +
  191 +# Microsoft Azure Web App publish settings. Comment the next line if you want to
  192 +# checkin your Azure Web App publish settings, but sensitive information contained
  193 +# in these scripts will be unencrypted
  194 +PublishScripts/
  195 +
  196 +# NuGet Packages
  197 +*.nupkg
  198 +# NuGet Symbol Packages
  199 +*.snupkg
  200 +# The packages folder can be ignored because of Package Restore
  201 +**/[Pp]ackages/*
  202 +# except build/, which is used as an MSBuild target.
  203 +!**/[Pp]ackages/build/
  204 +# Uncomment if necessary however generally it will be regenerated when needed
  205 +#!**/[Pp]ackages/repositories.config
  206 +# NuGet v3's project.json files produces more ignorable files
  207 +*.nuget.props
  208 +*.nuget.targets
  209 +
  210 +# Microsoft Azure Build Output
  211 +csx/
  212 +*.build.csdef
  213 +
  214 +# Microsoft Azure Emulator
  215 +ecf/
  216 +rcf/
  217 +
  218 +# Windows Store app package directories and files
  219 +AppPackages/
  220 +BundleArtifacts/
  221 +Package.StoreAssociation.xml
  222 +_pkginfo.txt
  223 +*.appx
  224 +*.appxbundle
  225 +*.appxupload
  226 +
  227 +# Visual Studio cache files
  228 +# files ending in .cache can be ignored
  229 +*.[Cc]ache
  230 +# but keep track of directories ending in .cache
  231 +!?*.[Cc]ache/
  232 +
  233 +# Others
  234 +ClientBin/
  235 +~$*
  236 +*~
  237 +*.dbmdl
  238 +*.dbproj.schemaview
  239 +*.jfm
  240 +*.pfx
  241 +*.publishsettings
  242 +orleans.codegen.cs
  243 +
  244 +# Including strong name files can present a security risk
  245 +# (https://github.com/github/gitignore/pull/2483#issue-259490424)
  246 +#*.snk
  247 +
  248 +# Since there are multiple workflows, uncomment next line to ignore bower_components
  249 +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
  250 +#bower_components/
  251 +
  252 +# RIA/Silverlight projects
  253 +Generated_Code/
  254 +
  255 +# Backup & report files from converting an old project file
  256 +# to a newer Visual Studio version. Backup files are not needed,
  257 +# because we have git ;-)
  258 +_UpgradeReport_Files/
  259 +Backup*/
  260 +UpgradeLog*.XML
  261 +UpgradeLog*.htm
  262 +ServiceFabricBackup/
  263 +*.rptproj.bak
  264 +
  265 +# SQL Server files
  266 +*.mdf
  267 +*.ldf
  268 +*.ndf
  269 +
  270 +# Business Intelligence projects
  271 +*.rdl.data
  272 +*.bim.layout
  273 +*.bim_*.settings
  274 +*.rptproj.rsuser
  275 +*- [Bb]ackup.rdl
  276 +*- [Bb]ackup ([0-9]).rdl
  277 +*- [Bb]ackup ([0-9][0-9]).rdl
  278 +
  279 +# Microsoft Fakes
  280 +FakesAssemblies/
  281 +
  282 +# GhostDoc plugin setting file
  283 +*.GhostDoc.xml
  284 +
  285 +# Node.js Tools for Visual Studio
  286 +.ntvs_analysis.dat
  287 +node_modules/
  288 +
  289 +# Visual Studio 6 build log
  290 +*.plg
  291 +
  292 +# Visual Studio 6 workspace options file
  293 +*.opt
  294 +
  295 +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
  296 +*.vbw
  297 +
  298 +# Visual Studio 6 auto-generated project file (contains which files were open etc.)
  299 +*.vbp
  300 +
  301 +# Visual Studio 6 workspace and project file (working project files containing files to include in project)
  302 +*.dsw
  303 +*.dsp
  304 +
  305 +# Visual Studio 6 technical files
  306 +*.ncb
  307 +*.aps
  308 +
  309 +# Visual Studio LightSwitch build output
  310 +**/*.HTMLClient/GeneratedArtifacts
  311 +**/*.DesktopClient/GeneratedArtifacts
  312 +**/*.DesktopClient/ModelManifest.xml
  313 +**/*.Server/GeneratedArtifacts
  314 +**/*.Server/ModelManifest.xml
  315 +_Pvt_Extensions
  316 +
  317 +# Paket dependency manager
  318 +.paket/paket.exe
  319 +paket-files/
  320 +
  321 +# FAKE - F# Make
  322 +.fake/
  323 +
  324 +# CodeRush personal settings
  325 +.cr/personal
  326 +
  327 +# Python Tools for Visual Studio (PTVS)
  328 +__pycache__/
  329 +*.pyc
  330 +
  331 +# Cake - Uncomment if you are using it
  332 +# tools/**
  333 +# !tools/packages.config
  334 +
  335 +# Tabs Studio
  336 +*.tss
  337 +
  338 +# Telerik's JustMock configuration file
  339 +*.jmconfig
  340 +
  341 +# BizTalk build output
  342 +*.btp.cs
  343 +*.btm.cs
  344 +*.odx.cs
  345 +*.xsd.cs
  346 +
  347 +# OpenCover UI analysis results
  348 +OpenCover/
  349 +
  350 +# Azure Stream Analytics local run output
  351 +ASALocalRun/
  352 +
  353 +# MSBuild Binary and Structured Log
  354 +*.binlog
  355 +
  356 +# NVidia Nsight GPU debugger configuration file
  357 +*.nvuser
  358 +
  359 +# MFractors (Xamarin productivity tool) working folder
  360 +.mfractor/
  361 +
  362 +# Local History for Visual Studio
  363 +.localhistory/
  364 +
  365 +# Visual Studio History (VSHistory) files
  366 +.vshistory/
  367 +
  368 +# BeatPulse healthcheck temp database
  369 +healthchecksdb
  370 +
  371 +# Backup folder for Package Reference Convert tool in Visual Studio 2017
  372 +MigrationBackup/
  373 +
  374 +# Ionide (cross platform F# VS Code tools) working folder
  375 +.ionide/
  376 +
  377 +# Fody - auto-generated XML schema
  378 +FodyWeavers.xsd
  379 +
  380 +# VS Code files for those working on multiple tools
  381 +.vscode/*
  382 +!.vscode/settings.json
  383 +!.vscode/tasks.json
  384 +!.vscode/launch.json
  385 +!.vscode/extensions.json
  386 +*.code-workspace
  387 +
  388 +# Local History for Visual Studio Code
  389 +.history/
  390 +
  391 +# Windows Installer files from build outputs
  392 +*.cab
  393 +*.msi
  394 +*.msix
  395 +*.msm
  396 +*.msp
  397 +
  398 +# JetBrains Rider
  399 +*.sln.iml
  1 +# Speech recognition with Visual C++ MFC
  2 +
  3 +This directory contains examples showing how to use Next-gen Kaldi in MFC
  4 +for speech recognition.
  5 +
  6 +Caution: You need to use Windows and install Visual Studio in order to run it.
  7 +We use bash script below to demonstrate how to use it. Please change
  8 +the commands accordingly for Windows.
  9 +
  10 +## Streaming speech recognition
  11 +
  12 +```bash
  13 +mkdir -p $HOME/open-source
  14 +cd $HOME/open-source
  15 +
  16 +git clone https://github.com/k2-fsa/sherpa-onnx
  17 +cd sherpa-onnx
  18 +mkdir build
  19 +
  20 +cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install ..
  21 +cmake --build . --config Release --target install
  22 +
  23 +cd ../mfc-examples
  24 +
  25 +msbuild ./mfc-examples.sln /property:Configuration=Release /property:Platform=x64
  26 +cp ../build/install/lib/*.dll ./x64/Release/
  27 +
  28 +# now run the program
  29 +
  30 +./x64/Release/StreamingSpeechRecognition.exe
  31 +```
  32 +
  33 +Note that we also need to download pre-trained models. Please
  34 +refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/online-transducer/index.html
  35 +for a list of streaming models.
  36 +
  37 +We use the following model for demonstration.
  38 +
  39 +```bash
  40 +cd $HOME/open-source/sherpa-onnx/mfc-examples/x64/Release
  41 +wget https://huggingface.co/pkufool/icefall-asr-zipformer-streaming-wenetspeech-20230615/resolve/main/exp/encoder-epoch-12-avg-4-chunk-16-left-128.onnx
  42 +wget https://huggingface.co/pkufool/icefall-asr-zipformer-streaming-wenetspeech-20230615/resolve/main/exp/decoder-epoch-12-avg-4-chunk-16-left-128.onnx
  43 +wget https://huggingface.co/pkufool/icefall-asr-zipformer-streaming-wenetspeech-20230615/resolve/main/exp/joiner-epoch-12-avg-4-chunk-16-left-128.onnx
  44 +wget https://huggingface.co/pkufool/icefall-asr-zipformer-streaming-wenetspeech-20230615/resolve/main/data/lang_char/tokens.txt
  45 +
  46 +# now rename
  47 +mv encoder-epoch-12-avg-4-chunk-16-left-128.onnx encoder.onnx
  48 +mv decoder-epoch-12-avg-4-chunk-16-left-128.onnx decoder.onnx
  49 +mv joiner-epoch-12-avg-4-chunk-16-left-128.onnx joiner.onnx
  50 +
  51 +# Now run it!
  52 +./StreamingSpeechRecognition.exe
  53 +```
  1 +//{{NO_DEPENDENCIES}}
  2 +// Microsoft Visual C++ generated include file.
  3 +// Used by StreamingSpeechRecognition.rc
  4 +//
  5 +#define IDD_STREAMINGSPEECHRECOGNITION_DIALOG 102
  6 +#define IDR_MAINFRAME 128
  7 +#define IDC_EDIT1 1000
  8 +
  9 +// Next default values for new objects
  10 +//
  11 +#ifdef APSTUDIO_INVOKED
  12 +#ifndef APSTUDIO_READONLY_SYMBOLS
  13 +#define _APS_NEXT_RESOURCE_VALUE 130
  14 +#define _APS_NEXT_COMMAND_VALUE 32771
  15 +#define _APS_NEXT_CONTROL_VALUE 1001
  16 +#define _APS_NEXT_SYMED_VALUE 101
  17 +#endif
  18 +#endif
  1 +
  2 +// StreamingSpeechRecognition.cpp : Defines the class behaviors for the
  3 +// application.
  4 +//
  5 +
  6 +#include "StreamingSpeechRecognition.h"
  7 +
  8 +#include "StreamingSpeechRecognitionDlg.h"
  9 +#include "framework.h"
  10 +#include "pch.h"
  11 +
  12 +#ifdef _DEBUG
  13 +#define new DEBUG_NEW
  14 +#endif
  15 +
  16 +// CStreamingSpeechRecognitionApp
  17 +
  18 +BEGIN_MESSAGE_MAP(CStreamingSpeechRecognitionApp, CWinApp)
  19 +ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
  20 +END_MESSAGE_MAP()
  21 +
  22 +// CStreamingSpeechRecognitionApp construction
  23 +
  24 +CStreamingSpeechRecognitionApp::CStreamingSpeechRecognitionApp() {
  25 + // TODO: add construction code here,
  26 + // Place all significant initialization in InitInstance
  27 +}
  28 +
  29 +// The one and only CStreamingSpeechRecognitionApp object
  30 +
  31 +CStreamingSpeechRecognitionApp theApp;
  32 +
  33 +// CStreamingSpeechRecognitionApp initialization
  34 +
  35 +BOOL CStreamingSpeechRecognitionApp::InitInstance() {
  36 + CWinApp::InitInstance();
  37 +
  38 + // Create the shell manager, in case the dialog contains
  39 + // any shell tree view or shell list view controls.
  40 + CShellManager *pShellManager = new CShellManager;
  41 +
  42 + // Activate "Windows Native" visual manager for enabling themes in MFC
  43 + // controls
  44 + CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
  45 +
  46 + // Standard initialization
  47 + // If you are not using these features and wish to reduce the size
  48 + // of your final executable, you should remove from the following
  49 + // the specific initialization routines you do not need
  50 + // Change the registry key under which our settings are stored
  51 + // TODO: You should modify this string to be something appropriate
  52 + // such as the name of your company or organization
  53 + SetRegistryKey(_T("Local AppWizard-Generated Applications"));
  54 +
  55 + CStreamingSpeechRecognitionDlg dlg;
  56 + m_pMainWnd = &dlg;
  57 + INT_PTR nResponse = dlg.DoModal();
  58 + if (nResponse == IDOK) {
  59 + // TODO: Place code here to handle when the dialog is
  60 + // dismissed with OK
  61 + } else if (nResponse == IDCANCEL) {
  62 + // TODO: Place code here to handle when the dialog is
  63 + // dismissed with Cancel
  64 + } else if (nResponse == -1) {
  65 + TRACE(traceAppMsg, 0,
  66 + "Warning: dialog creation failed, so application is terminating "
  67 + "unexpectedly.\n");
  68 + TRACE(traceAppMsg, 0,
  69 + "Warning: if you are using MFC controls on the dialog, you cannot "
  70 + "#define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
  71 + }
  72 +
  73 + // Delete the shell manager created above.
  74 + if (pShellManager != nullptr) {
  75 + delete pShellManager;
  76 + }
  77 +
  78 +#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
  79 + ControlBarCleanUp();
  80 +#endif
  81 +
  82 + // Since the dialog has been closed, return FALSE so that we exit the
  83 + // application, rather than start the application's message pump.
  84 + return FALSE;
  85 +}
  1 +
  2 +// StreamingSpeechRecognition.h : main header file for the PROJECT_NAME
  3 +// application
  4 +//
  5 +
  6 +#pragma once
  7 +
  8 +#ifndef __AFXWIN_H__
  9 +#error "include 'pch.h' before including this file for PCH"
  10 +#endif
  11 +
  12 +#include "resource.h" // main symbols
  13 +
  14 +// CStreamingSpeechRecognitionApp:
  15 +// See StreamingSpeechRecognition.cpp for the implementation of this class
  16 +//
  17 +
  18 +class CStreamingSpeechRecognitionApp : public CWinApp {
  19 + public:
  20 + CStreamingSpeechRecognitionApp();
  21 +
  22 + // Overrides
  23 + public:
  24 + virtual BOOL InitInstance();
  25 +
  26 + // Implementation
  27 +
  28 + DECLARE_MESSAGE_MAP()
  29 +};
  30 +
  31 +extern CStreamingSpeechRecognitionApp theApp;
1 B// Microsoft Visual C++ generated resource script. 1 B// Microsoft Visual C++ generated resource script.
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3 + <ItemGroup Label="ProjectConfigurations">
  4 + <ProjectConfiguration Include="Debug|Win32">
  5 + <Configuration>Debug</Configuration>
  6 + <Platform>Win32</Platform>
  7 + </ProjectConfiguration>
  8 + <ProjectConfiguration Include="Release|Win32">
  9 + <Configuration>Release</Configuration>
  10 + <Platform>Win32</Platform>
  11 + </ProjectConfiguration>
  12 + <ProjectConfiguration Include="Debug|x64">
  13 + <Configuration>Debug</Configuration>
  14 + <Platform>x64</Platform>
  15 + </ProjectConfiguration>
  16 + <ProjectConfiguration Include="Release|x64">
  17 + <Configuration>Release</Configuration>
  18 + <Platform>x64</Platform>
  19 + </ProjectConfiguration>
  20 + </ItemGroup>
  21 + <PropertyGroup Label="Globals">
  22 + <VCProjectVersion>16.0</VCProjectVersion>
  23 + <ProjectGuid>{A79C2604-C33D-497C-9770-D34E118B77FE}</ProjectGuid>
  24 + <Keyword>MFCProj</Keyword>
  25 + <RootNamespace>StreamingSpeechRecognition</RootNamespace>
  26 + <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  27 + </PropertyGroup>
  28 + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  29 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
  30 + <ConfigurationType>Application</ConfigurationType>
  31 + <UseDebugLibraries>true</UseDebugLibraries>
  32 + <PlatformToolset>v142</PlatformToolset>
  33 + <CharacterSet>Unicode</CharacterSet>
  34 + <UseOfMfc>Static</UseOfMfc>
  35 + </PropertyGroup>
  36 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
  37 + <ConfigurationType>Application</ConfigurationType>
  38 + <UseDebugLibraries>false</UseDebugLibraries>
  39 + <PlatformToolset>v142</PlatformToolset>
  40 + <WholeProgramOptimization>true</WholeProgramOptimization>
  41 + <CharacterSet>Unicode</CharacterSet>
  42 + <UseOfMfc>Static</UseOfMfc>
  43 + </PropertyGroup>
  44 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
  45 + <ConfigurationType>Application</ConfigurationType>
  46 + <UseDebugLibraries>true</UseDebugLibraries>
  47 + <PlatformToolset>v142</PlatformToolset>
  48 + <CharacterSet>Unicode</CharacterSet>
  49 + <UseOfMfc>Static</UseOfMfc>
  50 + </PropertyGroup>
  51 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
  52 + <ConfigurationType>Application</ConfigurationType>
  53 + <UseDebugLibraries>false</UseDebugLibraries>
  54 + <PlatformToolset>v142</PlatformToolset>
  55 + <WholeProgramOptimization>true</WholeProgramOptimization>
  56 + <CharacterSet>Unicode</CharacterSet>
  57 + <UseOfMfc>Static</UseOfMfc>
  58 + </PropertyGroup>
  59 + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  60 + <ImportGroup Label="ExtensionSettings">
  61 + </ImportGroup>
  62 + <ImportGroup Label="Shared">
  63 + </ImportGroup>
  64 + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
  65 + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  66 + <Import Project="sherpa-onnx-deps.props" />
  67 + </ImportGroup>
  68 + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
  69 + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  70 + <Import Project="sherpa-onnx-deps.props" />
  71 + </ImportGroup>
  72 + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
  73 + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  74 + <Import Project="sherpa-onnx-deps.props" />
  75 + </ImportGroup>
  76 + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
  77 + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  78 + <Import Project="sherpa-onnx-deps.props" />
  79 + </ImportGroup>
  80 + <PropertyGroup Label="UserMacros" />
  81 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
  82 + <LinkIncremental>true</LinkIncremental>
  83 + </PropertyGroup>
  84 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
  85 + <LinkIncremental>true</LinkIncremental>
  86 + </PropertyGroup>
  87 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
  88 + <LinkIncremental>false</LinkIncremental>
  89 + </PropertyGroup>
  90 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
  91 + <LinkIncremental>false</LinkIncremental>
  92 + </PropertyGroup>
  93 + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
  94 + <ClCompile>
  95 + <PrecompiledHeader>Use</PrecompiledHeader>
  96 + <WarningLevel>Level3</WarningLevel>
  97 + <SDLCheck>true</SDLCheck>
  98 + <PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  99 + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
  100 + </ClCompile>
  101 + <Link>
  102 + <SubSystem>Windows</SubSystem>
  103 + </Link>
  104 + <Midl>
  105 + <MkTypLibCompatible>false</MkTypLibCompatible>
  106 + <ValidateAllParameters>true</ValidateAllParameters>
  107 + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  108 + </Midl>
  109 + <ResourceCompile>
  110 + <Culture>0x0409</Culture>
  111 + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  112 + <AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  113 + </ResourceCompile>
  114 + </ItemDefinitionGroup>
  115 + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
  116 + <ClCompile>
  117 + <PrecompiledHeader>Use</PrecompiledHeader>
  118 + <WarningLevel>Level3</WarningLevel>
  119 + <SDLCheck>true</SDLCheck>
  120 + <PreprocessorDefinitions>_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  121 + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
  122 + </ClCompile>
  123 + <Link>
  124 + <SubSystem>Windows</SubSystem>
  125 + </Link>
  126 + <Midl>
  127 + <MkTypLibCompatible>false</MkTypLibCompatible>
  128 + <ValidateAllParameters>true</ValidateAllParameters>
  129 + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  130 + </Midl>
  131 + <ResourceCompile>
  132 + <Culture>0x0409</Culture>
  133 + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  134 + <AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  135 + </ResourceCompile>
  136 + </ItemDefinitionGroup>
  137 + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
  138 + <ClCompile>
  139 + <PrecompiledHeader>Use</PrecompiledHeader>
  140 + <WarningLevel>Level3</WarningLevel>
  141 + <FunctionLevelLinking>true</FunctionLevelLinking>
  142 + <IntrinsicFunctions>true</IntrinsicFunctions>
  143 + <SDLCheck>true</SDLCheck>
  144 + <PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  145 + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
  146 + </ClCompile>
  147 + <Link>
  148 + <SubSystem>Windows</SubSystem>
  149 + <EnableCOMDATFolding>true</EnableCOMDATFolding>
  150 + <OptimizeReferences>true</OptimizeReferences>
  151 + </Link>
  152 + <Midl>
  153 + <MkTypLibCompatible>false</MkTypLibCompatible>
  154 + <ValidateAllParameters>true</ValidateAllParameters>
  155 + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  156 + </Midl>
  157 + <ResourceCompile>
  158 + <Culture>0x0409</Culture>
  159 + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  160 + <AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  161 + </ResourceCompile>
  162 + </ItemDefinitionGroup>
  163 + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
  164 + <ClCompile>
  165 + <PrecompiledHeader>Use</PrecompiledHeader>
  166 + <WarningLevel>Level3</WarningLevel>
  167 + <FunctionLevelLinking>true</FunctionLevelLinking>
  168 + <IntrinsicFunctions>true</IntrinsicFunctions>
  169 + <SDLCheck>true</SDLCheck>
  170 + <PreprocessorDefinitions>_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  171 + <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
  172 + </ClCompile>
  173 + <Link>
  174 + <SubSystem>Windows</SubSystem>
  175 + <EnableCOMDATFolding>true</EnableCOMDATFolding>
  176 + <OptimizeReferences>true</OptimizeReferences>
  177 + </Link>
  178 + <Midl>
  179 + <MkTypLibCompatible>false</MkTypLibCompatible>
  180 + <ValidateAllParameters>true</ValidateAllParameters>
  181 + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  182 + </Midl>
  183 + <ResourceCompile>
  184 + <Culture>0x0409</Culture>
  185 + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  186 + <AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  187 + </ResourceCompile>
  188 + </ItemDefinitionGroup>
  189 + <ItemGroup>
  190 + <ClInclude Include="framework.h" />
  191 + <ClInclude Include="pch.h" />
  192 + <ClInclude Include="Resource.h" />
  193 + <ClInclude Include="StreamingSpeechRecognition.h" />
  194 + <ClInclude Include="StreamingSpeechRecognitionDlg.h" />
  195 + <ClInclude Include="targetver.h" />
  196 + </ItemGroup>
  197 + <ItemGroup>
  198 + <ClCompile Include="pch.cpp">
  199 + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
  200 + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
  201 + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
  202 + <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
  203 + </ClCompile>
  204 + <ClCompile Include="StreamingSpeechRecognition.cpp" />
  205 + <ClCompile Include="StreamingSpeechRecognitionDlg.cpp" />
  206 + </ItemGroup>
  207 + <ItemGroup>
  208 + <ResourceCompile Include="StreamingSpeechRecognition.rc" />
  209 + </ItemGroup>
  210 + <ItemGroup>
  211 + <None Include="res\StreamingSpeechRecognition.rc2" />
  212 + </ItemGroup>
  213 + <ItemGroup>
  214 + <Image Include="res\StreamingSpeechRecognition.ico" />
  215 + </ItemGroup>
  216 + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  217 + <ImportGroup Label="ExtensionTargets">
  218 + </ImportGroup>
  219 +</Project>
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3 + <ItemGroup>
  4 + <Filter Include="Source Files">
  5 + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
  6 + <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
  7 + </Filter>
  8 + <Filter Include="Header Files">
  9 + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
  10 + <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
  11 + </Filter>
  12 + <Filter Include="Resource Files">
  13 + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
  14 + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
  15 + </Filter>
  16 + </ItemGroup>
  17 + <ItemGroup>
  18 + <ClInclude Include="StreamingSpeechRecognition.h">
  19 + <Filter>Header Files</Filter>
  20 + </ClInclude>
  21 + <ClInclude Include="StreamingSpeechRecognitionDlg.h">
  22 + <Filter>Header Files</Filter>
  23 + </ClInclude>
  24 + <ClInclude Include="framework.h">
  25 + <Filter>Header Files</Filter>
  26 + </ClInclude>
  27 + <ClInclude Include="targetver.h">
  28 + <Filter>Header Files</Filter>
  29 + </ClInclude>
  30 + <ClInclude Include="Resource.h">
  31 + <Filter>Header Files</Filter>
  32 + </ClInclude>
  33 + <ClInclude Include="pch.h">
  34 + <Filter>Header Files</Filter>
  35 + </ClInclude>
  36 + </ItemGroup>
  37 + <ItemGroup>
  38 + <ClCompile Include="StreamingSpeechRecognition.cpp">
  39 + <Filter>Source Files</Filter>
  40 + </ClCompile>
  41 + <ClCompile Include="StreamingSpeechRecognitionDlg.cpp">
  42 + <Filter>Source Files</Filter>
  43 + </ClCompile>
  44 + <ClCompile Include="pch.cpp">
  45 + <Filter>Source Files</Filter>
  46 + </ClCompile>
  47 + </ItemGroup>
  48 + <ItemGroup>
  49 + <ResourceCompile Include="StreamingSpeechRecognition.rc">
  50 + <Filter>Resource Files</Filter>
  51 + </ResourceCompile>
  52 + </ItemGroup>
  53 + <ItemGroup>
  54 + <None Include="res\StreamingSpeechRecognition.rc2">
  55 + <Filter>Resource Files</Filter>
  56 + </None>
  57 + </ItemGroup>
  58 + <ItemGroup>
  59 + <Image Include="res\StreamingSpeechRecognition.ico">
  60 + <Filter>Resource Files</Filter>
  61 + </Image>
  62 + </ItemGroup>
  63 +</Project>
  1 +
  2 +// StreamingSpeechRecognitionDlg.cpp : implementation file
  3 +//
  4 +
  5 +#include "StreamingSpeechRecognitionDlg.h"
  6 +
  7 +#include <fstream>
  8 +#include <sstream>
  9 +#include <string>
  10 +#include <vector>
  11 +
  12 +#include "StreamingSpeechRecognition.h"
  13 +#include "afxdialogex.h"
  14 +#include "framework.h"
  15 +#include "pch.h"
  16 +
  17 +#ifdef _DEBUG
  18 +#define new DEBUG_NEW
  19 +#endif
  20 +
  21 +Microphone::Microphone() {
  22 + PaError err = Pa_Initialize();
  23 + if (err != paNoError) {
  24 + fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
  25 + exit(-2);
  26 + }
  27 +}
  28 +
  29 +Microphone::~Microphone() {
  30 + PaError err = Pa_Terminate();
  31 + if (err != paNoError) {
  32 + fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
  33 + exit(-2);
  34 + }
  35 +}
  36 +
  37 +// CStreamingSpeechRecognitionDlg dialog
  38 +
  39 +CStreamingSpeechRecognitionDlg::CStreamingSpeechRecognitionDlg(
  40 + CWnd *pParent /*=nullptr*/)
  41 + : CDialogEx(IDD_STREAMINGSPEECHRECOGNITION_DIALOG, pParent) {
  42 + m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  43 +}
  44 +
  45 +CStreamingSpeechRecognitionDlg::~CStreamingSpeechRecognitionDlg() {
  46 + if (recognizer_) {
  47 + DestroyOnlineRecognizer(recognizer_);
  48 + recognizer_ = nullptr;
  49 + }
  50 +}
  51 +
  52 +void CStreamingSpeechRecognitionDlg::DoDataExchange(CDataExchange *pDX) {
  53 + CDialogEx::DoDataExchange(pDX);
  54 + DDX_Control(pDX, IDOK, my_btn_);
  55 + DDX_Control(pDX, IDC_EDIT1, my_text_);
  56 +}
  57 +
  58 +BEGIN_MESSAGE_MAP(CStreamingSpeechRecognitionDlg, CDialogEx)
  59 +ON_WM_PAINT()
  60 +ON_WM_QUERYDRAGICON()
  61 +ON_BN_CLICKED(IDOK, &CStreamingSpeechRecognitionDlg::OnBnClickedOk)
  62 +END_MESSAGE_MAP()
  63 +
  64 +// CStreamingSpeechRecognitionDlg message handlers
  65 +
  66 +BOOL CStreamingSpeechRecognitionDlg::OnInitDialog() {
  67 + CDialogEx::OnInitDialog();
  68 +
  69 + // Set the icon for this dialog. The framework does this automatically
  70 + // when the application's main window is not a dialog
  71 + SetIcon(m_hIcon, TRUE); // Set big icon
  72 + SetIcon(m_hIcon, FALSE); // Set small icon
  73 +
  74 + // TODO: Add extra initialization here
  75 + SetWindowText(_T("Real-time speech recogntion with Next-gen Kaldi"));
  76 + InitMicrophone();
  77 +
  78 + return TRUE; // return TRUE unless you set the focus to a control
  79 +}
  80 +
  81 +// If you add a minimize button to your dialog, you will need the code below
  82 +// to draw the icon. For MFC applications using the document/view model,
  83 +// this is automatically done for you by the framework.
  84 +
  85 +void CStreamingSpeechRecognitionDlg::OnPaint() {
  86 + if (IsIconic()) {
  87 + CPaintDC dc(this); // device context for painting
  88 +
  89 + SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()),
  90 + 0);
  91 +
  92 + // Center icon in client rectangle
  93 + int cxIcon = GetSystemMetrics(SM_CXICON);
  94 + int cyIcon = GetSystemMetrics(SM_CYICON);
  95 + CRect rect;
  96 + GetClientRect(&rect);
  97 + int x = (rect.Width() - cxIcon + 1) / 2;
  98 + int y = (rect.Height() - cyIcon + 1) / 2;
  99 +
  100 + // Draw the icon
  101 + dc.DrawIcon(x, y, m_hIcon);
  102 + } else {
  103 + CDialogEx::OnPaint();
  104 + }
  105 +}
  106 +
  107 +// The system calls this function to obtain the cursor to display while the user
  108 +// drags
  109 +// the minimized window.
  110 +HCURSOR CStreamingSpeechRecognitionDlg::OnQueryDragIcon() {
  111 + return static_cast<HCURSOR>(m_hIcon);
  112 +}
  113 +
  114 +static int32_t RecordCallback(const void *input_buffer,
  115 + void * /*output_buffer*/,
  116 + unsigned long frames_per_buffer, // NOLINT
  117 + const PaStreamCallbackTimeInfo * /*time_info*/,
  118 + PaStreamCallbackFlags /*status_flags*/,
  119 + void *user_data) {
  120 + auto dlg = reinterpret_cast<CStreamingSpeechRecognitionDlg *>(user_data);
  121 +
  122 + auto stream = dlg->stream_;
  123 + if (stream) {
  124 + AcceptWaveform(stream, 16000, reinterpret_cast<const float *>(input_buffer),
  125 + frames_per_buffer);
  126 + }
  127 +
  128 + return dlg->started_ ? paContinue : paComplete;
  129 +}
  130 +
  131 +void CStreamingSpeechRecognitionDlg::OnBnClickedOk() {
  132 + if (!recognizer_) {
  133 + AppendLineToMultilineEditCtrl("Creating recognizer...");
  134 + InitRecognizer();
  135 + if (!recognizer_) {
  136 + // failed to create the recognizer
  137 + return;
  138 + }
  139 + AppendLineToMultilineEditCtrl("Recognizer created!");
  140 + }
  141 +
  142 + if (!started_) {
  143 + started_ = true;
  144 +
  145 + if (stream_) {
  146 + DestroyOnlineStream(stream_);
  147 + stream_ = nullptr;
  148 + }
  149 +
  150 + stream_ = CreateOnlineStream(recognizer_);
  151 +
  152 + PaStreamParameters param;
  153 + param.device = Pa_GetDefaultInputDevice();
  154 + const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device);
  155 + param.channelCount = 1;
  156 + param.sampleFormat = paFloat32;
  157 + param.suggestedLatency = info->defaultLowInputLatency;
  158 + param.hostApiSpecificStreamInfo = nullptr;
  159 + float sample_rate = 16000;
  160 + pa_stream_ = nullptr;
  161 + PaError err =
  162 + Pa_OpenStream(&pa_stream_, &param, nullptr, /* &outputParameters, */
  163 + sample_rate,
  164 + 0, // frames per buffer
  165 + paClipOff, // we won't output out of range samples
  166 + // so don't bother clipping them
  167 + RecordCallback, this);
  168 + if (err != paNoError) {
  169 + AppendLineToMultilineEditCtrl(std::string("PortAudio error: ") +
  170 + Pa_GetErrorText(err));
  171 + my_btn_.EnableWindow(FALSE);
  172 + return;
  173 + }
  174 +
  175 + err = Pa_StartStream(pa_stream_);
  176 + if (err != paNoError) {
  177 + AppendLineToMultilineEditCtrl(std::string("PortAudio error: ") +
  178 + Pa_GetErrorText(err));
  179 + my_btn_.EnableWindow(FALSE);
  180 + return;
  181 + }
  182 + AppendLineToMultilineEditCtrl("Started! Please speak");
  183 + my_btn_.SetWindowText(_T("Stop"));
  184 +
  185 + thread_ = new RecognizerThread(this);
  186 + thread_->CreateThread(CREATE_SUSPENDED);
  187 + thread_->m_bAutoDelete = false; // Let me delete it.
  188 + thread_->ResumeThread();
  189 + } else {
  190 + started_ = false;
  191 + Pa_Sleep(200); // sleep for 200ms
  192 + if (pa_stream_) {
  193 + PaError err = Pa_CloseStream(pa_stream_);
  194 + if (err != paNoError) {
  195 + AppendLineToMultilineEditCtrl(std::string("PortAudio error: ") +
  196 + Pa_GetErrorText(err));
  197 + my_btn_.EnableWindow(FALSE);
  198 + return;
  199 + }
  200 + }
  201 + pa_stream_ = nullptr;
  202 +
  203 + WaitForSingleObject(thread_->m_hThread, INFINITE);
  204 + delete thread_;
  205 + thread_ = nullptr;
  206 +
  207 + // AfxMessageBox("stopped", MB_OK);
  208 + my_btn_.SetWindowText(_T("Start"));
  209 + AppendLineToMultilineEditCtrl("Stopped");
  210 + }
  211 +}
  212 +
  213 +void CStreamingSpeechRecognitionDlg::InitMicrophone() {
  214 + int default_device = Pa_GetDefaultInputDevice();
  215 + int device_count = Pa_GetDeviceCount();
  216 + if (default_device == paNoDevice) {
  217 + // CString str;
  218 + // str.Format(_T("No default input device found!"));
  219 + // AfxMessageBox(str, MB_OK | MB_ICONSTOP);
  220 + // exit(-1);
  221 + AppendLineToMultilineEditCtrl("No default input device found!");
  222 + my_btn_.EnableWindow(FALSE);
  223 + }
  224 + AppendLineToMultilineEditCtrl(std::string("Selected device ") +
  225 + Pa_GetDeviceInfo(default_device)->name);
  226 +}
  227 +
  228 +bool CStreamingSpeechRecognitionDlg::Exists(const std::string &filename) {
  229 + std::ifstream is(filename);
  230 + return is.good();
  231 +}
  232 +
  233 +void CStreamingSpeechRecognitionDlg::InitRecognizer() {
  234 + std::string encoder = "./encoder.onnx";
  235 + std::string decoder = "./decoder.onnx";
  236 + std::string joiner = "./joiner.onnx";
  237 + std::string tokens = "./tokens.txt";
  238 +#if 1
  239 + std::string prefix =
  240 + "C:/Users/fangjun/source/repos/MFCApplication2/x64/Release";
  241 + encoder = prefix + encoder;
  242 + decoder = prefix + decoder;
  243 + joiner = prefix + joiner;
  244 + tokens = prefix + tokens;
  245 +#endif
  246 +
  247 + bool is_ok = true;
  248 + if (!Exists(encoder)) {
  249 + std::string msg = encoder + " does not exist!";
  250 + AppendLineToMultilineEditCtrl(msg);
  251 + is_ok = false;
  252 + }
  253 +
  254 + if (!Exists(decoder)) {
  255 + std::string msg = decoder + " does not exist!";
  256 + AppendLineToMultilineEditCtrl(msg);
  257 + is_ok = false;
  258 + }
  259 +
  260 + if (!Exists(joiner)) {
  261 + std::string msg = joiner + " does not exist!";
  262 + AppendLineToMultilineEditCtrl(msg);
  263 + is_ok = false;
  264 + }
  265 +
  266 + if (!Exists(tokens)) {
  267 + std::string msg = tokens + " does not exist!";
  268 + AppendLineToMultilineEditCtrl(msg);
  269 + is_ok = false;
  270 + }
  271 +
  272 + if (!is_ok) {
  273 + my_btn_.EnableWindow(FALSE);
  274 + std::string msg =
  275 + "\r\nPlease go to "
  276 + "https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html "
  277 + "\r\n";
  278 + msg += "to download an offline model.";
  279 + msg +=
  280 + " You need to rename them to encoder.onnx, decoder.onnx, and "
  281 + "joiner.onnx correspoondingly";
  282 + AppendLineToMultilineEditCtrl(msg);
  283 + return;
  284 + }
  285 +
  286 + SherpaOnnxOnlineRecognizerConfig config;
  287 + config.model_config.debug = 0;
  288 + config.model_config.num_threads = 2;
  289 + config.model_config.provider = "cpu";
  290 +
  291 + config.decoding_method = "greedy_search";
  292 + config.max_active_paths = 4;
  293 +
  294 + config.feat_config.sample_rate = 16000;
  295 + config.feat_config.feature_dim = 80;
  296 +
  297 + config.enable_endpoint = 1;
  298 + config.rule1_min_trailing_silence = 1.2f;
  299 + config.rule2_min_trailing_silence = 0.8f;
  300 + config.rule3_min_utterance_length = 300.0f;
  301 +
  302 + config.model_config.tokens = tokens.c_str();
  303 + config.model_config.encoder = encoder.c_str();
  304 + config.model_config.decoder = decoder.c_str();
  305 + config.model_config.joiner = joiner.c_str();
  306 +
  307 + recognizer_ = CreateOnlineRecognizer(&config);
  308 +}
  309 +
  310 +// see
  311 +// https://stackoverflow.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring
  312 +std::wstring Utf8ToUtf16(const std::string &utf8) {
  313 + std::vector<unsigned long> unicode;
  314 + size_t i = 0;
  315 + while (i < utf8.size()) {
  316 + unsigned long uni;
  317 + size_t todo;
  318 + bool error = false;
  319 + unsigned char ch = utf8[i++];
  320 + if (ch <= 0x7F) {
  321 + uni = ch;
  322 + todo = 0;
  323 + } else if (ch <= 0xBF) {
  324 + throw std::logic_error("not a UTF-8 string");
  325 + } else if (ch <= 0xDF) {
  326 + uni = ch & 0x1F;
  327 + todo = 1;
  328 + } else if (ch <= 0xEF) {
  329 + uni = ch & 0x0F;
  330 + todo = 2;
  331 + } else if (ch <= 0xF7) {
  332 + uni = ch & 0x07;
  333 + todo = 3;
  334 + } else {
  335 + throw std::logic_error("not a UTF-8 string");
  336 + }
  337 + for (size_t j = 0; j < todo; ++j) {
  338 + if (i == utf8.size()) throw std::logic_error("not a UTF-8 string");
  339 + unsigned char ch = utf8[i++];
  340 + if (ch < 0x80 || ch > 0xBF) throw std::logic_error("not a UTF-8 string");
  341 + uni <<= 6;
  342 + uni += ch & 0x3F;
  343 + }
  344 + if (uni >= 0xD800 && uni <= 0xDFFF)
  345 + throw std::logic_error("not a UTF-8 string");
  346 + if (uni > 0x10FFFF) throw std::logic_error("not a UTF-8 string");
  347 + unicode.push_back(uni);
  348 + }
  349 + std::wstring utf16;
  350 + for (size_t i = 0; i < unicode.size(); ++i) {
  351 + unsigned long uni = unicode[i];
  352 + if (uni <= 0xFFFF) {
  353 + utf16 += (wchar_t)uni;
  354 + } else {
  355 + uni -= 0x10000;
  356 + utf16 += (wchar_t)((uni >> 10) + 0xD800);
  357 + utf16 += (wchar_t)((uni & 0x3FF) + 0xDC00);
  358 + }
  359 + }
  360 + return utf16;
  361 +}
  362 +
  363 +void CStreamingSpeechRecognitionDlg::AppendTextToEditCtrl(
  364 + const std::string &s) {
  365 + // get the initial text length
  366 + int nLength = my_text_.GetWindowTextLength();
  367 + // put the selection at the end of text
  368 + my_text_.SetSel(nLength, nLength);
  369 + // replace the selection
  370 + CString str;
  371 + str.Format(_T("%s"), s.c_str());
  372 +
  373 + std::wstring wstr = Utf8ToUtf16(s);
  374 +
  375 + // my_text_.ReplaceSel(wstr.c_str());
  376 + my_text_.ReplaceSel(wstr.c_str());
  377 +}
  378 +
  379 +void CStreamingSpeechRecognitionDlg::AppendLineToMultilineEditCtrl(
  380 + const std::string &s) {
  381 + AppendTextToEditCtrl("\r\n" + s);
  382 +}
  383 +
  384 +static std::string Cat(const std::vector<std::string> &results,
  385 + const std::string &s) {
  386 + std::ostringstream os;
  387 + std::string sep;
  388 +
  389 + int i = 0;
  390 + for (i = 0; i != results.size(); ++i) {
  391 + os << sep << i << ": " << results[i];
  392 + sep = "\r\n";
  393 + }
  394 +
  395 + if (!s.empty()) {
  396 + os << sep << i << ": " << s;
  397 + }
  398 + return os.str();
  399 +}
  400 +
  401 +int CStreamingSpeechRecognitionDlg::RunThread() {
  402 + std::vector<std::string> results;
  403 + std::string last_text;
  404 + while (started_) {
  405 + while (IsOnlineStreamReady(recognizer_, stream_)) {
  406 + DecodeOnlineStream(recognizer_, stream_);
  407 + }
  408 +
  409 + auto r = GetOnlineStreamResult(recognizer_, stream_);
  410 + std::string text = r->text;
  411 + DestroyOnlineRecognizerResult(r);
  412 + if (!text.empty() && last_text != text) {
  413 + // CString str;
  414 + // str.Format(_T("%s"), Cat(results, text).c_str());
  415 + auto str = Utf8ToUtf16(Cat(results, text).c_str());
  416 + my_text_.SetWindowText(str.c_str());
  417 + last_text = text;
  418 + }
  419 + int is_endpoint = IsEndpoint(recognizer_, stream_);
  420 + if (is_endpoint) {
  421 + Reset(recognizer_, stream_);
  422 + if (!text.empty()) {
  423 + results.push_back(std::move(text));
  424 + }
  425 + }
  426 +
  427 + Pa_Sleep(100); // sleep for 100ms
  428 + }
  429 +
  430 + return 0;
  431 +}
  1 +
  2 +// StreamingSpeechRecognitionDlg.h : header file
  3 +//
  4 +
  5 +#pragma once
  6 +
  7 +#include <string>
  8 +
  9 +#include "portaudio.h"
  10 +#include "sherpa-onnx/c-api/c-api.h"
  11 +
  12 +class Microphone {
  13 + public:
  14 + Microphone();
  15 + ~Microphone();
  16 +};
  17 +
  18 +class RecognizerThread;
  19 +
  20 +// CStreamingSpeechRecognitionDlg dialog
  21 +class CStreamingSpeechRecognitionDlg : public CDialogEx {
  22 + // Construction
  23 + public:
  24 + CStreamingSpeechRecognitionDlg(
  25 + CWnd *pParent = nullptr); // standard constructor
  26 + ~CStreamingSpeechRecognitionDlg();
  27 +
  28 +// Dialog Data
  29 +#ifdef AFX_DESIGN_TIME
  30 + enum { IDD = IDD_STREAMINGSPEECHRECOGNITION_DIALOG };
  31 +#endif
  32 +
  33 + protected:
  34 + virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support
  35 +
  36 + // Implementation
  37 + protected:
  38 + HICON m_hIcon;
  39 +
  40 + // Generated message map functions
  41 + virtual BOOL OnInitDialog();
  42 + afx_msg void OnPaint();
  43 + afx_msg HCURSOR OnQueryDragIcon();
  44 + DECLARE_MESSAGE_MAP()
  45 + private:
  46 + Microphone mic_;
  47 +
  48 + SherpaOnnxOnlineRecognizer *recognizer_ = nullptr;
  49 +
  50 + PaStream *pa_stream_ = nullptr;
  51 + RecognizerThread *thread_;
  52 + CButton my_btn_;
  53 + CEdit my_text_;
  54 +
  55 + public:
  56 + bool started_ = false;
  57 + SherpaOnnxOnlineStream *stream_ = nullptr;
  58 +
  59 + public:
  60 + int RunThread();
  61 + afx_msg void OnBnClickedOk();
  62 +
  63 + private:
  64 + void AppendTextToEditCtrl(const std::string &s);
  65 + void AppendLineToMultilineEditCtrl(const std::string &s);
  66 + void InitMicrophone();
  67 +
  68 + bool Exists(const std::string &filename);
  69 + void InitRecognizer();
  70 +};
  71 +
  72 +class RecognizerThread : public CWinThread {
  73 + public:
  74 + RecognizerThread(CStreamingSpeechRecognitionDlg *dlg) : dlg_(dlg) {}
  75 + virtual BOOL InitInstance() { return TRUE; }
  76 + virtual int Run() { return dlg_->RunThread(); }
  77 +
  78 + private:
  79 + CStreamingSpeechRecognitionDlg *dlg_;
  80 +};
  1 +#pragma once
  2 +
  3 +#ifndef VC_EXTRALEAN
  4 +#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
  5 +#endif
  6 +
  7 +#include "targetver.h"
  8 +
  9 +#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be
  10 + // explicit
  11 +
  12 +// turns off MFC's hiding of some common and often safely ignored warning
  13 +// messages
  14 +#define _AFX_ALL_WARNINGS
  15 +
  16 +#include <afxext.h> // MFC extensions
  17 +#include <afxwin.h> // MFC core and standard components
  18 +
  19 +#ifndef _AFX_NO_OLE_SUPPORT
  20 +#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
  21 +#endif
  22 +#ifndef _AFX_NO_AFXCMN_SUPPORT
  23 +#include <afxcmn.h> // MFC support for Windows Common Controls
  24 +#endif // _AFX_NO_AFXCMN_SUPPORT
  25 +
  26 +#include <afxcontrolbars.h> // MFC support for ribbons and control bars
  1 +// pch.cpp: source file corresponding to the pre-compiled header
  2 +
  3 +#include "pch.h"
  4 +
  5 +// When you are using pre-compiled headers, this source file is necessary for
  6 +// compilation to succeed.
  1 +// pch.h: This is a precompiled header file.
  2 +// Files listed below are compiled only once, improving build performance for
  3 +// future builds. This also affects IntelliSense performance, including code
  4 +// completion and many code browsing features. However, files listed here are
  5 +// ALL re-compiled if any one of them is updated between builds. Do not add
  6 +// files here that you will be updating frequently as this negates the
  7 +// performance advantage.
  8 +
  9 +#ifndef PCH_H
  10 +#define PCH_H
  11 +
  12 +// add headers that you want to pre-compile here
  13 +#include "framework.h"
  14 +
  15 +#endif // PCH_H
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3 + <ImportGroup Label="PropertySheets" />
  4 + <PropertyGroup Label="UserMacros" />
  5 + <PropertyGroup>
  6 + <SherpaOnnxBuildDirectory>..\..\build</SherpaOnnxBuildDirectory>
  7 + <SherpaOnnxInstallDirectory>..\..\build\install</SherpaOnnxInstallDirectory>
  8 + <SherpaOnnxLibraries>sherpa-onnx-portaudio.lib;sherpa-onnx-c-api.lib;sherpa-onnx-core.lib</SherpaOnnxLibraries>
  9 + </PropertyGroup>
  10 + <ItemDefinitionGroup>
  11 + <ClCompile>
  12 + <AdditionalIncludeDirectories>
  13 + $(SherpaOnnxBuildDirectory)\_deps\portaudio-src\include;$(SherpaOnnxInstallDirectory)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  14 + </ClCompile>
  15 + <Link>
  16 + <AdditionalLibraryDirectories>$(SherpaOnnxInstallDirectory)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
  17 + <AdditionalDependencies>$(SherpaOnnxLibraries)</AdditionalDependencies>
  18 + </Link>
  19 + </ItemDefinitionGroup>
  20 + <ItemGroup />
  21 +</Project>
  1 +#pragma once
  2 +
  3 +// Including SDKDDKVer.h defines the highest available Windows platform.
  4 +
  5 +// If you wish to build your application for a previous Windows platform,
  6 +// include WinSDKVer.h and set the _WIN32_WINNT macro to the platform you wish
  7 +// to support before including SDKDDKVer.h.
  8 +
  9 +#include <SDKDDKVer.h>
  1 +
  2 +Microsoft Visual Studio Solution File, Format Version 12.00
  3 +# Visual Studio Version 16
  4 +VisualStudioVersion = 16.0.32630.194
  5 +MinimumVisualStudioVersion = 10.0.40219.1
  6 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StreamingSpeechRecognition", "StreamingSpeechRecognition\StreamingSpeechRecognition.vcxproj", "{A79C2604-C33D-497C-9770-D34E118B77FE}"
  7 +EndProject
  8 +Global
  9 + GlobalSection(SolutionConfigurationPlatforms) = preSolution
  10 + Debug|x64 = Debug|x64
  11 + Debug|x86 = Debug|x86
  12 + Release|x64 = Release|x64
  13 + Release|x86 = Release|x86
  14 + EndGlobalSection
  15 + GlobalSection(ProjectConfigurationPlatforms) = postSolution
  16 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Debug|x64.ActiveCfg = Debug|x64
  17 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Debug|x64.Build.0 = Debug|x64
  18 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Debug|x86.ActiveCfg = Debug|Win32
  19 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Debug|x86.Build.0 = Debug|Win32
  20 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Release|x64.ActiveCfg = Release|x64
  21 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Release|x64.Build.0 = Release|x64
  22 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Release|x86.ActiveCfg = Release|Win32
  23 + {A79C2604-C33D-497C-9770-D34E118B77FE}.Release|x86.Build.0 = Release|Win32
  24 + EndGlobalSection
  25 + GlobalSection(SolutionProperties) = preSolution
  26 + HideSolutionNode = FALSE
  27 + EndGlobalSection
  28 + GlobalSection(ExtensibilityGlobals) = postSolution
  29 + SolutionGuid = {C0A85719-CF8C-4BCD-BDF6-7C57EE651CBB}
  30 + EndGlobalSection
  31 +EndGlobal