Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: webrtc/modules/utility/source/audio_frame_operations.cc

Issue 1230503003: Update a ton of audio code to use size_t more correctly and in general reduce (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Resync Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/interface/module_common_types.h" 11 #include "webrtc/modules/interface/module_common_types.h"
12 #include "webrtc/modules/utility/interface/audio_frame_operations.h" 12 #include "webrtc/modules/utility/interface/audio_frame_operations.h"
13 13
14 namespace webrtc { 14 namespace webrtc {
15 15
16 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, 16 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio,
17 int samples_per_channel, 17 size_t samples_per_channel,
18 int16_t* dst_audio) { 18 int16_t* dst_audio) {
19 for (int i = 0; i < samples_per_channel; i++) { 19 for (size_t i = 0; i < samples_per_channel; i++) {
20 dst_audio[2 * i] = src_audio[i]; 20 dst_audio[2 * i] = src_audio[i];
21 dst_audio[2 * i + 1] = src_audio[i]; 21 dst_audio[2 * i + 1] = src_audio[i];
22 } 22 }
23 } 23 }
24 24
25 int AudioFrameOperations::MonoToStereo(AudioFrame* frame) { 25 int AudioFrameOperations::MonoToStereo(AudioFrame* frame) {
26 if (frame->num_channels_ != 1) { 26 if (frame->num_channels_ != 1) {
27 return -1; 27 return -1;
28 } 28 }
29 if ((frame->samples_per_channel_ * 2) >= AudioFrame::kMaxDataSizeSamples) { 29 if ((frame->samples_per_channel_ * 2) >= AudioFrame::kMaxDataSizeSamples) {
30 // Not enough memory to expand from mono to stereo. 30 // Not enough memory to expand from mono to stereo.
31 return -1; 31 return -1;
32 } 32 }
33 33
34 int16_t data_copy[AudioFrame::kMaxDataSizeSamples]; 34 int16_t data_copy[AudioFrame::kMaxDataSizeSamples];
35 memcpy(data_copy, frame->data_, 35 memcpy(data_copy, frame->data_,
36 sizeof(int16_t) * frame->samples_per_channel_); 36 sizeof(int16_t) * frame->samples_per_channel_);
37 MonoToStereo(data_copy, frame->samples_per_channel_, frame->data_); 37 MonoToStereo(data_copy, frame->samples_per_channel_, frame->data_);
38 frame->num_channels_ = 2; 38 frame->num_channels_ = 2;
39 39
40 return 0; 40 return 0;
41 } 41 }
42 42
43 void AudioFrameOperations::StereoToMono(const int16_t* src_audio, 43 void AudioFrameOperations::StereoToMono(const int16_t* src_audio,
44 int samples_per_channel, 44 size_t samples_per_channel,
45 int16_t* dst_audio) { 45 int16_t* dst_audio) {
46 for (int i = 0; i < samples_per_channel; i++) { 46 for (size_t i = 0; i < samples_per_channel; i++) {
47 dst_audio[i] = (src_audio[2 * i] + src_audio[2 * i + 1]) >> 1; 47 dst_audio[i] = (src_audio[2 * i] + src_audio[2 * i + 1]) >> 1;
48 } 48 }
49 } 49 }
50 50
51 int AudioFrameOperations::StereoToMono(AudioFrame* frame) { 51 int AudioFrameOperations::StereoToMono(AudioFrame* frame) {
52 if (frame->num_channels_ != 2) { 52 if (frame->num_channels_ != 2) {
53 return -1; 53 return -1;
54 } 54 }
55 55
56 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); 56 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_);
57 frame->num_channels_ = 1; 57 frame->num_channels_ = 1;
58 58
59 return 0; 59 return 0;
60 } 60 }
61 61
62 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { 62 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) {
63 if (frame->num_channels_ != 2) return; 63 if (frame->num_channels_ != 2) return;
64 64
65 for (int i = 0; i < frame->samples_per_channel_ * 2; i += 2) { 65 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
66 int16_t temp_data = frame->data_[i]; 66 int16_t temp_data = frame->data_[i];
67 frame->data_[i] = frame->data_[i + 1]; 67 frame->data_[i] = frame->data_[i + 1];
68 frame->data_[i + 1] = temp_data; 68 frame->data_[i + 1] = temp_data;
69 } 69 }
70 } 70 }
71 71
72 void AudioFrameOperations::Mute(AudioFrame& frame) { 72 void AudioFrameOperations::Mute(AudioFrame& frame) {
73 memset(frame.data_, 0, sizeof(int16_t) * 73 memset(frame.data_, 0, sizeof(int16_t) *
74 frame.samples_per_channel_ * frame.num_channels_); 74 frame.samples_per_channel_ * frame.num_channels_);
75 } 75 }
76 76
77 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { 77 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
78 if (frame.num_channels_ != 2) { 78 if (frame.num_channels_ != 2) {
79 return -1; 79 return -1;
80 } 80 }
81 81
82 for (int i = 0; i < frame.samples_per_channel_; i++) { 82 for (size_t i = 0; i < frame.samples_per_channel_; i++) {
83 frame.data_[2 * i] = 83 frame.data_[2 * i] =
84 static_cast<int16_t>(left * frame.data_[2 * i]); 84 static_cast<int16_t>(left * frame.data_[2 * i]);
85 frame.data_[2 * i + 1] = 85 frame.data_[2 * i + 1] =
86 static_cast<int16_t>(right * frame.data_[2 * i + 1]); 86 static_cast<int16_t>(right * frame.data_[2 * i + 1]);
87 } 87 }
88 return 0; 88 return 0;
89 } 89 }
90 90
91 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { 91 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) {
92 int32_t temp_data = 0; 92 int32_t temp_data = 0;
93 93
94 // Ensure that the output result is saturated [-32768, +32767]. 94 // Ensure that the output result is saturated [-32768, +32767].
95 for (int i = 0; i < frame.samples_per_channel_ * frame.num_channels_; 95 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_;
96 i++) { 96 i++) {
97 temp_data = static_cast<int32_t>(scale * frame.data_[i]); 97 temp_data = static_cast<int32_t>(scale * frame.data_[i]);
98 if (temp_data < -32768) { 98 if (temp_data < -32768) {
99 frame.data_[i] = -32768; 99 frame.data_[i] = -32768;
100 } else if (temp_data > 32767) { 100 } else if (temp_data > 32767) {
101 frame.data_[i] = 32767; 101 frame.data_[i] = 32767;
102 } else { 102 } else {
103 frame.data_[i] = static_cast<int16_t>(temp_data); 103 frame.data_[i] = static_cast<int16_t>(temp_data);
104 } 104 }
105 } 105 }
106 return 0; 106 return 0;
107 } 107 }
108 108
109 } // namespace webrtc 109 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/utility/interface/file_player.h ('k') | webrtc/modules/utility/source/audio_frame_operations_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698