1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_AUDIO_TEST_FAKE_CONSUMER_H_
6 #define SERVICES_AUDIO_TEST_FAKE_CONSUMER_H_
7 
8 #include <vector>
9 
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 
13 namespace media {
14 class AudioBus;
15 }
16 
17 namespace audio {
18 
19 // Consumes and records the audio signal. Then, test procedures may use the
20 // utility methods to analyze the recording.
21 class FakeConsumer {
22  public:
23   FakeConsumer(int channels, int sample_rate);
24 
25   ~FakeConsumer();
26 
27   // Returns the total number of frames recorded (the end position of the
28   // recording).
29   int GetRecordedFrameCount() const;
30 
31   // Throws away all samples recorded so far.
32   void Clear();
33 
34   // Appends the audio signal in |bus| to the recording.
35   void Consume(const media::AudioBus& bus);
36 
37   // Returns true if all (or a part) of the recording in |channel| is flat
38   // (i.e., not oscillating, and effectively silent).
39   bool IsSilent(int channel) const;
40   bool IsSilentInRange(int channel, int begin_frame, int end_frame) const;
41 
42   // Returns the position at which silence in the recording ends, at or after
43   // |begin_frame|. If the entire recording after the given position is silent,
44   // this will return GetRecordedFrameCount().
45   int FindEndOfSilence(int channel, int begin_frame) const;
46 
47   // Returns the amplitude of the given |frequency| in the given |channel| just
48   // before the given |end_frame| position.
49   double ComputeAmplitudeAt(int channel, double frequency, int end_frame) const;
50 
51   // Saves the recorded content to a WAV-format file, overwriting it if it
52   // exists.
53   void SaveToFile(const base::FilePath& path) const;
54 
55  private:
56   const int sample_rate_;
57   std::vector<std::vector<float>> recorded_channel_data_;
58 
59   DISALLOW_COPY_AND_ASSIGN(FakeConsumer);
60 };
61 
62 }  // namespace audio
63 
64 #endif  // SERVICES_AUDIO_TEST_FAKE_CONSUMER_H_
65