@@ -28,78 +28,92 @@ bool AudioOutputALSA::Start() {
2828 }
2929
3030 snd_pcm_t * pcm_handle;
31- int pcm_open_ret = snd_pcm_open (&pcm_handle, " default" , SND_PCM_STREAM_PLAYBACK, 0 );
31+ int pcm_open_ret =
32+ snd_pcm_open (&pcm_handle, " default" , SND_PCM_STREAM_PLAYBACK, 0 );
3233 if (pcm_open_ret < 0 ) {
33- std::cerr << " AudioOutputALSA snd_pcm_open returned " << pcm_open_ret << std::endl;
34+ std::cerr << " AudioOutputALSA snd_pcm_open returned " << pcm_open_ret
35+ << std::endl;
3436 return false ;
3537 }
38+
3639 snd_pcm_hw_params_t * pcm_params;
3740 int malloc_param_ret = snd_pcm_hw_params_malloc (&pcm_params);
3841 if (malloc_param_ret < 0 ) {
39- std::cerr << " AudioOutputALSA snd_pcm_hw_params_malloc returned " << malloc_param_ret
40- << std::endl;
42+ std::cerr << " AudioOutputALSA snd_pcm_hw_params_malloc returned "
43+ << malloc_param_ret << std::endl;
4144 return false ;
4245 }
46+
4347 snd_pcm_hw_params_any (pcm_handle, pcm_params);
44- int set_param_ret =
45- snd_pcm_hw_params_set_access ( pcm_handle, pcm_params, SND_PCM_ACCESS_RW_INTERLEAVED);
48+ int set_param_ret = snd_pcm_hw_params_set_access (
49+ pcm_handle, pcm_params, SND_PCM_ACCESS_RW_INTERLEAVED);
4650 if (set_param_ret < 0 ) {
47- std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_access returned " << set_param_ret
48- << std::endl;
51+ std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_access returned "
52+ << set_param_ret << std::endl;
4953 return false ;
5054 }
51- set_param_ret =
52- snd_pcm_hw_params_set_format (pcm_handle, pcm_params, SND_PCM_FORMAT_S16_LE);
55+
56+ set_param_ret = snd_pcm_hw_params_set_format (pcm_handle, pcm_params,
57+ SND_PCM_FORMAT_S16_LE);
5358 if (set_param_ret < 0 ) {
54- std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_format returned " << set_param_ret
55- << std::endl;
59+ std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_format returned "
60+ << set_param_ret << std::endl;
5661 return false ;
5762 }
58- set_param_ret =
59- snd_pcm_hw_params_set_channels (pcm_handle, pcm_params, 1 );
63+
64+ set_param_ret = snd_pcm_hw_params_set_channels (pcm_handle, pcm_params, 1 );
6065 if (set_param_ret < 0 ) {
61- std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_channels returned " << set_param_ret
62- << std::endl;
66+ std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_channels returned "
67+ << set_param_ret << std::endl;
6368 return false ;
6469 }
70+
6571 unsigned int rate = 16000 ;
6672 set_param_ret =
6773 snd_pcm_hw_params_set_rate_near (pcm_handle, pcm_params, &rate, nullptr );
6874 if (set_param_ret < 0 ) {
69- std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_rate_near returned " << set_param_ret
70- << std::endl;
75+ std::cerr << " AudioOutputALSA snd_pcm_hw_params_set_rate_near returned "
76+ << set_param_ret << std::endl;
7177 return false ;
7278 }
79+
7380 set_param_ret = snd_pcm_hw_params (pcm_handle, pcm_params);
7481 if (set_param_ret < 0 ) {
75- std::cerr << " AudioOutputALSA snd_pcm_hw_params returned " << set_param_ret << std::endl;
82+ std::cerr << " AudioOutputALSA snd_pcm_hw_params returned " << set_param_ret
83+ << std::endl;
7684 return false ;
7785 }
86+
7887 snd_pcm_hw_params_free (pcm_params);
7988
8089 isRunning = true ;
8190 alsaThread.reset (new std::thread ([this , pcm_handle]() {
8291 while (isRunning) {
8392 std::unique_lock<std::mutex> lock (audioDataMutex);
93+
8494 while (audioData.size () == 0 && isRunning) {
8595 audioDataCv.wait_for (lock, std::chrono::milliseconds (100 ));
8696 }
97+
8798 if (!isRunning) {
8899 break ;
89100 }
90101
91102 std::shared_ptr<std::vector<unsigned char >> data = audioData[0 ];
92103 audioData.erase (audioData.begin ());
93- int frames = data->size () / 2 ; // 1 channel, S16LE, so 2 bytes each frame.
104+ // 1 channel, S16LE, so 2 bytes each frame.
105+ int frames = data->size () / 2 ;
94106 int pcm_write_ret = snd_pcm_writei (pcm_handle, &(*data.get ())[0 ], frames);
95107 if (pcm_write_ret < 0 ) {
96108 int pcm_recover_ret = snd_pcm_recover (pcm_handle, pcm_write_ret, 0 );
97109 if (pcm_recover_ret < 0 ) {
98- std::cerr << " AudioOutputALSA snd_pcm_recover returns " << pcm_recover_ret << std::endl;
110+ std::cerr << " AudioOutputALSA snd_pcm_recover returns "
111+ << pcm_recover_ret << std::endl;
99112 break ;
100113 }
101114 }
102115 }
116+
103117 // Wait for all data to be consumed.
104118 snd_pcm_drain (pcm_handle);
105119 snd_pcm_close (pcm_handle);
0 commit comments