不韋 呂 / F746_Spectrogram

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //------------------------------------------------
00002 //  リアルタイム・スペクトログラム
00003 //      入力: MEMS マイク
00004 //
00005 //  2017/04/10, Copyright (c) 2017 MIKAMI, Naoki
00006 //------------------------------------------------
00007 
00008 #include "SAI_InOut.hpp"
00009 #include "F746_GUI.hpp"
00010 #include "MethodCollection.hpp"
00011 using namespace Mikami;
00012 
00013 int main()
00014 {
00015     const int FS = AUDIO_FREQUENCY_16K; // 標本化周波数:16 kHz
00016     const int N_FFT = 512;              // FFT の点数
00017     SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS,          // 入力用
00018                 INPUT_DEVICE_DIGITAL_MICROPHONE_2); // 入力デバイス:MEMS マイク
00019 
00020     LCD_DISCO_F746NG &lcd = GuiBase::GetLcd();  // LCD 表示器のオブジェクトの参照を取得
00021     lcd.Clear(GuiBase::ENUM_BACK);
00022     Label myLabel1(240, 2, "Real-time spectrogram", Label::CENTER, Font16);
00023 
00024     // ButtonGroup の設定
00025     const uint16_t B_W = 50;
00026     const uint16_t B_Y = 242;
00027     const uint16_t B_H = 30;
00028     const string RUN_STOP[2] = {"RUN", "STOP"};
00029     ButtonGroup runStop(325, B_Y, B_W, B_H, 2, RUN_STOP, 0, 0, 2, 1);
00030 
00031     Button clearButton(430, B_Y, B_W, B_H, "CLEAR");
00032     clearButton.Inactivate();
00033 
00034     // 座標軸
00035     const uint16_t X0 = 40;         // 表示領域の x 座標の原点
00036     const uint16_t Y0 = 200;        // 表示領域の y 座標の原点
00037     const uint16_t PX_1KHZ = 32;    // 1 kHz に対応するピクセル数
00038     const uint16_t H0 = PX_1KHZ*5;  // 周波数軸の長さ(5 kHz に対応)に対応するピクセル数
00039     const uint16_t W0 = 360;        // 横方向の全体の表示の幅(単位:ピクセル)
00040     const float FRAME = (N_FFT/(float)FS)*1000.0f;  // 1 フレームに対応する時間(単位:ms)
00041     const uint16_t H_BAR = 2;       // 表示する際の 1 フレームに対応する横方向のピクセル数
00042     const uint16_t MS100 = 100*H_BAR/FRAME; // 100 ms に対応するピクセル数
00043     const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;    
00044     DrawAxis(X0, Y0, W0, H0, AXIS_COLOR, MS100, PX_1KHZ, lcd);
00045 
00046     // 色と dB の関係の表示
00047     ColorDb(Y0, AXIS_COLOR, lcd);
00048 
00049     Array<float> sn(N_FFT+1);   // スペクトル解析する信号を格納するバッファ
00050     Array<float> db(N_FFT/2+1); // 計算された対数スペクトルを格納するバッファ
00051     // スペクトルの大きさに対応する色データを格納する2次元配列
00052     Matrix<uint32_t> spectra(W0/H_BAR, H0+1, GuiBase::ENUM_BACK);
00053     FftAnalyzer fftAnalyzer(N_FFT+1, N_FFT);
00054 
00055     // ループ内で使う変数の初期化
00056     int stop = 0;       // 0: run, 1: stop
00057 
00058     while(!runStop.Touched(0)) {}   // "RUN" をタッチするまで待つ
00059     // データ読み込み開始
00060     mySai.RecordIn();
00061     while(!mySai.IsCaptured()) {}
00062 
00063     while (true)
00064     {
00065         runStop.GetTouchedNumber(stop);
00066         if (stop == 0)
00067         {
00068             clearButton.Inactivate();
00069             if (mySai.IsCaptured())
00070             {
00071                 // 1フレーム分の信号の入力
00072                 for (int n=0; n<mySai.GetLength(); n++)
00073                 {
00074                     int16_t xL, xR;
00075                     mySai.Input(xL, xR);
00076                     sn[n] = (float)xL;
00077                 }
00078 
00079                 // スペクトルの更新
00080                 SpectrumUpdate(spectra, fftAnalyzer, sn, db);
00081                 // スペクトルの表示
00082                 DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);
00083             }
00084         }
00085         else
00086         {
00087             clearButton.Activate();
00088             if (clearButton.Touched())
00089             {
00090                 spectra.Fill(GuiBase::ENUM_BACK);   // スペクトルの表示をクリアするための処理
00091                 DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);   // スペクトルの表示をクリア
00092                 clearButton.Draw();
00093             }
00094         }
00095     }
00096 }
00097