Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "cogrob/cloud/speech/record_audio.h"
00028
00029 #include <cmath>
00030 #include <memory>
00031 #include <string>
00032 #include <thread>
00033 #include <utility>
00034 #include <vector>
00035
00036 #include "cogrob/cloud/speech/audio_sample.h"
00037 #include "third_party/portaudio.h"
00038 #include "third_party/glog.h"
00039 #include "third_party/gflags.h"
00040
00041 DEFINE_string(mic, "USB", "Name of the microphone");
00042
00043 using std::unique_ptr;
00044 using std::string;
00045
00046 namespace cogrob {
00047 namespace cloud {
00048 namespace speech {
00049
00050 AudioRecorder::AudioRecorder(AudioQueue* output_queue) {
00051 queue_ = output_queue;
00052 StartRecording();
00053 }
00054
00055 AudioRecorder::~AudioRecorder() {
00056 StopRecording();
00057 }
00058
00059 void AudioRecorder::StartRecording() {
00060 PaError pa_err = paNoError;
00061
00062
00063 pa_err = Pa_Initialize();
00064 if (pa_err != paNoError) {
00065 LOG(FATAL) << "PortAudio init error: " << Pa_GetErrorText(pa_err);
00066 }
00067
00068
00069 PaDeviceIndex num_devices;
00070 num_devices = Pa_GetDeviceCount();
00071 if (num_devices < 0) {
00072 LOG(FATAL) << "Pa_CountDevices returned " << num_devices;
00073 }
00074 bool found_mic = false;
00075 PaDeviceIndex device_index = 0;
00076 const PaDeviceInfo* device_info;
00077 for (PaDeviceIndex i = 0; i < num_devices; ++i) {
00078 device_info = Pa_GetDeviceInfo(i);
00079 LOG(INFO) << "Device " << device_info->name << " has "
00080 << device_info->maxInputChannels << " input channels.";
00081 if (string(device_info->name).find(FLAGS_mic) != string::npos) {
00082 found_mic = true;
00083 device_index = i;
00084 LOG(INFO) << "Use device " << device_info->name;
00085 break;
00086 }
00087 }
00088 if (!found_mic) {
00089 LOG(FATAL) << "Can not find device " << FLAGS_mic;
00090 }
00091
00092
00093 PaStreamParameters input_param = {
00094 .device = device_index,
00095 .channelCount = 1,
00096 .sampleFormat = paInt16,
00097 .suggestedLatency = 0,
00098 .hostApiSpecificStreamInfo = nullptr
00099 };
00100
00101 pa_err = Pa_OpenStream(&pa_stream_, &input_param, nullptr, SAMPLE_RATE,
00102 SAMPLES_PER_SLICE, paNoFlag,
00103 AudioRecorder::PortAudioCallback, this);
00104
00105 if (pa_err != paNoError) {
00106 LOG(FATAL) << "PortAudio open stream error: " << Pa_GetErrorText(pa_err);
00107 }
00108
00109 Pa_StartStream(pa_stream_);
00110 if (pa_err != paNoError) {
00111 LOG(FATAL) << "PortAudio start stream error: " << Pa_GetErrorText(pa_err);
00112 }
00113
00114 }
00115
00116 void AudioRecorder::StopRecording() {
00117 PaError pa_err = paNoError;
00118 Pa_StopStream(pa_stream_);
00119 if (pa_err != paNoError) {
00120 LOG(FATAL) << "PortAudio stop stream error: " << Pa_GetErrorText(pa_err);
00121 }
00122 Pa_CloseStream(pa_stream_);
00123 if (pa_err != paNoError) {
00124 LOG(FATAL) << "PortAudio close stream error: " << Pa_GetErrorText(pa_err);
00125 }
00126 }
00127
00128 int AudioRecorder::PortAudioCallback(
00129 const void* input, void* output, unsigned long frame_count,
00130 const PaStreamCallbackTimeInfo* time_info,
00131 PaStreamCallbackFlags status_flags, void* user_data) {
00132 AudioRecorder* recorder = static_cast<AudioRecorder*>(user_data);
00133
00134 LOG_IF(ERROR, frame_count != SAMPLES_PER_SLICE) << "Callback frame_count is "
00135 << frame_count << ", which is not " << SAMPLES_PER_SLICE;
00136 LOG_IF(ERROR, status_flags) << "Callback status flag is " << status_flags;
00137
00138 std::unique_ptr<AudioSample> audio_sample(new AudioSample());
00139 audio_sample->resize(frame_count * 2);
00140
00141 memcpy(audio_sample->data(), input, frame_count * 2);
00142
00143 recorder->queue_->push(std::move(audio_sample));
00144
00145 return paContinue;
00146 }
00147
00148 }
00149 }
00150 }