blob: cc4bc97c30c79b8ff4e56618d04730cfee10b86c [file] [log] [blame]
jan.skoglund@webrtc.orgc3d13d32014-03-10 22:50:19 +00001/*
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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#include "webrtc/modules/audio_coding/neteq/audio_classifier.h"
jan.skoglund@webrtc.orgc3d13d32014-03-10 22:50:19 +000012
13#include <assert.h>
14#include <string.h>
15
16namespace webrtc {
17
18static const int kDefaultSampleRateHz = 48000;
19static const int kDefaultFrameRateHz = 50;
20static const int kDefaultFrameSizeSamples =
21 kDefaultSampleRateHz / kDefaultFrameRateHz;
22static const float kDefaultThreshold = 0.5f;
23
24AudioClassifier::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
38AudioClassifier::~AudioClassifier() {}
39
40bool 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
71} // namespace webrtc