![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Chapter003_TRobot
Dependencies: F746_GUI Array_Matrix TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG SDFileSystem_Warning_Fixed FrequencyResponseDrawer F746_SAI_IO
main.cpp@2:2478e7a8e8d5, 2016-04-25 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Apr 25 00:49:09 2016 +0000
- Revision:
- 2:2478e7a8e8d5
- Parent:
- 1:cc2669b9d1e2
- Child:
- 5:3e8ca1ed31a1
3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:04b43b777fae | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:04b43b777fae | 2 | // フィルタ処理付き SD オーディオプレーヤー |
MikamiUitOpen | 0:04b43b777fae | 3 | // SD のファイル: *.wav |
MikamiUitOpen | 0:04b43b777fae | 4 | // PCM,16 ビットステレオ,標本化周波数 44.1 kHz |
MikamiUitOpen | 0:04b43b777fae | 5 | // IIR フィルタ ---- 低域通過および高域通過フィルタ |
MikamiUitOpen | 2:2478e7a8e8d5 | 6 | // 出力:モノラル |
MikamiUitOpen | 0:04b43b777fae | 7 | // |
MikamiUitOpen | 2:2478e7a8e8d5 | 8 | // 2016/04/25, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:04b43b777fae | 9 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:04b43b777fae | 10 | |
MikamiUitOpen | 0:04b43b777fae | 11 | #include "MyFunctions.hpp" |
MikamiUitOpen | 0:04b43b777fae | 12 | #include "BlinkLabel.hpp" |
MikamiUitOpen | 0:04b43b777fae | 13 | |
MikamiUitOpen | 0:04b43b777fae | 14 | using namespace Mikami; |
MikamiUitOpen | 0:04b43b777fae | 15 | |
MikamiUitOpen | 0:04b43b777fae | 16 | int main() |
MikamiUitOpen | 0:04b43b777fae | 17 | { |
MikamiUitOpen | 0:04b43b777fae | 18 | Label myLabel(80, 4, "SD Card Audio Player", Label::LEFT, Font16); |
MikamiUitOpen | 0:04b43b777fae | 19 | |
MikamiUitOpen | 0:04b43b777fae | 20 | const int FS = AUDIO_FREQUENCY_44K; // 標本化周波数: 44.1 kHz |
MikamiUitOpen | 0:04b43b777fae | 21 | SaiIO_O mySai(2048, FS); |
MikamiUitOpen | 0:04b43b777fae | 22 | |
MikamiUitOpen | 0:04b43b777fae | 23 | SD_WavReader sdReader(mySai.GetLength()); // SD カード読み込み用オブジェクト |
MikamiUitOpen | 0:04b43b777fae | 24 | const int MAX_FILES = 7; |
MikamiUitOpen | 0:04b43b777fae | 25 | FileSelector selector(4, 28, MAX_FILES, 34, sdReader); |
MikamiUitOpen | 0:04b43b777fae | 26 | if (!selector.CreateTable()) |
MikamiUitOpen | 0:04b43b777fae | 27 | BlinkLabel errLabel(240, 100, "SD CARD ERROR", Label::CENTER); |
MikamiUitOpen | 0:04b43b777fae | 28 | |
MikamiUitOpen | 0:04b43b777fae | 29 | // ボタン用の定数 |
MikamiUitOpen | 0:04b43b777fae | 30 | const uint16_t BG_LEFT = 390; |
MikamiUitOpen | 0:04b43b777fae | 31 | const uint16_t BG_WIDTH = 80; |
MikamiUitOpen | 0:04b43b777fae | 32 | const uint16_t BG_HEIGHT = 36; |
MikamiUitOpen | 0:04b43b777fae | 33 | |
MikamiUitOpen | 0:04b43b777fae | 34 | // ButtonGroup: "OPEN", "PLAY", "PAUSE", "RESUME", "STOP" |
MikamiUitOpen | 0:04b43b777fae | 35 | const string MENU[5] = {"OPEN", "PLAY", "PAUSE", "RESUME", "STOP"}; |
MikamiUitOpen | 0:04b43b777fae | 36 | ButtonGroup menu(BG_LEFT, 2, BG_WIDTH, BG_HEIGHT, |
MikamiUitOpen | 0:04b43b777fae | 37 | 5, MENU, 0, 2, 1); |
MikamiUitOpen | 0:04b43b777fae | 38 | // OPEN のみアクティブ |
MikamiUitOpen | 0:04b43b777fae | 39 | menu.Activate(0); |
MikamiUitOpen | 0:04b43b777fae | 40 | for (int n=1; n<5; n++) menu.Inactivate(n); |
MikamiUitOpen | 0:04b43b777fae | 41 | |
MikamiUitOpen | 0:04b43b777fae | 42 | // ButtonGroup: "LPF", "HPF" |
MikamiUitOpen | 0:04b43b777fae | 43 | const string LP_HP[2] = {"LPF", "HPF"}; |
MikamiUitOpen | 0:04b43b777fae | 44 | ButtonGroup lpHp(BG_LEFT, 197, BG_WIDTH/2, BG_HEIGHT, |
MikamiUitOpen | 0:04b43b777fae | 45 | 2, LP_HP, 0, 0, 2, 0); |
MikamiUitOpen | 0:04b43b777fae | 46 | |
MikamiUitOpen | 0:04b43b777fae | 47 | // ButtonGroup: "ON", "OFF" |
MikamiUitOpen | 0:04b43b777fae | 48 | const string ON_OFF[2] = {"ON", "OFF"}; |
MikamiUitOpen | 0:04b43b777fae | 49 | ButtonGroup onOff(BG_LEFT, 235, BG_WIDTH/2, BG_HEIGHT, |
MikamiUitOpen | 0:04b43b777fae | 50 | 2, ON_OFF, 0, 0, 2, 1); |
MikamiUitOpen | 0:04b43b777fae | 51 | |
MikamiUitOpen | 0:04b43b777fae | 52 | // フィルタの設計と周波数特性描画用 |
MikamiUitOpen | 0:04b43b777fae | 53 | const int ORDER = 6; // フィルタの次数 |
MikamiUitOpen | 0:04b43b777fae | 54 | DesignerDrawer drawerObj( |
MikamiUitOpen | 0:04b43b777fae | 55 | 45, // グラフの左端の位置 |
MikamiUitOpen | 0:04b43b777fae | 56 | 230, // グラフの下端の位置 |
MikamiUitOpen | 2:2478e7a8e8d5 | 57 | 3, // 1 dB 当たりのピクセル数 |
MikamiUitOpen | 0:04b43b777fae | 58 | FS, // 標本化周波数 |
MikamiUitOpen | 0:04b43b777fae | 59 | ORDER, // フィルタの次数 |
MikamiUitOpen | 0:04b43b777fae | 60 | 400, // 最初に与える遮断周波数 |
MikamiUitOpen | 0:04b43b777fae | 61 | 200, // 遮断周波数の最小値 |
MikamiUitOpen | 0:04b43b777fae | 62 | 10000, // 遮断周波数の最大値 |
MikamiUitOpen | 0:04b43b777fae | 63 | BilinearDesign::LPF); // 低域通過フィルタ |
MikamiUitOpen | 0:04b43b777fae | 64 | |
MikamiUitOpen | 0:04b43b777fae | 65 | // フィルタの準備 |
MikamiUitOpen | 0:04b43b777fae | 66 | Biquad::Coefs ck[ORDER/2]; |
MikamiUitOpen | 0:04b43b777fae | 67 | float g0; |
MikamiUitOpen | 0:04b43b777fae | 68 | drawerObj.GetCoefficients(ck, g0); |
MikamiUitOpen | 0:04b43b777fae | 69 | Biquad hn[ORDER/2]; |
MikamiUitOpen | 0:04b43b777fae | 70 | for (int k=0; k<ORDER/2; k++) hn[k] = Biquad(ck[k]); |
MikamiUitOpen | 0:04b43b777fae | 71 | |
MikamiUitOpen | 0:04b43b777fae | 72 | int32_t frameSize = mySai.GetLength(); |
MikamiUitOpen | 0:04b43b777fae | 73 | int16_t *sn = new int16_t[frameSize+1]; // フレームバッファ |
MikamiUitOpen | 0:04b43b777fae | 74 | bool playOk = false; |
MikamiUitOpen | 0:04b43b777fae | 75 | bool filterOn = false; |
MikamiUitOpen | 0:04b43b777fae | 76 | bool whileFirst = true; |
MikamiUitOpen | 0:04b43b777fae | 77 | string fileName; |
MikamiUitOpen | 0:04b43b777fae | 78 | int32_t loopCount; |
MikamiUitOpen | 0:04b43b777fae | 79 | |
MikamiUitOpen | 0:04b43b777fae | 80 | while (true) |
MikamiUitOpen | 0:04b43b777fae | 81 | { |
MikamiUitOpen | 0:04b43b777fae | 82 | if (!playOk) |
MikamiUitOpen | 0:04b43b777fae | 83 | { |
MikamiUitOpen | 0:04b43b777fae | 84 | if (whileFirst) |
MikamiUitOpen | 0:04b43b777fae | 85 | { |
MikamiUitOpen | 0:04b43b777fae | 86 | whileFirst = false; |
MikamiUitOpen | 0:04b43b777fae | 87 | while (!menu.Touched(0)) // OPEN がタッチされるまで待つ |
MikamiUitOpen | 0:04b43b777fae | 88 | ModifyFilter(drawerObj, lpHp, onOff, |
MikamiUitOpen | 0:04b43b777fae | 89 | hn, ck, g0, filterOn); |
MikamiUitOpen | 0:04b43b777fae | 90 | SelectFile(menu, selector, myLabel, fileName); |
MikamiUitOpen | 0:04b43b777fae | 91 | } |
MikamiUitOpen | 0:04b43b777fae | 92 | else |
MikamiUitOpen | 0:04b43b777fae | 93 | { |
MikamiUitOpen | 0:04b43b777fae | 94 | menu.Activate(1); // PLAY 有効 |
MikamiUitOpen | 0:04b43b777fae | 95 | int touch10; |
MikamiUitOpen | 0:04b43b777fae | 96 | while (!menu.GetTouchedNumber(touch10)) |
MikamiUitOpen | 0:04b43b777fae | 97 | ModifyFilter(drawerObj, lpHp, onOff, |
MikamiUitOpen | 0:04b43b777fae | 98 | hn, ck, g0, filterOn); |
MikamiUitOpen | 0:04b43b777fae | 99 | if (touch10 == 0) |
MikamiUitOpen | 0:04b43b777fae | 100 | SelectFile(menu, selector, myLabel, fileName); |
MikamiUitOpen | 0:04b43b777fae | 101 | } |
MikamiUitOpen | 0:04b43b777fae | 102 | |
MikamiUitOpen | 0:04b43b777fae | 103 | loopCount = SD_Open(sdReader, fileName, frameSize); |
MikamiUitOpen | 0:04b43b777fae | 104 | while (!menu.Touched(1)) // PLAY がタッチされるまで待つ |
MikamiUitOpen | 0:04b43b777fae | 105 | ModifyFilter(drawerObj, lpHp, onOff, |
MikamiUitOpen | 0:04b43b777fae | 106 | hn, ck, g0, filterOn); |
MikamiUitOpen | 0:04b43b777fae | 107 | } |
MikamiUitOpen | 0:04b43b777fae | 108 | else |
MikamiUitOpen | 0:04b43b777fae | 109 | loopCount = SD_Open(sdReader, fileName, frameSize); |
MikamiUitOpen | 0:04b43b777fae | 110 | |
MikamiUitOpen | 0:04b43b777fae | 111 | selector.Erase(BG_LEFT-4, 244); |
MikamiUitOpen | 0:04b43b777fae | 112 | myLabel.Draw("IIR Butterworth filter"); |
MikamiUitOpen | 0:04b43b777fae | 113 | drawerObj.DrawResponse(); |
MikamiUitOpen | 0:04b43b777fae | 114 | menu.Inactivate(0); // OPEN 無効 |
MikamiUitOpen | 0:04b43b777fae | 115 | menu.Activate(2); // PAUSE 有効 |
MikamiUitOpen | 0:04b43b777fae | 116 | menu.Activate(4); // STOP 有効 |
MikamiUitOpen | 0:04b43b777fae | 117 | |
MikamiUitOpen | 0:04b43b777fae | 118 | playOk = false; |
MikamiUitOpen | 0:04b43b777fae | 119 | bool stopOk = false; |
MikamiUitOpen | 0:04b43b777fae | 120 | mySai.InitCodecOut(); // SAI の初期化 |
MikamiUitOpen | 0:04b43b777fae | 121 | |
MikamiUitOpen | 0:04b43b777fae | 122 | // IIR フィルタの内部の遅延器のクリア |
MikamiUitOpen | 0:04b43b777fae | 123 | for (int k=0; k<ORDER/2; k++) hn[k].Clear(); |
MikamiUitOpen | 0:04b43b777fae | 124 | |
MikamiUitOpen | 0:04b43b777fae | 125 | for (int k=0; k<loopCount; k++) |
MikamiUitOpen | 0:04b43b777fae | 126 | { |
MikamiUitOpen | 0:04b43b777fae | 127 | int touch42 = -1; |
MikamiUitOpen | 0:04b43b777fae | 128 | menu.GetTouchedNumber(touch42); |
MikamiUitOpen | 0:04b43b777fae | 129 | if (touch42 == 4) break; // STOP |
MikamiUitOpen | 0:04b43b777fae | 130 | if (touch42 == 2) // PAUSE |
MikamiUitOpen | 0:04b43b777fae | 131 | { |
MikamiUitOpen | 0:04b43b777fae | 132 | menu.Inactivate(2); // PAUSE 無効 |
MikamiUitOpen | 0:04b43b777fae | 133 | menu.Activate(3); // RESUME 有効 |
MikamiUitOpen | 0:04b43b777fae | 134 | mySai.Pause(); |
MikamiUitOpen | 0:04b43b777fae | 135 | |
MikamiUitOpen | 0:04b43b777fae | 136 | // PLAY か RESUME か STOP がタッチされるまで待つ |
MikamiUitOpen | 0:04b43b777fae | 137 | int touch134 = -1; |
MikamiUitOpen | 0:04b43b777fae | 138 | while (!menu.GetTouchedNumber(touch134)) |
MikamiUitOpen | 0:04b43b777fae | 139 | ModifyFilter(drawerObj, lpHp, onOff, |
MikamiUitOpen | 0:04b43b777fae | 140 | hn, ck, g0, filterOn); |
MikamiUitOpen | 0:04b43b777fae | 141 | switch (touch134) |
MikamiUitOpen | 0:04b43b777fae | 142 | { |
MikamiUitOpen | 0:04b43b777fae | 143 | case 1: playOk = true; // 最初から PLAY |
MikamiUitOpen | 0:04b43b777fae | 144 | break; |
MikamiUitOpen | 0:04b43b777fae | 145 | case 3: mySai.Resume(); // PAUSE したところから PLAY 再開 |
MikamiUitOpen | 0:04b43b777fae | 146 | menu.Activate(2); |
MikamiUitOpen | 0:04b43b777fae | 147 | menu.Inactivate(3); |
MikamiUitOpen | 0:04b43b777fae | 148 | menu.TouchedColor(1); |
MikamiUitOpen | 0:04b43b777fae | 149 | break; |
MikamiUitOpen | 0:04b43b777fae | 150 | case 4: stopOk = true; // STOP |
MikamiUitOpen | 0:04b43b777fae | 151 | break; |
MikamiUitOpen | 0:04b43b777fae | 152 | } |
MikamiUitOpen | 0:04b43b777fae | 153 | } |
MikamiUitOpen | 0:04b43b777fae | 154 | if (playOk || stopOk) break; |
MikamiUitOpen | 0:04b43b777fae | 155 | |
MikamiUitOpen | 0:04b43b777fae | 156 | ModifyFilter(drawerObj, lpHp, onOff, hn, ck, g0, filterOn); |
MikamiUitOpen | 0:04b43b777fae | 157 | // 1フレーム分の信号処理 (IIR フィルタ) の実行 |
MikamiUitOpen | 0:04b43b777fae | 158 | ProcessSignal(sdReader, mySai, sn, g0, hn, ORDER, filterOn); |
MikamiUitOpen | 0:04b43b777fae | 159 | } |
MikamiUitOpen | 0:04b43b777fae | 160 | mySai.Stop(); |
MikamiUitOpen | 0:04b43b777fae | 161 | menu.Activate(0); // OPEN 有効 |
MikamiUitOpen | 0:04b43b777fae | 162 | if (!playOk) menu.Activate(1); // PLAY 有効 |
MikamiUitOpen | 0:04b43b777fae | 163 | for (int n=2; n<5; n++) // その他は無効 |
MikamiUitOpen | 0:04b43b777fae | 164 | menu.Inactivate(n); |
MikamiUitOpen | 0:04b43b777fae | 165 | sdReader.Close(); // SD のファイルのクローズ |
MikamiUitOpen | 0:04b43b777fae | 166 | } |
MikamiUitOpen | 0:04b43b777fae | 167 | } |