不韋 呂 / F746_MySoundMachine

Dependencies:   F746_GUI F746_SAI_IO FrequencyResponseDrawer SD_PlayerSkeleton UIT_FFT_Real

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SpectrogramMain.hpp Source File

SpectrogramMain.hpp

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