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 // |level| must be within [0, 255]. |
| 23 void FakeRecordingDevice::set_analog_level(int level) { |
| 24 RTC_DCHECK_LE(0, level); |
| 25 RTC_DCHECK_LE(level, 255); |
| 26 level_ = level; |
| 27 } |
| 28 |
| 29 int FakeRecordingDevice::analog_level() const { |
| 30 return level_; |
| 31 } |
| 32 |
| 33 void FakeRecordingDevice::ProcessStream( |
| 34 std::vector<rtc::ArrayView<const float>> src, |
| 35 std::vector<rtc::ArrayView<float>> dest) { |
| 36 RTC_DCHECK_EQ(src.size(), dest.size()); |
| 37 |
| 38 const float scaling_factor = ComputeCompoundScalingFactor(); |
| 39 |
| 40 for (size_t i = 0; i < src.size(); ++i) { |
| 41 RTC_DCHECK_EQ(src[i].size(), dest[i].size()); |
| 42 std::transform(src[i].begin(), src[i].end(), dest[i].begin(), |
| 43 [scaling_factor](float x) { return scaling_factor * x; }); |
| 44 } |
| 45 real_device_level_.reset(); |
| 46 } |
| 47 |
| 48 void FakeRecordingDevice::ProcessStream(const AudioFrame* src, |
| 49 AudioFrame* dest) { |
| 50 RTC_DCHECK_EQ(src->num_channels_, dest->num_channels_); |
| 51 RTC_DCHECK_EQ(src->samples_per_channel_, dest->samples_per_channel_); |
| 52 const size_t number_of_samples = |
| 53 src->samples_per_channel_ * src->num_channels_; |
| 54 RTC_DCHECK_LE(number_of_samples, AudioFrame::kMaxDataSizeSamples); |
| 55 const float scaling_factor = ComputeCompoundScalingFactor(); |
| 56 |
| 57 std::transform(src->data_, src->data_ + number_of_samples, dest->data_, |
| 58 [scaling_factor](int16_t x) { |
| 59 return rtc::saturated_cast<int16_t>(scaling_factor * x); |
| 60 }); |
| 61 |
| 62 real_device_level_.reset(); |
| 63 } |
| 64 |
| 65 void FakeRecordingDevice::NotifyAudioDeviceLevel(int level) { |
| 66 real_device_level_.emplace(level); |
| 67 } |
| 68 |
| 69 float FakeRecordingDevice::ComputeCompoundScalingFactor() { |
| 70 const float level_scaling_factor = GetScalingFactor(level_); |
| 71 const float undo_factor = |
| 72 real_device_level_ ? GetScalingFactor(*real_device_level_) : 1.0f; |
| 73 |
| 74 if (undo_factor == 0) { |
| 75 LOG(LS_INFO) << "[fake rec device] Cannot undo analog level of 0."; |
| 76 return level_scaling_factor; |
| 77 } else { |
| 78 return level_scaling_factor / undo_factor; |
| 79 } |
| 80 } |
| 81 |
| 82 float FakeRecordingDevice::GetScalingFactor(int level) const { |
| 83 switch (mapping_kind_) { |
| 84 case LevelToScalingMappingKind::kLinear: { |
| 85 return static_cast<float>(level) / 255.0f; |
| 86 } |
| 87 default: { RTC_NOTREACHED(); } |
| 88 } |
| 89 } |
| 90 |
| 91 } // namespace webrtc |
OLD | NEW |