-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_snowboy_mb_test_8cc-example.html
77 lines (75 loc) · 17.8 KB
/
file_snowboy_mb_test_8cc-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.15"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>librespeaker: file_snowboy_mb_test.cc</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">librespeaker
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.15 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">file_snowboy_mb_test.cc</div> </div>
</div><!--header-->
<div class="contents">
<p>This is an exmaple that shows how to read a 8 channels wav file, send the recording stream to <code>VepAecBeamformingNode</code> node and detect hotword with <code>SnowboyMbDoaKwsNode</code> from the output beam.</p>
<div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include <cstring></span></div><div class="line"><span class="preprocessor">#include <memory></span></div><div class="line"><span class="preprocessor">#include <iostream></span></div><div class="line"><span class="preprocessor">#include <csignal></span></div><div class="line"><span class="preprocessor">#include <chrono></span></div><div class="line"><span class="preprocessor">#include <thread></span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <respeaker.h></span></div><div class="line"><span class="preprocessor">#include <chain_nodes/file_collector_node.h></span></div><div class="line"><span class="preprocessor">#include <chain_nodes/vep_aec_beamforming_node.h></span></div><div class="line"><span class="preprocessor">#include <chain_nodes/snowboy_mb_doa_kws_node.h></span></div><div class="line"></div><div class="line"><span class="keyword">extern</span> <span class="stringliteral">"C"</span></div><div class="line">{</div><div class="line"><span class="preprocessor">#include <sndfile.h></span></div><div class="line"><span class="preprocessor">#include <unistd.h></span></div><div class="line"><span class="preprocessor">#include <getopt.h></span></div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacerespeaker.html">respeaker</a>;</div><div class="line"></div><div class="line"><span class="preprocessor">#define BLOCK_SIZE_MS 8</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">bool</span> stop = <span class="keyword">false</span>;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> SignalHandler(<span class="keywordtype">int</span> signal){</div><div class="line"> cerr << <span class="stringliteral">"Caught signal "</span> << signal << <span class="stringliteral">", terminating..."</span> << endl;</div><div class="line"> stop = <span class="keyword">true</span>;</div><div class="line"> <span class="comment">//maintain the main thread untill the worker thread released its resource</span></div><div class="line"> <span class="comment">//std::this_thread::sleep_for(std::chrono::seconds(1));</span></div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help(<span class="keyword">const</span> <span class="keywordtype">char</span> *argv0) {</div><div class="line"> cout << <span class="stringliteral">"file_snowboy_mb_test [options]"</span> << endl;</div><div class="line"> cout << <span class="stringliteral">"This example only support CIRCULAR_6MIC_7BEAM microphone for now, read 8channel wav data and output 6+1 beam data. "</span> << endl << endl;</div><div class="line"> cout << <span class="stringliteral">" -h, --help Show this help"</span> << endl;</div><div class="line"> cout << <span class="stringliteral">" -f, --file=INPUT_FILE_NAME The input audio wav file"</span> << endl;</div><div class="line"> cout << <span class="stringliteral">" -k, --kws=KWS_NAME The keyword name: snowboy or alexa"</span> << endl;</div><div class="line"> cout << <span class="stringliteral">" -g, --agc=NEGTIVE INTEGER The target gain level of output, [-31, 0]"</span> << endl;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {</div><div class="line"></div><div class="line"> <span class="comment">// Configures signal handling.</span></div><div class="line"> <span class="keyword">struct </span>sigaction sig_int_handler;</div><div class="line"> sig_int_handler.sa_handler = SignalHandler;</div><div class="line"> sigemptyset(&sig_int_handler.sa_mask);</div><div class="line"> sig_int_handler.sa_flags = 0;</div><div class="line"> sigaction(SIGINT, &sig_int_handler, NULL);</div><div class="line"> sigaction(SIGTERM, &sig_int_handler, NULL);</div><div class="line"></div><div class="line"> <span class="comment">// parse opts</span></div><div class="line"> <span class="keywordtype">int</span> c;</div><div class="line"> <span class="keywordtype">string</span> file_path, kws;</div><div class="line"> <span class="keywordtype">bool</span> agc_enable = <span class="keyword">false</span>;</div><div class="line"> <span class="keywordtype">int</span> agc_level = 10;</div><div class="line"></div><div class="line"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">struct </span>option long_options[] = {</div><div class="line"> {<span class="stringliteral">"help"</span>, 0, NULL, <span class="charliteral">'h'</span>},</div><div class="line"> {<span class="stringliteral">"file"</span>, 1, NULL, <span class="charliteral">'f'</span>},</div><div class="line"> {<span class="stringliteral">"kws"</span>, 1, NULL, <span class="charliteral">'k'</span>},</div><div class="line"> {<span class="stringliteral">"agc"</span>, 1, NULL, <span class="charliteral">'g'</span>},</div><div class="line"> {NULL, 0, NULL, 0}</div><div class="line"> };</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span> ((c = getopt_long(argc, argv, <span class="stringliteral">"f:k:g:h"</span>, long_options, NULL)) != -1) {</div><div class="line"></div><div class="line"> <span class="keywordflow">switch</span> (c) {</div><div class="line"> <span class="keywordflow">case</span> <span class="charliteral">'h'</span> :</div><div class="line"> help(argv[0]);</div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line"> <span class="keywordflow">case</span> <span class="charliteral">'f'</span>:</div><div class="line"> file_path = string(optarg);</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> <span class="charliteral">'k'</span>:</div><div class="line"> kws = string(optarg);</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> <span class="charliteral">'g'</span>:</div><div class="line"> agc_enable = <span class="keyword">true</span>;</div><div class="line"> agc_level = stoi(optarg);</div><div class="line"> <span class="keywordflow">if</span> ((agc_level > 31) || (agc_level < -31)) agc_level = 31;</div><div class="line"> <span class="keywordflow">if</span> (agc_level < 0) agc_level = (0-agc_level);</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">default</span>:</div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"></div><div class="line"> unique_ptr<FileCollectorNode> collector;</div><div class="line"> unique_ptr<VepAecBeamformingNode> vep_bf;</div><div class="line"> unique_ptr<SnowboyMbDoaKwsNode> vep_kws;</div><div class="line"> unique_ptr<ReSpeaker> <a class="code" href="namespacerespeaker.html">respeaker</a>;</div><div class="line"></div><div class="line"> collector.reset(FileCollectorNode::Create(file_path, BLOCK_SIZE_MS));</div><div class="line"> vep_bf.reset(VepAecBeamformingNode::Create(CIRCULAR_6MIC_7BEAM,<span class="keyword">false</span>,6,<span class="keyword">true</span>));</div><div class="line"> <span class="keywordflow">if</span> (kws == <span class="stringliteral">"snowboy"</span>) {</div><div class="line"> cout << <span class="stringliteral">"using snowboy kws"</span> << endl;</div><div class="line"> vep_kws.reset(SnowboyMbDoaKwsNode::Create(<span class="stringliteral">"/usr/share/respeaker/snowboy/resources/common.res"</span>,</div><div class="line"> <span class="stringliteral">"/usr/share/respeaker/snowboy/resources//snowboy.umdl"</span>,</div><div class="line"> <span class="stringliteral">"0.5"</span>,</div><div class="line"> 10,</div><div class="line"> agc_enable,</div><div class="line"> <span class="keyword">false</span>));</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> cout << <span class="stringliteral">"using alexa kws"</span> << endl;</div><div class="line"> vep_kws.reset(SnowboyMbDoaKwsNode::Create(<span class="stringliteral">"/usr/share/respeaker/snowboy/resources/common.res"</span>,</div><div class="line"> <span class="stringliteral">"/usr/share/respeaker/snowboy/resources/alexa.umdl"</span>,</div><div class="line"> <span class="stringliteral">"0.5"</span>,</div><div class="line"> 10,</div><div class="line"> agc_enable,</div><div class="line"> <span class="keyword">false</span>));</div><div class="line"> }</div><div class="line"></div><div class="line"></div><div class="line"> vep_kws->DisableAutoStateTransfer();</div><div class="line"> <span class="keywordflow">if</span>(agc_enable) {</div><div class="line"> vep_kws->SetAgcTargetLevelDbfs(agc_level);</div><div class="line"> cout << <span class="stringliteral">"AGC = -"</span><< agc_level<< endl;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> cout << <span class="stringliteral">"Disable AGC"</span> << endl;</div><div class="line"> }</div><div class="line"> vep_bf->Uplink(collector.get());</div><div class="line"> vep_kws->Uplink(vep_bf.get());</div><div class="line"></div><div class="line"> <a class="code" href="namespacerespeaker.html">respeaker</a>.reset(ReSpeaker::Create());</div><div class="line"> <a class="code" href="namespacerespeaker.html">respeaker</a>->RegisterChainByHead(collector.get());</div><div class="line"> <a class="code" href="namespacerespeaker.html">respeaker</a>->RegisterOutputNode(vep_kws.get());</div><div class="line"></div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="namespacerespeaker.html">respeaker</a>->Start(&stop)) {</div><div class="line"> cout << <span class="stringliteral">"Can not start the respeaker node chain."</span> << endl;</div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordtype">string</span> data;</div><div class="line"> <span class="keywordtype">int</span> frames;</div><div class="line"> <span class="keywordtype">size_t</span> num_channels = <a class="code" href="namespacerespeaker.html">respeaker</a>->GetNumOutputChannels();</div><div class="line"> <span class="keywordtype">int</span> rate = <a class="code" href="namespacerespeaker.html">respeaker</a>->GetNumOutputRate();</div><div class="line"></div><div class="line"> cout << <span class="stringliteral">"num channels: "</span> << num_channels << <span class="stringliteral">", rate: "</span> << rate << endl;</div><div class="line"></div><div class="line"> <span class="comment">// init libsndfile</span></div><div class="line"> SNDFILE *file ;</div><div class="line"> SF_INFO sfinfo ;</div><div class="line"></div><div class="line"> memset (&sfinfo, 0, <span class="keyword">sizeof</span> (sfinfo));</div><div class="line"> sfinfo.samplerate = rate ;</div><div class="line"> sfinfo.channels = num_channels ;</div><div class="line"> sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;</div><div class="line"> <span class="keywordflow">if</span> (! (file = sf_open (<span class="stringliteral">"file_snowboy_mb_test.wav"</span>, SFM_WRITE, &sfinfo)))</div><div class="line"> {</div><div class="line"> cout << <span class="stringliteral">"Error : Not able to open output file."</span> << endl;</div><div class="line"> <span class="keywordflow">return</span> -1 ;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span> (!stop)</div><div class="line"> {</div><div class="line"> data = <a class="code" href="namespacerespeaker.html">respeaker</a>->Listen();</div><div class="line"> frames = data.length() / (<span class="keyword">sizeof</span>(int16_t) * num_channels);</div><div class="line"> sf_writef_short(file, (<span class="keyword">const</span> int16_t *)(data.data()), frames);</div><div class="line"> cout << <span class="stringliteral">"."</span> << flush;</div><div class="line"> }</div><div class="line"></div><div class="line"> cout << <span class="stringliteral">"stopping the respeaker worker thread..."</span> << endl;</div><div class="line"></div><div class="line"> <a class="code" href="namespacerespeaker.html">respeaker</a>->Stop();</div><div class="line"></div><div class="line"> cout << <span class="stringliteral">"cleanup done."</span> << endl;</div><div class="line"></div><div class="line"> sf_close (file);</div><div class="line"></div><div class="line"> cout << <span class="stringliteral">"wav file closed."</span> << endl;</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.15
</small></address>
</body>
</html>