blob: 29944187d82913d4c4d07f8297c7cbad54609c00 [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <string.h>
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000014
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()
andrew@webrtc.org31628aa2013-10-22 12:50:00 +000022 : src_sample_rate_hz_(0),
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000023 dst_sample_rate_hz_(0),
24 num_channels_(0),
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000025 src_left_(NULL),
26 src_right_(NULL),
27 dst_left_(NULL),
28 dst_right_(NULL) {
29}
30
31PushResampler::~PushResampler() {
32}
33
34int PushResampler::InitializeIfNeeded(int src_sample_rate_hz,
35 int dst_sample_rate_hz,
36 int num_channels) {
37 if (src_sample_rate_hz == src_sample_rate_hz_ &&
38 dst_sample_rate_hz == dst_sample_rate_hz_ &&
andrew@webrtc.orgb86fbaf2013-07-25 22:04:30 +000039 num_channels == num_channels_)
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000040 // No-op if settings haven't changed.
41 return 0;
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000042
43 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 ||
andrew@webrtc.orgb86fbaf2013-07-25 22:04:30 +000044 num_channels <= 0 || num_channels > 2)
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000045 return -1;
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000046
47 src_sample_rate_hz_ = src_sample_rate_hz;
48 dst_sample_rate_hz_ = dst_sample_rate_hz;
49 num_channels_ = num_channels;
50
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000051 const int src_size_10ms_mono = src_sample_rate_hz / 100;
52 const int dst_size_10ms_mono = dst_sample_rate_hz / 100;
53 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
54 dst_size_10ms_mono));
55 if (num_channels_ == 2) {
56 src_left_.reset(new int16_t[src_size_10ms_mono]);
57 src_right_.reset(new int16_t[src_size_10ms_mono]);
58 dst_left_.reset(new int16_t[dst_size_10ms_mono]);
59 dst_right_.reset(new int16_t[dst_size_10ms_mono]);
60 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
61 dst_size_10ms_mono));
62 }
63
64 return 0;
65}
66
67int PushResampler::Resample(const int16_t* src, int src_length,
68 int16_t* dst, int dst_capacity) {
69 const int src_size_10ms = src_sample_rate_hz_ * num_channels_ / 100;
70 const int dst_size_10ms = dst_sample_rate_hz_ * num_channels_ / 100;
andrew@webrtc.orgb86fbaf2013-07-25 22:04:30 +000071 if (src_length != src_size_10ms || dst_capacity < dst_size_10ms)
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000072 return -1;
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000073
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000074 if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
75 // The old resampler provides this memcpy facility in the case of matching
76 // sample rates, so reproduce it here for the sinc resampler.
77 memcpy(dst, src, src_length * sizeof(int16_t));
78 return src_length;
79 }
80 if (num_channels_ == 2) {
81 const int src_length_mono = src_length / num_channels_;
82 const int dst_capacity_mono = dst_capacity / num_channels_;
83 int16_t* deinterleaved[] = {src_left_.get(), src_right_.get()};
84 Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
85
86 int dst_length_mono =
87 sinc_resampler_->Resample(src_left_.get(), src_length_mono,
88 dst_left_.get(), dst_capacity_mono);
89 sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
90 dst_right_.get(), dst_capacity_mono);
91
92 deinterleaved[0] = dst_left_.get();
93 deinterleaved[1] = dst_right_.get();
94 Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
95 return dst_length_mono * num_channels_;
96 } else {
97 return sinc_resampler_->Resample(src, src_length, dst, dst_capacity);
98 }
99}
100
101} // namespace webrtc