Skip to content

Commit 7429777

Browse files
mpoulletFleker
authored andcommitted
Apply clang-format with Google's style. (#46)
* Apply clang-format with Google's style. * Isolate API version behind a namespace alias.
1 parent b132a0c commit 7429777

12 files changed

+256
-220
lines changed

.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BasedOnStyle: Google
2+
Language: Cpp
3+
Standard: Cpp11

src/audio_input.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ limitations under the License.
1818
#define AUDIO_INPUT_H
1919

2020
#include <functional>
21+
#include <iostream>
2122
#include <memory>
2223
#include <mutex>
2324
#include <thread>
2425
#include <vector>
25-
#include <iostream>
2626

2727
// Base class for audio input. Input data should be mono, s16_le, 16000kz.
2828
// This class uses a separate thread to send audio data to listeners.
@@ -33,7 +33,7 @@ class AudioInput {
3333
// Listeners might be called in different thread.
3434
void AddDataListener(
3535
std::function<void(std::shared_ptr<std::vector<unsigned char>>)>
36-
listener) {
36+
listener) {
3737
data_listeners_.push_back(listener);
3838
}
3939
void AddStopListener(std::function<void()> listener) {

src/audio_input_alsa.cc

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,80 @@ std::unique_ptr<std::thread> AudioInputALSA::GetBackgroundThread() {
2424
return std::unique_ptr<std::thread>(new std::thread([this]() {
2525
// Initialize.
2626
snd_pcm_t* pcm_handle;
27-
int pcm_open_ret = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_CAPTURE, 0);
27+
int pcm_open_ret =
28+
snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_CAPTURE, 0);
2829
if (pcm_open_ret < 0) {
29-
std::cerr << "AudioInputALSA snd_pcm_open returned " << pcm_open_ret << std::endl;
30+
std::cerr << "AudioInputALSA snd_pcm_open returned " << pcm_open_ret
31+
<< std::endl;
3032
return;
3133
}
34+
3235
int pcm_nonblock_ret = snd_pcm_nonblock(pcm_handle, SND_PCM_NONBLOCK);
3336
if (pcm_nonblock_ret < 0) {
34-
std::cerr << "AudioInputALSA snd_pcm_nonblock returned " << pcm_nonblock_ret << std::endl;
37+
std::cerr << "AudioInputALSA snd_pcm_nonblock returned "
38+
<< pcm_nonblock_ret << std::endl;
3539
return;
3640
}
41+
3742
snd_pcm_hw_params_t* pcm_params;
3843
int malloc_param_ret = snd_pcm_hw_params_malloc(&pcm_params);
3944
if (malloc_param_ret < 0) {
40-
std::cerr << "AudioInputALSA snd_pcm_hw_params_malloc returned " << malloc_param_ret
41-
<< std::endl;
45+
std::cerr << "AudioInputALSA snd_pcm_hw_params_malloc returned "
46+
<< malloc_param_ret << std::endl;
4247
return;
4348
}
49+
4450
snd_pcm_hw_params_any(pcm_handle, pcm_params);
45-
int set_param_ret =
46-
snd_pcm_hw_params_set_access(pcm_handle, pcm_params, SND_PCM_ACCESS_RW_INTERLEAVED);
51+
int set_param_ret = snd_pcm_hw_params_set_access(
52+
pcm_handle, pcm_params, SND_PCM_ACCESS_RW_INTERLEAVED);
4753
if (set_param_ret < 0) {
48-
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_access returned " << set_param_ret
49-
<< std::endl;
54+
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_access returned "
55+
<< set_param_ret << std::endl;
5056
return;
5157
}
52-
set_param_ret =
53-
snd_pcm_hw_params_set_format(pcm_handle, pcm_params, SND_PCM_FORMAT_S16_LE);
58+
59+
set_param_ret = snd_pcm_hw_params_set_format(pcm_handle, pcm_params,
60+
SND_PCM_FORMAT_S16_LE);
5461
if (set_param_ret < 0) {
55-
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_format returned " << set_param_ret
56-
<< std::endl;
62+
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_format returned "
63+
<< set_param_ret << std::endl;
5764
return;
5865
}
59-
set_param_ret =
60-
snd_pcm_hw_params_set_channels(pcm_handle, pcm_params, 1);
66+
67+
set_param_ret = snd_pcm_hw_params_set_channels(pcm_handle, pcm_params, 1);
6168
if (set_param_ret < 0) {
62-
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_channels returned " << set_param_ret
63-
<< std::endl;
69+
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_channels returned "
70+
<< set_param_ret << std::endl;
6471
return;
6572
}
73+
6674
unsigned int rate = 16000;
6775
set_param_ret =
6876
snd_pcm_hw_params_set_rate_near(pcm_handle, pcm_params, &rate, nullptr);
6977
if (set_param_ret < 0) {
70-
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_rate_near returned " << set_param_ret
71-
<< std::endl;
78+
std::cerr << "AudioInputALSA snd_pcm_hw_params_set_rate_near returned "
79+
<< set_param_ret << std::endl;
7280
return;
7381
}
82+
7483
set_param_ret = snd_pcm_hw_params(pcm_handle, pcm_params);
7584
if (set_param_ret < 0) {
76-
std::cerr << "AudioInputALSA snd_pcm_hw_params returned " << set_param_ret << std::endl;
85+
std::cerr << "AudioInputALSA snd_pcm_hw_params returned " << set_param_ret
86+
<< std::endl;
7787
return;
7888
}
7989
snd_pcm_hw_params_free(pcm_params);
8090

8191
while (is_running_) {
8292
std::shared_ptr<std::vector<unsigned char>> audio_data(
83-
new std::vector<unsigned char>(kFramesPerPacket * kBytesPerFrame));
84-
int pcm_read_ret = snd_pcm_readi(pcm_handle, &(*audio_data.get())[0], kFramesPerPacket);
93+
new std::vector<unsigned char>(kFramesPerPacket * kBytesPerFrame));
94+
int pcm_read_ret =
95+
snd_pcm_readi(pcm_handle, &(*audio_data.get())[0], kFramesPerPacket);
8596
if (pcm_read_ret == -EAGAIN) {
8697
std::this_thread::sleep_for(std::chrono::milliseconds(20));
8798
} else if (pcm_read_ret < 0) {
88-
std::cerr << "AudioInputALSA snd_pcm_readi returned " << pcm_read_ret << std::endl;
99+
std::cerr << "AudioInputALSA snd_pcm_readi returned " << pcm_read_ret
100+
<< std::endl;
89101
break;
90102
} else if (pcm_read_ret > 0) {
91103
audio_data->resize(kBytesPerFrame * pcm_read_ret);

src/audio_input_file.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ std::unique_ptr<std::thread> AudioInputFile::GetBackgroundThread() {
2424
// Initialize.
2525
std::ifstream file_stream(file_path_);
2626
if (!file_stream) {
27-
std::cerr << "AudioInputFile cannot open file " << file_path_ << std::endl;
27+
std::cerr << "AudioInputFile cannot open file " << file_path_
28+
<< std::endl;
2829
return;
2930
}
3031

src/audio_input_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818

1919
class AudioInputFile : public AudioInput {
2020
public:
21-
AudioInputFile(const std::string& file_path): file_path_(file_path) {}
21+
AudioInputFile(const std::string& file_path) : file_path_(file_path) {}
2222
~AudioInputFile() override {}
2323

2424
virtual std::unique_ptr<std::thread> GetBackgroundThread() override;

src/audio_output_alsa.cc

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/base64_encode.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@ limitations under the License.
1616

1717
#include "base64_encode.h"
1818

19-
// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c by Manuel Martinez
19+
// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c by
20+
// Manuel Martinez
2021
std::string base64_encode(const std::string &in) {
2122
std::string out;
2223

23-
int val=0, valb=-6;
24+
int val = 0, valb = -6;
2425
for (u_char c : in) {
25-
val = (val<<8) + c;
26-
valb += 8;
27-
while (valb>=0) {
28-
out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(val>>valb)&0x3F]);
29-
valb-=6;
30-
}
26+
val = (val << 8) + c;
27+
valb += 8;
28+
while (valb >= 0) {
29+
out.push_back(
30+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
31+
[(val >> valb) & 0x3F]);
32+
valb -= 6;
33+
}
3134
}
32-
if (valb>-6) {
33-
out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((val<<8)>>(valb+8))&0x3F]);
35+
if (valb > -6) {
36+
out.push_back(
37+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
38+
[((val << 8) >> (valb + 8)) & 0x3F]);
3439
}
35-
while (out.size()%4) {
40+
while (out.size() % 4) {
3641
out.push_back('=');
3742
}
3843
return out;

src/json_util_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ limitations under the License.
2121
bool check_result(const std::string& input_json,
2222
std::unique_ptr<std::string> intended_result) {
2323
std::unique_ptr<std::string> result = GetCustomResponseOrNull(input_json);
24-
return (intended_result == nullptr && result == nullptr)
25-
|| (intended_result != nullptr && result != nullptr
26-
&& *intended_result == *result);
24+
return (intended_result == nullptr && result == nullptr) ||
25+
(intended_result != nullptr && result != nullptr &&
26+
*intended_result == *result);
2727
}
2828

2929
int main() {

0 commit comments

Comments
 (0)