mirror of
https://github.com/pschatzmann/arduino-audio-tools.git
synced 2024-09-21 02:17:31 +00:00
Equilizer 24 and 32 bit support
This commit is contained in:
parent
812b698056
commit
0e77bebb2a
@ -93,7 +93,7 @@ class DecoderL8 : public AudioDecoder {
|
||||
if (!is_signed) {
|
||||
tmp -= 129;
|
||||
}
|
||||
return NumberConverter::clip<int16_t>(tmp * 258);
|
||||
return NumberConverter::clipT<int16_t>(tmp * 258);
|
||||
}
|
||||
|
||||
virtual operator bool() override { return p_print!=nullptr; }
|
||||
@ -168,7 +168,7 @@ class EncoderL8 : public AudioEncoder {
|
||||
operator bool() override { return is_open; }
|
||||
|
||||
int16_t convertSample(int16_t sample) {
|
||||
int16_t tmp = NumberConverter::clip<int8_t>(sample / 258);
|
||||
int16_t tmp = NumberConverter::clipT<int8_t>(sample / 258);
|
||||
if (!is_signed) {
|
||||
tmp += 129;
|
||||
// clip to range
|
||||
|
@ -224,7 +224,7 @@ class Synthesizer : public SoundGenerator<int16_t> {
|
||||
// prevent divide by zero
|
||||
int result = 0;
|
||||
if (count>0){
|
||||
result = NumberConverter::clip<int16_t>(total / count);
|
||||
result = NumberConverter::clipT<int16_t>(total / count);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -167,10 +167,25 @@ class Equilizer3Bands : public ModifyingStream {
|
||||
size_t sample_count = len / sizeof(int16_t);
|
||||
for (size_t j = 0; j < sample_count; j += p_cfg->channels) {
|
||||
for (int ch = 0; ch < p_cfg->channels; ch++) {
|
||||
// p_dataT[j+ch] = sample(state[ch], 1.0 / 32767.0 * p_dataT[j+ch])
|
||||
// * 32767;
|
||||
p_dataT[j + ch] =
|
||||
toInt16(sample(state[ch], toFloat(p_dataT[j + ch])));
|
||||
p_dataT[j + ch] = NumberConverter::fromFloat(sample(state[ch], NumberConverter::toFloat(p_dataT[j + ch], 16)), 16);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case 24: {
|
||||
int24_t *p_dataT = (int24_t *)data;
|
||||
size_t sample_count = len / sizeof(int24_t);
|
||||
for (size_t j = 0; j < sample_count; j += p_cfg->channels) {
|
||||
for (int ch = 0; ch < p_cfg->channels; ch++) {
|
||||
p_dataT[j + ch] = NumberConverter::fromFloat(sample(state[ch], NumberConverter::toFloat(p_dataT[j + ch], 24)), 24);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case 32: {
|
||||
int32_t *p_dataT = (int32_t *)data;
|
||||
size_t sample_count = len / sizeof(int32_t);
|
||||
for (size_t j = 0; j < sample_count; j += p_cfg->channels) {
|
||||
for (int ch = 0; ch < p_cfg->channels; ch++) {
|
||||
p_dataT[j + ch] = NumberConverter::fromFloat(sample(state[ch], NumberConverter::toFloat(p_dataT[j + ch], 32)), 32);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
@ -181,22 +196,6 @@ class Equilizer3Bands : public ModifyingStream {
|
||||
}
|
||||
}
|
||||
|
||||
/// convert float in the range -1 to 1 to a int16 and clip the values that are
|
||||
/// out of range
|
||||
inline int16_t toInt16(float v) {
|
||||
float result = v * 32767.0f;
|
||||
// clip result
|
||||
if (result > 32767) {
|
||||
result = 32767;
|
||||
} else if (result < -32767) {
|
||||
result = -32767;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// convert float in the range -1 to 1 to a int16 and clip the values that are
|
||||
/// out of range
|
||||
inline float toFloat(int16_t v) { return static_cast<float>(v) / 32767.0f; }
|
||||
|
||||
// calculates a single sample using the indicated state
|
||||
float sample(EQSTATE &es, float sample) {
|
||||
|
@ -355,7 +355,7 @@ class NumberConverter {
|
||||
|
||||
/// Clips the value to avoid any over or underflows
|
||||
template <typename T>
|
||||
static T clip(int64_t value){
|
||||
static T clipT(int64_t value){
|
||||
T mv = maxValue(sizeof(T)*8);
|
||||
if (value > mv){
|
||||
return mv;
|
||||
@ -365,11 +365,40 @@ class NumberConverter {
|
||||
return value;
|
||||
}
|
||||
|
||||
inline static int32_t clip(float value, int bits){
|
||||
float mv = maxValue(bits);
|
||||
if (value > mv){
|
||||
return mv;
|
||||
} else if (value < -mv){
|
||||
return -mv;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static float toFloatT(T value) {
|
||||
return static_cast<float>(value) / maxValueT<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static T fromFloatT(float value){
|
||||
return value * maxValueT<T>();
|
||||
}
|
||||
|
||||
inline static float toFloat(int32_t value, int bits) {
|
||||
return static_cast<float>(value) / maxValue(bits);
|
||||
}
|
||||
|
||||
inline static int32_t fromFloat(float value, int bits){
|
||||
return clip(value * maxValue(bits), bits);
|
||||
}
|
||||
|
||||
|
||||
/// Convert a number from one type to another
|
||||
template <typename FromT, typename ToT>
|
||||
static ToT convert(FromT value){
|
||||
int64_t value1 = value;
|
||||
return clip<ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
|
||||
return clipT<ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
|
||||
}
|
||||
|
||||
template <typename FromT, typename ToT>
|
||||
@ -377,7 +406,7 @@ class NumberConverter {
|
||||
float factor = static_cast<float>(maxValueT<ToT>()) / maxValueT<FromT>();
|
||||
float vol_factor = factor * vol;
|
||||
for (int j=0;j<samples;j++){
|
||||
to[j] = clip<ToT>(vol_factor * from[j]);
|
||||
to[j] = clipT<ToT>(vol_factor * from[j]);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user