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 "rtc_base/pathutils.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),
29           audiotrack_names_params_(params) {
30   ON_CALL(*this, Create(_)).WillByDefault(Invoke(
31       this, &MockWavReaderFactory::CreateMock));
32 }
33 
MockWavReaderFactory(const Params & default_params)34 MockWavReaderFactory::MockWavReaderFactory(const Params& default_params)
35     : MockWavReaderFactory(default_params,
36                            std::map<std::string, const Params>{}) {}
37 
38 MockWavReaderFactory::~MockWavReaderFactory() = default;
39 
CreateMock(const std::string & filepath)40 std::unique_ptr<WavReaderInterface> MockWavReaderFactory::CreateMock(
41     const std::string& filepath) {
42   // Search the parameters corresponding to filepath.
43   const rtc::Pathname audiotrack_file_path(filepath);
44   const auto it = audiotrack_names_params_.find(
45       audiotrack_file_path.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>(
51         new MockWavReader(default_params_.sample_rate,
52                           default_params_.num_channels,
53                           default_params_.num_samples));
54   }
55 
56   // Found, use the audiotrack-specific parameters.
57   RTC_LOG(LS_VERBOSE) << "using ad-hoc parameters for " << filepath;
58   RTC_LOG(LS_VERBOSE) << "sample_rate " << it->second.sample_rate;
59   RTC_LOG(LS_VERBOSE) << "num_channels " << it->second.num_channels;
60   RTC_LOG(LS_VERBOSE) << "num_samples " << it->second.num_samples;
61   return std::unique_ptr<WavReaderInterface>(
62       new MockWavReader(it->second.sample_rate,
63                         it->second.num_channels,
64                         it->second.num_samples));
65 }
66 
67 }  // namespace conversational_speech
68 }  // namespace test
69 }  // namespace webrtc
70