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