Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: webrtc/modules/audio_processing/test/fake_recording_device.cc

Issue 2834643002: audioproc_f with simulated mic analog gain (Closed)
Patch Set: comments addressed Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "webrtc/modules/audio_processing/test/fake_recording_device.h"
12
13 #include "webrtc/base/logging.h"
14
15 namespace webrtc {
16
17 FakeRecordingDevice::FakeRecordingDevice(MicrophoneKind mapping_kind)
18 : mapping_kind_(mapping_kind) {
19 // Create the analog gain simulatory callback depending on the wanted
20 // microphone kind.
21 switch (mapping_kind_) {
22 case MicrophoneKind::kIdentity: {
23 simulator_callback_int16_ = AnalogGainSimulatorIdentityInt16();
peah-webrtc 2017/05/16 12:19:36 These are not used as callbacks, so I don't think
AleBzk 2017/05/17 11:52:23 Acknowledged.
24 simulator_callback_float_ = AnalogGainSimulatorIdentityFloat();
25 break;
26 }
27 case MicrophoneKind::kLinear: {
28 simulator_callback_int16_ = AnalogGainSimulatorLinearInt16();
peah-webrtc 2017/05/16 12:19:36 I would rather have an interface with different im
AleBzk 2017/05/17 11:52:23 Perfect. Let's then go for interface + implementat
29 simulator_callback_float_ = AnalogGainSimulatorLinearFloat();
30 break;
31 }
32 default: { RTC_NOTREACHED(); }
33 }
34 }
35
36 FakeRecordingDevice::~FakeRecordingDevice() = default;
37
38 void FakeRecordingDevice::SimulateAnalogGain(
39 int level, rtc::Optional<int> real_device_level,
40 std::vector<rtc::ArrayView<float>> buffer) {
41 for (size_t i = 0; i < buffer.size(); ++i) {
42 std::transform(
peah-webrtc 2017/05/16 12:19:35 I think std::for_each is better suited here.
AleBzk 2017/05/17 11:52:23 Acknowledged.
43 buffer[i].begin(), buffer[i].end(), buffer[i].begin(),
44 [this, level, real_device_level](float x) {
45 return simulator_callback_float_(x, level, real_device_level);
peah-webrtc 2017/05/16 12:19:36 As you stated offline, this will not scale with me
AleBzk 2017/05/17 11:52:23 Acknowledged.
46 });
47 }
48 }
49
50 void FakeRecordingDevice::SimulateAnalogGain(
peah-webrtc 2017/05/16 12:19:36 Why is this needed as a separate method?
AleBzk 2017/05/17 11:52:23 Please, see my answer to a related question of you
51 int level, rtc::Optional<int> real_device_level,
52 ChannelBuffer<float>* buffer) {
53 std::vector<rtc::ArrayView<float>> buffer_view;
54 for (size_t i = 0; i < buffer->num_channels(); ++i) {
55 buffer_view.emplace_back(buffer->channels()[i], buffer->num_frames());
56 }
57 SimulateAnalogGain(level, real_device_level, buffer_view);
58 }
59
60 void FakeRecordingDevice::SimulateAnalogGain(
61 int level, rtc::Optional<int> real_device_level, AudioFrame* buffer) {
62 const size_t number_of_samples =
63 buffer->samples_per_channel_ * buffer->num_channels_;
64 RTC_DCHECK_LE(number_of_samples, AudioFrame::kMaxDataSizeSamples);
65 std::transform(
peah-webrtc 2017/05/16 12:19:36 std::for_each
AleBzk 2017/05/17 11:52:23 Acknowledged.
66 buffer->data_, buffer->data_ + number_of_samples, buffer->data_,
67 [this, level, real_device_level](int16_t x) {
68 return simulator_callback_int16_(x, level, real_device_level);
69 });
70 }
71
72 SimulatorCallbackInt16 FakeRecordingDevice::AnalogGainSimulatorIdentityInt16() {
73 return [this](int16_t sample, int level,
74 rtc::Optional<int> real_device_level) {
75 return sample;
76 };
77 }
78
79 SimulatorCallbackFloat FakeRecordingDevice::AnalogGainSimulatorIdentityFloat() {
80 return [this](float sample, int level, rtc::Optional<int> real_device_level) {
81 return sample;
82 };
83 }
84
85 SimulatorCallbackInt16 FakeRecordingDevice::AnalogGainSimulatorLinearInt16() {
86 return [this](int16_t sample, int level,
87 rtc::Optional<int> real_device_level) {
88 float sample_f = static_cast<float>(sample);
89
90 // Virtually restore the unmodified microphone level.
91 if (real_device_level) {
92 RTC_DCHECK_GT(*real_device_level, 0)
93 << "zero real device level, cannot undo mic level";
94 sample_f = ClipSample(
95 sample_f * 255.0f / static_cast<float>(*real_device_level));
96 }
97
98 // Simulate the mic gain.
99 return ClipSample(sample_f * static_cast<float>(level) / 255.0f);
100 };
101 }
102
103 SimulatorCallbackFloat FakeRecordingDevice::AnalogGainSimulatorLinearFloat() {
104 return [this](float sample, int level, rtc::Optional<int> real_device_level) {
105 // Virtually restore the unmodified microphone level.
106 if (real_device_level) {
107 RTC_DCHECK_GT(*real_device_level, 0)
108 << "zero real device level, cannot undo mic level";
109 sample = ClipSample(
110 sample * 255.0f / static_cast<float>(*real_device_level));
111 }
112
113 // Simulate the mic gain.
114 return ClipSample(sample * static_cast<float>(level) / 255.0f);
115 };
116 }
117
118 int16_t FakeRecordingDevice::ClipSample(int16_t sample) {
119 return sample < kSampleMinInt16 ?
120 kSampleMinInt16 : (sample > kSampleMaxInt16 ? kSampleMaxInt16 : sample);
peah-webrtc 2017/05/16 12:19:35 Use std::min/std::max instead
peah-webrtc 2017/05/16 12:19:36 Why do you clip the sample value to +-255?
AleBzk 2017/05/17 11:52:23 Done.
AleBzk 2017/05/17 11:52:23 Right, I got confused with the mic level.
121 }
122
123 float FakeRecordingDevice::ClipSample(float sample) {
124 return sample < kSampleMinFloat ?
peah-webrtc 2017/05/16 12:19:36 Use std::min/std::max instead
AleBzk 2017/05/17 11:52:23 Done.
125 kSampleMinFloat : (sample > kSampleMaxFloat ? kSampleMaxFloat : sample);
126 }
127
128 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698