-
Notifications
You must be signed in to change notification settings - Fork 1
/
alsa_base_test_8cc-example.html
77 lines (75 loc) · 13.8 KB
/
alsa_base_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: alsa_base_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_base_test.cc</div> </div>
</div><!--header-->
<div class="contents">
<p>This is an exmaple that shows the basic usage of the <code>AlsaCollectorNode</code> node, the <code>SelectorNode</code> node, and the <code>ReSpeaker</code> supervisor.</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/selector_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="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_base_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">}</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="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"> {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">"s: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">'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">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<SelectorNode> selector;</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"> <span class="comment">//std::vector<int> selected_channels {1, 0, 3};</span></div><div class="line"> std::vector<int> selected_channels {0, 1, 2, 4, 5, 6};</div><div class="line"> <span class="comment">// selector node output interleaved data and store into wav file</span></div><div class="line"> selector.reset(SelectorNode::Create(selected_channels, <span class="keyword">true</span>));</div><div class="line"></div><div class="line"> selector->Uplink(collector.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(selector.get());</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">"alsa_base_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"> sf_count_t n = 0;</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"> n = 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"> std::this_thread::sleep_for(std::chrono::milliseconds(1000));</div><div class="line"> <span class="keywordflow">if</span> (sf_close (file) != 0) </div><div class="line"> {</div><div class="line"> cout << <span class="stringliteral">"sf close error"</span> << endl;</div><div class="line"> }</div><div class="line"></div><div class="line"> cout << <span class="stringliteral">"cleanup done."</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>