-
Notifications
You must be signed in to change notification settings - Fork 1
/
alsa_aloop_test_8cc-example.html
78 lines (76 loc) · 26 KB
/
alsa_aloop_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
78
<!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: alsa_aloop_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">alsa_aloop_test.cc</div> </div>
</div><!--header-->
<div class="contents">
<p>This is an example that shows how to redirect the processed audio stream into a specific Alsa device(Loopback PCM). To run this example, you have to run 'sudo modprobe snd-aloop' first. And make sure "pulseaudio" doesn't start, then <br />
you can use "arecord -Dhw:Loopback,1,0 -c 1 -r 16000 -f S16_LE loop_test.wav" to arecord the processed audio stream. Further more, you can setup a third party voice assistant to capture voice from "hw:Loopback,1,0", to run the assistant directly.</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/alsa_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_1b_doa_kws_node.h></span></div><div class="line"><span class="preprocessor">#include <chain_nodes/snips_1b_doa_kws_node.h></span></div><div class="line"><span class="preprocessor">#include <chain_nodes/aloop_output_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">"alsa_aloop_test [options]"</span> << endl;</div><div class="line"> cout << <span class="stringliteral">"A demo application for librespeaker."</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">" -s, --source=SOURCE_NAME The source (microphone) to connect to"</span> << endl;</div><div class="line"> cout << <span class="stringliteral">" -t, --type=MIC_TYPE The MICROPHONE TYPE, support: CIRCULAR_6MIC_7BEAM, LINEAR_6MIC_8BEAM, LINEAR_4MIC_1BEAM, CIRCULAR_4MIC_9BEAM"</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"> cout << <span class="stringliteral">" -w, --wav Enable output wav log, default is false."</span> << endl;</div><div class="line"> cout << <span class="stringliteral">" -k, --kws=KWS_NAME The keyword name: snowboy or alexa or heysnips, default is snowboy"</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> source = <span class="stringliteral">"default"</span>;</div><div class="line"></div><div class="line"> <span class="keywordtype">bool</span> enable_agc = <span class="keyword">false</span>;</div><div class="line"> <span class="keywordtype">bool</span> enable_wav = <span class="keyword">false</span>;</div><div class="line"> <span class="keywordtype">int</span> agc_level = 10;</div><div class="line"> <span class="keywordtype">string</span> mic_type, kws;</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">"source"</span>, 1, NULL, <span class="charliteral">'s'</span>},</div><div class="line"> {<span class="stringliteral">"kws"</span>, 1, NULL, <span class="charliteral">'k'</span>},</div><div class="line"> {<span class="stringliteral">"type"</span>, 1, NULL, <span class="charliteral">'t'</span>},</div><div class="line"> {<span class="stringliteral">"agc"</span>, 1, NULL, <span class="charliteral">'g'</span>},</div><div class="line"> {<span class="stringliteral">"wav"</span>, 0, NULL, <span class="charliteral">'w'</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">"k:t:g:s:hw"</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">'s'</span>:</div><div class="line"> source = 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">'t'</span>:</div><div class="line"> mic_type = 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"> enable_agc = <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">case</span> <span class="charliteral">'w'</span>:</div><div class="line"> enable_wav = <span class="keyword">true</span>;</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<AlsaCollectorNode> collector;</div><div class="line"> unique_ptr<VepAecBeamformingNode> vep_1beam;</div><div class="line"> unique_ptr<Snowboy1bDoaKwsNode> snowboy_kws;</div><div class="line"> unique_ptr<Snips1bDoaKwsNode> snips_kws;</div><div class="line"> unique_ptr<AloopOutputNode> aloop;</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(AlsaCollectorNode::Create(source, 48000, 8, <span class="keyword">false</span>));</div><div class="line"> vep_1beam.reset(VepAecBeamformingNode::Create(StringToMicType(mic_type), <span class="keyword">true</span>, 6, enable_wav));</div><div class="line"> <span class="comment">// run "sudo modprobe snd-aloop" to enable aloop module</span></div><div class="line"> aloop.reset(AloopOutputNode::Create(<span class="stringliteral">"hw:Loopback,0,0"</span>,<span class="keyword">true</span>));</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (kws == <span class="stringliteral">"heysnips"</span>) {</div><div class="line"> cout << <span class="stringliteral">"using hey-snips kws"</span> << endl;</div><div class="line"> snips_kws.reset(Snips1bDoaKwsNode::Create(<span class="stringliteral">"/usr/share/respeaker/snips/model"</span>,</div><div class="line"> 0.5,</div><div class="line"> enable_agc,</div><div class="line"> <span class="keyword">true</span>));</div><div class="line"> snips_kws->DisableAutoStateTransfer();</div><div class="line"> <span class="keywordflow">if</span> (enable_agc) {</div><div class="line"> snips_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_1beam->Uplink(collector.get());</div><div class="line"> snips_kws->Uplink(vep_1beam.get());</div><div class="line"> aloop->Uplink(snips_kws.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>->RegisterDirectionManagerNode(snips_kws.get());</div><div class="line"> <a class="code" href="namespacerespeaker.html">respeaker</a>->RegisterHotwordDetectionNode(snips_kws.get());</div><div class="line"></div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> <span class="keywordflow">if</span> (kws == <span class="stringliteral">"alexa"</span>) {</div><div class="line"> cout << <span class="stringliteral">"using alexa kws"</span> << endl;</div><div class="line"> snowboy_kws.reset(Snowboy1bDoaKwsNode::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_02092017.umdl"</span>,</div><div class="line"> <span class="stringliteral">"0.5"</span>,</div><div class="line"> 5,</div><div class="line"> enable_agc,</div><div class="line"> <span class="keyword">true</span>));</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> cout << <span class="stringliteral">"using snowboy kws"</span> << endl;</div><div class="line"> snowboy_kws.reset(Snowboy1bDoaKwsNode::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"> 5,</div><div class="line"> enable_agc,</div><div class="line"> <span class="keyword">true</span>));</div><div class="line"> }</div><div class="line"> snowboy_kws->DisableAutoStateTransfer();</div><div class="line"> <span class="keywordflow">if</span> (enable_agc) {</div><div class="line"> snowboy_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_1beam->Uplink(collector.get());</div><div class="line"> snowboy_kws->Uplink(vep_1beam.get()); </div><div class="line"> aloop->Uplink(snowboy_kws.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>->RegisterDirectionManagerNode(snowboy_kws.get());</div><div class="line"> <a class="code" href="namespacerespeaker.html">respeaker</a>->RegisterHotwordDetectionNode(snowboy_kws.get()); </div><div class="line"> }</div><div class="line"></div><div class="line"></div><div class="line"> <span class="comment">// collector->SetThreadPriority(50);</span></div><div class="line"> <span class="comment">// vep_1beam->SetThreadPriority(99);</span></div><div class="line"> <span class="comment">// snowboy_kws->SetThreadPriority(51);</span></div><div class="line"> <span class="comment">// vep_1beam->BindToCore(1);</span></div><div class="line"> <span class="comment">// snowboy_kws->BindToCore(2);</span></div><div class="line"></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(aloop.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="comment">// You should call this after respeaker->Start()</span></div><div class="line"> aloop->SetMaxBlockDelayTime(250);</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"> <span class="keywordflow">if</span> (enable_wav) {</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">"alsa_aloop_test.wav"</span>, SFM_WRITE, &sfinfo)))</div><div class="line"> {</div><div class="line"> cout << sf_strerror(file) << endl;</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"></div><div class="line"> <span class="keywordtype">int</span> tick;</div><div class="line"> <span class="keywordtype">int</span> hotword_index = 0, hotword_count = 0;</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>->DetectHotword(hotword_index);</div><div class="line"> <span class="keywordflow">if</span> (hotword_index >= 1) {</div><div class="line"> hotword_count++;</div><div class="line"> cout << <span class="stringliteral">"hotword_count = "</span> << hotword_count << endl;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (enable_wav) {</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"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (tick++ % 5 == 0) {</div><div class="line"> <span class="keywordflow">if</span> (kws == <span class="stringliteral">"heysnips"</span>) {</div><div class="line"> std::cout << <span class="stringliteral">"collector: "</span> << collector->GetQueueDeepth() << <span class="stringliteral">", vep_1beam: "</span> <<</div><div class="line"> vep_1beam->GetQueueDeepth() << <span class="stringliteral">", snips_kws: "</span> << snips_kws->GetQueueDeepth() << </div><div class="line"> <span class="stringliteral">", aloop: "</span> << aloop->GetQueueDeepth() << std::endl;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> std::cout << <span class="stringliteral">"collector: "</span> << collector->GetQueueDeepth() << <span class="stringliteral">", vep_1beam: "</span> <<</div><div class="line"> vep_1beam->GetQueueDeepth() << <span class="stringliteral">", snowboy_kws: "</span> << snowboy_kws->GetQueueDeepth() << </div><div class="line"> <span class="stringliteral">", aloop: "</span> << aloop->GetQueueDeepth() << std::endl;</div><div class="line"> }</div><div class="line"> }</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"> <span class="keywordflow">if</span> (enable_wav) {</div><div class="line"> sf_close (file);</div><div class="line"> cout << <span class="stringliteral">"wav file closed."</span> << endl;</div><div class="line"> }</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>