Realtime spectrogram for DISCO-F746NG. On-board MEMS microphone is used for input sound signal. リアルタイムスペクトログラム.入力:MEMSマイク
Dependencies: F746_GUI F746_SAI_IO UIT_FFT_Real
Diff: main.cpp
- Revision:
- 0:9470a174c910
- Child:
- 1:f570067f3841
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun May 22 06:28:40 2016 +0000 @@ -0,0 +1,104 @@ +//------------------------------------------------ +// Realtime spectrogram +// Input: MEMS microphone +// +// 2016/05/22, Copyright (c) 2016 MIKAMI, Naoki +//------------------------------------------------ + +#include "SAI_InOut.hpp" +#include "ButtonGroup.hpp" +#include "Label.hpp" +#include "MethodCollection.hpp" + +#include "Matrix.hpp" + +using namespace Mikami; + +int main() +{ + printf("\r\n15:19\r\n"); + const int FS = I2S_AUDIOFREQ_16K; // 標本化周波数: 16 kHz + const int N_FFT = 512; // FFT の点数 + const float FRAME = (N_FFT/(float)FS)*1000.0f; // 1 フレームに対応する時間(単位:ms) + + const int X0 = 40; // 表示領域の x 座標の原点 + const int Y0 = 200; // 表示領域の y 座標の原点 + const int H0 = 160; // 表示する際の周波数軸の長さ(5 kHz に対応) + const uint16_t PX_1KHZ = H0/5; // 1 kHz に対応するピクセル数 + const int W0 = 360; // 横方向の全体の表示の幅(単位:ピクセル) + const int H_BAR = 2; // 表示する際の 1 フレームに対応する横方向のピクセル数 + const int SIZE_X = W0/H_BAR; + const uint16_t MS100 = 100*H_BAR/FRAME; // 100 ms に対応するピクセル数 + const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;//0xFFCCFFFF; + + Matrix<uint32_t> spectra(SIZE_X, H0+1, GuiBase::ENUM_BACK); + + SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS, + INPUT_DEVICE_DIGITAL_MICROPHONE_2); +//// INPUT_DEVICE_INPUT_LINE_1); + + LCD_DISCO_F746NG *lcd = GuiBase::GetLcdPtr(); // LCD 表示器のオブジェクト + lcd->Clear(GuiBase::ENUM_BACK); + Label myLabel1(240, 2, "Real-time spectrogram", Label::CENTER, Font16); + + // ButtonGroup の設定 + const uint16_t B_W = 50; + const uint16_t B_Y = 242; + const uint16_t B_H = 30; + const string RUN_STOP[2] = {"RUN", "STOP"}; + ButtonGroup runStop(325, B_Y, B_W, B_H, 2, RUN_STOP, 0, 0, 2, 1); + + Button clear(430, B_Y, B_W, B_H, "CLEAR"); + // ButtonGroup の設定(ここまで) + + // 座標軸 + DrawAxis(X0, Y0, W0, H0, AXIS_COLOR, MS100, PX_1KHZ, lcd); + + Array<float> sn(N_FFT+1); + Array<float> db(N_FFT/2+1); + + // 色と dB の関係の表示 + ColorDb(Y0, AXIS_COLOR, lcd); + + FftAnalyzer fftAnalyzer(N_FFT+1, N_FFT); + + // ループ内で使う変数の初期化 + int stop = 1; // 0: run, 1: stop + + // データ読み込み開始 + mySai.RecordIn(); + + while (true) + { + runStop.GetTouchedNumber(stop); + + if (stop == 0) + { + if (mySai.IsCaptured()) + { + // 1フレーム分の信号の入力 + for (int n=0; n<mySai.GetLength(); n++) + { + int16_t xL, xR; + mySai.Input(xL, xR); + sn[n] = (float)xL; + } + mySai.ResetCaptured(); + + // スペクトルの更新 + SpectrumUpdate(spectra, fftAnalyzer, sn, db); + // スペクトルの表示 + DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd); + } + } + else + { + if (clear.Touched()) + { + spectra.Fill(GuiBase::ENUM_BACK); // スペクトルの表示をクリア + DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd); + clear.Draw(); + } + } + } +}