Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Side by Side Diff: webrtc/modules/audio_processing/test/conversational_speech/simulator.cc

Issue 2790933002: Conversational speech tool, simualtor + unit tests (Closed)
Patch Set: map iterators simplified Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/modules/audio_processing/test/conversational_speech/simulator.h "
12
13 #include <set>
14 #include <utility>
15 #include <vector>
16
17 #include "webrtc/base/array_view.h"
18 #include "webrtc/base/constructormagic.h"
19 #include "webrtc/base/logging.h"
20 #include "webrtc/base/pathutils.h"
21 #include "webrtc/base/ptr_util.h"
22 #include "webrtc/common_audio/wav_file.h"
23 #include "webrtc/modules/audio_processing/test/conversational_speech/wavreader_i nterface.h"
24
25 namespace webrtc {
26 namespace test {
27 namespace {
28
29 using conversational_speech::MultiEndCall;
30 using conversational_speech::SpeakerOutputFilePaths;
31 using conversational_speech::WavReaderInterface;
32
33 // Combines output path and speaker names to define the output file paths for
34 // the near-end and far=end audio tracks.
35 std::unique_ptr<std::map<std::string, SpeakerOutputFilePaths>>
36 InitSpeakerOutputFilePaths(const std::set<std::string>& speaker_names,
37 const std::string& output_path) {
38 // Create map.
39 auto speaker_output_file_paths_map = rtc::MakeUnique<
40 std::map<std::string, SpeakerOutputFilePaths>>();
41
42 // Add near-end and far-end output paths into the map.
43 for (const auto& speaker_name : speaker_names) {
44 const rtc::Pathname near_end_path(
45 output_path, "s_" + speaker_name + "-near_end.wav");
46 LOG(LS_VERBOSE) << "creating " << near_end_path.pathname();
minyue-webrtc 2017/05/16 15:05:17 I'd like full sentence in the log
AleBzk 2017/05/17 12:49:38 Done.
47
48 const rtc::Pathname far_end_path(
49 output_path, "s_" + speaker_name + "-far_end.wav");
50 LOG(LS_VERBOSE) << "creating " << far_end_path.pathname();
51
52 // Add to map.
53 speaker_output_file_paths_map->emplace(
54 std::piecewise_construct,
minyue-webrtc 2017/05/16 15:05:16 I am not a fan of this, ok, I think I understand n
AleBzk 2017/05/17 12:49:38 Acknowledged.
55 std::forward_as_tuple(speaker_name),
56 std::forward_as_tuple(near_end_path.pathname(),
57 far_end_path.pathname()));
58 }
59
60 return speaker_output_file_paths_map;
61 }
62
63 // Class that provides one WavWriter for the near-end and one for the far-end
64 // output track of a speaker.
65 class SpeakerWavWriters {
66 public:
67 SpeakerWavWriters(
68 const SpeakerOutputFilePaths& output_file_paths, int sample_rate)
69 : near_end_(output_file_paths.near_end, sample_rate, 1u),
70 far_end_(output_file_paths.far_end, sample_rate, 1u) {}
71 WavWriter& near_end() {
minyue-webrtc 2017/05/16 15:05:17 WavWriter* near_end_wav_writer()
72 return near_end_;
73 }
74 WavWriter& far_end() {
minyue-webrtc 2017/05/16 15:05:17 WavWriter* far_end_wav_writer()
AleBzk 2017/05/17 12:49:38 Done.
75 return far_end_;
76 }
77 private:
78 WavWriter near_end_;
minyue-webrtc 2017/05/16 15:05:17 near_end_wav_writer_
AleBzk 2017/05/17 12:49:38 Done.
minyue-webrtc 2017/05/19 12:46:51 I'd prefer change the var name to near_end_wav_wri
AleBzk 2017/05/19 15:45:04 Done.
79 WavWriter far_end_;
minyue-webrtc 2017/05/16 15:05:17 far_end_wav_writer_
AleBzk 2017/05/17 12:49:38 Done.
minyue-webrtc 2017/05/19 12:46:51 same here
AleBzk 2017/05/19 15:45:04 Done.
80 };
81
82 // Initializes one WavWriter instance for each speaker and both the near-end and
83 // far-end output tracks.
84 std::unique_ptr<std::map<std::string, SpeakerWavWriters>>
85 InitSpeakersWavWriters(const std::map<std::string, SpeakerOutputFilePaths>&
86 speaker_output_file_paths, int sample_rate) {
87 // Create map.
88 auto speaker_wav_writers_map = rtc::MakeUnique<
89 std::map<std::string, SpeakerWavWriters>>();
90
91 // Add SpeakerWavWriters instance into the map.
92 for (auto it = speaker_output_file_paths.begin();
93 it != speaker_output_file_paths.end(); ++it) {
94 speaker_wav_writers_map->emplace(
95 std::piecewise_construct,
96 std::forward_as_tuple(it->first),
97 std::forward_as_tuple(it->second, sample_rate));
98 }
99
100 return speaker_wav_writers_map;
101 }
102
103 // Reads all the samples for each audio track.
104 std::unique_ptr<std::map<std::string, std::vector<int16_t>>> PreloadAudioTracks(
105 const std::map<std::string, std::unique_ptr<WavReaderInterface>>&
106 audiotrack_readers) {
107 // Create map.
108 auto audiotracks_map = rtc::MakeUnique<
109 std::map<std::string, std::vector<int16_t>>>();
110
111 // Add audio track vectors.
112 for (auto it = audiotrack_readers.begin(); it != audiotrack_readers.end();
113 ++it) {
114 // Add map entry.
115 audiotracks_map->emplace(
116 std::piecewise_construct,
117 std::forward_as_tuple(it->first),
118 std::forward_as_tuple(it->second->NumSamples()));
119
120 // Read samples.
121 it->second->ReadInt16Samples(audiotracks_map->at(it->first));
122 }
123
124 return audiotracks_map;
125 }
126
127 // Writes all the values in |source_samples| via |wav_writer|. If the number of
128 // previously written samples in |wav_writer| is less than |interval_begin|, it
129 // adds zeros as left padding. The padding corresponds to intervals during which
130 // a speaker is not active.
131 void PadLeftWriteChunk(rtc::ArrayView<const int16_t> source_samples,
132 size_t interval_begin, WavWriter* wav_writer) {
133 // Add left padding.
134 RTC_CHECK(wav_writer);
135 RTC_CHECK_GE(interval_begin, wav_writer->num_samples());
136 size_t padding_size = interval_begin - wav_writer->num_samples();
137 if (padding_size > 0) {
minyue-webrtc 2017/05/16 15:05:17 I prefer "!=0": a bit easier to read
AleBzk 2017/05/17 12:49:38 Done.
138 const std::vector<int16_t> padding(padding_size, 0);
139 wav_writer->WriteSamples(padding.data(), padding_size);
140 }
141
142 // Write source samples.
143 wav_writer->WriteSamples(source_samples.data(), source_samples.size());
144 }
145
146 // Appends zeros via |wav_writer|. The number of zeros is always non-negative
147 // and equal to the difference between the previously written samples and
148 // |pad_samples|.
149 void PadRightWrite(WavWriter* wav_writer, size_t pad_samples) {
150 RTC_CHECK(wav_writer);
151 RTC_CHECK_GE(pad_samples, wav_writer->num_samples());
152 size_t padding_size = pad_samples - wav_writer->num_samples();
153 if (padding_size > 0) {
minyue-webrtc 2017/05/16 15:05:17 != 0
AleBzk 2017/05/17 12:49:38 Done.
154 const std::vector<int16_t> padding(padding_size, 0);
155 wav_writer->WriteSamples(padding.data(), padding_size);
156 }
157 }
158
159 } // namespace
160
161 namespace conversational_speech {
162
163 std::unique_ptr<std::map<std::string, SpeakerOutputFilePaths>> Simulate(
164 const MultiEndCall& multiend_call, const std::string& output_path) {
165 // Set output file paths and initialize wav writers.
166 const auto& speaker_names = multiend_call.speaker_names();
167 auto speaker_output_file_paths = InitSpeakerOutputFilePaths(
168 speaker_names, output_path);
169 auto speakers_wav_writers = InitSpeakersWavWriters(
170 *speaker_output_file_paths, multiend_call.sample_rate());
171
172 // Preload all the input audio tracks.
173 const auto& audiotrack_readers = multiend_call.audiotrack_readers();
174 auto audiotracks = PreloadAudioTracks(audiotrack_readers);
175
176 // TODO(alessiob): When speaker_names.size() == 2, near-end and far-end
177 // across the 2 speakers are symmetric; hence, the code below could be
178 // replaced by only creating the near-end or the far-end. However, this would
179 // require to split the unit tests and document the behavior in README.md.
180 // In practice, it should not be an issue since the files are not expected to
181 // be signinificant.
182
183 // Write near-end and far-end output tracks.
184 const auto& speaking_turns = multiend_call.speaking_turns();
185 for (const auto& speaking_turn : speaking_turns) {
minyue-webrtc 2017/05/16 15:05:16 I think you may put multiend_call.speaking_turns()
AleBzk 2017/05/17 12:49:38 Done.
186 const std::string& active_speaker_name = speaking_turn.speaker_name;
187 auto source_audiotrack = audiotracks->at(
188 speaking_turn.audiotrack_file_name);
189
190 // Write active speaker's chunk to active speaker's near-end.
191 PadLeftWriteChunk(source_audiotrack, speaking_turn.begin,
192 &speakers_wav_writers->at(
minyue-webrtc 2017/05/16 15:05:17 remove & if you return * with near_end() there a
AleBzk 2017/05/17 12:49:38 Done.
193 active_speaker_name).near_end());
194
195 // Write active speaker's chunk to other participants' far-ends.
196 for (const std::string& speaker_name : speaker_names) {
197 if (speaker_name == active_speaker_name)
198 continue;
199 PadLeftWriteChunk(source_audiotrack, speaking_turn.begin,
200 &speakers_wav_writers->at(speaker_name).far_end());
201 }
202 }
203
204 // Finalize all the output tracks with right padding.
205 // This is required to make all the output tracks duration equal.
206 size_t duration_samples = multiend_call.total_duration_samples();
207 for (const std::string& speaker_name : speaker_names) {
208 PadRightWrite(&speakers_wav_writers->at(speaker_name).near_end(),
209 duration_samples);
210 PadRightWrite(&speakers_wav_writers->at(speaker_name).far_end(),
211 duration_samples);
212 }
213
214 return speaker_output_file_paths;
215 }
216
217 } // namespace conversational_speech
218 } // namespace test
219 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698