forked from toniebox-reverse-engineering/hackiebox_cfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioOutputResample.cpp
130 lines (111 loc) · 3.27 KB
/
AudioOutputResample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
AudioOutputResample
Adds additional bufferspace to the output chain
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BaseHeader.h"
#include <Arduino.h>
#include "AudioOutputResample.h"
AudioOutputResample::AudioOutputResample(uint32_t maxRate, AudioOutputCC3200I2S *dest)
{
this->sink = dest;
this->leftSample = 0;
this->rightSample = 0;
this->resampleFactor = 1;
this->resampleCount = 0;
originalSampleRate = this->sink->GetRate();
SetMaxRate(maxRate);
}
AudioOutputResample::~AudioOutputResample() { }
bool AudioOutputResample::SetRate(int hz) {
this->originalSampleRate = hz;
if (this->maxSampleRate >= hz) {
this->resampleFactor = 1;
Log.info("AudioOutputResample SetRate=%i", hz);
return this->sink->SetRate(hz);
}
for (this->resampleFactor = 2; this->resampleFactor < 7; this->resampleFactor++) { // 48000/8000
if (this->maxSampleRate >= (hz / this->resampleFactor))
break;
}
Log.info("AudioOutputResample limited SetRate=%i, hz=%i, resampleFactor=%i", hz / this->resampleFactor, hz, this->resampleFactor);
return sink->SetRate(hz / this->resampleFactor);
}
void AudioOutputResample::SetMaxRate(uint32_t hz) {
switch (hz)
{
case 48000:
case 44100:
case 32000:
case 24000:
case 22050:
case 16000:
case 11025:
case 8000:
this->maxSampleRate = hz;
break;
default:
this->maxSampleRate = 48000;
}
SetRate(this->originalSampleRate);
}
uint32_t AudioOutputResample::GetMaxRate() {
return this->maxSampleRate;
}
int AudioOutputResample::GetRate() {
return this->sink->GetRate();
}
bool AudioOutputResample::SetBitsPerSample(int bits)
{
return this->sink->SetBitsPerSample(bits);
}
bool AudioOutputResample::SetChannels(int channels)
{
return this->sink->SetChannels(channels);
}
bool AudioOutputResample::begin()
{
return this->sink->begin();
}
bool AudioOutputResample::ConsumeSample(int16_t sample[2])
{
if (1==0) { //Slow
this->leftSample += sample[0];
this->rightSample += sample[1];
this->resampleCount++;
if (this->resampleCount >= this->resampleFactor) {
int16_t s[2] = {(int16_t)(leftSample/this->resampleFactor), (int16_t)(rightSample/this->resampleFactor)};
if (!sink->ConsumeSample(s)) {
return false;
} else {
this->leftSample = 0;
this->rightSample = 0;
this->resampleCount = 0;
}
}
} else { //Fast
this->resampleCount++;
if (this->resampleCount >= this->resampleFactor) {
if (!sink->ConsumeSample(sample)) {
return false;
} else {
this->resampleCount = 0;
}
}
}
return true;
}
bool AudioOutputResample::stop()
{
return this->sink->stop();
}