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/mock_wavreader_factory.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "modules/audio_processing/test/conversational_speech/mock_wavreader.h"
15 #include "rtc_base/logging.h"
16 #include "test/gmock.h"
17 
18 namespace webrtc {
19 namespace test {
20 namespace conversational_speech {
21 
22 using ::testing::_;
23 using ::testing::Invoke;
24 
MockWavReaderFactory(const Params & default_params,const std::map<std::string,const Params> & params)25 MockWavReaderFactory::MockWavReaderFactory(
26     const Params& default_params,
27     const std::map<std::string, const Params>& params)
28     : default_params_(default_params), audiotrack_names_params_(params) {
29   ON_CALL(*this, Create(_))
30       .WillByDefault(Invoke(this, &MockWavReaderFactory::CreateMock));
31 }
32 
MockWavReaderFactory(const Params & default_params)33 MockWavReaderFactory::MockWavReaderFactory(const Params& default_params)
34     : MockWavReaderFactory(default_params,
35                            std::map<std::string, const Params>{}) {}
36 
37 MockWavReaderFactory::~MockWavReaderFactory() = default;
38 
CreateMock(absl::string_view filepath)39 std::unique_ptr<WavReaderInterface> MockWavReaderFactory::CreateMock(
40     absl::string_view filepath) {
41   // Search the parameters corresponding to filepath.
42   size_t delimiter = filepath.find_last_of("/\\");  // Either windows or posix
43   std::string filename(filepath.substr(
44       delimiter == absl::string_view::npos ? 0 : delimiter + 1));
45   const auto it = audiotrack_names_params_.find(filename);
46 
47   // If not found, use default parameters.
48   if (it == audiotrack_names_params_.end()) {
49     RTC_LOG(LS_VERBOSE) << "using default parameters for " << filepath;
50     return std::unique_ptr<WavReaderInterface>(new MockWavReader(
51         default_params_.sample_rate, default_params_.num_channels,
52         default_params_.num_samples));
53   }
54 
55   // Found, use the audiotrack-specific parameters.
56   RTC_LOG(LS_VERBOSE) << "using ad-hoc parameters for " << filepath;
57   RTC_LOG(LS_VERBOSE) << "sample_rate " << it->second.sample_rate;
58   RTC_LOG(LS_VERBOSE) << "num_channels " << it->second.num_channels;
59   RTC_LOG(LS_VERBOSE) << "num_samples " << it->second.num_samples;
60   return std::unique_ptr<WavReaderInterface>(new MockWavReader(
61       it->second.sample_rate, it->second.num_channels, it->second.num_samples));
62 }
63 
64 }  // namespace conversational_speech
65 }  // namespace test
66 }  // namespace webrtc
67