alessiob | ce302b8 | 2017-03-22 08:23:46 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_processing/test/conversational_speech/timing.h" |
alessiob | ce302b8 | 2017-03-22 08:23:46 -0700 | [diff] [blame] | 12 | |
| 13 | #include <fstream> |
| 14 | #include <iostream> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 16 | #include "rtc_base/stringencode.h" |
alessiob | ce302b8 | 2017-03-22 08:23:46 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | namespace conversational_speech { |
| 21 | |
| 22 | bool Turn::operator==(const Turn &b) const { |
| 23 | return b.speaker_name == speaker_name && |
| 24 | b.audiotrack_file_name == audiotrack_file_name && |
| 25 | b.offset == offset; |
| 26 | } |
| 27 | |
| 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_EQ(fields.size(), 3); |
| 34 | return Turn(fields[0], fields[1], std::atol(fields[2].c_str())); |
| 35 | }; |
| 36 | |
| 37 | // Init. |
| 38 | std::vector<Turn> timing; |
| 39 | |
| 40 | // Parse lines. |
| 41 | std::string line; |
| 42 | std::ifstream infile(timing_filepath); |
| 43 | while (std::getline(infile, line)) { |
| 44 | if (line.empty()) |
| 45 | continue; |
| 46 | timing.push_back(parse_line(line)); |
| 47 | } |
| 48 | infile.close(); |
| 49 | |
| 50 | return timing; |
| 51 | } |
| 52 | |
| 53 | void SaveTiming(const std::string& timing_filepath, |
| 54 | rtc::ArrayView<const Turn> timing) { |
| 55 | std::ofstream outfile(timing_filepath); |
alessiob | 19e087f | 2017-06-15 03:49:57 -0700 | [diff] [blame] | 56 | RTC_CHECK(outfile.is_open()); |
alessiob | ce302b8 | 2017-03-22 08:23:46 -0700 | [diff] [blame] | 57 | for (const Turn& turn : timing) { |
| 58 | outfile << turn.speaker_name << " " << turn.audiotrack_file_name |
| 59 | << " " << turn.offset << std::endl; |
| 60 | } |
| 61 | outfile.close(); |
| 62 | } |
| 63 | |
| 64 | } // namespace conversational_speech |
| 65 | } // namespace test |
| 66 | } // namespace webrtc |