revised version of F746_SD_GraphicEqualizer

Dependencies:   BSP_DISCO_F746NG F746_GUI F746_SAI_IO FrequencyResponseDrawer LCD_DISCO_F746NG SDFileSystem_Warning_Fixed TS_DISCO_F746NG mbed

Fork of F746_SD_GraphicEqualizer by 不韋 呂

Committer:
edamame22
Date:
Thu Jul 07 05:58:52 2016 +0000
Revision:
12:87f6955b5a80
Parent:
10:fc6367c2ffcf
added highlighted line for track selection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 0:e953eb392151 1 //--------------------------------------------------------------
MikamiUitOpen 0:e953eb392151 2 // グラフィックイコライザ付き SD オーディオプレーヤー
MikamiUitOpen 0:e953eb392151 3 // SD のファイル: *.wav
MikamiUitOpen 0:e953eb392151 4 // PCM,16 ビットステレオ,標本化周波数 44.1 kHz
MikamiUitOpen 0:e953eb392151 5 // 上記以外の形式は扱わない
MikamiUitOpen 0:e953eb392151 6 // 出力:モノラル
MikamiUitOpen 0:e953eb392151 7 //
MikamiUitOpen 7:be29ba9c83b2 8 // 2016/05/09, Copyright (c) 2016 MIKAMI, Naoki
MikamiUitOpen 0:e953eb392151 9 //--------------------------------------------------------------
MikamiUitOpen 0:e953eb392151 10
MikamiUitOpen 0:e953eb392151 11 #include "MyFunctions.hpp"
MikamiUitOpen 0:e953eb392151 12 #include "BlinkLabel.hpp"
MikamiUitOpen 0:e953eb392151 13
MikamiUitOpen 0:e953eb392151 14 using namespace Mikami;
edamame22 10:fc6367c2ffcf 15 enum ts_State {
edamame22 10:fc6367c2ffcf 16 IDLE_NOPALY, SELECTED_PREPARE_PALY, PALYING, PAUSED, STOPPED, RESUMED
edamame22 10:fc6367c2ffcf 17 };
MikamiUitOpen 0:e953eb392151 18
MikamiUitOpen 0:e953eb392151 19 int main()
MikamiUitOpen 0:e953eb392151 20 {
MikamiUitOpen 2:2a5c93bf729a 21 Label myLabel1(212, 2, "SD Card Audio Player", Label::CENTER, Font16);
MikamiUitOpen 2:2a5c93bf729a 22 Label myLabel2(212, 16, "---- Graphic equalizer ----", Label::CENTER, Font12);
MikamiUitOpen 0:e953eb392151 23
MikamiUitOpen 0:e953eb392151 24 const int FS = AUDIO_FREQUENCY_44K; // 標本化周波数: 44.1 kHz
MikamiUitOpen 5:a5a4f9d7b26c 25 SaiIO mySai(SaiIO::OUTPUT, 2048, FS);
MikamiUitOpen 0:e953eb392151 26
MikamiUitOpen 0:e953eb392151 27 SD_WavReader sdReader(mySai.GetLength()); // SD カード読み込み用オブジェクト
MikamiUitOpen 0:e953eb392151 28 const int MAX_FILES = 7;
edamame22 10:fc6367c2ffcf 29 string PlayList[MAX_FILES];
edamame22 12:87f6955b5a80 30 int list_id = -9; // if <0 then doesn't highlight any track in the list
MikamiUitOpen 2:2a5c93bf729a 31 FileSelector selector(4, 26, MAX_FILES, 37, sdReader);
MikamiUitOpen 0:e953eb392151 32 if (!selector.CreateTable())
MikamiUitOpen 0:e953eb392151 33 BlinkLabel errLabel(240, 100, "SD CARD ERROR", Label::CENTER);
MikamiUitOpen 0:e953eb392151 34
MikamiUitOpen 0:e953eb392151 35 // ボタン用の定数
MikamiUitOpen 2:2a5c93bf729a 36 const uint16_t BG_LEFT = 414;
MikamiUitOpen 2:2a5c93bf729a 37 const uint16_t BG_WIDTH = 66;
MikamiUitOpen 7:be29ba9c83b2 38 const uint16_t BG_HEIGHT = 36;
MikamiUitOpen 0:e953eb392151 39
MikamiUitOpen 0:e953eb392151 40 // ButtonGroup: "OPEN", "PLAY", "PAUSE", "RESUME", "STOP"
MikamiUitOpen 0:e953eb392151 41 const string MENU[5] = {"OPEN", "PLAY", "PAUSE", "RESUME", "STOP"};
MikamiUitOpen 0:e953eb392151 42 ButtonGroup menu(BG_LEFT, 2, BG_WIDTH, BG_HEIGHT,
MikamiUitOpen 0:e953eb392151 43 5, MENU, 0, 2, 1);
MikamiUitOpen 0:e953eb392151 44 // OPEN のみアクティブ
MikamiUitOpen 0:e953eb392151 45 menu.Activate(0);
MikamiUitOpen 0:e953eb392151 46 for (int n=1; n<5; n++) menu.Inactivate(n);
edamame22 10:fc6367c2ffcf 47 // printf("Button flat with 0 expanded detection Area\n");
edamame22 10:fc6367c2ffcf 48 Button flat(BG_LEFT, 197, BG_WIDTH, BG_HEIGHT, 0, "FLAT");
MikamiUitOpen 7:be29ba9c83b2 49
MikamiUitOpen 2:2a5c93bf729a 50 const string ON_OFF[2] = {"ON", "OFF"};
MikamiUitOpen 7:be29ba9c83b2 51 ButtonGroup onOff(BG_LEFT, 235, BG_WIDTH/2, BG_HEIGHT,
MikamiUitOpen 2:2a5c93bf729a 52 2, ON_OFF, 0, 0, 2, 0);
MikamiUitOpen 0:e953eb392151 53
MikamiUitOpen 0:e953eb392151 54 // フィルタの設計と周波数特性描画用
MikamiUitOpen 0:e953eb392151 55 const int STAGES = 9; // バンド数
MikamiUitOpen 0:e953eb392151 56 DesignerDrawer drawerObj(
edamame22 10:fc6367c2ffcf 57 28, // グラフの左端の位置
edamame22 10:fc6367c2ffcf 58 130, // グラフの下端の位置
edamame22 10:fc6367c2ffcf 59 STAGES, // バンド数
edamame22 10:fc6367c2ffcf 60 62.5f, // 最低域バンドの中心周波数
edamame22 10:fc6367c2ffcf 61 FS, // 標本化周波数
edamame22 10:fc6367c2ffcf 62 2.5f); // 1 dB 当たりのピクセル数
MikamiUitOpen 0:e953eb392151 63
MikamiUitOpen 0:e953eb392151 64 // 周波数特性変更用スライダ
MikamiUitOpen 2:2a5c93bf729a 65 SeekbarGroup myBars(drawerObj.GetX0(), 178, 82, STAGES,
MikamiUitOpen 7:be29ba9c83b2 66 drawerObj.GetSpaceX(), -8.0f, 8.0f, 0,
MikamiUitOpen 0:e953eb392151 67 SeekBar::Vertical);
MikamiUitOpen 0:e953eb392151 68
MikamiUitOpen 0:e953eb392151 69 // フィルタの準備
MikamiUitOpen 0:e953eb392151 70 BiquadGrEq::Coefs ck[STAGES];
MikamiUitOpen 0:e953eb392151 71 drawerObj.GetCoefficients(ck);
MikamiUitOpen 0:e953eb392151 72 BiquadGrEq hn[STAGES];
MikamiUitOpen 0:e953eb392151 73 for (int k=0; k<STAGES; k++) hn[k] = BiquadGrEq(ck[k]);
MikamiUitOpen 0:e953eb392151 74
MikamiUitOpen 0:e953eb392151 75 int32_t frameSize = mySai.GetLength();
MikamiUitOpen 0:e953eb392151 76 int16_t *sn = new int16_t[frameSize+1]; // フレームバッファ
MikamiUitOpen 2:2a5c93bf729a 77 bool on = true;
MikamiUitOpen 0:e953eb392151 78 string fileName;
MikamiUitOpen 0:e953eb392151 79 int32_t loopCount;
edamame22 10:fc6367c2ffcf 80 ts_State ui_mode = IDLE_NOPALY;
edamame22 10:fc6367c2ffcf 81 int k;
edamame22 10:fc6367c2ffcf 82 while (true) {
edamame22 10:fc6367c2ffcf 83 switch(ui_mode) {
edamame22 10:fc6367c2ffcf 84 case IDLE_NOPALY:
MikamiUitOpen 0:e953eb392151 85 while (!menu.Touched(0)) // OPEN がタッチされるまで待つ
MikamiUitOpen 7:be29ba9c83b2 86 ModifyFilter(drawerObj, myBars, hn, flat, onOff, on);
edamame22 10:fc6367c2ffcf 87 for(int m=0; m<MAX_FILES; m++) PlayList[m]=""; // clear playlist
edamame22 10:fc6367c2ffcf 88 SelectFile(menu, selector, myLabel1, PlayList, &list_id); // returned playlist and selected ID
edamame22 10:fc6367c2ffcf 89 ui_mode = SELECTED_PREPARE_PALY ;
edamame22 10:fc6367c2ffcf 90 case SELECTED_PREPARE_PALY:
edamame22 10:fc6367c2ffcf 91 printf(" current track is %s ******\n", PlayList[list_id].c_str());
edamame22 10:fc6367c2ffcf 92 loopCount = SD_Open(sdReader, PlayList[list_id], frameSize);
edamame22 10:fc6367c2ffcf 93 // while (!menu.Touched(1)) // PLAY がタッチされるまで待つ
edamame22 10:fc6367c2ffcf 94 // ModifyFilter(drawerObj, myBars, hn, flat, onOff, on);
edamame22 10:fc6367c2ffcf 95 selector.Erase(0, 0, BG_LEFT-4, 288);
edamame22 10:fc6367c2ffcf 96 myLabel1.Draw("9-band Graphic Equalizer");
edamame22 10:fc6367c2ffcf 97 myBars.RedrawAll();
edamame22 10:fc6367c2ffcf 98 drawerObj.DrawResponse();
edamame22 10:fc6367c2ffcf 99 menu.Inactivate(0); // OPEN 無効
edamame22 10:fc6367c2ffcf 100 menu.Activate(2); // PAUSE 有効
edamame22 10:fc6367c2ffcf 101 menu.Activate(4); // STOP 有効
edamame22 10:fc6367c2ffcf 102 // IIR フィルタの内部の遅延器のクリア
edamame22 10:fc6367c2ffcf 103 for (int j=0; j<STAGES; j++) hn[j].Clear();
edamame22 10:fc6367c2ffcf 104 mySai.PlayOut(); // Play 開始
edamame22 10:fc6367c2ffcf 105 ui_mode = PALYING ;
edamame22 10:fc6367c2ffcf 106 k=0;
edamame22 10:fc6367c2ffcf 107 case PALYING:
edamame22 10:fc6367c2ffcf 108 for (; k<loopCount && ui_mode==PALYING; k++) {
edamame22 10:fc6367c2ffcf 109 int touch42 = -1;
edamame22 10:fc6367c2ffcf 110 menu.GetTouchedNumber(touch42);
edamame22 10:fc6367c2ffcf 111 if (touch42 == 4) ui_mode = STOPPED; // STOP
edamame22 10:fc6367c2ffcf 112 else if (touch42 == 2) ui_mode = PAUSED; // PAUSE
edamame22 10:fc6367c2ffcf 113 ModifyFilter(drawerObj, myBars, hn, flat, onOff, on);
edamame22 10:fc6367c2ffcf 114 // 1フレーム分の信号処理 (イコライザ) の実行
edamame22 10:fc6367c2ffcf 115 SignalProcessing(sdReader, mySai, sn, hn, STAGES, on);
edamame22 10:fc6367c2ffcf 116 }
edamame22 10:fc6367c2ffcf 117 if(ui_mode == PAUSED) break; // Paused
edamame22 10:fc6367c2ffcf 118 mySai.StopOut(); // current track play ended
edamame22 10:fc6367c2ffcf 119 sdReader.Close(); // SD のファイルのクローズ
edamame22 10:fc6367c2ffcf 120 if(ui_mode == STOPPED) break; // Stopped
edamame22 10:fc6367c2ffcf 121
edamame22 10:fc6367c2ffcf 122 if (list_id < MAX_FILES-1) { // goto next track
edamame22 10:fc6367c2ffcf 123 if (PlayList[list_id+1].empty()) list_id=0;
edamame22 10:fc6367c2ffcf 124 else list_id++;
edamame22 10:fc6367c2ffcf 125 } else list_id=0;
edamame22 10:fc6367c2ffcf 126 ui_mode = SELECTED_PREPARE_PALY;
edamame22 10:fc6367c2ffcf 127 break;
edamame22 10:fc6367c2ffcf 128 case PAUSED: {
edamame22 10:fc6367c2ffcf 129 menu.Inactivate(2); // PAUSE 無効
edamame22 10:fc6367c2ffcf 130 menu.Activate(3); // RESUME 有効
edamame22 10:fc6367c2ffcf 131 mySai.PauseOut();
edamame22 10:fc6367c2ffcf 132 // PLAY か RESUME か STOP がタッチされるまで待つ
edamame22 10:fc6367c2ffcf 133 int touch134 = -1;
edamame22 10:fc6367c2ffcf 134 while (!menu.GetTouchedNumber(touch134))
edamame22 10:fc6367c2ffcf 135 ModifyFilter(drawerObj, myBars, hn, flat, onOff, on);
edamame22 10:fc6367c2ffcf 136 if(touch134 == 3) {
edamame22 10:fc6367c2ffcf 137 ui_mode = RESUMED;
edamame22 10:fc6367c2ffcf 138 break; // PAUSE したところから PLAY 再開
edamame22 10:fc6367c2ffcf 139 }
edamame22 10:fc6367c2ffcf 140 mySai.ResumeOut(); // PAUSE したところから PLAY 再開 またはSTOPの後処理
edamame22 10:fc6367c2ffcf 141 mySai.StopOut();
edamame22 10:fc6367c2ffcf 142 sdReader.Close(); // SD のファイルのクローズ
edamame22 10:fc6367c2ffcf 143
edamame22 10:fc6367c2ffcf 144 if(touch134 == 4) {
edamame22 10:fc6367c2ffcf 145 ui_mode = STOPPED; break; // STOP
edamame22 10:fc6367c2ffcf 146 }
edamame22 10:fc6367c2ffcf 147 if(touch134 == 1) {
edamame22 10:fc6367c2ffcf 148 ui_mode = SELECTED_PREPARE_PALY; // 最初から PLAY
edamame22 10:fc6367c2ffcf 149 menu.Inactivate(3); // RESUME disable to start play
edamame22 10:fc6367c2ffcf 150 break;
edamame22 10:fc6367c2ffcf 151 }
edamame22 10:fc6367c2ffcf 152 printf("a very bad wrong point\n");
edamame22 10:fc6367c2ffcf 153 break;
MikamiUitOpen 0:e953eb392151 154 }
edamame22 10:fc6367c2ffcf 155 case STOPPED:
edamame22 10:fc6367c2ffcf 156 menu.Activate(0); // OPEN 有効
edamame22 10:fc6367c2ffcf 157 menu.Activate(1); // PLAY 有効
edamame22 10:fc6367c2ffcf 158 for (int n=2; n<5; n++) // その他は無効
edamame22 10:fc6367c2ffcf 159 menu.Inactivate(n);
MikamiUitOpen 0:e953eb392151 160 int touch10;
MikamiUitOpen 0:e953eb392151 161 while (!menu.GetTouchedNumber(touch10))
MikamiUitOpen 7:be29ba9c83b2 162 ModifyFilter(drawerObj, myBars, hn, flat, onOff, on);
edamame22 12:87f6955b5a80 163 if (touch10 == 0) // OPEN タッチしたら
edamame22 10:fc6367c2ffcf 164 SelectFile(menu, selector, myLabel1, PlayList, &list_id);
edamame22 10:fc6367c2ffcf 165 ui_mode = SELECTED_PREPARE_PALY;
edamame22 10:fc6367c2ffcf 166 break;
edamame22 10:fc6367c2ffcf 167 case RESUMED:
edamame22 10:fc6367c2ffcf 168 mySai.ResumeOut(); // PAUSE したところから PLAY 再開
edamame22 10:fc6367c2ffcf 169 menu.Activate(2);
edamame22 10:fc6367c2ffcf 170 menu.Inactivate(3);
edamame22 10:fc6367c2ffcf 171 menu.TouchedColor(1);
edamame22 10:fc6367c2ffcf 172 ui_mode = PALYING;
edamame22 10:fc6367c2ffcf 173 break;
MikamiUitOpen 0:e953eb392151 174 }
MikamiUitOpen 0:e953eb392151 175 }
MikamiUitOpen 0:e953eb392151 176 }