blob: 6c59e0b123937e1c1bbe97c974af87d18370ad9b [file] [log] [blame]
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +00001/*
2 * Copyright (c) 2013 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/common_audio/resampler/include/push_resampler.h"
12
13#include <cstring>
14
15#include "webrtc/common_audio/include/audio_util.h"
16#include "webrtc/common_audio/resampler/include/resampler.h"
17#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
18
19namespace webrtc {
20
21PushResampler::PushResampler()
22 // Requires valid values at construction, so give it something arbitrary.
23 : resampler_(new Resampler(48000, 48000, kResamplerSynchronous)),
24 sinc_resampler_(NULL),
25 sinc_resampler_right_(NULL),
26 src_sample_rate_hz_(0),
27 dst_sample_rate_hz_(0),
28 num_channels_(0),
29 use_sinc_resampler_(false),
30 src_left_(NULL),
31 src_right_(NULL),
32 dst_left_(NULL),
33 dst_right_(NULL) {
34}
35
36PushResampler::~PushResampler() {
37}
38
39int PushResampler::InitializeIfNeeded(int src_sample_rate_hz,
40 int dst_sample_rate_hz,
41 int num_channels) {
42 if (src_sample_rate_hz == src_sample_rate_hz_ &&
43 dst_sample_rate_hz == dst_sample_rate_hz_ &&
44 num_channels == num_channels_) {
45 // No-op if settings haven't changed.
46 return 0;
47 }
48
49 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 ||
50 num_channels <= 0 || num_channels > 2) {
51 return -1;
52 }
53
54 src_sample_rate_hz_ = src_sample_rate_hz;
55 dst_sample_rate_hz_ = dst_sample_rate_hz;
56 num_channels_ = num_channels;
57
58 const ResamplerType resampler_type =
59 num_channels == 1 ? kResamplerSynchronous : kResamplerSynchronousStereo;
60 if (resampler_->Reset(src_sample_rate_hz, dst_sample_rate_hz,
61 resampler_type) == 0) {
62 // The resampler supports these rates.
63 use_sinc_resampler_ = false;
64 return 0;
65 }
66
67 use_sinc_resampler_ = true;
68 const int src_size_10ms_mono = src_sample_rate_hz / 100;
69 const int dst_size_10ms_mono = dst_sample_rate_hz / 100;
70 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
71 dst_size_10ms_mono));
72 if (num_channels_ == 2) {
73 src_left_.reset(new int16_t[src_size_10ms_mono]);
74 src_right_.reset(new int16_t[src_size_10ms_mono]);
75 dst_left_.reset(new int16_t[dst_size_10ms_mono]);
76 dst_right_.reset(new int16_t[dst_size_10ms_mono]);
77 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
78 dst_size_10ms_mono));
79 }
80
81 return 0;
82}
83
84int PushResampler::Resample(const int16_t* src, int src_length,
85 int16_t* dst, int dst_capacity) {
86 const int src_size_10ms = src_sample_rate_hz_ * num_channels_ / 100;
87 const int dst_size_10ms = dst_sample_rate_hz_ * num_channels_ / 100;
88 if (src_length != src_size_10ms || dst_capacity < dst_size_10ms) {
89 return -1;
90 }
91
92 if (use_sinc_resampler_) {
93 return ResampleSinc(src, src_length, dst, dst_capacity);
94 }
95
96 int resulting_length = 0;
97 if (resampler_->Push(src, src_length, dst, dst_capacity,
98 resulting_length) != 0) {
99 return -1;
100 }
101 return resulting_length;
102}
103
104int PushResampler::ResampleSinc(const int16_t* src, int src_length,
105 int16_t* dst, int dst_capacity) {
106 if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
107 // The old resampler provides this memcpy facility in the case of matching
108 // sample rates, so reproduce it here for the sinc resampler.
109 memcpy(dst, src, src_length * sizeof(int16_t));
110 return src_length;
111 }
112 if (num_channels_ == 2) {
113 const int src_length_mono = src_length / num_channels_;
114 const int dst_capacity_mono = dst_capacity / num_channels_;
115 int16_t* deinterleaved[] = {src_left_.get(), src_right_.get()};
116 Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
117
118 int dst_length_mono =
119 sinc_resampler_->Resample(src_left_.get(), src_length_mono,
120 dst_left_.get(), dst_capacity_mono);
121 sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
122 dst_right_.get(), dst_capacity_mono);
123
124 deinterleaved[0] = dst_left_.get();
125 deinterleaved[1] = dst_right_.get();
126 Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
127 return dst_length_mono * num_channels_;
128 } else {
129 return sinc_resampler_->Resample(src, src_length, dst, dst_capacity);
130 }
131}
132
133} // namespace webrtc