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