blob: 773a42ebd75f101298041994471e63722c7de019 [file] [log] [blame]
alessiobce302b82017-03-22 08:23:46 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/test/conversational_speech/timing.h"
alessiobce302b82017-03-22 08:23:46 -070012
13#include <fstream>
14#include <iostream>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/stringencode.h"
alessiobce302b82017-03-22 08:23:46 -070017
18namespace webrtc {
19namespace test {
20namespace conversational_speech {
21
22bool 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
28std::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
53void SaveTiming(const std::string& timing_filepath,
54 rtc::ArrayView<const Turn> timing) {
55 std::ofstream outfile(timing_filepath);
alessiob19e087f2017-06-15 03:49:57 -070056 RTC_CHECK(outfile.is_open());
alessiobce302b82017-03-22 08:23:46 -070057 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