| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "ambisonics/stereo_from_soundfield_converter.h" |
| 18 | |
| 19 | #include "base/constants_and_types.h" |
| 20 | #include "dsp/gain.h" |
| 21 | |
| 22 | namespace vraudio { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const float kMidSideChannelGain = 0.5f; |
| 27 | |
| 28 | } // namespace |
| 29 | |
| 30 | void StereoFromSoundfield(const AudioBuffer& soundfield_input, |
| 31 | AudioBuffer* stereo_output) { |
| 32 | DCHECK(stereo_output); |
| 33 | DCHECK_EQ(kNumStereoChannels, stereo_output->num_channels()); |
| 34 | DCHECK_EQ(soundfield_input.num_frames(), stereo_output->num_frames()); |
| 35 | DCHECK_GE(soundfield_input.num_channels(), kNumFirstOrderAmbisonicChannels); |
| 36 | const AudioBuffer::Channel& channel_audio_space_w = soundfield_input[0]; |
| 37 | const AudioBuffer::Channel& channel_audio_space_y = soundfield_input[1]; |
| 38 | AudioBuffer::Channel* left_channel_output = &(*stereo_output)[0]; |
| 39 | AudioBuffer::Channel* right_channel_output = &(*stereo_output)[1]; |
| 40 | // Left = 0.5 * (Mid + Side). |
| 41 | *left_channel_output = channel_audio_space_w; |
| 42 | *left_channel_output += channel_audio_space_y; |
| 43 | ConstantGain(offset_index: 0 /* no offset */, gain: kMidSideChannelGain, input_samples: *left_channel_output, |
| 44 | output_samples: left_channel_output, accumulate_output: false /* accumulate_output */); |
| 45 | // Right = 0.5 * (Mid - Side). |
| 46 | *right_channel_output = channel_audio_space_w; |
| 47 | *right_channel_output -= channel_audio_space_y; |
| 48 | ConstantGain(offset_index: 0 /* no offset */, gain: kMidSideChannelGain, input_samples: *right_channel_output, |
| 49 | output_samples: right_channel_output, accumulate_output: false /* accumulate_output */); |
| 50 | } |
| 51 | |
| 52 | } // namespace vraudio |
| 53 | |