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