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 "modules/audio_processing/test/conversational_speech/timing.h"
12
13 #include <fstream>
14 #include <iostream>
15
16 #include "rtc_base/string_encode.h"
17
18 namespace webrtc {
19 namespace test {
20 namespace conversational_speech {
21
operator ==(const Turn & b) const22 bool Turn::operator==(const Turn& b) const {
23 return b.speaker_name == speaker_name &&
24 b.audiotrack_file_name == audiotrack_file_name && b.offset == offset &&
25 b.gain == gain;
26 }
27
LoadTiming(const std::string & timing_filepath)28 std::vector<Turn> LoadTiming(const std::string& timing_filepath) {
29 // Line parser.
30 auto parse_line = [](const std::string& line) {
31 std::vector<std::string> fields;
32 rtc::split(line, ' ', &fields);
33 RTC_CHECK_GE(fields.size(), 3);
34 RTC_CHECK_LE(fields.size(), 4);
35 int gain = 0;
36 if (fields.size() == 4) {
37 gain = std::atof(fields[3].c_str());
38 }
39 return Turn(fields[0], fields[1], std::atol(fields[2].c_str()), gain);
40 };
41
42 // Init.
43 std::vector<Turn> timing;
44
45 // Parse lines.
46 std::string line;
47 std::ifstream infile(timing_filepath);
48 while (std::getline(infile, line)) {
49 if (line.empty())
50 continue;
51 timing.push_back(parse_line(line));
52 }
53 infile.close();
54
55 return timing;
56 }
57
SaveTiming(const std::string & timing_filepath,rtc::ArrayView<const Turn> timing)58 void SaveTiming(const std::string& timing_filepath,
59 rtc::ArrayView<const Turn> timing) {
60 std::ofstream outfile(timing_filepath);
61 RTC_CHECK(outfile.is_open());
62 for (const Turn& turn : timing) {
63 outfile << turn.speaker_name << " " << turn.audiotrack_file_name << " "
64 << turn.offset << " " << turn.gain << std::endl;
65 }
66 outfile.close();
67 }
68
69 } // namespace conversational_speech
70 } // namespace test
71 } // namespace webrtc
72