43 using std::unique_ptr;
60 PaError pa_err = paNoError;
63 pa_err = Pa_Initialize();
64 if (pa_err != paNoError) {
65 LOG(FATAL) <<
"PortAudio init error: " << Pa_GetErrorText(pa_err);
69 PaDeviceIndex num_devices;
70 num_devices = Pa_GetDeviceCount();
71 if (num_devices < 0) {
72 LOG(FATAL) <<
"Pa_CountDevices returned " << num_devices;
74 bool found_mic =
false;
75 PaDeviceIndex device_index = 0;
76 const PaDeviceInfo* device_info;
77 for (PaDeviceIndex i = 0; i < num_devices; ++i) {
78 device_info = Pa_GetDeviceInfo(i);
79 LOG(INFO) <<
"Device " << device_info->name <<
" has " 80 << device_info->maxInputChannels <<
" input channels.";
81 if (
string(device_info->name).find(FLAGS_mic) != string::npos) {
84 LOG(INFO) <<
"Use device " << device_info->name;
89 LOG(FATAL) <<
"Can not find device " << FLAGS_mic;
93 PaStreamParameters input_param = {
94 .device = device_index,
96 .sampleFormat = paInt16,
97 .suggestedLatency = 0,
98 .hostApiSpecificStreamInfo =
nullptr 105 if (pa_err != paNoError) {
106 LOG(FATAL) <<
"PortAudio open stream error: " << Pa_GetErrorText(pa_err);
110 if (pa_err != paNoError) {
111 LOG(FATAL) <<
"PortAudio start stream error: " << Pa_GetErrorText(pa_err);
117 PaError pa_err = paNoError;
119 if (pa_err != paNoError) {
120 LOG(FATAL) <<
"PortAudio stop stream error: " << Pa_GetErrorText(pa_err);
123 if (pa_err != paNoError) {
124 LOG(FATAL) <<
"PortAudio close stream error: " << Pa_GetErrorText(pa_err);
129 const void* input,
void* output,
unsigned long frame_count,
130 const PaStreamCallbackTimeInfo* time_info,
131 PaStreamCallbackFlags status_flags,
void* user_data) {
136 LOG_IF(ERROR, status_flags) <<
"Callback status flag is " << status_flags;
138 std::unique_ptr<AudioSample> audio_sample(
new AudioSample());
139 audio_sample->resize(frame_count * 2);
141 memcpy(audio_sample->data(), input, frame_count * 2);
143 recorder->
queue_->
push(std::move(audio_sample));
DEFINE_string(mic,"USB","Name of the microphone")
constexpr size_t SAMPLE_RATE
static int PortAudioCallback(const void *input, void *output, unsigned long frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags, void *user_data)
void push(Args &&...args)
AudioRecorder(AudioQueue *output_queue)
std::vector< uint8_t > AudioSample
constexpr size_t SAMPLES_PER_SLICE