Chromium Code Reviews| OLD | NEW |
|---|---|
| (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(LevelToScalingMappingKind mapping_kind) | |
| 18 : mapping_kind_(mapping_kind) {} | |
| 19 | |
| 20 FakeRecordingDevice::~FakeRecordingDevice() = default; | |
| 21 | |
| 22 void FakeRecordingDevice::SimulateAnalogGain( | |
| 23 std::vector<rtc::ArrayView<const float>> src, | |
| 24 std::vector<rtc::ArrayView<float>> dest, | |
| 25 int level, | |
| 26 int real_device_level) { | |
| 27 RTC_DCHECK_EQ(src.size(), dest.size()); | |
| 28 const float scaling_factor = ComputeCompoundScalingFactor( | |
| 29 level, real_device_level); | |
| 30 | |
| 31 for (size_t i = 0; i < src.size(); ++i) { | |
| 32 RTC_DCHECK_EQ(src[i].size(), dest[i].size()); | |
| 33 std::transform(src[i].begin(), src[i].end(), dest[i].begin(), | |
| 34 [scaling_factor](float x) { return scaling_factor * x; }); | |
|
peah-webrtc
2017/05/05 20:25:21
This scaling could create absolute sample values a
AleBzk
2017/05/16 08:53:03
Good point. And we should apply saturation twice:
peah-webrtc
2017/05/16 12:19:35
I don't see why we need the hard-clipping in step
| |
| 35 } | |
| 36 } | |
| 37 | |
| 38 void FakeRecordingDevice::SimulateAnalogGain(const AudioFrame* src, | |
| 39 AudioFrame* dest, | |
| 40 int level, | |
| 41 int real_device_level) { | |
| 42 RTC_DCHECK_EQ(src->num_channels_, dest->num_channels_); | |
| 43 RTC_DCHECK_EQ(src->samples_per_channel_, dest->samples_per_channel_); | |
| 44 const size_t number_of_samples = | |
| 45 src->samples_per_channel_ * src->num_channels_; | |
| 46 RTC_DCHECK_LE(number_of_samples, AudioFrame::kMaxDataSizeSamples); | |
| 47 const float scaling_factor = ComputeCompoundScalingFactor( | |
| 48 level, real_device_level); | |
| 49 | |
| 50 std::transform(src->data_, src->data_ + number_of_samples, dest->data_, | |
| 51 [scaling_factor](int16_t x) { | |
| 52 return rtc::saturated_cast<int16_t>(scaling_factor * x); | |
|
peah-webrtc
2017/05/05 20:25:21
This simulation of the microphone scaling is too s
AleBzk
2017/05/16 08:53:03
Great point! Thanks.
I redesigned what goes on be
| |
| 53 }); | |
| 54 } | |
| 55 | |
| 56 float FakeRecordingDevice::ComputeCompoundScalingFactor( | |
| 57 int level, int real_device_level) const { | |
| 58 const float scaling_factor = GetScalingFactor(level); | |
| 59 const float undo_factor = (real_device_level == kRealDeviceLevelUnknown) ? | |
| 60 1.0f : GetScalingFactor(real_device_level); | |
| 61 | |
| 62 if (undo_factor == 0) { | |
| 63 LOG(LS_VERBOSE) << "[fake rec device] Cannot undo analog level of 0."; | |
|
peah-webrtc
2017/05/05 20:25:21
As this is only simulation/testing code, you shoul
aleloi
2017/05/08 10:15:23
I think a level of 0 can appear in real recordings
peah-webrtc
2017/05/08 11:41:33
No, nothing that occurs in real recordings should
AleBzk
2017/05/16 08:53:03
I'm a bit confused here.
We shouldn't include erro
peah-webrtc
2017/05/16 12:19:35
The action to take there is totally dependent on t
| |
| 64 return scaling_factor; | |
| 65 } else { | |
| 66 return scaling_factor / undo_factor; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 float FakeRecordingDevice::GetScalingFactor(int level) const { | |
| 71 RTC_DCHECK_LE(0, level); | |
| 72 RTC_DCHECK_LE(level, 255); | |
| 73 | |
| 74 switch (mapping_kind_) { | |
| 75 case LevelToScalingMappingKind::kLinear: { | |
| 76 return static_cast<float>(level) / 255.0f; | |
| 77 } | |
| 78 case LevelToScalingMappingKind::kIdentity: { | |
| 79 return 1.0f; | |
| 80 } | |
| 81 default: { RTC_NOTREACHED(); } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 } // namespace webrtc | |
| OLD | NEW |