Apollo 10.0
自动驾驶开放平台
microphone_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19namespace apollo {
20namespace drivers {
21namespace microphone {
22
24 microphone_config_ptr_ = std::make_shared<MicrophoneConfig>();
26 microphone_config_ptr_.get())) {
27 return false;
28 }
29 AINFO << "Microphone config: " << microphone_config_ptr_->DebugString();
30
31 // new microphone device
32 microphone_device_ptr_.reset(new Respeaker());
33 microphone_device_ptr_->init(microphone_config_ptr_);
34
35 // dump config, calculate size and reserve buffer for audio
36 n_channels_ = microphone_config_ptr_->channel_type_size();
37 sample_width_ = microphone_config_ptr_->sample_width();
38 // chunk_: number of frames per chunk; chunk_size_: number of bytes per chunk
39 chunk_ = microphone_config_ptr_->chunk();
40 n_chunk_ = static_cast<int>(
41 std::ceil(microphone_config_ptr_->sample_rate() *
42 microphone_config_ptr_->record_seconds() / chunk_));
43 chunk_size_ = chunk_ * n_channels_ * sample_width_;
44 buffer_ = new char[chunk_size_];
45 if (buffer_ == nullptr) {
46 AERROR << "System new memory error, size:" << chunk_size_;
47 return false;
48 }
49
50 // assemble AudioData -- fill microphone_config, allocate memory for
51 // channel_data
52 audio_data_ptr_.reset(new AudioData());
53 std::string config;
54 microphone_config_ptr_->SerializeToString(&config);
55 audio_data_ptr_->mutable_microphone_config()->ParseFromString(config);
56 audio_data_ptr_->mutable_header()->set_frame_id(
57 microphone_config_ptr_->frame_id());
58
59 ChannelData *channel_data = nullptr;
60 int channel_size = n_chunk_ * chunk_ * sample_width_;
61 for (int i = 0; i < n_channels_; ++i) {
62 channel_data = audio_data_ptr_->add_channel_data();
63 channel_data->set_channel_type(microphone_config_ptr_->channel_type(i));
64 channel_data->mutable_data()->resize(channel_size);
65 channel_data_ptrs_.push_back(channel_data->mutable_data());
66 }
67
68 writer_ptr_ =
69 node_->CreateWriter<AudioData>(microphone_config_ptr_->channel_name());
70 async_result_ = cyber::Async(&MicrophoneComponent::run, this);
71 return true;
72}
73
74void MicrophoneComponent::fill_channel_data(int chunk_i) {
75 // next index of channel data to be filled
76 int pos = chunk_i * chunk_ * sample_width_;
77 for (int buff_i = 0; buff_i < chunk_size_; pos += sample_width_) {
78 for (int channel_i = 0; channel_i < n_channels_; ++channel_i) {
79 for (int di = 0; di < sample_width_; ++di) {
80 (*channel_data_ptrs_[channel_i])[pos + di] = buffer_[buff_i++];
81 }
82 }
83 }
84}
85
86void MicrophoneComponent::run() {
87 int chunk_i;
88 while (!cyber::IsShutdown()) {
89 try {
90 for (chunk_i = 0; chunk_i < n_chunk_; ++chunk_i) {
91 microphone_device_ptr_->read_stream(chunk_, buffer_);
92 fill_channel_data(chunk_i);
93 }
94 } catch (const std::exception &e) {
95 return;
96 }
97 FillHeader(node_->Name(), audio_data_ptr_.get());
98 writer_ptr_->Write(audio_data_ptr_);
99 }
100}
101
103 delete[] buffer_;
104 if (running_.load()) {
105 running_.exchange(false);
106 async_result_.wait();
107 }
108}
109
110} // namespace microphone
111} // namespace drivers
112} // namespace apollo
std::shared_ptr< Node > node_
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
bool IsShutdown()
Definition state.h:46
class register implement
Definition arena_queue.h:37