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/wavreader_factory.h"
12 
13 #include <cstddef>
14 
15 #include "absl/strings/string_view.h"
16 #include "api/array_view.h"
17 #include "common_audio/wav_file.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 namespace test {
22 namespace {
23 
24 using conversational_speech::WavReaderInterface;
25 
26 class WavReaderAdaptor final : public WavReaderInterface {
27  public:
WavReaderAdaptor(absl::string_view filepath)28   explicit WavReaderAdaptor(absl::string_view filepath)
29       : wav_reader_(filepath) {}
30   ~WavReaderAdaptor() override = default;
31 
ReadFloatSamples(rtc::ArrayView<float> samples)32   size_t ReadFloatSamples(rtc::ArrayView<float> samples) override {
33     return wav_reader_.ReadSamples(samples.size(), samples.begin());
34   }
35 
ReadInt16Samples(rtc::ArrayView<int16_t> samples)36   size_t ReadInt16Samples(rtc::ArrayView<int16_t> samples) override {
37     return wav_reader_.ReadSamples(samples.size(), samples.begin());
38   }
39 
SampleRate() const40   int SampleRate() const override { return wav_reader_.sample_rate(); }
41 
NumChannels() const42   size_t NumChannels() const override { return wav_reader_.num_channels(); }
43 
NumSamples() const44   size_t NumSamples() const override { return wav_reader_.num_samples(); }
45 
46  private:
47   WavReader wav_reader_;
48 };
49 
50 }  // namespace
51 
52 namespace conversational_speech {
53 
54 WavReaderFactory::WavReaderFactory() = default;
55 
56 WavReaderFactory::~WavReaderFactory() = default;
57 
Create(absl::string_view filepath) const58 std::unique_ptr<WavReaderInterface> WavReaderFactory::Create(
59     absl::string_view filepath) const {
60   return std::unique_ptr<WavReaderAdaptor>(new WavReaderAdaptor(filepath));
61 }
62 
63 }  // namespace conversational_speech
64 }  // namespace test
65 }  // namespace webrtc
66