1 /*
2 * Copyright (c) 2014 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_coding/neteq/audio_classifier.h"
12
13 #include <assert.h>
14 #include <string.h>
15
16 namespace webrtc {
17
18 static const int kDefaultSampleRateHz = 48000;
19 static const int kDefaultFrameRateHz = 50;
20 static const int kDefaultFrameSizeSamples =
21 kDefaultSampleRateHz / kDefaultFrameRateHz;
22 static const float kDefaultThreshold = 0.5f;
23
AudioClassifier()24 AudioClassifier::AudioClassifier()
25 : analysis_info_(),
26 is_music_(false),
27 music_probability_(0),
28 // This actually assigns the pointer to a static constant struct
29 // rather than creates a struct and |celt_mode_| does not need
30 // to be deleted.
31 celt_mode_(opus_custom_mode_create(kDefaultSampleRateHz,
32 kDefaultFrameSizeSamples,
33 NULL)),
34 analysis_state_() {
35 assert(celt_mode_);
36 }
37
~AudioClassifier()38 AudioClassifier::~AudioClassifier() {}
39
Analysis(const int16_t * input,int input_length,int channels)40 bool AudioClassifier::Analysis(const int16_t* input,
41 int input_length,
42 int channels) {
43 // Must be 20 ms frames at 48 kHz sampling.
44 assert((input_length / channels) == kDefaultFrameSizeSamples);
45
46 // Only mono or stereo are allowed.
47 assert(channels == 1 || channels == 2);
48
49 // Call Opus' classifier, defined in
50 // "third_party/opus/src/src/analysis.h", with lsb_depth = 16.
51 // Also uses a down-mixing function downmix_int, defined in
52 // "third_party/opus/src/src/opus_private.h", with
53 // constants c1 = 0, and c2 = -2.
54 run_analysis(&analysis_state_,
55 celt_mode_,
56 input,
57 kDefaultFrameSizeSamples,
58 kDefaultFrameSizeSamples,
59 0,
60 -2,
61 channels,
62 kDefaultSampleRateHz,
63 16,
64 downmix_int,
65 &analysis_info_);
66 music_probability_ = analysis_info_.music_prob;
67 is_music_ = music_probability_ > kDefaultThreshold;
68 return is_music_;
69 }
70
is_music() const71 bool AudioClassifier::is_music() const {
72 return is_music_;
73 }
74
75 } // namespace webrtc
76