Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/modules/utility/source/audio_frame_operations.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 3.3 KB
Line 
1/*
2 *  Copyright (c) 2012 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/interface/module_common_types.h"
12#include "webrtc/modules/utility/interface/audio_frame_operations.h"
13
14namespace webrtc {
15
16void AudioFrameOperations::MonoToStereo(const int16_t* src_audio,
17                                        int samples_per_channel,
18                                        int16_t* dst_audio) {
19  for (int i = 0; i < samples_per_channel; i++) {
20    dst_audio[2 * i] = src_audio[i];
21    dst_audio[2 * i + 1] = src_audio[i];
22  }
23}
24
25int AudioFrameOperations::MonoToStereo(AudioFrame* frame) {
26  if (frame->num_channels_ != 1) {
27    return -1;
28  }
29  if ((frame->samples_per_channel_ * 2) >= AudioFrame::kMaxDataSizeSamples) {
30    // Not enough memory to expand from mono to stereo.
31    return -1;
32  }
33
34  int16_t data_copy[AudioFrame::kMaxDataSizeSamples];
35  memcpy(data_copy, frame->data_,
36         sizeof(int16_t) * frame->samples_per_channel_);
37  MonoToStereo(data_copy, frame->samples_per_channel_, frame->data_);
38  frame->num_channels_ = 2;
39
40  return 0;
41}
42
43void AudioFrameOperations::StereoToMono(const int16_t* src_audio,
44                                        int samples_per_channel,
45                                        int16_t* dst_audio) {
46  for (int i = 0; i < samples_per_channel; i++) {
47    dst_audio[i] = (src_audio[2 * i] + src_audio[2 * i + 1]) >> 1;
48  }
49}
50
51int AudioFrameOperations::StereoToMono(AudioFrame* frame) {
52  if (frame->num_channels_ != 2) {
53    return -1;
54  }
55
56  StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_);
57  frame->num_channels_ = 1;
58
59  return 0;
60}
61
62void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) {
63  if (frame->num_channels_ != 2) return;
64
65  for (int i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
66    int16_t temp_data = frame->data_[i];
67    frame->data_[i] = frame->data_[i + 1];
68    frame->data_[i + 1] = temp_data;
69  }
70}
71
72void AudioFrameOperations::Mute(AudioFrame& frame) {
73  memset(frame.data_, 0, sizeof(int16_t) *
74      frame.samples_per_channel_ * frame.num_channels_);
75  frame.energy_ = 0;
76}
77
78int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
79  if (frame.num_channels_ != 2) {
80    return -1;
81  }
82
83  for (int i = 0; i < frame.samples_per_channel_; i++) {
84    frame.data_[2 * i] =
85        static_cast<int16_t>(left * frame.data_[2 * i]);
86    frame.data_[2 * i + 1] =
87        static_cast<int16_t>(right * frame.data_[2 * i + 1]);
88  }
89  return 0;
90}
91
92int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) {
93  int32_t temp_data = 0;
94
95  // Ensure that the output result is saturated [-32768, +32767].
96  for (int i = 0; i < frame.samples_per_channel_ * frame.num_channels_;
97       i++) {
98    temp_data = static_cast<int32_t>(scale * frame.data_[i]);
99    if (temp_data < -32768) {
100      frame.data_[i] = -32768;
101    } else if (temp_data > 32767) {
102      frame.data_[i] = 32767;
103    } else {
104      frame.data_[i] = static_cast<int16_t>(temp_data);
105    }
106  }
107  return 0;
108}
109
110}  // namespace webrtc
Note: See TracBrowser for help on using the repository browser.