【搬运】M5StickC Audio Spectrum Display
本节介绍一个麦克风应用,制作8段式音频频谱显示器。C的内部集成了一个微型麦克风可以采集声音信号,通过配置I2S就能进行数据通讯,所以如果你想使用麦克风采集音频信号的话可以参考以下程序的I2S配置。本例程需要安装一个快速傅里叶变换的Arduino库,此外作者开启了GCC编译器3级优化。实际测试中还存在问题,大概运行1个小时左右就死机,不过这个案例能帮助我们很好的理解内置麦克风的使用。#include "arduinoFFT.h"
arduinoFFT FFT = arduinoFFT();
#include <M5StickC.h>
#include <driver/i2s.h>
#pragma GCC optimize ("O3")
#define PIN_CLK0
#define PIN_DATA 34
#define READ_LEN (2 * 1024)
uint8_t BUFFER = {0};
uint16_t oldx;
uint16_t oldy;
uint16_t *adcBuffer = NULL; // uint16_t *adcBuffer = NULL;
#define SAMPLES 512 // Must be a power of 2
#define SAMPLING_FREQUENCY 40000
struct eqBand {
const char *freqname;
uint16_t amplitude;
int peak;
int lastpeak;
uint16_t lastval;
unsigned long lastmeasured;
};
eqBand audiospectrum = {
// Adjust the amplitude values to fit your microphone
// freqname,amplitude,peak,lastpeak,lastval,lastmeasured
{".1",1000, 0, 0, 0, 0},
{".2", 500, 0, 0, 0, 0},
{".5", 300, 0, 0, 0, 0},
{"1" , 250, 0, 0, 0, 0},
{"2" , 200, 0, 0, 0, 0},
{"4" , 100, 0, 0, 0, 0},
{"8" ,50, 0, 0, 0, 0},
{ "16" ,50, 0, 0, 0, 0}
};
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal;
double vImag;
unsigned long newTime, oldTime;
uint16_t tft_width= 160;
uint16_t tft_height =80;
uint8_t bands = 8;
uint8_t bands_width = floor( tft_width / bands );
uint8_t bands_pad = bands_width - 10;
uint16_t colormap;//color palette for the band meter(pre-fill in setup)
void i2sInit(){
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
.sample_rate =44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, //is fixed at 12bit,stereo,MSB
.channel_format = I2S_CHANNEL_FMT_ALL_RIGHT,
.communication_format = I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = 128,
};
i2s_pin_config_t pin_config;
pin_config.bck_io_num = I2S_PIN_NO_CHANGE;
pin_config.ws_io_num = PIN_CLK;
pin_config.data_out_num = I2S_PIN_NO_CHANGE;
pin_config.data_in_num= PIN_DATA;
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
i2s_set_clk(I2S_NUM_0, 44100,I2S_BITS_PER_SAMPLE_16BIT,I2S_CHANNEL_MONO);
}
void mic_record_task (void* arg){
while(1){
i2s_read_bytes(I2S_NUM_0,(char*)BUFFER,READ_LEN,(100/portTICK_RATE_MS));
adcBuffer = (uint16_t *)BUFFER;
showSignal();
vTaskDelay(100 / portTICK_RATE_MS);
}
}
void setup() {
M5.begin();
M5.Lcd.setRotation(1);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextColor(BLUE);
M5.Lcd.setTextSize(1);
i2sInit();
xTaskCreatePinnedToCore(mic_record_task,"mic_record_task",2048,NULL,1,NULL,1);
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
delay(2000);
for(uint8_t i=0;i<tft_height;i++) {
//colormap = M5.Lcd.color565(tft_height-i*.5,i*1.1,0); //RGB
colormap = M5.Lcd.color565(tft_height-i*4.4,i*2.5,0);//RGB:rev macsbug
}
for (byte band = 0; band <= 7; band++) {
M5.Lcd.setCursor(bands_width*band + 2, 0);
M5.Lcd.print(audiospectrum.freqname);
}
}
void showSignal(){
for (int i = 0; i < SAMPLES; i++) {
newTime= micros() - oldTime;
oldTime= newTime;
vReal = adcBuffer;
vImag = 0;
while (micros() < (newTime + sampling_period_us)){//do nothing to wait
}
}
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
for (int i = 2; i < (SAMPLES/2); i++){
// Don't use sample 0 and only first SAMPLES/2 are usable.
// Each array eleement represents a frequency and its value the amplitude.
if (vReal > 1500) { // Add a crude noise filter, 10 x amplitude or more
byte bandNum = getBand(i);
if(bandNum!=8) {
displayBand(bandNum, (int)vReal/audiospectrum.amplitude);
}
}
}
long vnow = millis();
for (byte band = 0; band <= 7; band++) {
// auto decay every 50ms on low activity bands
if(vnow - audiospectrum.lastmeasured > 50) {
displayBand(band, audiospectrum.lastval>4 ? audiospectrum.lastval-4 : 0);
}
if (audiospectrum.peak > 0) {
audiospectrum.peak -= 2;
if(audiospectrum.peak<=0) {
audiospectrum.peak = 0;
}
}
// only draw if peak changed
if(audiospectrum.lastpeak != audiospectrum.peak) {
// delete last peak
M5.Lcd.drawFastHLine(bands_width*band,tft_height-audiospectrum.lastpeak,bands_pad,BLACK);
audiospectrum.lastpeak = audiospectrum.peak;
M5.Lcd.drawFastHLine(bands_width*band, tft_height-audiospectrum.peak,
bands_pad, colormap.peak]);
}
}
}
void displayBand(int band, int dsize){
uint16_t hpos = bands_width*band;
int dmax = 200;
if(dsize>tft_height-10) {
dsize = tft_height-10; // leave some hspace for text
}
if(dsize < audiospectrum.lastval) {
// lower value, delete some lines
M5.Lcd.fillRect(hpos, tft_height-audiospectrum.lastval,
bands_pad, audiospectrum.lastval - dsize,BLACK);
}
if (dsize > dmax) dsize = dmax;
for (int s = 0; s <= dsize; s=s+4){
M5.Lcd.drawFastHLine(hpos, tft_height-s, bands_pad, colormap);
}
if (dsize > audiospectrum.peak){audiospectrum.peak = dsize;}
audiospectrum.lastval = dsize;
audiospectrum.lastmeasured = millis();
}
byte getBand(int i) {
if (i<=2 ) return 0;// 125Hz
if (i >3 && i<=5 ) return 1;// 250Hz
if (i >5 && i<=7 ) return 2;// 500Hz
if (i >7 && i<=15 )return 3;// 1000Hz
if (i >15&& i<=30 )return 4;// 2000Hz
if (i >30&& i<=53 )return 5;// 4000Hz
if (i >53&& i<=200 ) return 6;// 8000Hz
if (i >200 ) return 7;// 16000Hz
return 8;
}
void loop() {} 看起来挺有意思的啊
现在的 arm 核心的都支持 i2s 直接获得音频了
页:
[1]