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 #include "webrtc/modules/audio_processing/test/fake_rec_device_identity.h" |
| 13 #include "webrtc/modules/audio_processing/test/fake_rec_device_linear.h" |
| 14 |
| 15 #include "webrtc/base/logging.h" |
| 16 #include "webrtc/base/ptr_util.h" |
| 17 |
| 18 namespace webrtc { |
| 19 namespace test { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const int16_t kSampleMinInt16 = 32767; |
| 24 const int16_t kSampleMaxInt16 = -32768; |
| 25 const float kSampleMinFloat = -1.0f; |
| 26 const float kSampleMaxFloat = 1.0f; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 FakeRecordingDevice::FakeRecordingDevice(int initial_mic_level) |
| 31 : mic_level_(initial_mic_level) {} |
| 32 |
| 33 std::unique_ptr<FakeRecordingDevice> FakeRecordingDevice::GetFakeRecDevice( |
| 34 FakeRecordingDevice::DeviceKind kind, int initial_mic_level) { |
| 35 switch (kind) { |
| 36 case FakeRecordingDevice::DeviceKind::IDENTITY: { |
| 37 return rtc::MakeUnique<FakeRecordingDeviceIdentity>(initial_mic_level); |
| 38 } |
| 39 case FakeRecordingDevice::DeviceKind::LINEAR: { |
| 40 return rtc::MakeUnique<FakeRecordingDeviceLinear>(initial_mic_level); |
| 41 } |
| 42 default: { |
| 43 RTC_NOTREACHED(); |
| 44 } |
| 45 } |
| 46 } |
| 47 |
| 48 FakeRecordingDevice::~FakeRecordingDevice() = default; |
| 49 |
| 50 void FakeRecordingDevice::set_mic_level(int level) { |
| 51 mic_level_ = level; |
| 52 } |
| 53 |
| 54 int FakeRecordingDevice::mic_level() const { |
| 55 return mic_level_; |
| 56 } |
| 57 |
| 58 void FakeRecordingDevice::set_undo_mic_level(rtc::Optional<int> level) { |
| 59 undo_mic_level_ = level; |
| 60 } |
| 61 |
| 62 rtc::Optional<int> FakeRecordingDevice::undo_mic_level() const { |
| 63 return undo_mic_level_; |
| 64 } |
| 65 |
| 66 void FakeRecordingDevice::SimulateAnalogGain( |
| 67 std::vector<rtc::ArrayView<float>> buffer) { |
| 68 for (size_t i = 0; i < buffer.size(); ++i) { |
| 69 std::for_each(buffer[i].begin(), buffer[i].end(), |
| 70 [this](float& x) { ModifySampleFloat(&x); }); |
| 71 } |
| 72 } |
| 73 |
| 74 void FakeRecordingDevice::SimulateAnalogGain(ChannelBuffer<float>* buffer) { |
| 75 std::vector<rtc::ArrayView<float>> buffer_view; |
| 76 for (size_t i = 0; i < buffer->num_channels(); ++i) { |
| 77 buffer_view.emplace_back(buffer->channels()[i], buffer->num_frames()); |
| 78 } |
| 79 SimulateAnalogGain(buffer_view); |
| 80 } |
| 81 |
| 82 void FakeRecordingDevice::SimulateAnalogGain(AudioFrame* buffer) { |
| 83 const size_t number_of_samples = |
| 84 buffer->samples_per_channel_ * buffer->num_channels_; |
| 85 RTC_DCHECK_LE(number_of_samples, AudioFrame::kMaxDataSizeSamples); |
| 86 std::for_each(buffer->data_, buffer->data_ + number_of_samples, |
| 87 [this](int16_t& x) { ModifySampleInt16(&x); }); |
| 88 } |
| 89 |
| 90 int16_t FakeRecordingDevice::ClipSampleInt16(int16_t sample) { |
| 91 return std::max(std::min(sample, kSampleMaxInt16), kSampleMinInt16); |
| 92 } |
| 93 |
| 94 float FakeRecordingDevice::ClipSampleFloat(float sample) { |
| 95 return std::max(std::min(sample, kSampleMaxFloat), kSampleMinFloat); |
| 96 } |
| 97 |
| 98 } // namespace test |
| 99 } // namespace webrtc |
OLD | NEW |