Apollo 10.0
自动驾驶开放平台
audio_info.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
19#include <algorithm>
20
22
23namespace apollo {
24namespace audio {
25
30
31void AudioInfo::Insert(const AudioData& audio_data) {
32 std::size_t index = 0;
33 for (const auto& channel_data : audio_data.channel_data()) {
34 if (channel_data.channel_type() == ChannelType::RAW) {
35 InsertChannelData(index, channel_data, audio_data.microphone_config());
36 ++index;
37 }
38 }
39}
40
41void AudioInfo::InsertChannelData(const std::size_t index,
42 const ChannelData& channel_data,
43 const MicrophoneConfig& microphone_config) {
44 while (index >= signals_.size()) {
45 signals_.push_back(std::deque<double>());
46 }
47 int width = microphone_config.sample_width();
48 const std::string& data = channel_data.data();
49 for (std::size_t i = 0; i < data.length(); i += width) {
50 int16_t signal = ((int16_t(data[i + 1])) << 8) | (0x00ff & data[i]);
51 signals_[index].push_back(static_cast<double>(signal));
52 }
53 std::size_t max_signal_length = static_cast<std::size_t>(
54 FLAGS_cache_signal_time * microphone_config.sample_rate());
55 while (signals_[index].size() > max_signal_length) {
56 signals_[index].pop_front();
57 }
58}
59
60std::vector<std::vector<double>> AudioInfo::GetSignals(
61 const int signal_length) {
62 std::vector<std::vector<double>> signals;
63 for (std::size_t i = 0; i < signals_.size(); ++i) {
64 int start_index = static_cast<int>(signals_[i].size()) - signal_length;
65 start_index = std::max(0, start_index);
66 std::deque<double>::iterator iter = signals_[i].begin();
67 iter += start_index;
68 std::vector<double> signal(iter, signals_[i].end());
69 signals.push_back(signal);
70 }
71 return signals;
72}
73
74} // namespace audio
75} // namespace apollo
std::vector< std::vector< double > > GetSignals(const int signal_length)
Definition audio_info.cc:60
void Insert(const apollo::drivers::microphone::config::AudioData &)
Definition audio_info.cc:31
class register implement
Definition arena_queue.h:37
optional MicrophoneConfig microphone_config
Definition audio.proto:15