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