Debian Codegraph @

BambooTracker/chip/resampler.cpp

raw
1 /*
2 * Copyright (C) 2018-2023 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "resampler.hpp"
27 #include <algorithm>
28 #include <iterator>
29 #include "./blip_buf/blip_buf.h"
30
31 namespace chip
32 {
33 AbstractResampler::AbstractResampler()
34 {
35 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
36 destBuf_[pan] = new sample[CHIP_SMPL_BUF_SIZE_]();
37 }
38 }
39
40 AbstractResampler::~AbstractResampler()
41 {
42 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
43 delete[] destBuf_[pan];
44 }
45 }
46
47 void AbstractResampler::init(int srcRate, int destRate, size_t maxDuration)
48 {
49 srcRate_ = srcRate;
50 maxDuration_ = maxDuration;
51 destRate_ = destRate;
52 updateRateRatio();
53 }
54
55 void AbstractResampler::setMaxDuration(size_t maxDuration) noexcept
56 {
57 maxDuration_ = maxDuration;
58 }
59
60 /****************************************/
61 sample** LinearResampler::interpolate(sample** src, size_t nSamples, size_t)
62 {
63 if (srcRate_ == destRate_) return src;
64
65 // Linear interplation
66 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
67 for (size_t n = 0; n < nSamples; ++n) {
68 float curnf = n * rateRatio_;
69 int curni = static_cast<int>(curnf);
70 float sub = curnf - curni;
71 if (sub) {
72 destBuf_[pan][n] = static_cast<sample>(src[pan][curni] + (src[pan][curni + 1] - src[pan][curni]) * sub);
73 }
74 else /* if (sub == 0) */ {
75 destBuf_[pan][n] = src[pan][curni];
76 }
77 }
78 }
79
80 return destBuf_;
81 }
82
83 /****************************************/
84 BlipResampler::BlipResampler(bool fast)
85 {
86 addDelta = fast ? blip_add_delta_fast : blip_add_delta;
87
88 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
89 ch_[pan].blipBuf_ = blip_new(CHIP_SMPL_BUF_SIZE_);
90 }
91 }
92
93 BlipResampler::~BlipResampler()
94 {
95 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
96 blip_delete(ch_[pan].blipBuf_);
97 }
98 }
99
100 void BlipResampler::init(int srcRate, int destRate, size_t maxDuration)
101 {
102 AbstractResampler::init(srcRate, destRate, maxDuration);
103
104 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
105 auto& ch = ch_[pan];
106 blip_clear(ch.blipBuf_);
107 blip_set_rates(ch.blipBuf_, srcRate, destRate);
108 ch.prevSample_ = 0;
109 }
110 }
111
112 void BlipResampler::reset()
113 {
114 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
115 blip_clear(ch_[pan].blipBuf_);
116 }
117 }
118
119 size_t BlipResampler::calculateInternalSampleSize(size_t nSamples, bool& ok)
120 {
121 if (srcRate_ == destRate_) {
122 ok = true;
123 return nSamples;
124 }
125 else {
126 int clocks = blip_clocks_needed(ch_[0].blipBuf_, nSamples - blip_samples_avail(ch_[0].blipBuf_));
127 ok = clocks >= 0;
128 return clocks;
129 }
130 }
131
132 sample** BlipResampler::interpolate(sample** src, size_t nSamples, size_t intrSize)
133 {
134 if (srcRate_ == destRate_) return src;
135
136 short tmpBuf[CHIP_SMPL_BUF_SIZE_];
137 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
138 auto& ch = ch_[pan];
139 if (ch.blipBuf_ == nullptr) continue;
140
141 auto srcBuf = src[pan];
142 for (size_t i = 0; i < intrSize; ++i) {
143 addDelta(ch.blipBuf_, i, srcBuf[i] - ch.prevSample_);
144 ch.prevSample_ = srcBuf[i];
145 }
146 blip_end_frame(ch.blipBuf_, intrSize);
147
148 size_t size = std::min<size_t>(nSamples, blip_samples_avail(ch.blipBuf_));
149 blip_read_samples(ch.blipBuf_, tmpBuf, size, 0);
150 std::copy_n(std::begin(tmpBuf), size, destBuf_[pan]);
151 }
152
153 return destBuf_;
154 }
155 }