xref: /aosp_15_r20/external/webrtc/modules/audio_processing/test/fake_recording_device.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #ifndef MODULES_AUDIO_PROCESSING_TEST_FAKE_RECORDING_DEVICE_H_
12 #define MODULES_AUDIO_PROCESSING_TEST_FAKE_RECORDING_DEVICE_H_
13 
14 #include <algorithm>
15 #include <memory>
16 #include <vector>
17 
18 #include "api/array_view.h"
19 #include "common_audio/channel_buffer.h"
20 #include "rtc_base/checks.h"
21 
22 namespace webrtc {
23 namespace test {
24 
25 class FakeRecordingDeviceWorker;
26 
27 // Class for simulating a microphone with analog gain.
28 //
29 // The intended modes of operation are the following:
30 //
31 // FakeRecordingDevice fake_mic(255, 1);
32 //
33 // fake_mic.SetMicLevel(170);
34 // fake_mic.SimulateAnalogGain(buffer);
35 //
36 // When the mic level to undo is known:
37 //
38 // fake_mic.SetMicLevel(170);
39 // fake_mic.SetUndoMicLevel(30);
40 // fake_mic.SimulateAnalogGain(buffer);
41 //
42 // The second option virtually restores the unmodified microphone level. Calling
43 // SimulateAnalogGain() will first "undo" the gain applied by the real
44 // microphone (e.g., 30).
45 class FakeRecordingDevice final {
46  public:
47   FakeRecordingDevice(int initial_mic_level, int device_kind);
48   ~FakeRecordingDevice();
49 
50   int MicLevel() const;
51   void SetMicLevel(int level);
52   void SetUndoMicLevel(int level);
53 
54   // Simulates the analog gain.
55   // If `real_device_level` is a valid level, the unmodified mic signal is
56   // virtually restored. To skip the latter step set `real_device_level` to
57   // an empty value.
58   void SimulateAnalogGain(rtc::ArrayView<int16_t> buffer);
59 
60   // Simulates the analog gain.
61   // If `real_device_level` is a valid level, the unmodified mic signal is
62   // virtually restored. To skip the latter step set `real_device_level` to
63   // an empty value.
64   void SimulateAnalogGain(ChannelBuffer<float>* buffer);
65 
66  private:
67   // Fake recording device worker.
68   std::unique_ptr<FakeRecordingDeviceWorker> worker_;
69 };
70 
71 }  // namespace test
72 }  // namespace webrtc
73 
74 #endif  // MODULES_AUDIO_PROCESSING_TEST_FAKE_RECORDING_DEVICE_H_
75