You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
5.8 KiB

#include <Audio.h>
#include "AudioSampleSdtest1.h"
#define EQ_TYPE_0 FILTER_HIPASS
#define EQ_CENTER_FRQ_0 15.0
#define EQ_BW_0 2
#define EQ_TYPE_1 FILTER_LOSHELF
#define EQ_CENTER_FRQ_1 120.0
#define EQ_BW_1 2
#define EQ_TYPE_2 FILTER_PARAEQ
#define EQ_CENTER_FRQ_2 220.0
#define EQ_BW_2 2
#define EQ_TYPE_3 FILTER_PARAEQ
#define EQ_CENTER_FRQ_3 1000.0
#define EQ_BW_3 2
#define EQ_TYPE_4 FILTER_PARAEQ
#define EQ_CENTER_FRQ_4 2000.0
#define EQ_BW_4 2
#define EQ_TYPE_5 FILTER_HISHELF
#define EQ_CENTER_FRQ_5 7000.0
#define EQ_BW_5 2
#define EQ_TYPE_6 FILTER_LOPASS
#define EQ_CENTER_FRQ_6 18000.0
#define EQ_BW_6 2
class AudioControlSGTL5000Plus : public AudioControlSGTL5000
{
public:
AudioControlSGTL5000Plus(uint8_t n = 7) {
num_bands = constrain(n, 1, 7);
init_parametric_eq();
};
void setEQType(uint8_t band, uint8_t ft);
void setEQFc(uint8_t band, float frq);
void setEQQ(uint8_t band, float q);
void setEQBandwidth(uint8_t band, float bw);
void setEQGain(uint8_t band, float gain);
void commitFilter(uint8_t band);
void show_params(uint8_t band);
private:
void init_parametric_eq(void);
uint8_t num_bands;
uint8_t* filter_type;
float* Fc;
float* Q;
float* peakGainDB;
};
AudioPlayMemory playMem1;
AudioOutputI2S audioOutput;
AudioConnection patchCord1(playMem1, 0, audioOutput, 0);
AudioConnection patchCord2(playMem1, 1, audioOutput, 1);
AudioControlSGTL5000Plus sgtl5000;
// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// Use these with the Teensy 3.5 & 3.6 SD card
//#define SDCARD_CS_PIN BUILTIN_SDCARD
//#define SDCARD_MOSI_PIN 11 // not actually used
//#define SDCARD_SCK_PIN 13 // not actually used
// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN 4
//#define SDCARD_MOSI_PIN 11
//#define SDCARD_SCK_PIN 13
void AudioControlSGTL5000Plus::show_params(uint8_t band)
{
Serial.println();
Serial.print(F("Band: "));
Serial.print(band, DEC);
Serial.print(F(" Type:"));
Serial.print(filter_type[band - 1], DEC);
Serial.print(F(" Fc:"));
Serial.print(Fc[band - 1], DEC);
Serial.print(F(" Q:"));
Serial.print(Q[band - 1], DEC);
Serial.print(F(" peakGainDB:"));
Serial.print(peakGainDB[band - 1], DEC);
Serial.println();
}
void AudioControlSGTL5000Plus::init_parametric_eq(void)
{
eqSelect(PARAMETRIC_EQUALIZER);
eqFilterCount(num_bands);
filter_type = new uint8_t[num_bands];
Fc = new float[num_bands];
Q = new float[num_bands];
peakGainDB = new float[num_bands];
setEQType(1, EQ_TYPE_0);
setEQFc(1, EQ_CENTER_FRQ_0);
setEQBandwidth(1, EQ_BW_0);
setEQGain(1, 0.0);
if (num_bands > 1)
{
setEQType(2, EQ_TYPE_1);
setEQFc(2, EQ_CENTER_FRQ_1);
setEQBandwidth(2, EQ_BW_1);
setEQGain(2, 0.0);
commitFilter(2);
}
if (num_bands > 2)
{
setEQType(3, EQ_TYPE_2);
setEQFc(3, EQ_CENTER_FRQ_2);
setEQBandwidth(3, EQ_BW_2);
setEQGain(3, 0.0);
commitFilter(3);
}
if (num_bands > 3)
{
setEQType(4, EQ_TYPE_3);
setEQFc(4, EQ_CENTER_FRQ_3);
setEQBandwidth(4, EQ_BW_3);
setEQGain(4, 0.0);
commitFilter(4);
}
if (num_bands > 4)
{
setEQType(5, EQ_TYPE_4);
setEQFc(5, EQ_CENTER_FRQ_4);
setEQBandwidth(5, EQ_BW_4);
setEQGain(5, 0.0);
commitFilter(5);
}
if (num_bands > 5)
{
setEQType(6, EQ_TYPE_5);
setEQFc(6, EQ_CENTER_FRQ_5);
setEQBandwidth(6, EQ_BW_5);
setEQGain(6, 0.0);
commitFilter(6);
}
if (num_bands > 6)
{
setEQType(7, EQ_TYPE_6);
setEQFc(7, EQ_CENTER_FRQ_6);
setEQBandwidth(7, EQ_BW_6);
setEQGain(7, 0.0);
commitFilter(7);
}
}
void AudioControlSGTL5000Plus::setEQType(uint8_t band, uint8_t ft)
{
if (filter_type)
{
band = constrain(band, 1, num_bands);
filter_type[band - 1] = ft;
}
}
void AudioControlSGTL5000Plus::setEQFc(uint8_t band, float frq)
{
if (Fc)
{
band = constrain(band, 1, num_bands);
Fc[band - 1] = frq;
}
}
void AudioControlSGTL5000Plus::setEQQ(uint8_t band, float q)
{
if (Q)
{
band = constrain(band, 1, num_bands);
Q[band - 1] = q;
}
}
// Calculate Q: http://www.sengpielaudio.com/calculator-bandwidth.htm
// http://jdm12.ch/Audio/EQ_BPF-Q-bandwidth.asp
void AudioControlSGTL5000Plus::setEQBandwidth(uint8_t band, float bw)
{
if (Q && Fc)
{
band = constrain(band, 1, num_bands);
Q[band - 1] = sqrt(pow(2, bw)) / (pow(2, bw) - 1);
}
}
void AudioControlSGTL5000Plus::setEQGain(uint8_t band, float gain)
{
if (peakGainDB)
{
band = constrain(band, 1, num_bands);
peakGainDB[band - 1] = gain;
}
}
void AudioControlSGTL5000Plus::commitFilter(uint8_t band)
{
int filter[5] = {0, 0, 0, 0, 0};
band = constrain(band, 1, num_bands);
calcBiquad(filter_type[band - 1], Fc[band - 1], peakGainDB[band - 1], Q[band - 1], 0x80000, AUDIO_SAMPLE_RATE, filter);
eqFilter(band, filter);
}
void setup() {
Serial.begin(115200);
AudioMemory(8);
sgtl5000.enable();
sgtl5000.volume(0.5);
sgtl5000.audioPostProcessorEnable();
sgtl5000.setEQType(1, FILTER_PARAEQ);
sgtl5000.setEQFc(1, 1500);
sgtl5000.setEQBandwidth(1, 2);
sgtl5000.show_params(1);
sgtl5000.commitFilter(1);
}
#if !defined(_MAPFLOAT)
#define _MAPFLOAT
inline float mapfloat(float val, float in_min, float in_max, float out_min, float out_max)
{
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#endif
void playMem(void)
{
Serial.println("Playing...");
playMem1.play(AudioSampleSdtest1);
sgtl5000.setEQGain(1, 15.0);
while (playMem1.isPlaying())
{
//sgtl5000.setEQGain(1, random(-10000, 10000) / 1000.0);
sgtl5000.setEQBandwidth(1, random(1, 5));
sgtl5000.commitFilter(1);
sgtl5000.show_params(1);
delay(1000);
}
}
void loop() {
playMem();
delay(500);
}