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

Side by Side Diff: webrtc/modules/audio_processing/vad/standalone_vad.cc

Issue 1228803003: Update audio code to use size_t more correctly, (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Review comments Created 5 years, 5 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
(...skipping 24 matching lines...) Expand all
35 35
36 int err = WebRtcVad_Init(vad); 36 int err = WebRtcVad_Init(vad);
37 err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode); 37 err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode);
38 if (err != 0) { 38 if (err != 0) {
39 WebRtcVad_Free(vad); 39 WebRtcVad_Free(vad);
40 return nullptr; 40 return nullptr;
41 } 41 }
42 return new StandaloneVad(vad); 42 return new StandaloneVad(vad);
43 } 43 }
44 44
45 int StandaloneVad::AddAudio(const int16_t* data, int length) { 45 int StandaloneVad::AddAudio(const int16_t* data, size_t length) {
46 if (length != kLength10Ms) 46 if (length != kLength10Ms)
47 return -1; 47 return -1;
48 48
49 if (index_ + length > kLength10Ms * kMaxNum10msFrames) 49 if (index_ + length > kLength10Ms * kMaxNum10msFrames)
50 // Reset the buffer if it's full. 50 // Reset the buffer if it's full.
51 // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we 51 // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we
52 // can forgo the buffering. 52 // can forgo the buffering.
53 index_ = 0; 53 index_ = 0;
54 54
55 memcpy(&buffer_[index_], data, sizeof(int16_t) * length); 55 memcpy(&buffer_[index_], data, sizeof(int16_t) * length);
56 index_ += length; 56 index_ += length;
57 return 0; 57 return 0;
58 } 58 }
59 59
60 int StandaloneVad::GetActivity(double* p, int length_p) { 60 int StandaloneVad::GetActivity(double* p, size_t length_p) {
61 if (index_ == 0) 61 if (index_ == 0)
62 return -1; 62 return -1;
63 63
64 const int num_frames = index_ / kLength10Ms; 64 const size_t num_frames = index_ / kLength10Ms;
65 if (num_frames > length_p) 65 if (num_frames > length_p)
66 return -1; 66 return -1;
67 assert(WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_) == 0); 67 assert(WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_) == 0);
68 68
69 int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_); 69 int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_);
70 if (activity < 0) 70 if (activity < 0)
71 return -1; 71 return -1;
72 else if (activity == 0) 72 else if (activity == 0)
73 p[0] = 0.01; // Arbitrary but small and non-zero. 73 p[0] = 0.01; // Arbitrary but small and non-zero.
74 else 74 else
75 p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities. 75 p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities.
76 for (int n = 1; n < num_frames; n++) 76 for (size_t n = 1; n < num_frames; n++)
77 p[n] = p[0]; 77 p[n] = p[0];
78 // Reset the buffer to start from the beginning. 78 // Reset the buffer to start from the beginning.
79 index_ = 0; 79 index_ = 0;
80 return activity; 80 return activity;
81 } 81 }
82 82
83 int StandaloneVad::set_mode(int mode) { 83 int StandaloneVad::set_mode(int mode) {
84 if (mode < 0 || mode > 3) 84 if (mode < 0 || mode > 3)
85 return -1; 85 return -1;
86 if (WebRtcVad_set_mode(vad_, mode) != 0) 86 if (WebRtcVad_set_mode(vad_, mode) != 0)
87 return -1; 87 return -1;
88 88
89 mode_ = mode; 89 mode_ = mode;
90 return 0; 90 return 0;
91 } 91 }
92 92
93 } // namespace webrtc 93 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/vad/standalone_vad.h ('k') | webrtc/modules/audio_processing/vad/standalone_vad_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698