├── .gitattributes ├── .gitignore ├── Arduino-IDE-Sketch ├── 8bitNESSynthaudioout │ ├── 8bitNESSynthaudioout.ino │ ├── Tunes.cpp │ └── Tunes.h ├── BLE-LOCAL_MIDI_Translator │ └── BLE-LOCAL_MIDI_Translator.ino ├── M5Stack3VoiceSynth │ ├── M5Stack3VoiceSynth.ino │ ├── Tunes.cpp │ └── Tunes.h ├── M5Stack5VoiceSynth │ ├── M5Stack5VoiceSynth.ino │ ├── Tunes.cpp │ └── Tunes.h ├── M5StackSimpleSampler │ ├── M5StackSimpleSampler.ino │ └── cat4410016.wav ├── M5StackWavPlay │ └── M5StackWavPlay.ino ├── M5Stack_Gyrocontroller │ └── M5Stack_Gyrocontroller.ino ├── M5Stack_MIDI_R_test │ └── M5Stack_MIDI_R_test.ino ├── M5Stack_MIDI_T_RndSeq │ └── M5Stack_MIDI_T_RndSeq.ino ├── M5Stack_MIDI_T_test │ └── M5Stack_MIDI_T_test.ino ├── M5Stack_SMF_Player │ ├── IntervalCheck.h │ ├── IntervalCheckCounts.h │ ├── IntervalCheckMicros.h │ ├── M5Stack_SMF_Player.ino │ ├── MidiFunc.cpp │ ├── MidiFunc.h │ ├── MidiPort.h │ ├── SMF_for_test │ │ ├── playdat0.mid │ │ └── playdat1.mid │ ├── SmfFileAccess.h │ ├── SmfSeq.cpp │ ├── SmfSeq.h │ └── common.h └── sync_test │ └── sync_test.ino ├── Case-3Ddata ├── m5stackmidi1.6mm.stl ├── m5stackmidiDIN5.stl └── m5stackmidiDIN5top.stl ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | Arduino-IDE-Sketch/8bitNESSynthaudioout/8bitNESSynthaudioout.ino 3 | Arduino-IDE-Sketch/sync_test/sync_test.ino 4 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/8bitNESSynthaudioout/8bitNESSynthaudioout.ino: -------------------------------------------------------------------------------- 1 | //NES Like Synth ver. 0.35 2 | //M5Stack用MIDIモジュールを利用したファミコン風音源です。 3 | //https://necobit.base.shop/items/14047506 4 | //MIDIモジュールはこちらで販売しています。 5 | // 6 | //シンセエンジンは@ina_aniさんのエンジンを参考に、 7 | //https://github.com/inajob/o-bako-esp32 8 | //NESの音源の仕組みはConnor Nishijimaさんのプログラムを参考にしました。 9 | //https://github.com/connornishijima/Cartridge 10 | //Lixie LabsのソースはGPLv3なので継承するか迷いましたが、 11 | //変数の定義を除くとソースはほぼ残っていないのでMITライセンスとします。 12 | // 13 | //1,2ch パルス系 プログラムチェンジ1-4で 12.5%,25%,50%,75%を切り替えられます 14 | //3ch 疑似三角波 15 | //4ch ノイズ プログラムチェンジ1 高周期ノイズ 2 低周期ノイズ 16 | //1,2,4chはCC7,11で音量制御ができます。 17 | //CC6でピッチベンドのレンジを0-24で調整できます。 18 | //CC75 0-64でディケイを調整できます。64は変化なし。 19 | //スウィープはまだ未搭載です。 20 | // 21 | //認知の不具合 22 | //音に周期的な変化がある 23 | //減衰の消え際が急 24 | 25 | #include 26 | #include 27 | #include 28 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 29 | 30 | #include "Tunes.h" 31 | Tunes tunes; 32 | 33 | void setup() { 34 | M5.begin(); 35 | MIDI.begin(MIDI_CHANNEL_OMNI); 36 | MIDI.turnThruOn(); 37 | M5.Lcd.setTextSize(3); 38 | M5.Lcd.setCursor(47, 0); 39 | M5.Lcd.print("8bit NES Like"); 40 | M5.Lcd.setCursor(200, 30); 41 | M5.Lcd.print("Synth"); 42 | M5.Lcd.setTextSize(1); 43 | M5.Lcd.setCursor(180, 60); 44 | M5.Lcd.print("by necobit"); 45 | M5.Lcd.setTextSize(1); 46 | M5.Lcd.setCursor(160, 70); 47 | M5.Lcd.print("Special Thanks"); 48 | M5.Lcd.setTextSize(1); 49 | M5.Lcd.setCursor(180, 80); 50 | M5.Lcd.print("@ina_ani"); 51 | M5.Lcd.setCursor(180, 90); 52 | M5.Lcd.print("Lixie Labs"); 53 | 54 | 55 | //Catbot 56 | M5.Lcd.fillRect(230, 150, 89, 70 , 0xFF80); 57 | M5.Lcd.fillTriangle(240, 150, 255, 130, 270, 150, 0xFF80); 58 | M5.Lcd.fillTriangle(280, 150, 295, 130, 310, 150, 0xFF80); 59 | M5.Lcd.drawLine(250, 210, 295, 210, 0x0000); 60 | M5.Lcd.drawLine(250, 205, 250, 210, 0x0000); 61 | M5.Lcd.drawLine(295, 205, 295, 210, 0x0000); 62 | M5.Lcd.fillCircle(250, 180, 8 , 0xE8E4); 63 | M5.Lcd.fillCircle(290, 180, 20 , 0xE8E4); 64 | 65 | Tunes::outpin = 25; 66 | M5.Lcd.setTextSize(3); 67 | M5.Lcd.setCursor(20, 200); 68 | M5.Lcd.print("OUT:25"); 69 | M5.Lcd.setTextSize(3); 70 | M5.Lcd.setCursor(20, 160); 71 | M5.Lcd.print("Volume:30"); 72 | 73 | dacWrite(25, 0); 74 | if (Tunes::outpin == 26) { 75 | ledcDetachPin(SPEAKER_PIN); 76 | pinMode(SPEAKER_PIN, INPUT); 77 | } 78 | tunes.init(); 79 | M5.Speaker.setVolume(255); 80 | pinMode(5, OUTPUT); 81 | pinMode(2, OUTPUT); 82 | digitalWrite(2, HIGH); 83 | } 84 | 85 | int onpu; 86 | boolean onpudraw; 87 | int amp = 1; 88 | 89 | void loop() { 90 | tunes.run(); 91 | 92 | int ch, data1, data2; 93 | 94 | /* if (MIDI.read() && MIDI.getChannel() == 10) 95 | { 96 | if (MIDI.getType() == midi::NoteOn) 97 | { 98 | data1 = MIDI.getData1(); 99 | if (data1 == 60) 100 | digitalWrite(5,HIGH); 101 | } 102 | else if (MIDI.getType() == midi::NoteOff) 103 | { 104 | data1 = MIDI.getData1(); 105 | if (data1 == 60) 106 | digitalWrite(5,LOW); 107 | } 108 | 109 | } 110 | */ 111 | /* 112 | if (MIDI.read() && MIDI.getChannel() == 5) 113 | { 114 | if (MIDI.getType() == midi::NoteOn) 115 | { 116 | data1 = MIDI.getData1(); 117 | data2 = MIDI.getData2(); 118 | if (data1 == 60) { 119 | digitalWrite(5, HIGH); 120 | } 121 | } 122 | else if (MIDI.getType() == midi::NoteOff) 123 | { 124 | data1 = MIDI.getData1(); 125 | data2 = MIDI.getData2(); 126 | if (data1 == 60) { 127 | digitalWrite(5, LOW); 128 | } 129 | } 130 | } 131 | */ 132 | if (MIDI.read() && MIDI.getChannel() <= 4) 133 | { 134 | if (MIDI.getType() == midi::NoteOn) 135 | { 136 | ch = MIDI.getChannel(); 137 | data1 = MIDI.getData1(); 138 | data2 = MIDI.getData2(); 139 | portENTER_CRITICAL(&Tunes::timerMux); 140 | tunes.noteon(ch, data1, data2); 141 | portEXIT_CRITICAL(&Tunes::timerMux); 142 | } 143 | 144 | else if (MIDI.getType() == midi::NoteOff) 145 | { 146 | ch = MIDI.getChannel(); 147 | data1 = MIDI.getData1(); 148 | portENTER_CRITICAL(&Tunes::timerMux); 149 | tunes.noteoff(ch, data1); 150 | portEXIT_CRITICAL(&Tunes::timerMux); 151 | } 152 | 153 | else if (MIDI.getType() == midi::ProgramChange) 154 | { 155 | ch = MIDI.getChannel(); 156 | data1 = MIDI.getData1(); 157 | portENTER_CRITICAL(&Tunes::timerMux); 158 | tunes.pchange(ch, data1); 159 | portEXIT_CRITICAL(&Tunes::timerMux); 160 | } 161 | 162 | else if (MIDI.getType() == midi::ControlChange) 163 | { 164 | ch = MIDI.getChannel(); 165 | data1 = MIDI.getData1(); 166 | data2 = MIDI.getData2(); 167 | if (data1 == 7) Tunes::vol[ch - 1] = data2; 168 | else if (data1 == 11) Tunes::exp[ch - 1] = data2; 169 | else if (data1 == 75) Tunes::decay[ch - 1] = data2; 170 | else if (data1 == 6 && data2 <= 24) Tunes::pbrange[ch - 1] = data2; 171 | } 172 | else if (MIDI.getType() == midi::PitchBend) 173 | { 174 | ch = MIDI.getChannel(); 175 | data1 = MIDI.getData1(); 176 | data2 = MIDI.getData2(); 177 | portENTER_CRITICAL(&Tunes::timerMux); 178 | tunes.pbend(ch, data1, data2); 179 | portEXIT_CRITICAL(&Tunes::timerMux); 180 | } 181 | } 182 | if (M5.BtnA.wasReleased()) { 183 | if (Tunes::outpin == 26) 184 | { 185 | Tunes::outpin = 25; 186 | M5.Lcd.setTextSize(3); 187 | M5.Lcd.setCursor(20, 200); 188 | M5.Lcd.print("OUT:25"); 189 | } else { 190 | Tunes::outpin = 26; 191 | M5.Lcd.setTextSize(3); 192 | M5.Lcd.setCursor(20, 200); 193 | M5.Lcd.print("OUT:26"); 194 | } 195 | } 196 | 197 | if (M5.BtnB.wasReleased() && Tunes::M_vol > 0 ) { 198 | Tunes::M_vol --; 199 | M5.Lcd.setTextSize(3); 200 | M5.Lcd.setCursor(145, 160); 201 | M5.Lcd.print(" "); 202 | M5.Lcd.setCursor(145, 160); 203 | M5.Lcd.print(Tunes::M_vol); 204 | } 205 | if (M5.BtnC.wasReleased() && Tunes::M_vol < 99 ) { 206 | //amp = !amp; 207 | //digitalWrite(2,amp); 208 | Tunes::M_vol ++; 209 | M5.Lcd.setTextSize(3); 210 | M5.Lcd.setCursor(145, 160); 211 | M5.Lcd.print(" "); 212 | M5.Lcd.setCursor(145, 160); 213 | M5.Lcd.print(Tunes::M_vol); 214 | } 215 | M5.update(); 216 | } 217 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/8bitNESSynthaudioout/Tunes.cpp: -------------------------------------------------------------------------------- 1 | #include "Tunes.h" 2 | #include 3 | 4 | volatile SemaphoreHandle_t Tunes::timerSemaphore; 5 | portMUX_TYPE Tunes::timerMux = portMUX_INITIALIZER_UNLOCKED; 6 | volatile uint32_t Tunes::isrCounter = 0; 7 | volatile uint32_t Tunes::lastIsrAt = 0; 8 | volatile uint8_t Tunes::outpin = 25; 9 | 10 | volatile uint8_t Tunes::M_vol = 30; 11 | 12 | 13 | volatile uint16_t Tunes::osc[4] = {0, 0, 0, 0}; 14 | volatile uint16_t Tunes::d[4] = {0, 0, 0, 0}; 15 | volatile uint16_t Tunes::voice[4] = {0, 0, 0, 0}; 16 | volatile uint16_t Tunes::bnno[4] = {0, 0, 0, 0}; 17 | volatile uint8_t Tunes::vol[4] = {100, 100, 100, 100}; 18 | volatile uint8_t Tunes::exp[4] = {127, 127, 127, 127}; 19 | volatile uint8_t Tunes::velo[4] = {100, 100, 100, 100}; 20 | volatile float Tunes::chbend[4] = {1, 1, 1, 1}; 21 | volatile uint16_t Tunes::pold[4] = {1, 1, 1, 1}; 22 | volatile uint8_t Tunes::pbrange[4] = {2, 2, 2, 2}; 23 | volatile uint8_t Tunes::atack[4] = {64, 64, 64, 64}; 24 | volatile uint32_t Tunes::atack_counter[4] = {0, 0, 0, 0}; 25 | volatile uint8_t Tunes::decay[4] = {64, 64, 64, 64}; 26 | volatile int32_t Tunes::decay_counter[4] = {44100, 44100, 44100, 44100}; 27 | volatile uint16_t Tunes::counter = 0; 28 | volatile uint8_t Tunes::wave_index[2] = {0, 0}; 29 | 30 | volatile int Tunes::n_reg = 0x8000; 31 | volatile int Tunes::shortFreq = 0; 32 | 33 | volatile uint8_t Tunes::duty_point = 0; 34 | volatile uint8_t Tunes::duty_table [4][8] = { 35 | {0, 0, 0, 0, 0, 0, 0, 1}, // 12.5% 36 | {0, 0, 0, 0, 0, 0, 1, 1}, // 25% 37 | {0, 0, 0, 0, 1, 1, 1, 1}, // 50% 38 | {1, 1, 1, 1, 1, 1, 0, 0}, // 25% (inv.) 39 | }; 40 | 41 | volatile uint8_t Tunes::tri_table[32] = { 42 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 43 | 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 44 | }; 45 | 46 | volatile uint16_t Tunes::noise_table[16] = { 47 | 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068 48 | }; 49 | 50 | int Tunes::PulseValues[4][256]; 51 | int Tunes::TriValues[256]; 52 | int Tunes::NoiseValues[256]; 53 | 54 | hw_timer_t* Tunes::timer; 55 | 56 | void Tunes::noteon(uint8_t mch, float nno, uint8_t vel) { 57 | voice[mch - 1] = nno; 58 | float freq = std::exp(nno * 0.05776226f + 2.101178f) * 2.945296f; 59 | d[mch - 1] = (uint16_t)(freq); 60 | decay_counter[mch - 1] = 88200; 61 | velo[mch - 1] = vel; 62 | } 63 | 64 | void Tunes::noteoff(uint8_t mch, uint8_t nno) { 65 | if (voice[mch - 1] == nno) { 66 | voice[mch - 1] = 0; 67 | d[mch - 1] = 0; 68 | } 69 | } 70 | 71 | void Tunes::pchange(uint8_t mch, uint8_t patch) { 72 | if (mch <= 2 && patch <= 3) { 73 | wave_index[mch - 1] = patch; 74 | } 75 | if (mch == 4 && patch <= 2 ) { 76 | shortFreq = patch; 77 | } 78 | } 79 | 80 | void Tunes::pbend(uint8_t mch, uint8_t data1, uint8_t data2) { 81 | int fusion = data2 << 7; 82 | fusion = fusion + data1; 83 | float pb = (float)(fusion); 84 | if (fusion != pold[mch - 1]) { 85 | float freq = std::exp((voice[mch - 1] + ((pb - 8192) / 8192 * pbrange[mch - 1])) * 0.05776226f + 2.101178f) * 2.945296f; 86 | boolean onoff = voice[mch - 1]; 87 | d[mch - 1] = (uint16_t)(freq) * onoff; 88 | pold[mch - 1] = fusion; 89 | } 90 | } 91 | 92 | void Tunes::onTimer() { 93 | portENTER_CRITICAL_ISR(&Tunes::timerMux); 94 | Tunes::isrCounter++; 95 | Tunes::lastIsrAt = millis(); 96 | for (int i = 0 ; i <= 4; i ++) { 97 | Tunes::osc[i] += d[i]; 98 | } 99 | 100 | //Noise生成 101 | if (Tunes::d[3] != 0) { 102 | counter = counter + 1 ; 103 | if (counter > 127 - (voice[3] * 2)) { 104 | counter = 0; 105 | Tunes::n_reg >>= 1; 106 | Tunes::n_reg |= ((Tunes::n_reg ^ (Tunes::n_reg >> (shortFreq ? 6 : 1))) & 1) << 15; 107 | } 108 | } 109 | boolean nsw = d[3]; 110 | 111 | //Decay計算 112 | 113 | for (int i = 0; i < 4; i ++) { 114 | if (decay[i] < 64 && d[i] != 0 && decay_counter[i] != 0) { 115 | decay_counter[i] = decay_counter[i] - (64 - decay[i]); 116 | if ( decay_counter[i] <= 0 ) { 117 | decay_counter[i] = 0; 118 | } 119 | } 120 | } 121 | 122 | //出力計算 123 | int out = 0; 124 | out += Tunes::PulseValues[wave_index[0]][(osc[0] >> 8)] * 0.8 * decay_counter[0] / 88200 * vol[0] / 127 * velo[0] / 127 * exp[0] / 127; 125 | out += Tunes::PulseValues[wave_index[1]][(osc[1] >> 8)] * 0.8 * decay_counter[1] / 88200 * vol[1] / 127 * velo[1] / 127 * exp[1] / 127; 126 | out += Tunes::TriValues[(osc[2] >> 8)] * decay_counter[2] / 88200; 127 | out += Tunes::n_reg & 1 * nsw * 128 * decay_counter[3] / 88200 * vol[3] / 127 * velo[3] / 127 * exp[3] / 127; 128 | 129 | if (d[0] == 0 && d[1] == 0 && d[2] == 0 && d[3] == 0) out = out * 0.9; 130 | 131 | //出力 132 | // dacWrite(Tunes::outpin, (out/16)); 133 | dacWrite(Tunes::outpin, (out*Tunes::M_vol/100)); 134 | // dacWrite(Tunes::outpin, (out/2)); 135 | 136 | 137 | portEXIT_CRITICAL_ISR(&Tunes::timerMux); 138 | 139 | xSemaphoreGiveFromISR(Tunes::timerSemaphore, NULL); 140 | } 141 | 142 | void Tunes::init() { 143 | 144 | for (int p_select = 0; p_select < 4; p_select ++) { 145 | for (int MyAngle = 0; MyAngle < 256; MyAngle++) { 146 | Tunes::PulseValues[p_select][MyAngle] = duty_table[p_select][MyAngle / 32] < 1 ? 0 : 255; 147 | Tunes::TriValues[MyAngle] = (tri_table[MyAngle / 8] + 1) * 16 - 1; 148 | Tunes::NoiseValues[MyAngle] = (noise_table[MyAngle / 16]); 149 | } 150 | } 151 | Tunes::timerSemaphore = xSemaphoreCreateBinary(); 152 | 153 | Tunes::timer = timerBegin(0, 80, true); // /80 prescale = 1us = 1/1000000s = 1MHz 154 | timerAttachInterrupt(Tunes::timer, &Tunes::onTimer, true); 155 | timerAlarmWrite(Tunes::timer, 45, true); 156 | timerAlarmEnable(Tunes::timer); 157 | } 158 | 159 | void Tunes::pause() { 160 | timerAlarmDisable(Tunes::timer); 161 | } 162 | void Tunes::resume() { 163 | timerAlarmEnable(Tunes::timer); 164 | } 165 | 166 | void Tunes::run() { 167 | if (xSemaphoreTake(Tunes::timerSemaphore, 0) == pdTRUE) { 168 | uint32_t isrCount = 0, isrTime = 0; 169 | 170 | portENTER_CRITICAL(&Tunes::timerMux); 171 | isrCount = Tunes::isrCounter; 172 | isrTime = Tunes::lastIsrAt; 173 | portEXIT_CRITICAL(&Tunes::timerMux); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/8bitNESSynthaudioout/Tunes.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef TUNES_H 4 | #define TUNES_H 5 | 6 | class Tunes 7 | { 8 | public: 9 | static volatile SemaphoreHandle_t timerSemaphore; 10 | static portMUX_TYPE timerMux; 11 | 12 | static volatile uint32_t isrCounter; 13 | static volatile uint32_t lastIsrAt; 14 | static volatile uint16_t osc[4]; 15 | static volatile uint16_t d[4]; 16 | static volatile uint16_t voice[4]; 17 | static volatile uint16_t bnno[4]; 18 | static volatile float chbend[4]; 19 | static volatile uint16_t pold[4]; 20 | static volatile uint8_t pbrange[4]; 21 | static volatile uint8_t vol[4]; 22 | static volatile uint8_t exp[4]; 23 | static volatile uint8_t velo[4]; 24 | static volatile uint8_t decay[4]; 25 | static volatile int32_t decay_counter[4]; 26 | static volatile uint8_t atack[4]; 27 | static volatile uint32_t atack_counter[4]; 28 | static volatile uint8_t duty_table[4][8]; 29 | static volatile uint8_t wave_index[2]; 30 | 31 | static volatile int n_reg; 32 | static volatile int shortFreq; 33 | 34 | static volatile uint8_t tri_table[32]; 35 | static volatile uint16_t noise_table[16]; 36 | static volatile uint8_t duty_point; 37 | static volatile uint16_t counter; 38 | static int TriValues[256]; 39 | static int PulseValues[4][256]; 40 | static int NoiseValues[256]; 41 | 42 | static volatile uint8_t M_vol; 43 | 44 | static hw_timer_t* timer; 45 | static volatile uint8_t outpin; 46 | 47 | void noteon(uint8_t mch, float nno, uint8_t vel); 48 | void noteoff(uint8_t mch, uint8_t nno); 49 | void pchange(uint8_t mch, uint8_t patch); 50 | void pbend(uint8_t mch, uint8_t data1, uint8_t data2); 51 | 52 | void sample_audio(); 53 | void render_audio(); 54 | 55 | void init(); 56 | void pinset(uint8_t p1_p = 12, uint8_t p2_p = 14, uint8_t n_p = 26, uint8_t t_p = 27); 57 | void static IRAM_ATTR onTimer(); 58 | void run(); 59 | void pause(); 60 | void resume(); 61 | 62 | private: 63 | 64 | // Pulse 1 Variables 65 | 66 | uint8_t p1_output = 0; 67 | long p1_timer = 0; 68 | 69 | uint8_t p1_duty_index = 2; 70 | uint16_t p1_length_counter = 0; 71 | uint8_t p1_envelope_divider = 0; 72 | uint8_t p1_decay_counter = 0; 73 | uint8_t p1_volume = 15; 74 | uint8_t p1_channel = 0; 75 | uint8_t p1_pin = 25; 76 | 77 | // Pulse 2 Variables 78 | uint8_t p2_output = 0; 79 | int16_t p2_11_bit_timer = 0; 80 | uint16_t p2_length_counter = 0; 81 | uint8_t p2_envelope_divider = 0; 82 | uint8_t p2_decay_counter = 0; 83 | uint8_t p2_volume = 0; 84 | uint8_t p2_channel = 1; 85 | uint8_t p2_pin = 14; 86 | 87 | // Noise Variables 88 | uint8_t n_output = 0; 89 | int16_t n_timer = 0; 90 | uint16_t n_length_counter = 0; 91 | uint8_t n_envelope_divider = 0; 92 | uint8_t n_decay_counter = 0; 93 | uint8_t n_volume = 0; 94 | uint8_t n_xor = 0; 95 | uint16_t n_lsfr = 1; 96 | uint8_t n_channel = 2; 97 | uint8_t n_pin = 27; 98 | 99 | // Triangle Variables 100 | uint8_t t_output = 0; 101 | int16_t t_11_bit_timer = 0; 102 | uint8_t t_wave_index = 0; 103 | uint16_t t_length_counter = 0; 104 | uint16_t t_linear_counter = 0; 105 | uint8_t t_channel = 3; 106 | uint8_t t_pin = 26; 107 | 108 | }; 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/BLE-LOCAL_MIDI_Translator/BLE-LOCAL_MIDI_Translator.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // M5STACK(ESP32)向け BLE MIDI 4 | // Programed by Kazuyuki Eguchi 2018/04/24 5 | // BLE->LOCAL Transrate code add by necobit 2020/07/03 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #include 13 | // MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI_H); 14 | 15 | #define MIDI_SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700" 16 | #define MIDI_CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3" 17 | #define DEVIVE_NAME "M5" 18 | 19 | BLEServer *pServer; 20 | BLEAdvertising *pAdvertising; 21 | BLECharacteristic *pCharacteristic; 22 | int pos = 0; 23 | char midi_s[5]; 24 | char midi_send[3]; 25 | 26 | class MyServerCallbacks: public BLEServerCallbacks { 27 | void onConnect(BLEServer* pServer) { 28 | M5.Lcd.fillScreen(BLACK); 29 | M5.Lcd.setTextSize(1); 30 | M5.Lcd.setCursor(10, 0); 31 | M5.Lcd.printf("BLE MIDI Connected."); 32 | }; 33 | 34 | void onDisconnect(BLEServer* pServer) { 35 | M5.Lcd.fillScreen(BLACK); 36 | M5.Lcd.setTextSize(1); 37 | M5.Lcd.setCursor(10, 0); 38 | M5.Lcd.printf("BLE MIDI Disconnect."); 39 | } 40 | }; 41 | 42 | class MyCallbacks: public BLECharacteristicCallbacks { 43 | void onWrite(BLECharacteristic *pCharacteristic) { 44 | std::string rxValue = pCharacteristic->getValue(); 45 | pos = 0; 46 | 47 | if (rxValue.length() > 0) { 48 | for (int i = 0; i < rxValue.length(); i++) 49 | { 50 | midi_s[pos] = rxValue[i]; 51 | 52 | pos++; 53 | 54 | if (pos == 5) 55 | { 56 | Serial2.write(midi_s[2]); 57 | Serial2.write(midi_s[3]); 58 | Serial2.write(midi_s[4]); 59 | Serial.printf("%02x %02x %02x", midi_s[2], midi_s[3], midi_s[4]); 60 | pos = 0; 61 | } 62 | } 63 | Serial.println(); 64 | } 65 | } 66 | }; 67 | 68 | void setup() { 69 | M5.begin(); 70 | Serial.begin(115200); 71 | Serial2.begin(31250); 72 | M5.Lcd.fillScreen(BLACK); 73 | M5.Lcd.setTextColor(WHITE); 74 | M5.Lcd.setTextSize(1); 75 | M5.Lcd.setCursor(10, 0); 76 | M5.Lcd.printf("BLE MIDI Disconnect."); 77 | 78 | BLEDevice::init(DEVIVE_NAME); 79 | pServer = BLEDevice::createServer(); 80 | pServer->setCallbacks(new MyServerCallbacks()); 81 | BLEService *pService = pServer->createService(BLEUUID(MIDI_SERVICE_UUID)); 82 | pCharacteristic = pService->createCharacteristic( 83 | BLEUUID(MIDI_CHARACTERISTIC_UUID), 84 | BLECharacteristic::PROPERTY_READ | 85 | BLECharacteristic::PROPERTY_WRITE | 86 | BLECharacteristic::PROPERTY_NOTIFY | 87 | BLECharacteristic::PROPERTY_WRITE_NR 88 | ); 89 | pCharacteristic->setCallbacks(new MyCallbacks()); 90 | pService->start(); 91 | 92 | BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); 93 | oAdvertisementData.setFlags(0x04); 94 | oAdvertisementData.setCompleteServices(BLEUUID(MIDI_SERVICE_UUID)); 95 | oAdvertisementData.setName(DEVIVE_NAME); 96 | pAdvertising = pServer->getAdvertising(); 97 | pAdvertising->setAdvertisementData(oAdvertisementData); 98 | pAdvertising->start(); 99 | } 100 | 101 | void loop() { 102 | // delay(10); 103 | } 104 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack3VoiceSynth/M5Stack3VoiceSynth.ino: -------------------------------------------------------------------------------- 1 | //認知のバグ 2 | //手弾きで和音を鳴らすと一つ音が鳴らないことがあります。 3 | 4 | //setup()内のコメントアウトを外して、Tunes.hの 5 | // int DAC_OUT = 25; 6 | //をint DAC_OUT = 26;に変更して 7 | //Bottomモジュールに出ているGPIO25と26の間に50k程度の 8 | //可変抵抗を挟むとある程度音量制御ができます。 9 | 10 | #include 11 | #include //Arduino-ESP32 driver 12 | #include 13 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 14 | 15 | #include "Tunes.h" 16 | Tunes tunes; 17 | 18 | void setup() { 19 | M5.begin(); 20 | // dacWrite(25, 0); // Speaker OFF 21 | // ledcDetachPin(SPEAKER_PIN); 22 | // pinMode(SPEAKER_PIN, INPUT); 23 | MIDI.begin(); 24 | MIDI.turnThruOff(); 25 | 26 | tunes.init(); 27 | M5.Speaker.setVolume(1); 28 | 29 | M5.Lcd.setTextSize(3); 30 | M5.Lcd.setCursor(50, 0); 31 | M5.Lcd.print("Simple 3Voice"); 32 | M5.Lcd.setCursor(160, 30); 33 | M5.Lcd.print("Synth"); 34 | M5.Lcd.setTextSize(1); 35 | M5.Lcd.setCursor(180, 60); 36 | M5.Lcd.print("Powered by @ina_ani"); 37 | } 38 | 39 | int vt[3]; 40 | int v; 41 | int vn[3]; 42 | 43 | void noteon(int data1, int data2) 44 | { 45 | if (vt[0] <= vt[1] and vt[0] <= vt[2]) { 46 | noteoff(vn[0]); 47 | vt[0] = millis(); 48 | v = 0; 49 | vn[0] = data1; 50 | } 51 | else if (vt[1] <= vt[2]) { 52 | noteoff(vn[1]); 53 | vt[1] = millis(); 54 | v = 1; 55 | vn[1] = data1; 56 | } 57 | else { 58 | noteoff(vn[2]); 59 | vt[2] = millis(); 60 | v = 2; 61 | vn[2] = data1; 62 | } 63 | 64 | portENTER_CRITICAL(&Tunes::timerMux); 65 | Tunes::d[v] = (uint16_t)(tunes.tones[data1]); 66 | portEXIT_CRITICAL(&Tunes::timerMux); 67 | } 68 | 69 | void noteoff(int data1) 70 | { 71 | if (vn[0] == data1) { 72 | v = 0; 73 | } 74 | else if (vn[1] == data1) { 75 | v = 1; 76 | } 77 | else if (vn[2] == data1) { 78 | v = 2; 79 | } 80 | else return; 81 | portENTER_CRITICAL(&Tunes::timerMux); 82 | Tunes::d[v] = 0; 83 | portEXIT_CRITICAL(&Tunes::timerMux); 84 | vt[v] = 0; 85 | } 86 | 87 | void loop() { 88 | tunes.resume(); 89 | tunes.run(); 90 | int data1, data2; 91 | if (MIDI.read()) 92 | { 93 | if (MIDI.getType() == midi::NoteOn) 94 | { 95 | data1 = MIDI.getData1(); 96 | data2 = MIDI.getData2(); 97 | noteon(data1, data2); 98 | } 99 | } 100 | 101 | if (MIDI.getType() == midi::NoteOff) 102 | { 103 | data1 = MIDI.getData1(); 104 | noteoff(data1); 105 | } 106 | 107 | M5.update(); 108 | 109 | } 110 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack3VoiceSynth/Tunes.cpp: -------------------------------------------------------------------------------- 1 | #include "Tunes.h" 2 | 3 | volatile SemaphoreHandle_t Tunes::timerSemaphore; 4 | portMUX_TYPE Tunes::timerMux = portMUX_INITIALIZER_UNLOCKED; 5 | volatile uint32_t Tunes::isrCounter = 0; 6 | volatile uint32_t Tunes::lastIsrAt = 0; 7 | volatile uint16_t Tunes::osc1 = 0; 8 | volatile uint16_t Tunes::osc2 = 0; 9 | volatile uint16_t Tunes::osc3 = 0; 10 | volatile uint16_t Tunes::d[3] = {0, 0, 0}; 11 | int Tunes::SineValues[256]; 12 | int Tunes::SquareValues[256]; 13 | hw_timer_t* Tunes::timer; 14 | unsigned int Tunes::tones[] = { 15 | 27, 16 | 28, 17 | 30, 18 | 32, 19 | 34, 20 | 36, 21 | 38, 22 | 40, 23 | 43, 24 | 45, 25 | 48, 26 | 51, 27 | 54, 28 | 57, 29 | 60, 30 | 64, 31 | 68, 32 | 72, 33 | 76, 34 | 80, 35 | 85, 36 | 90, 37 | 95, 38 | 101, 39 | 107, 40 | 114, 41 | 120, 42 | 127, 43 | 135, 44 | 143, 45 | 152, 46 | 161, 47 | 170, 48 | 180, 49 | 191, 50 | 202, 51 | 214, 52 | 227, 53 | 241, 54 | 255, 55 | 270, 56 | 286, 57 | 303, 58 | 321, 59 | 340, 60 | 360, 61 | 382, 62 | 405, 63 | 429, 64 | 454, 65 | 481, 66 | 510, 67 | 540, 68 | 572, 69 | 606, 70 | 642, 71 | 680, 72 | 721, 73 | 764, 74 | 809, 75 | 857, 76 | 908, 77 | 962, 78 | 1020, 79 | 1080, 80 | 1144, 81 | 1212, 82 | 1284, 83 | 1361, 84 | 1442, 85 | 1528, 86 | 1618, 87 | 1715, 88 | 1817, 89 | 1925, 90 | 2039, 91 | 2160, 92 | 2289, 93 | 2425, 94 | 2569, 95 | 2722, 96 | 2884, 97 | 3055, 98 | 3237, 99 | 3429, 100 | 3633, 101 | 3849, 102 | 4078, 103 | 4320, 104 | 4577, 105 | 4850, 106 | 5138, 107 | 5443, 108 | 5767, 109 | 6110, 110 | 6473, 111 | 6858, 112 | 7266, 113 | 7698, 114 | 8156, 115 | 8641, 116 | 9155, 117 | 9699, 118 | 10276, 119 | 10887, 120 | 11534, 121 | 12220, 122 | 12947, 123 | 13717, 124 | 14532, 125 | 15396, 126 | 16312, 127 | 17282, 128 | 18310, 129 | 19398, 130 | 20552, 131 | 21774, 132 | 23069, 133 | 24440, 134 | 25894, 135 | 27433, 136 | 29065, 137 | 30793, 138 | 32624, 139 | 34564, 140 | 36619, 141 | 38797, 142 | 41104 143 | 144 | }; 145 | 146 | void Tunes::onTimer() { 147 | // Increment the counter and set the time of ISR 148 | portENTER_CRITICAL_ISR(&Tunes::timerMux); 149 | Tunes::isrCounter++; 150 | Tunes::lastIsrAt = millis(); 151 | Tunes::osc1 += d[0]; 152 | Tunes::osc2 += d[1]; 153 | Tunes::osc3 += d[2]; 154 | portEXIT_CRITICAL_ISR(&Tunes::timerMux); 155 | // Give a semaphore that we can check in the loop 156 | 157 | xSemaphoreGiveFromISR(Tunes::timerSemaphore, NULL); 158 | int out = 0; 159 | //out += (Tunes::osc1 >> 8);//((osc1 >> 15)&1 == 1)?0:200; 160 | //out += (Tunes::osc2 >> 8);//((osc2 >> 15)&1 == 1)?0:200; 161 | //out += (Tunes::osc3 >> 8);//((osc3 >> 15)&1 == 1)?0:200; 162 | out += Tunes::SineValues[(osc1 >> 8)];//((osc1 >> 15)&1 == 1)?0:200; 163 | out += Tunes::SineValues[(osc2 >> 8)];//((osc2 >> 15)&1 == 1)?0:200; 164 | out += Tunes::SineValues[(osc3 >> 8)];//((osc3 >> 15)&1 == 1)?0:200; 165 | //out += ((osc1 >> 15)&1 == 1)?0:200; 166 | //out += ((osc2 >> 15)&1 == 1)?0:200; 167 | //out += ((osc3 >> 15)&1 == 1)?0:200; 168 | int DAC_OUT = 25; 169 | if (d[0] == 0 && d[1] == 0 && d[2] == 0) { 170 | digitalWrite(DAC_OUT, LOW); 171 | } else { 172 | dacWrite(DAC_OUT, (out / 3 + 128)); 173 | } 174 | } 175 | 176 | void Tunes::init() { 177 | float ConversionFactor = (2 * PI) / 256; // convert my 0-255 bits in a circle to radians 178 | // there are 2 x PI radians in a circle hence the 2*PI 179 | // Then divide by 256 to get the value in radians 180 | // for one of my 0-255 bits. 181 | float RadAngle; // Angle in Radians 182 | // calculate sine values 183 | for (int MyAngle = 0; MyAngle < 256; MyAngle++) { 184 | RadAngle = MyAngle * ConversionFactor; // 8 bit angle converted to radians 185 | Tunes::SineValues[MyAngle] = (sin(RadAngle) * 128); // get the sine of this angle and 'shift' up so 186 | // there are no negative values in the data 187 | // as the DAC does not understand them and would 188 | // convert to positive values. 189 | Tunes::SquareValues[MyAngle] = MyAngle < 128 ? 0 : 128; 190 | } 191 | Tunes::timerSemaphore = xSemaphoreCreateBinary(); 192 | 193 | Tunes::timer = timerBegin(0, 80, true); // /80 prescale = 1us = 1/1000000s = 1MHz 194 | timerAttachInterrupt(Tunes::timer, &Tunes::onTimer, true); 195 | timerAlarmWrite(Tunes::timer, 50, true); 196 | timerAlarmEnable(Tunes::timer); 197 | 198 | } 199 | 200 | void Tunes::pause() { 201 | timerAlarmDisable(Tunes::timer); 202 | } 203 | void Tunes::resume() { 204 | timerAlarmEnable(Tunes::timer); 205 | } 206 | 207 | void Tunes::run() { 208 | // get semaphore to use GPIO registers (maybe? 209 | if (xSemaphoreTake(Tunes::timerSemaphore, 0) == pdTRUE) { 210 | uint32_t isrCount = 0, isrTime = 0; 211 | portENTER_CRITICAL(&Tunes::timerMux); 212 | // ==== critical section begin ==== 213 | isrCount = Tunes::isrCounter; 214 | isrTime = Tunes::lastIsrAt; 215 | 216 | // ==== critical section end ==== 217 | portEXIT_CRITICAL(&Tunes::timerMux); 218 | 219 | } 220 | // below script is safe? 221 | } 222 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack3VoiceSynth/Tunes.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef TUNES_H 4 | #define TUNES_H 5 | 6 | class Tunes 7 | { 8 | public: 9 | static volatile SemaphoreHandle_t timerSemaphore; 10 | static portMUX_TYPE timerMux; 11 | 12 | static volatile uint32_t isrCounter; 13 | static volatile uint32_t lastIsrAt; 14 | static volatile uint16_t osc1; 15 | static volatile uint16_t osc2; 16 | static volatile uint16_t osc3; 17 | static volatile uint16_t d[3]; 18 | static unsigned int tones[128]; 19 | static int SineValues[256]; 20 | static int SquareValues[256]; 21 | static hw_timer_t* timer; 22 | 23 | void init(); 24 | void static IRAM_ATTR onTimer(); 25 | void run(); 26 | void pause(); 27 | void resume(); 28 | 29 | }; 30 | #endif 31 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack5VoiceSynth/M5Stack5VoiceSynth.ino: -------------------------------------------------------------------------------- 1 | //認知のバグ 2 | //手弾きで和音を鳴らすと一つ音が鳴らないことがあります。 3 | 4 | //setup()内のコメントアウトを外して、Tunes.hの 5 | // int DAC_OUT = 25; 6 | //をint DAC_OUT = 26;に変更して 7 | //Bottomモジュールに出ているGPIO25と26の間に50k程度の 8 | //可変抵抗を挟むとある程度音量制御ができます。 9 | 10 | #include 11 | #include //Arduino-ESP32 driver 12 | #include 13 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 14 | 15 | #include "Tunes.h" 16 | Tunes tunes; 17 | 18 | void setup() { 19 | M5.begin(); 20 | dacWrite(25, 0); // Speaker OFF 21 | ledcDetachPin(SPEAKER_PIN); 22 | pinMode(SPEAKER_PIN, INPUT); 23 | MIDI.begin(); 24 | MIDI.turnThruOff(); 25 | 26 | tunes.init(); 27 | M5.Speaker.setVolume(1); 28 | 29 | M5.Lcd.setTextSize(3); 30 | M5.Lcd.setCursor(50, 0); 31 | M5.Lcd.print("Simple 3Voice"); 32 | M5.Lcd.setCursor(160, 30); 33 | M5.Lcd.print("Synth"); 34 | M5.Lcd.setTextSize(1); 35 | M5.Lcd.setCursor(180, 60); 36 | M5.Lcd.print("Powered by @ina_ani"); 37 | } 38 | 39 | int vt[5]; 40 | int v; 41 | int vn[5]; 42 | 43 | void noteon(int data1, int data2) 44 | { 45 | if (vt[0] <= vt[1] and vt[0] <= vt[2]) { 46 | noteoff(vn[0]); 47 | vt[0] = millis(); 48 | v = 0; 49 | vn[0] = data1; 50 | } 51 | else if (vt[1] <= vt[2]) { 52 | noteoff(vn[1]); 53 | vt[1] = millis(); 54 | v = 1; 55 | vn[1] = data1; 56 | } 57 | else { 58 | noteoff(vn[2]); 59 | vt[2] = millis(); 60 | v = 2; 61 | vn[2] = data1; 62 | } 63 | 64 | portENTER_CRITICAL(&Tunes::timerMux); 65 | Tunes::d[v] = (uint16_t)(tunes.tones[data1]); 66 | portEXIT_CRITICAL(&Tunes::timerMux); 67 | } 68 | 69 | void noteoff(int data1) 70 | { 71 | if (vn[0] == data1) { 72 | v = 0; 73 | } 74 | else if (vn[1] == data1) { 75 | v = 1; 76 | } 77 | else if (vn[2] == data1) { 78 | v = 2; 79 | } 80 | else if (vn[3] == data1) { 81 | v = 3; 82 | } 83 | else if (vn[4] == data1) { 84 | v = 4; 85 | } 86 | 87 | else return; 88 | portENTER_CRITICAL(&Tunes::timerMux); 89 | Tunes::d[v] = 0; 90 | portEXIT_CRITICAL(&Tunes::timerMux); 91 | vt[v] = 0; 92 | } 93 | 94 | void loop() { 95 | tunes.resume(); 96 | tunes.run(); 97 | int data1, data2; 98 | if (MIDI.read()) 99 | { 100 | if (MIDI.getType() == midi::NoteOn) 101 | { 102 | data1 = MIDI.getData1(); 103 | data2 = MIDI.getData2(); 104 | noteon(data1, data2); 105 | } 106 | } 107 | 108 | if (MIDI.getType() == midi::NoteOff) 109 | { 110 | data1 = MIDI.getData1(); 111 | noteoff(data1); 112 | } 113 | 114 | M5.update(); 115 | 116 | } 117 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack5VoiceSynth/Tunes.cpp: -------------------------------------------------------------------------------- 1 | #include "Tunes.h" 2 | 3 | volatile SemaphoreHandle_t Tunes::timerSemaphore; 4 | portMUX_TYPE Tunes::timerMux = portMUX_INITIALIZER_UNLOCKED; 5 | volatile uint32_t Tunes::isrCounter = 0; 6 | volatile uint32_t Tunes::lastIsrAt = 0; 7 | volatile uint16_t Tunes::osc1 = 0; 8 | volatile uint16_t Tunes::osc2 = 0; 9 | volatile uint16_t Tunes::osc3 = 0; 10 | volatile uint16_t Tunes::osc4 = 0; 11 | volatile uint16_t Tunes::osc5 = 0; 12 | volatile uint16_t Tunes::d[5] = {0,0,0,0,0}; 13 | int Tunes::SineValues[256]; 14 | int Tunes::SquareValues[256]; 15 | hw_timer_t* Tunes::timer; 16 | unsigned int Tunes::tones[] = { 17 | 27, 18 | 28, 19 | 30, 20 | 32, 21 | 34, 22 | 36, 23 | 38, 24 | 40, 25 | 43, 26 | 45, 27 | 48, 28 | 51, 29 | 54, 30 | 57, 31 | 60, 32 | 64, 33 | 68, 34 | 72, 35 | 76, 36 | 80, 37 | 85, 38 | 90, 39 | 95, 40 | 101, 41 | 107, 42 | 114, 43 | 120, 44 | 127, 45 | 135, 46 | 143, 47 | 152, 48 | 161, 49 | 170, 50 | 180, 51 | 191, 52 | 202, 53 | 214, 54 | 227, 55 | 241, 56 | 255, 57 | 270, 58 | 286, 59 | 303, 60 | 321, 61 | 340, 62 | 360, 63 | 382, 64 | 405, 65 | 429, 66 | 454, 67 | 481, 68 | 510, 69 | 540, 70 | 572, 71 | 606, 72 | 642, 73 | 680, 74 | 721, 75 | 764, 76 | 809, 77 | 857, 78 | 908, 79 | 962, 80 | 1020, 81 | 1080, 82 | 1144, 83 | 1212, 84 | 1284, 85 | 1361, 86 | 1442, 87 | 1528, 88 | 1618, 89 | 1715, 90 | 1817, 91 | 1925, 92 | 2039, 93 | 2160, 94 | 2289, 95 | 2425, 96 | 2569, 97 | 2722, 98 | 2884, 99 | 3055, 100 | 3237, 101 | 3429, 102 | 3633, 103 | 3849, 104 | 4078, 105 | 4320, 106 | 4577, 107 | 4850, 108 | 5138, 109 | 5443, 110 | 5767, 111 | 6110, 112 | 6473, 113 | 6858, 114 | 7266, 115 | 7698, 116 | 8156, 117 | 8641, 118 | 9155, 119 | 9699, 120 | 10276, 121 | 10887, 122 | 11534, 123 | 12220, 124 | 12947, 125 | 13717, 126 | 14532, 127 | 15396, 128 | 16312, 129 | 17282, 130 | 18310, 131 | 19398, 132 | 20552, 133 | 21774, 134 | 23069, 135 | 24440, 136 | 25894, 137 | 27433, 138 | 29065, 139 | 30793, 140 | 32624, 141 | 34564, 142 | 36619, 143 | 38797, 144 | 41104 145 | 146 | }; 147 | 148 | void Tunes::onTimer() { 149 | // Increment the counter and set the time of ISR 150 | portENTER_CRITICAL_ISR(&Tunes::timerMux); 151 | Tunes::isrCounter++; 152 | Tunes::lastIsrAt = millis(); 153 | Tunes::osc1 += d[0]; 154 | Tunes::osc2 += d[1]; 155 | Tunes::osc3 += d[2]; 156 | Tunes::osc4 += d[3]; 157 | Tunes::osc5 += d[4]; 158 | portEXIT_CRITICAL_ISR(&Tunes::timerMux); 159 | // Give a semaphore that we can check in the loop 160 | 161 | xSemaphoreGiveFromISR(Tunes::timerSemaphore, NULL); 162 | int out = 0; 163 | //out += (Tunes::osc1 >> 8);//((osc1 >> 15)&1 == 1)?0:200; 164 | //out += (Tunes::osc2 >> 8);//((osc2 >> 15)&1 == 1)?0:200; 165 | //out += (Tunes::osc3 >> 8);//((osc3 >> 15)&1 == 1)?0:200; 166 | out += Tunes::SineValues[(osc1 >> 8)];//((osc1 >> 15)&1 == 1)?0:200; 167 | out += Tunes::SineValues[(osc2 >> 8)];//((osc2 >> 15)&1 == 1)?0:200; 168 | out += Tunes::SineValues[(osc3 >> 8)];//((osc3 >> 15)&1 == 1)?0:200; 169 | out += Tunes::SineValues[(osc4 >> 8)];//((osc3 >> 15)&1 == 1)?0:200; 170 | out += Tunes::SineValues[(osc5 >> 8)];//((osc3 >> 15)&1 == 1)?0:200; 171 | //out += ((osc1 >> 15)&1 == 1)?0:200; 172 | //out += ((osc2 >> 15)&1 == 1)?0:200; 173 | //out += ((osc3 >> 15)&1 == 1)?0:200; 174 | int DAC_OUT = 26; 175 | if (d[0] == 0 && d[1] == 0 && d[2] == 0 && d[3] == 0 && d[4] == 0) { 176 | digitalWrite(DAC_OUT, LOW); 177 | } else { 178 | dacWrite(DAC_OUT, (out / 5 + 128)); 179 | } 180 | } 181 | 182 | void Tunes::init() { 183 | float ConversionFactor = (2 * PI) / 256; // convert my 0-255 bits in a circle to radians 184 | // there are 2 x PI radians in a circle hence the 2*PI 185 | // Then divide by 256 to get the value in radians 186 | // for one of my 0-255 bits. 187 | float RadAngle; // Angle in Radians 188 | // calculate sine values 189 | for (int MyAngle = 0; MyAngle < 256; MyAngle++) { 190 | RadAngle = MyAngle * ConversionFactor; // 8 bit angle converted to radians 191 | Tunes::SineValues[MyAngle] = (sin(RadAngle) * 128); // get the sine of this angle and 'shift' up so 192 | // there are no negative values in the data 193 | // as the DAC does not understand them and would 194 | // convert to positive values. 195 | Tunes::SquareValues[MyAngle] = MyAngle < 128 ? 0 : 128; 196 | } 197 | Tunes::timerSemaphore = xSemaphoreCreateBinary(); 198 | 199 | Tunes::timer = timerBegin(0, 80, true); // /80 prescale = 1us = 1/1000000s = 1MHz 200 | timerAttachInterrupt(Tunes::timer, &Tunes::onTimer, true); 201 | timerAlarmWrite(Tunes::timer, 50, true); 202 | timerAlarmEnable(Tunes::timer); 203 | 204 | } 205 | 206 | void Tunes::pause() { 207 | timerAlarmDisable(Tunes::timer); 208 | } 209 | void Tunes::resume() { 210 | timerAlarmEnable(Tunes::timer); 211 | } 212 | 213 | void Tunes::run() { 214 | // get semaphore to use GPIO registers (maybe? 215 | if (xSemaphoreTake(Tunes::timerSemaphore, 0) == pdTRUE) { 216 | uint32_t isrCount = 0, isrTime = 0; 217 | portENTER_CRITICAL(&Tunes::timerMux); 218 | // ==== critical section begin ==== 219 | isrCount = Tunes::isrCounter; 220 | isrTime = Tunes::lastIsrAt; 221 | 222 | // ==== critical section end ==== 223 | portEXIT_CRITICAL(&Tunes::timerMux); 224 | 225 | } 226 | // below script is safe? 227 | } 228 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack5VoiceSynth/Tunes.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef TUNES_H 4 | #define TUNES_H 5 | 6 | class Tunes 7 | { 8 | public: 9 | static volatile SemaphoreHandle_t timerSemaphore; 10 | static portMUX_TYPE timerMux; 11 | 12 | static volatile uint32_t isrCounter; 13 | static volatile uint32_t lastIsrAt; 14 | static volatile uint16_t osc1; 15 | static volatile uint16_t osc2; 16 | static volatile uint16_t osc3; 17 | static volatile uint16_t osc4; 18 | static volatile uint16_t osc5; 19 | static volatile uint16_t d[5]; 20 | static unsigned int tones[128]; 21 | static int SineValues[256]; 22 | static int SquareValues[256]; 23 | static hw_timer_t* timer; 24 | 25 | void init(); 26 | void static IRAM_ATTR onTimer(); 27 | void run(); 28 | void pause(); 29 | void resume(); 30 | 31 | }; 32 | #endif 33 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5StackSimpleSampler/M5StackSimpleSampler.ino: -------------------------------------------------------------------------------- 1 | #pragma mark - Depend ESP8266Audio and ESP8266_Spiram libraries 2 | /* 3 | cd ~/Arduino/libraries 4 | git clone https://github.com/earlephilhower/ESP8266Audio 5 | git clone https://github.com/Gianbacchio/ESP8266_Spiram 6 | 7 | Use the "Tools->ESP32 Sketch Data Upload" menu to write the MP3 to SPIFFS 8 | Then upload the sketch normally. 9 | https://github.com/me-no-dev/arduino-esp32fs-plugin 10 | */ 11 | 12 | #include 13 | #include "AudioFileSourceSD.h" 14 | #include "AudioGeneratorWAV.h" 15 | #include "AudioOutputI2S.h" 16 | 17 | #include 18 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 19 | 20 | AudioGeneratorWAV *wav; 21 | AudioFileSourceSD *file; 22 | AudioOutputI2S *out; 23 | 24 | void setup() 25 | { 26 | M5.begin(); 27 | MIDI.begin(); 28 | MIDI.turnThruOff(); 29 | 30 | M5.Lcd.setTextSize(3); 31 | M5.Lcd.setCursor(50, 0); 32 | M5.Lcd.print("Simple Sampler"); 33 | 34 | M5.Lcd.setCursor(10, 40); 35 | M5.Lcd.print("PLAY Note C3"); 36 | 37 | } 38 | 39 | void loop() 40 | { 41 | int data1, data2; 42 | if (MIDI.read()) 43 | { 44 | if (MIDI.getType() == midi::NoteOn) 45 | { 46 | if (MIDI.getData1() == 60) 47 | { 48 | M5.Lcd.setCursor(10, 60); 49 | M5.Lcd.print("Play"); 50 | M5.update(); 51 | 52 | file = new AudioFileSourceSD("/cat4410016.wav"); 53 | out = new AudioOutputI2S(0, 1); 54 | out->SetOutputModeMono(true); 55 | out->SetGain(0.1); 56 | wav = new AudioGeneratorWAV(); 57 | wav->begin(file, out); 58 | 59 | while (wav->isRunning()) { 60 | if (!wav->loop()) wav->stop(); 61 | } 62 | } 63 | } 64 | else if (MIDI.getType() == midi::NoteOff) 65 | { 66 | if (MIDI.getData1() == 60) 67 | { 68 | M5.Lcd.setCursor(10, 60); 69 | M5.Lcd.print("Stop"); 70 | M5.update(); 71 | wav->stop(); 72 | 73 | 74 | } 75 | } 76 | 77 | } 78 | } 79 | 80 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5StackSimpleSampler/cat4410016.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/necobit/M5Stack-MIDI-Module/3e8d713115ec434e45967656a841eba9f527afba/Arduino-IDE-Sketch/M5StackSimpleSampler/cat4410016.wav -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5StackWavPlay/M5StackWavPlay.ino: -------------------------------------------------------------------------------- 1 | #pragma mark - Depend ESP8266Audio and ESP8266_Spiram libraries 2 | /* 3 | cd ~/Arduino/libraries 4 | git clone https://github.com/earlephilhower/ESP8266Audio 5 | git clone https://github.com/Gianbacchio/ESP8266_Spiram 6 | 7 | Use the "Tools->ESP32 Sketch Data Upload" menu to write the MP3 to SPIFFS 8 | Then upload the sketch normally. 9 | https://github.com/me-no-dev/arduino-esp32fs-plugin 10 | */ 11 | #include 12 | #include 13 | #include "AudioFileSourceSD.h" 14 | #include "AudioGeneratorWAV.h" 15 | #include "AudioOutputI2S.h" 16 | 17 | #include 18 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 19 | 20 | AudioGeneratorWAV *wav; 21 | AudioFileSourceSD *file; 22 | AudioOutputI2S *out; 23 | 24 | void setup() 25 | { 26 | M5.begin(); 27 | MIDI.begin(); 28 | MIDI.turnThruOff(); 29 | 30 | M5.Lcd.setTextSize(3); 31 | M5.Lcd.setCursor(50, 0); 32 | M5.Lcd.print("Simple Sampler"); 33 | 34 | M5.Lcd.setCursor(10, 40); 35 | M5.Lcd.print("PUSH BUTTON A"); 36 | 37 | M5.Lcd.setCursor(10, 60); 38 | M5.Lcd.print("Play"); 39 | M5.update(); 40 | pinMode(25,INPUT); 41 | //pinMode(26,OUTPUT); 42 | //dac_output_enable(DAC_CHANNEL_2); //26 43 | dac_output_disable(DAC_CHANNEL_1); // 25 44 | file = new AudioFileSourceSD("/cat4410016.wav"); 45 | out = new AudioOutputI2S(0, 1); 46 | out->SetOutputModeMono(false); 47 | out->SetGain(0.5); 48 | wav = new AudioGeneratorWAV(); 49 | wav->begin(file, out); 50 | 51 | while (wav->isRunning()) { 52 | if (!wav->loop()) wav->stop(); 53 | } 54 | 55 | } 56 | 57 | void loop() 58 | { 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_Gyrocontroller/M5Stack_Gyrocontroller.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "utility/MPU9250.h" 3 | #include "utility/quaternionFilters.h" 4 | 5 | #include 6 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 7 | 8 | #define processing_out false 9 | #define AHRS true // Set to false for basic data read 10 | #define SerialDebug false // Set to true to get Serial output for debugging 11 | #define LCD 12 | 13 | MPU9250 IMU; 14 | // Kalman kalmanX, kalmanY, kalmanZ; // Create the Kalman instances 15 | 16 | void setup() 17 | { 18 | M5.begin(); 19 | Wire.begin(); 20 | 21 | MIDI.begin(); 22 | MIDI.turnThruOff(); 23 | 24 | #ifdef LCD 25 | // Start device display with ID of sensor 26 | M5.Lcd.fillScreen(BLACK); 27 | M5.Lcd.setTextColor(WHITE , BLACK); // Set pixel color; 1 on the monochrome screen 28 | M5.Lcd.setTextSize(2); 29 | M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250"); 30 | M5.Lcd.setTextSize(1); 31 | M5.Lcd.setCursor(0, 20); M5.Lcd.print("9-DOF 16-bit"); 32 | M5.Lcd.setCursor(0, 30); M5.Lcd.print("motion sensor"); 33 | M5.Lcd.setCursor(20, 40); M5.Lcd.print("60 ug LSB"); 34 | delay(1000); 35 | 36 | // Set up for data display 37 | M5.Lcd.setTextSize(1); // Set text size to normal, 2 is twice normal etc. 38 | M5.Lcd.fillScreen(BLACK); // clears the screen and buffer 39 | #endif // LCD 40 | 41 | // Read the WHO_AM_I register, this is a good test of communication 42 | byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); 43 | Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); 44 | Serial.print(" I should be "); Serial.println(0x71, HEX); 45 | 46 | #ifdef LCD 47 | M5.Lcd.setCursor(20, 0); M5.Lcd.print("MPU9250"); 48 | M5.Lcd.setCursor(0, 10); M5.Lcd.print("I AM"); 49 | M5.Lcd.setCursor(0, 20); M5.Lcd.print(c, HEX); 50 | M5.Lcd.setCursor(0, 30); M5.Lcd.print("I Should Be"); 51 | M5.Lcd.setCursor(0, 40); M5.Lcd.print(0x71, HEX); 52 | delay(1000); 53 | #endif // LCD 54 | 55 | // if (c == 0x71) // WHO_AM_I should always be 0x68 56 | { 57 | Serial.println("MPU9250 is online..."); 58 | 59 | // Start by performing self test and reporting values 60 | IMU.MPU9250SelfTest(IMU.SelfTest); 61 | Serial.print("x-axis self test: acceleration trim within : "); 62 | Serial.print(IMU.SelfTest[0], 1); Serial.println("% of factory value"); 63 | Serial.print("y-axis self test: acceleration trim within : "); 64 | Serial.print(IMU.SelfTest[1], 1); Serial.println("% of factory value"); 65 | Serial.print("z-axis self test: acceleration trim within : "); 66 | Serial.print(IMU.SelfTest[2], 1); Serial.println("% of factory value"); 67 | Serial.print("x-axis self test: gyration trim within : "); 68 | Serial.print(IMU.SelfTest[3], 1); Serial.println("% of factory value"); 69 | Serial.print("y-axis self test: gyration trim within : "); 70 | Serial.print(IMU.SelfTest[4], 1); Serial.println("% of factory value"); 71 | Serial.print("z-axis self test: gyration trim within : "); 72 | Serial.print(IMU.SelfTest[5], 1); Serial.println("% of factory value"); 73 | 74 | // Calibrate gyro and accelerometers, load biases in bias registers 75 | IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias); 76 | 77 | #ifdef LCD 78 | M5.Lcd.fillScreen(BLACK); 79 | M5.Lcd.setTextSize(1); 80 | M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250 bias"); 81 | M5.Lcd.setCursor(0, 16); M5.Lcd.print(" x y z "); 82 | 83 | M5.Lcd.setCursor(0, 32); M5.Lcd.print((int)(1000 * IMU.accelBias[0])); 84 | M5.Lcd.setCursor(32, 32); M5.Lcd.print((int)(1000 * IMU.accelBias[1])); 85 | M5.Lcd.setCursor(64, 32); M5.Lcd.print((int)(1000 * IMU.accelBias[2])); 86 | M5.Lcd.setCursor(96, 32); M5.Lcd.print("mg"); 87 | 88 | M5.Lcd.setCursor(0, 48); M5.Lcd.print(IMU.gyroBias[0], 1); 89 | M5.Lcd.setCursor(32, 48); M5.Lcd.print(IMU.gyroBias[1], 1); 90 | M5.Lcd.setCursor(64, 48); M5.Lcd.print(IMU.gyroBias[2], 1); 91 | M5.Lcd.setCursor(96, 48); M5.Lcd.print("o/s"); 92 | delay(1000); 93 | #endif // LCD 94 | 95 | IMU.initMPU9250(); 96 | // Initialize device for active mode read of acclerometer, gyroscope, and 97 | // temperature 98 | Serial.println("MPU9250 initialized for active data mode...."); 99 | 100 | // Read the WHO_AM_I register of the magnetometer, this is a good test of 101 | // communication 102 | byte d = IMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963); 103 | Serial.print("AK8963 "); Serial.print("I AM "); Serial.print(d, HEX); 104 | Serial.print(" I should be "); Serial.println(0x48, HEX); 105 | 106 | #ifdef LCD 107 | M5.Lcd.fillScreen(BLACK); 108 | M5.Lcd.setCursor(20, 0); M5.Lcd.print("AK8963"); 109 | M5.Lcd.setCursor(0, 10); M5.Lcd.print("I AM"); 110 | M5.Lcd.setCursor(0, 20); M5.Lcd.print(d, HEX); 111 | M5.Lcd.setCursor(0, 30); M5.Lcd.print("I Should Be"); 112 | M5.Lcd.setCursor(0, 40); M5.Lcd.print(0x48, HEX); 113 | delay(1000); 114 | #endif // LCD 115 | 116 | // Get magnetometer calibration from AK8963 ROM 117 | IMU.initAK8963(IMU.magCalibration); 118 | // Initialize device for active mode read of magnetometer 119 | Serial.println("AK8963 initialized for active data mode...."); 120 | if (Serial) 121 | { 122 | // Serial.println("Calibration values: "); 123 | Serial.print("X-Axis sensitivity adjustment value "); 124 | Serial.println(IMU.magCalibration[0], 2); 125 | Serial.print("Y-Axis sensitivity adjustment value "); 126 | Serial.println(IMU.magCalibration[1], 2); 127 | Serial.print("Z-Axis sensitivity adjustment value "); 128 | Serial.println(IMU.magCalibration[2], 2); 129 | } 130 | 131 | #ifdef LCD 132 | M5.Lcd.fillScreen(BLACK); 133 | M5.Lcd.setCursor(20, 0); M5.Lcd.print("AK8963"); 134 | M5.Lcd.setCursor(0, 10); M5.Lcd.print("ASAX "); M5.Lcd.setCursor(50, 10); 135 | M5.Lcd.print(IMU.magCalibration[0], 2); 136 | M5.Lcd.setCursor(0, 20); M5.Lcd.print("ASAY "); M5.Lcd.setCursor(50, 20); 137 | M5.Lcd.print(IMU.magCalibration[1], 2); 138 | M5.Lcd.setCursor(0, 30); M5.Lcd.print("ASAZ "); M5.Lcd.setCursor(50, 30); 139 | M5.Lcd.print(IMU.magCalibration[2], 2); 140 | delay(1000); 141 | #endif // LCD 142 | } // if (c == 0x71) 143 | // else 144 | // { 145 | // Serial.print("Could not connect to MPU9250: 0x"); 146 | // Serial.println(c, HEX); 147 | // while(1) ; // Loop forever if communication doesn't happen 148 | // } 149 | 150 | M5.Lcd.setTextSize(1); 151 | M5.Lcd.setTextColor(GREEN , BLACK); 152 | M5.Lcd.fillScreen(BLACK); 153 | } 154 | 155 | int cc74, bcc74, cc71, bcc71, cc1, bcc1; 156 | 157 | void loop() 158 | { 159 | // If intPin goes high, all data registers have new data 160 | // On interrupt, check if data ready interrupt 161 | if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) 162 | { 163 | IMU.readAccelData(IMU.accelCount); // Read the x/y/z adc values 164 | IMU.getAres(); 165 | 166 | // Now we'll calculate the accleration value into actual g's 167 | // This depends on scale being set 168 | IMU.ax = (float)IMU.accelCount[0] * IMU.aRes; // - accelBias[0]; 169 | IMU.ay = (float)IMU.accelCount[1] * IMU.aRes; // - accelBias[1]; 170 | IMU.az = (float)IMU.accelCount[2] * IMU.aRes; // - accelBias[2]; 171 | 172 | cc74 = map(IMU.ax * 100, -100, 100, 127, 0); 173 | cc71 = map(IMU.ay * 100, -100, 100, 127, 0); 174 | cc1 = map(IMU.az * 100, -100, 100, 0, 127); 175 | 176 | if (bcc74 != cc74) { 177 | MIDI.sendControlChange(74, cc74, 1); 178 | bcc74 = cc74; 179 | } 180 | if (bcc71 != cc71) { 181 | MIDI.sendControlChange(71, cc71, 1); 182 | bcc71 = cc71; 183 | } 184 | 185 | if (bcc1 != cc1) { 186 | MIDI.sendControlChange(1, cc1, 1); 187 | bcc1 = cc1; 188 | } 189 | 190 | 191 | 192 | 193 | IMU.readGyroData(IMU.gyroCount); // Read the x/y/z adc values 194 | IMU.getGres(); 195 | 196 | // Calculate the gyro value into actual degrees per second 197 | // This depends on scale being set 198 | IMU.gx = (float)IMU.gyroCount[0] * IMU.gRes; 199 | IMU.gy = (float)IMU.gyroCount[1] * IMU.gRes; 200 | IMU.gz = (float)IMU.gyroCount[2] * IMU.gRes; 201 | 202 | IMU.readMagData(IMU.magCount); // Read the x/y/z adc values 203 | IMU.getMres(); 204 | // User environmental x-axis correction in milliGauss, should be 205 | // automatically calculated 206 | IMU.magbias[0] = +470.; 207 | // User environmental x-axis correction in milliGauss TODO axis?? 208 | IMU.magbias[1] = +120.; 209 | // User environmental x-axis correction in milliGauss 210 | IMU.magbias[2] = +125.; 211 | 212 | // Calculate the magnetometer values in milliGauss 213 | // Include factory calibration per data sheet and user environmental 214 | // corrections 215 | // Get actual magnetometer value, this depends on scale being set 216 | IMU.mx = (float)IMU.magCount[0] * IMU.mRes * IMU.magCalibration[0] - 217 | IMU.magbias[0]; 218 | IMU.my = (float)IMU.magCount[1] * IMU.mRes * IMU.magCalibration[1] - 219 | IMU.magbias[1]; 220 | IMU.mz = (float)IMU.magCount[2] * IMU.mRes * IMU.magCalibration[2] - 221 | IMU.magbias[2]; 222 | } // if (readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) 223 | 224 | // Must be called before updating quaternions! 225 | IMU.updateTime(); 226 | 227 | // Sensors x (y)-axis of the accelerometer is aligned with the y (x)-axis of 228 | // the magnetometer; the magnetometer z-axis (+ down) is opposite to z-axis 229 | // (+ up) of accelerometer and gyro! We have to make some allowance for this 230 | // orientationmismatch in feeding the output to the quaternion filter. For the 231 | // MPU-9250, we have chosen a magnetic rotation that keeps the sensor forward 232 | // along the x-axis just like in the LSM9DS0 sensor. This rotation can be 233 | // modified to allow any convenient orientation convention. This is ok by 234 | // aircraft orientation standards! Pass gyro rate as rad/s 235 | // MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 236 | MahonyQuaternionUpdate(IMU.ax, IMU.ay, IMU.az, IMU.gx * DEG_TO_RAD, 237 | IMU.gy * DEG_TO_RAD, IMU.gz * DEG_TO_RAD, IMU.my, 238 | IMU.mx, IMU.mz, IMU.deltat); 239 | 240 | 241 | 242 | if (!AHRS) 243 | { 244 | IMU.delt_t = millis() - IMU.count; 245 | if (IMU.delt_t > 500) 246 | { 247 | if (SerialDebug) 248 | { 249 | // Print acceleration values in milligs! 250 | Serial.print("X-acceleration: "); Serial.print(1000 * IMU.ax); 251 | Serial.print(" mg "); 252 | Serial.print("Y-acceleration: "); Serial.print(1000 * IMU.ay); 253 | Serial.print(" mg "); 254 | Serial.print("Z-acceleration: "); Serial.print(1000 * IMU.az); 255 | Serial.println(" mg "); 256 | 257 | // Print gyro values in degree/sec 258 | Serial.print("X-gyro rate: "); Serial.print(IMU.gx, 3); 259 | Serial.print(" degrees/sec "); 260 | Serial.print("Y-gyro rate: "); Serial.print(IMU.gy, 3); 261 | Serial.print(" degrees/sec "); 262 | Serial.print("Z-gyro rate: "); Serial.print(IMU.gz, 3); 263 | Serial.println(" degrees/sec"); 264 | 265 | // Print mag values in degree/sec 266 | Serial.print("X-mag field: "); Serial.print(IMU.mx); 267 | Serial.print(" mG "); 268 | Serial.print("Y-mag field: "); Serial.print(IMU.my); 269 | Serial.print(" mG "); 270 | Serial.print("Z-mag field: "); Serial.print(IMU.mz); 271 | Serial.println(" mG"); 272 | 273 | IMU.tempCount = IMU.readTempData(); // Read the adc values 274 | // Temperature in degrees Centigrade 275 | IMU.temperature = ((float) IMU.tempCount) / 333.87 + 21.0; 276 | Serial.print("Temperature is "); Serial.print(IMU.temperature, 1); 277 | Serial.println(" degrees C"); 278 | Serial.println(""); 279 | } 280 | 281 | #ifdef LCD 282 | M5.Lcd.fillScreen(BLACK); 283 | M5.Lcd.setTextColor(GREEN , BLACK); 284 | M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250/AK8963"); 285 | M5.Lcd.setCursor(0, 32); M5.Lcd.print(" x y z "); 286 | 287 | M5.Lcd.setCursor(0, 48); M5.Lcd.print((int)(1000 * IMU.ax)); 288 | M5.Lcd.setCursor(32, 48); M5.Lcd.print((int)(1000 * IMU.ay)); 289 | M5.Lcd.setCursor(64, 48); M5.Lcd.print((int)(1000 * IMU.az)); 290 | M5.Lcd.setCursor(96, 48); M5.Lcd.print("mg"); 291 | 292 | M5.Lcd.setCursor(0, 64); M5.Lcd.print((int)(IMU.gx)); 293 | M5.Lcd.setCursor(32, 64); M5.Lcd.print((int)(IMU.gy)); 294 | M5.Lcd.setCursor(64, 64); M5.Lcd.print((int)(IMU.gz)); 295 | M5.Lcd.setCursor(96, 64); M5.Lcd.print("o/s"); 296 | 297 | M5.Lcd.setCursor(0, 96); M5.Lcd.print((int)(IMU.mx)); 298 | M5.Lcd.setCursor(32, 96); M5.Lcd.print((int)(IMU.my)); 299 | M5.Lcd.setCursor(64, 96); M5.Lcd.print((int)(IMU.mz)); 300 | M5.Lcd.setCursor(96, 96); M5.Lcd.print("mG"); 301 | 302 | M5.Lcd.setCursor(0, 128); M5.Lcd.print("Gyro T "); 303 | M5.Lcd.setCursor(50, 128); M5.Lcd.print(IMU.temperature, 1); 304 | M5.Lcd.print(" C"); 305 | #endif // LCD 306 | 307 | IMU.count = millis(); 308 | // digitalWrite(myLed, !digitalRead(myLed)); // toggle led 309 | } // if (IMU.delt_t > 500) 310 | } // if (!AHRS) 311 | else 312 | { 313 | // Serial print and/or display at 0.5 s rate independent of data rates 314 | IMU.delt_t = millis() - IMU.count; 315 | 316 | // update LCD once per half-second independent of read rate 317 | // if (IMU.delt_t > 500) 318 | if (IMU.delt_t > 100) 319 | { 320 | if (SerialDebug) 321 | { 322 | Serial.print("ax = "); Serial.print((int)1000 * IMU.ax); 323 | Serial.print(" ay = "); Serial.print((int)1000 * IMU.ay); 324 | Serial.print(" az = "); Serial.print((int)1000 * IMU.az); 325 | Serial.println(" mg"); 326 | 327 | Serial.print("gx = "); Serial.print( IMU.gx, 2); 328 | Serial.print(" gy = "); Serial.print( IMU.gy, 2); 329 | Serial.print(" gz = "); Serial.print( IMU.gz, 2); 330 | Serial.println(" deg/s"); 331 | 332 | Serial.print("mx = "); Serial.print( (int)IMU.mx ); 333 | Serial.print(" my = "); Serial.print( (int)IMU.my ); 334 | Serial.print(" mz = "); Serial.print( (int)IMU.mz ); 335 | Serial.println(" mG"); 336 | 337 | Serial.print("q0 = "); Serial.print(*getQ()); 338 | Serial.print(" qx = "); Serial.print(*(getQ() + 1)); 339 | Serial.print(" qy = "); Serial.print(*(getQ() + 2)); 340 | Serial.print(" qz = "); Serial.println(*(getQ() + 3)); 341 | } 342 | 343 | // Define output variables from updated quaternion---these are Tait-Bryan 344 | // angles, commonly used in aircraft orientation. In this coordinate system, 345 | // the positive z-axis is down toward Earth. Yaw is the angle between Sensor 346 | // x-axis and Earth magnetic North (or true North if corrected for local 347 | // declination, looking down on the sensor positive yaw is counterclockwise. 348 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the 349 | // Earth is positive, up toward the sky is negative. Roll is angle between 350 | // sensor y-axis and Earth ground plane, y-axis up is positive roll. These 351 | // arise from the definition of the homogeneous rotation matrix constructed 352 | // from quaternions. Tait-Bryan angles as well as Euler angles are 353 | // non-commutative; that is, the get the correct orientation the rotations 354 | // must be applied in the correct order which for this configuration is yaw, 355 | // pitch, and then roll. 356 | // For more see 357 | // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles 358 | // which has additional links. 359 | IMU.yaw = atan2(2.0f * (*(getQ() + 1) * *(getQ() + 2) + *getQ() * 360 | *(getQ() + 3)), *getQ() * *getQ() + * (getQ() + 1) * *(getQ() + 1) 361 | - * (getQ() + 2) * *(getQ() + 2) - * (getQ() + 3) * *(getQ() + 3)); 362 | IMU.pitch = -asin(2.0f * (*(getQ() + 1) * *(getQ() + 3) - *getQ() * 363 | *(getQ() + 2))); 364 | IMU.roll = atan2(2.0f * (*getQ() * *(getQ() + 1) + * (getQ() + 2) * 365 | *(getQ() + 3)), *getQ() * *getQ() - * (getQ() + 1) * *(getQ() + 1) 366 | - * (getQ() + 2) * *(getQ() + 2) + * (getQ() + 3) * *(getQ() + 3)); 367 | IMU.pitch *= RAD_TO_DEG; 368 | IMU.yaw *= RAD_TO_DEG; 369 | // Declination of SparkFun Electronics (40°05'26.6"N 105°11'05.9"W) is 370 | // 8° 30' E ± 0° 21' (or 8.5°) on 2016-07-19 371 | // - http://www.ngdc.noaa.gov/geomag-web/#declination 372 | IMU.yaw -= 8.5; 373 | IMU.roll *= RAD_TO_DEG; 374 | 375 | if (SerialDebug) 376 | { 377 | Serial.print("Yaw, Pitch, Roll: "); 378 | Serial.print(IMU.yaw, 2); 379 | Serial.print(", "); 380 | Serial.print(IMU.pitch, 2); 381 | Serial.print(", "); 382 | Serial.println(IMU.roll, 2); 383 | 384 | Serial.print("rate = "); 385 | Serial.print((float)IMU.sumCount / IMU.sum, 2); 386 | Serial.println(" Hz"); 387 | Serial.println(""); 388 | } 389 | 390 | #ifdef LCD 391 | // M5.Lcd.fillScreen(BLACK); 392 | M5.Lcd.setTextFont(2); 393 | 394 | M5.Lcd.setCursor(0, 0); M5.Lcd.print(" x(74) y(71) z(1) "); 395 | M5.Lcd.setCursor(0, 24); 396 | M5.Lcd.printf("% 6d % 6d % 6d mg \r\n", (int)(1000 * IMU.ax), (int)(1000 * IMU.ay), (int)(1000 * IMU.az)); 397 | M5.Lcd.setCursor(0, 44); 398 | M5.Lcd.printf("% 6d % 6d % 6d cc \r\n", (int)(cc74), (int)(cc71), (int)(cc1)); 399 | M5.Lcd.setCursor(0, 64); 400 | M5.Lcd.printf("% 6d % 6d % 6d o/s \r\n", (int)(IMU.gx), (int)(IMU.gy), (int)(IMU.gz)); 401 | M5.Lcd.setCursor(0, 84); 402 | M5.Lcd.printf("% 6d % 6d % 6d mG \r\n", (int)(IMU.mx), (int)(IMU.my), (int)(IMU.mz)); 403 | 404 | M5.Lcd.setCursor(0, 100); 405 | M5.Lcd.printf(" yaw: % 5.2f pitch: % 5.2f roll: % 5.2f \r\n", (IMU.yaw), (IMU.pitch), (IMU.roll)); 406 | 407 | // With these settings the filter is updating at a ~145 Hz rate using the 408 | // Madgwick scheme and >200 Hz using the Mahony scheme even though the 409 | // display refreshes at only 2 Hz. The filter update rate is determined 410 | // mostly by the mathematical steps in the respective algorithms, the 411 | // processor speed (8 MHz for the 3.3V Pro Mini), and the magnetometer ODR: 412 | // an ODR of 10 Hz for the magnetometer produce the above rates, maximum 413 | // magnetometer ODR of 100 Hz produces filter update rates of 36 - 145 and 414 | // ~38 Hz for the Madgwick and Mahony schemes, respectively. This is 415 | // presumably because the magnetometer read takes longer than the gyro or 416 | // accelerometer reads. This filter update rate should be fast enough to 417 | // maintain accurate platform orientation for stabilization control of a 418 | // fast-moving robot or quadcopter. Compare to the update rate of 200 Hz 419 | // produced by the on-board Digital Motion Processor of Invensense's MPU6050 420 | // 6 DoF and MPU9150 9DoF sensors. The 3.3 V 8 MHz Pro Mini is doing pretty 421 | // well! 422 | 423 | // M5.Lcd.setCursor(0, 60); 424 | // M5.Lcd.printf("yaw:%6.2f pitch:%6.2f roll:%6.2f ypr \r\n",(IMU.yaw), (IMU.pitch), (IMU.roll)); 425 | M5.Lcd.setCursor(12, 144); 426 | M5.Lcd.print("rt: "); 427 | M5.Lcd.print((float) IMU.sumCount / IMU.sum, 2); 428 | M5.Lcd.print(" Hz"); 429 | #endif // LCD 430 | 431 | IMU.count = millis(); 432 | IMU.sumCount = 0; 433 | IMU.sum = 0; 434 | 435 | #if(processing_out) 436 | 437 | Serial.print(((IMU.yaw))); Serial.print(";"); 438 | Serial.print(((IMU.pitch))); Serial.print(";"); 439 | Serial.print(((IMU.roll))); Serial.print(";"); 440 | Serial.print(26.5); Serial.print(";"); 441 | Serial.print(0.01); Serial.print(";"); 442 | Serial.print(0.02); Serial.println(); 443 | #endif 444 | } // if (IMU.delt_t > 500) 445 | } // if (AHRS) 446 | } 447 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_MIDI_R_test/M5Stack_MIDI_R_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include //Arduino-ESP32 driver 3 | #include 4 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 5 | 6 | void setup() { 7 | M5.begin(); 8 | dacWrite(25, 0); // Speaker OFF 9 | dac_output_disable( DAC_CHANNEL_1 ); 10 | MIDI.begin(); 11 | MIDI.turnThruOff(); 12 | M5.Lcd.setTextSize(3); 13 | M5.Lcd.setCursor(50, 0); 14 | M5.Lcd.print("MIDI Monitor"); 15 | M5.Lcd.setTextSize(2); 16 | M5.Lcd.setCursor(10, 40); 17 | M5.Lcd.print("Control Number:"); 18 | M5.Lcd.setCursor(10, 70); 19 | M5.Lcd.print("Value:"); 20 | M5.Lcd.setCursor(10, 130); 21 | M5.Lcd.print("Note Number:"); 22 | M5.Lcd.setCursor(10, 160); 23 | M5.Lcd.print("Velocity:"); 24 | } 25 | 26 | void loop() { 27 | int data1, data2, locateY1, locateY2; 28 | if (MIDI.read()) 29 | { 30 | if (MIDI.getType() == midi::ControlChange) 31 | { 32 | locateY1 = 40; 33 | locateY2 = 70; 34 | } 35 | else if (MIDI.getType() == midi::NoteOn) 36 | { 37 | locateY1 = 130; 38 | locateY2 = 160; 39 | } 40 | else return; 41 | 42 | data1 = MIDI.getData1(); //CC No取得 43 | data2 = MIDI.getData2(); //CC 値を取得 44 | char disp1[4] = ""; 45 | char disp2[4] = ""; 46 | sprintf(disp1, "%03d", data1); 47 | sprintf(disp2, "%03d", data2); 48 | M5.Lcd.setCursor(200, locateY1); 49 | M5.Lcd.printf(disp1); 50 | M5.Lcd.setCursor(200, locateY2); 51 | M5.Lcd.printf(disp2); 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_MIDI_T_RndSeq/M5Stack_MIDI_T_RndSeq.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 4 | 5 | void setup() { 6 | M5.begin(); 7 | dacWrite(25, 0); // Speaker OFF 8 | MIDI.begin(); 9 | MIDI.turnThruOff(); 10 | M5.Lcd.setTextSize(3); 11 | M5.Lcd.setCursor(50, 0); 12 | M5.Lcd.print("MIDI Rnd Seq."); 13 | M5.Lcd.setTextSize(2); 14 | M5.Lcd.setCursor(10, 130); 15 | M5.Lcd.print("Note Number:"); 16 | } 17 | 18 | void loop() { 19 | int RN; 20 | RN=random(46,120); 21 | 22 | MIDI.sendNoteOn(RN, 127, 1); // Send a Note (pitch 0, velo 127 on channel 1) 23 | M5.Lcd.setCursor(200, 130); 24 | M5.Lcd.print(RN); 25 | delay(100); // Wait for a second 26 | MIDI.sendNoteOff(RN, 0, 1); 27 | M5.Lcd.setCursor(200, 130); 28 | M5.Lcd.printf(" "); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_MIDI_T_test/M5Stack_MIDI_T_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI); 4 | 5 | void setup() { 6 | M5.begin(); 7 | dacWrite(25, 0); // Speaker OFF 8 | MIDI.begin(); 9 | MIDI.turnThruOff(); 10 | M5.Lcd.setTextSize(3); 11 | M5.Lcd.setCursor(50, 0); 12 | M5.Lcd.print("MIDI Tx TEST"); 13 | M5.Lcd.setTextSize(2); 14 | M5.Lcd.setCursor(10, 130); 15 | M5.Lcd.print("Note Number:"); 16 | } 17 | 18 | void loop() { 19 | MIDI.sendNoteOn(0, 127, 1); // Send a Note (pitch 0, velo 127 on channel 1) 20 | M5.Lcd.setCursor(200, 130); 21 | M5.Lcd.printf("0"); 22 | delay(1000); // Wait for a second 23 | MIDI.sendNoteOff(0, 0, 1); 24 | M5.Lcd.setCursor(200, 130); 25 | M5.Lcd.printf(" "); 26 | delay(1000); 27 | MIDI.sendNoteOn(1, 127, 1); // Send a Note (pitch 1, velo 127 on channel 1) 28 | M5.Lcd.setCursor(200, 130); 29 | M5.Lcd.printf("1"); 30 | delay(1000); // Wait for a second 31 | MIDI.sendNoteOff(1, 0, 1); 32 | M5.Lcd.setCursor(200, 130); 33 | M5.Lcd.printf(" "); 34 | delay(1000); 35 | } 36 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/IntervalCheck.h: -------------------------------------------------------------------------------- 1 | // 間隔測定クラス 2 | 3 | #ifndef D_INTERVAL_CHECK_H 4 | #define D_INTERVAL_CHECK_H 5 | 6 | class IntervalCheck 7 | { 8 | private: 9 | unsigned long m_lastTime; 10 | unsigned long m_intervalTime; 11 | bool m_autoReset; 12 | 13 | public: 14 | // コンストラクタ 15 | IntervalCheck( unsigned long interval=1000, bool autoReset=true ){ 16 | m_intervalTime = interval; 17 | m_autoReset = autoReset; 18 | reset(); 19 | } 20 | 21 | // 時間経過判定 22 | bool check(){ 23 | bool result = false; 24 | 25 | if ((millis()-m_lastTime)>m_intervalTime) { 26 | if( m_autoReset == true ){ 27 | // オートリセットが設定されいる場合、ここで自動リセット=リピートする 28 | reset(); 29 | } 30 | result = true; 31 | } 32 | return result; 33 | } 34 | 35 | // 外部からのリセット 36 | void reset(){ 37 | m_lastTime = millis(); 38 | } 39 | }; 40 | 41 | #endif // D_INTERVAL_CHECK_H 42 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/IntervalCheckCounts.h: -------------------------------------------------------------------------------- 1 | // 間隔測定クラス 2 | 3 | #ifndef D_INTERVAL_CHECK_COUNTS_H 4 | #define D_INTERVAL_CHECK_COUNTS_H 5 | 6 | class IntervalCheckCounts 7 | { 8 | private: 9 | volatile unsigned long m_count; 10 | volatile unsigned long m_limitRate; 11 | unsigned long m_lastTime; 12 | unsigned long m_intervalTime; 13 | bool m_autoReset; 14 | 15 | public: 16 | // コンストラクタ 17 | IntervalCheckCounts( unsigned long interval=1000, bool autoReset=true ){ 18 | m_count = 0; 19 | m_limitRate = 10; 20 | m_intervalTime = interval; 21 | m_autoReset = autoReset; 22 | reset(); 23 | } 24 | 25 | // 時間経過判定 26 | bool check(){ 27 | bool result = false; 28 | 29 | if ((m_count-m_lastTime)>m_intervalTime) { 30 | if( m_autoReset == true ){ 31 | // オートリセットが設定されいる場合、ここで自動リセット=リピートする 32 | if ((m_count-m_lastTime)>(m_intervalTime*m_limitRate)) { 33 | reset(); 34 | } 35 | else 36 | { 37 | m_lastTime += m_intervalTime; 38 | } 39 | } 40 | result = true; 41 | } 42 | return result; 43 | } 44 | 45 | // 外部からのリセット 46 | void reset(){ 47 | m_count = 0; 48 | m_lastTime = m_count; 49 | } 50 | 51 | void setLimitRate( unsigned long limitRate ) { 52 | m_limitRate = limitRate; 53 | if(m_limitRate == 0) { 54 | m_limitRate = 1; 55 | } 56 | } 57 | 58 | void updateCount() { 59 | m_count++; 60 | } 61 | }; 62 | 63 | #endif // D_INTERVAL_CHECK_COUNTS_H 64 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/IntervalCheckMicros.h: -------------------------------------------------------------------------------- 1 | // 間隔測定クラス 2 | 3 | #ifndef D_INTERVAL_CHECK_MICROS_H 4 | #define D_INTERVAL_CHECK_MICROS_H 5 | 6 | class IntervalCheckMicros 7 | { 8 | private: 9 | unsigned long m_lastTime; 10 | unsigned long m_intervalTime; 11 | bool m_autoReset; 12 | 13 | public: 14 | // コンストラクタ 15 | IntervalCheckMicros( unsigned long interval=1000, bool autoReset=true ){ 16 | m_intervalTime = interval; 17 | m_autoReset = autoReset; 18 | reset(); 19 | } 20 | 21 | // 時間経過判定 22 | bool check(){ 23 | bool result = false; 24 | 25 | if ((micros()-m_lastTime)>m_intervalTime) { 26 | if( m_autoReset == true ){ 27 | // オートリセットが設定されいる場合、ここで自動リセット=リピートする 28 | if ((micros()-m_lastTime)>(m_intervalTime*10)) { 29 | reset(); 30 | } 31 | else 32 | { 33 | m_lastTime += m_intervalTime; 34 | } 35 | } 36 | result = true; 37 | } 38 | return result; 39 | } 40 | 41 | // 外部からのリセット 42 | void reset(){ 43 | m_lastTime = micros(); 44 | } 45 | }; 46 | 47 | #endif // D_INTERVAL_CHECK_MICROS_H 48 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/M5Stack_SMF_Player.ino: -------------------------------------------------------------------------------- 1 | // Mini SMF(Standard Midi File) Sequencer Sample Program 2 | // 3 | // SDカード内に格納したSMFファイル"playdat0.mid"~"playdat9.mid"を自動で演奏します。 4 | // 5 | // 以下のI/F/ライブラリを使用します 6 | // M5Stack用MIDIモジュール2 https://necobit.com/denshi/m5-midi-module2/ 7 | // MSTimer2 8 | // LovyanGFX 9 | // 10 | // オリジナルは @catsin さんの https://bitbucket.org/kyoto-densouan/smfseq/src/m5stack/ 11 | // necobitでは画面描画部分と、起動時自動スタートの処理への変更をしています。 12 | // 13 | // コメントやコメントアウトなど、取っ散らかっているところがありますがご了承ください。 14 | 15 | #include "common.h" 16 | #include "SmfSeq.h" 17 | #include "IntervalCheck.h" 18 | #include "IntervalCheckMicros.h" 19 | 20 | SMF_SEQ_TABLE *pseqTbl; //SMFシーケンサハンドル 21 | 22 | LGFX lcd; 23 | 24 | //---------------------------------------------------------------------- 25 | //MIDIポートアクセス関数定義 26 | // MidiFunc.c/hから呼び出されるMIDI I/Fアクセス関数の実体を記述する。 27 | // 以下ではハードウェアシリアルを使用した場合の例を記述。 28 | #include "MidiPort.h" 29 | int MidiPort_open() 30 | { 31 | Serial2.begin(D_MIDI_PORT_BPS); 32 | return (0); 33 | } 34 | void MidiPort_close() 35 | { 36 | Serial2.end(); 37 | } 38 | int MidiPort_write(UCHAR data) 39 | { 40 | #ifdef DUMPMIDI 41 | DPRINT("1:"); 42 | int n = (int)data; 43 | DPRINTLN(n, HEX); 44 | #else 45 | Serial2.write(data); 46 | #endif 47 | // Serial.flush(); 48 | return (1); 49 | } 50 | int MidiPort_writeBuffer(UCHAR *pData, ULONG Len) 51 | { 52 | #ifdef DUMPMIDI 53 | int n; 54 | int i; 55 | DPRINT.print(Len); 56 | DPRINT.print(":"); 57 | for (i = 0; i < Len; i++) 58 | { 59 | n = (int)pData[i]; 60 | DPRINT.print(n, HEX); 61 | } 62 | DPRINTLN.println(""); 63 | #else 64 | Serial2.write(pData, Len); 65 | #endif 66 | // Serial.flush(); 67 | return (Len); 68 | } 69 | //---------------------------------------------------------------------- 70 | //SMFファイルアクセス関数定義 71 | // SmfSeq.c/hから呼び出されるSMFファイルへのアクセス関数の実体を記述する。 72 | // 以下ではSDカードシールドライブラリに対し、ファイルポインタで直接読み出し位置を指定する方法での例を記述。 73 | // ライブラリ自体の初期化はsetup関数に記述している。 74 | //#define D_SD_CHIP_SELECT_PIN 4 75 | //#include 76 | //#include 77 | #include "SmfFileAccess.h" 78 | 79 | File s_FileHd; 80 | bool SmfFileAccessOpen(UCHAR *Filename) 81 | { 82 | bool result = false; 83 | 84 | if (Filename != NULL) 85 | { 86 | // lcd.print(F("filename:")); 87 | lcd.setFont(&fonts::Font4); 88 | lcd.setTextSize(1); 89 | lcd.setCursor(5, 0); 90 | lcd.println((const char *)Filename); 91 | s_FileHd = SD.open((const char *)Filename); 92 | 93 | result = s_FileHd.available(); 94 | } 95 | return (result); 96 | } 97 | void SmfFileAccessClose() 98 | { 99 | s_FileHd.close(); 100 | } 101 | bool SmfFileAccessRead(UCHAR *Buf, unsigned long Ptr) 102 | { 103 | bool result = true; 104 | if (Buf != NULL) 105 | { 106 | if (s_FileHd.position() != Ptr) 107 | { 108 | s_FileHd.seek(Ptr); 109 | } 110 | int data = s_FileHd.read(); 111 | if (data >= 0) 112 | { 113 | *Buf = (UCHAR)data; 114 | } 115 | else 116 | { 117 | result = false; 118 | } 119 | } 120 | return (result); 121 | } 122 | bool SmfFileAccessReadNext(UCHAR *Buf) 123 | { 124 | bool result = true; 125 | if (Buf != NULL) 126 | { 127 | int data = s_FileHd.read(); 128 | if (data >= 0) 129 | { 130 | *Buf = (UCHAR)data; 131 | } 132 | else 133 | { 134 | result = false; 135 | } 136 | } 137 | return (result); 138 | } 139 | int SmfFileAccessReadBuf(UCHAR *Buf, unsigned long Ptr, int Lng) 140 | { 141 | int result = 0; 142 | if (Buf != NULL) 143 | { 144 | if (s_FileHd.position() != Ptr) 145 | { 146 | s_FileHd.seek(Ptr); 147 | } 148 | 149 | int i; 150 | int data; 151 | for (i = 0; i < Lng; i++) 152 | { 153 | data = s_FileHd.read(); 154 | if (data >= 0) 155 | { 156 | Buf[i] = (UCHAR)data; 157 | result++; 158 | } 159 | else 160 | { 161 | break; 162 | } 163 | } 164 | } 165 | return (result); 166 | } 167 | unsigned int SmfFileAccessSize() 168 | { 169 | unsigned int result = 0; 170 | result = s_FileHd.size(); 171 | 172 | return (result); 173 | } 174 | 175 | //---------------------------------------------------------------------- 176 | //#define D_PLAY_BUTTON_PIN 2 //開始/停止ボタン 177 | #define D_CH_OFFSET_PIN 3 //チャンネル番号オフセット(eVY1のGM音源としての演奏) 178 | //#define D_FF_BUTTON_PIN 5 //送りボタン 179 | #define D_STATUS_LED 10 //状態表示LED 180 | 181 | int playdataCnt = 0; //選曲番号 182 | #define D_PLAY_DATA_NUM 10 //SDカード格納SMFファイル数 183 | 184 | IntervalCheck sButtonCheckInterval(100, true); 185 | //IntervalCheck sTickProcInterval( ZTICK, true ); 186 | IntervalCheckMicros sTickProcInterval(ZTICK * 1000, true); 187 | IntervalCheck sStatusLedCheckInterval(100, true); 188 | unsigned int sLedPattern = 0x0f0f; 189 | IntervalCheck sUpdateScreenInterval(500, true); 190 | 191 | char filename[14] = {'/', 'p', 'l', 'a', 'y', 'd', 'a', 't', '0', '.', 'm', 'i', 'd', 0x00}; 192 | 193 | //SMFファイル名生成 194 | // playdat0.mid~playdat9.midの文字列を順次返す。 195 | char *makeFilename() 196 | { 197 | int cnt = 0; 198 | // char * filename = (char*)"playdat0.mid"; 199 | for (cnt = 0; cnt < D_PLAY_DATA_NUM; cnt++) 200 | { 201 | filename[8] = 0x30 + playdataCnt; 202 | playdataCnt++; 203 | if (playdataCnt >= D_PLAY_DATA_NUM) 204 | { 205 | playdataCnt = 0; 206 | } 207 | if (SD.exists(filename) == true) 208 | { 209 | break; 210 | } 211 | } 212 | return (filename); 213 | } 214 | 215 | void updateScreen() 216 | { 217 | static String last_filename = ""; 218 | static int last_status = -1; 219 | static int last_chNoOffset = -1; 220 | 221 | int status = SmfSeqGetStatus(pseqTbl); 222 | 223 | int chNoOffset = 0; 224 | //とりあえずバイパスしておく 225 | //if( digitalRead( D_CH_OFFSET_PIN )==LOW ){ 226 | //CH番号を1ずらす(eVY1のCH0が音声合成専用のため、GM音源として使用する場合CH0を使用しない) 227 | // chNoOffset = 1; 228 | //} 229 | 230 | if ((last_filename != filename) || (last_status != status) || (last_chNoOffset != chNoOffset)) 231 | { 232 | lcd.fillScreen(TFT_BLACK); 233 | backscreen(); 234 | lcd.setFont(&fonts::Font4); 235 | lcd.setTextSize(1); 236 | 237 | lcd.setCursor(5, 0); 238 | // lcd.print(F("Filename: ")); 239 | lcd.println(filename); 240 | lcd.setCursor(5, 27); 241 | lcd.print(F("Status:")); 242 | switch (status) 243 | { 244 | case SMF_STAT_FILENOTREAD: //SMFファイル未読み込み(演奏不能) 245 | lcd.println(F("File load failed.")); 246 | break; 247 | case SMF_STAT_STOP: //演奏停止 248 | lcd.println(F("stop.")); 249 | lcd.fillRect(260, 5, 40, 40, TFT_WHITE); 250 | break; 251 | case SMF_STAT_PLAY: //演奏中 252 | lcd.println(F("playing.")); 253 | lcd.fillRect(280, 5, 310, 45, TFT_BLACK); 254 | lcd.fillTriangle(280, 5, 280, 45, 310, 25, TFT_YELLOW); 255 | backscreen(); 256 | break; 257 | case SMF_STAT_PAUSE: //演奏一時停止中 258 | lcd.println(F("pause.")); 259 | break; 260 | case SMF_STAT_STOPWAIT: //演奏停止待ち(演奏中) 261 | lcd.println(F("wait.")); 262 | break; 263 | default: 264 | break; 265 | } 266 | 267 | // lcd.print(F("ch shift: ")); 268 | // lcd.println(chNoOffset); 269 | 270 | last_filename = filename; 271 | last_status = status; 272 | last_chNoOffset = chNoOffset; 273 | } 274 | } 275 | 276 | void backscreen() 277 | { 278 | lcd.setFont(&fonts::Font0); 279 | lcd.setTextSize(1); 280 | for (int chd = 1; chd <= 16; chd++) 281 | { 282 | int y = 49 + chd * 10; 283 | lcd.drawNumber(chd, 4, y + 1); 284 | lcd.drawFastHLine(2, y-1, 316, 0xF660); 285 | lcd.fillRect(18, y, 300, 9, TFT_DARKGREY); 286 | lcd.setColor(TFT_BLACK); 287 | for (int oct = 0; oct < 11; ++oct) { 288 | int x = 18 + oct * 28; 289 | for (int n = 0; n < 7; ++n) { 290 | lcd.drawFastVLine(x + n * 4 + 3, y, 9); 291 | } 292 | lcd.fillRect(x + 2, y, 3, 5); 293 | lcd.fillRect(x + 6, y, 3, 5); 294 | lcd.fillRect(x + 14, y, 3, 5); 295 | lcd.fillRect(x + 18, y, 3, 5); 296 | lcd.fillRect(x + 22, y, 3, 5); 297 | } 298 | } 299 | lcd.drawFastVLine( 16, 58, 161, 0xF660); 300 | lcd.drawRect( 1, 58, 318, 161, 0xF660); 301 | } 302 | 303 | void setup() 304 | { 305 | // delay(2000); 306 | 307 | M5.begin(); 308 | M5.Power.begin(); 309 | 310 | // 最初に初期化関数を呼び出します。 311 | lcd.init(); 312 | 313 | lcd.setRotation(1); 314 | 315 | // バックライトの輝度を 0~255 の範囲で設定します。 316 | lcd.setBrightness(255); 317 | 318 | lcd.clear(TFT_BLACK); 319 | 320 | Serial.begin(115200); 321 | 322 | // pinMode( D_PLAY_BUTTON_PIN, INPUT_PULLUP ); 323 | // pinMode( D_FF_BUTTON_PIN, INPUT_PULLUP ); 324 | pinMode(D_CH_OFFSET_PIN, INPUT_PULLUP); 325 | pinMode(D_STATUS_LED, OUTPUT); 326 | 327 | lcd.println(F("Initializing SD card...")); 328 | 329 | // if (!SD.begin(D_SD_CHIP_SELECT_PIN)) { 330 | // DPRINTLN(F("Card failed, or not present")); 331 | // // don't do anything more: 332 | // delay(2000); 333 | // return; 334 | // } 335 | // DPRINTLN(F("card initialized.")); 336 | // すぐにファイルアクセスするとフォーマット破壊することがあったため待ち 337 | delay(2000); 338 | 339 | // digitalWrite( D_STATUS_LED, HIGH ); 340 | int Ret; 341 | pseqTbl = SmfSeqInit(ZTICK); 342 | if (pseqTbl == NULL) 343 | { 344 | lcd.println(F("SmfSeqInit failed.")); 345 | delay(2000); 346 | return; 347 | } 348 | 349 | int chNoOffset = 0; 350 | // シフトは不要なのでとりあえずバイパスしておく 351 | // if( digitalRead( D_CH_OFFSET_PIN )==LOW ){ 352 | //CH番号を1ずらす(eVY1のCH0が音声合成専用のため、GM音源として使用する場合CH0を使用しない) 353 | // chNoOffset = 1; 354 | // } 355 | 356 | //SMFファイル読込 357 | SmfSeqFileLoadWithChNoOffset(pseqTbl, (char *)makeFilename(), chNoOffset); 358 | //GMリセット送信 359 | //Ret = SmfSeqGMReset(); 360 | //発音中全キーノートオフ 361 | Ret = SmfSeqAllNoteOff(pseqTbl); 362 | //トラックテーブルリセット 363 | SmfSeqPlayResetTrkTbl(pseqTbl); 364 | //演奏開始 365 | //SmfSeqStart(pseqTbl); 366 | 367 | // digitalWrite( D_STATUS_LED, LOW ); 368 | 369 | // sButtonCheckInterval.reset(); 370 | // sTickProcInterval.reset(); 371 | } 372 | 373 | int prePlayButtonStatus = HIGH; 374 | int preFfButtonStatus = HIGH; 375 | void loop() 376 | { 377 | int Ret; 378 | 379 | // 定期起動処理 380 | if (sTickProcInterval.check() == true) 381 | { 382 | if (SmfSeqGetStatus(pseqTbl) != SMF_STAT_STOP) 383 | { 384 | //状態が演奏停止中以外の場合 385 | //定期処理を実行 386 | Ret = SmfSeqTickProc(pseqTbl); 387 | //処理が間に合わない場合のリカバリ 388 | while (sTickProcInterval.check() == true) 389 | { 390 | //定期処理を実行 391 | Ret = SmfSeqTickProc(pseqTbl); 392 | } 393 | if (SmfSeqGetStatus(pseqTbl) == SMF_STAT_STOP) 394 | { 395 | //状態が演奏停止中になった場合 396 | //発音中全キーノートオフ 397 | Ret = SmfSeqAllNoteOff(pseqTbl); 398 | //トラックテーブルリセット 399 | SmfSeqPlayResetTrkTbl(pseqTbl); 400 | // ファイルクローズ 401 | SmfSeqEnd(pseqTbl); 402 | lcd.fillRect(280, 5, 310, 45, TFT_BLACK); 403 | lcd.setFont(&fonts::Font4); 404 | lcd.setCursor(5, 27); 405 | lcd.setTextSize(1); 406 | lcd.print(F("Status:")); 407 | lcd.println(F("SEQ end. ")); 408 | 409 | // digitalWrite( D_STATUS_LED, HIGH ); 410 | int chNoOffset = 0; 411 | //とりあえずバイパスしておく 412 | //if( digitalRead( D_CH_OFFSET_PIN )==LOW ){ 413 | //CH番号を1ずらす(eVY1のCH0が音声合成専用のため、GM音源として使用する場合CH0を使用しない) 414 | // chNoOffset = 1; 415 | //} 416 | 417 | pseqTbl = SmfSeqInit(ZTICK); 418 | // //SMFファイル読込n 419 | SmfSeqFileLoadWithChNoOffset(pseqTbl, (char *)makeFilename(), chNoOffset); 420 | // //GMリセット送信 421 | // //Ret = SmfSeqGMReset(); 422 | // //発音中全キーノートオフ 423 | //Ret = SmfSeqAllNoteOff(pseqTbl); 424 | // //トラックテーブルリセット 425 | SmfSeqPlayResetTrkTbl(pseqTbl); 426 | // //演奏開始 427 | SmfSeqStart(pseqTbl); 428 | // digitalWrite( D_STATUS_LED, LOW ); 429 | } 430 | } 431 | } 432 | 433 | // ボタン操作処理 434 | if (sButtonCheckInterval.check() == true) 435 | { 436 | M5.update(); 437 | //スイッチ状態取得 438 | // int buttonPlayStatus = digitalRead( D_PLAY_BUTTON_PIN ); 439 | int buttonPlayStatus = M5.BtnB.wasPressed(); 440 | if (prePlayButtonStatus != buttonPlayStatus) 441 | { 442 | //スイッチ状態が変化していた場合 443 | if (buttonPlayStatus == LOW) 444 | { 445 | //スイッチ状態がONの場合 446 | if (SmfSeqGetStatus(pseqTbl) == SMF_STAT_STOP) 447 | { 448 | //演奏開始 449 | SmfSeqStart(pseqTbl); 450 | } 451 | else 452 | { 453 | //演奏中なら演奏停止 454 | SmfSeqStop(pseqTbl); 455 | //発音中全キーノートオフ 456 | Ret = SmfSeqAllNoteOff(pseqTbl); 457 | } 458 | } 459 | } 460 | //スイッチ状態保持 461 | prePlayButtonStatus = buttonPlayStatus; 462 | 463 | // int buttonFfStatus = digitalRead( D_FF_BUTTON_PIN ); 464 | int buttonFfStatus = M5.BtnC.wasPressed(); 465 | if (preFfButtonStatus != buttonFfStatus) 466 | { 467 | //スイッチ状態が変化していた場合 468 | if (preFfButtonStatus == LOW) 469 | { 470 | //スイッチ状態がONの場合 471 | bool playing = false; 472 | if (SmfSeqGetStatus(pseqTbl) != SMF_STAT_STOP) 473 | { 474 | //演奏中なら演奏停止 475 | SmfSeqStop(pseqTbl); 476 | //発音中全キーノートオフ 477 | Ret = SmfSeqAllNoteOff(pseqTbl); 478 | //トラックテーブルリセット 479 | SmfSeqPlayResetTrkTbl(pseqTbl); 480 | // ファイルクローズ 481 | SmfSeqEnd(pseqTbl); 482 | playing = true; 483 | } 484 | else 485 | { 486 | playing = false; 487 | } 488 | 489 | // digitalWrite( D_STATUS_LED, HIGH ); 490 | int chNoOffset = 0; 491 | //とりあえずバイパスしておく 492 | //if( digitalRead( D_CH_OFFSET_PIN )==LOW ){ 493 | //CH番号を1ずらす(eVY1のCH0が音声合成専用のため、GM音源として使用する場合CH0を使用しない) 494 | // chNoOffset = 1; 495 | //} 496 | 497 | pseqTbl = SmfSeqInit(ZTICK); 498 | //SMFファイル読込 499 | SmfSeqFileLoadWithChNoOffset(pseqTbl, (char *)makeFilename(), chNoOffset); 500 | //GMリセット送信 501 | //Ret = SmfSeqGMReset(); 502 | //発音中全キーノートオフ 503 | Ret = SmfSeqAllNoteOff(pseqTbl); 504 | //トラックテーブルリセット 505 | SmfSeqPlayResetTrkTbl(pseqTbl); 506 | if (playing == true) 507 | { 508 | //演奏開始 509 | SmfSeqStart(pseqTbl); 510 | } 511 | // digitalWrite( D_STATUS_LED, LOW ); 512 | } 513 | } 514 | //スイッチ状態保持 515 | preFfButtonStatus = buttonFfStatus; 516 | } 517 | 518 | // 状態表示更新 519 | if (SmfSeqGetStatus(pseqTbl) != SMF_STAT_STOP) 520 | { 521 | if (sStatusLedCheckInterval.check() == true) 522 | { 523 | unsigned int led = sLedPattern & 0x0001; 524 | if (led > 0) 525 | { 526 | // digitalWrite( D_STATUS_LED, HIGH ); 527 | } 528 | else 529 | { 530 | // digitalWrite( D_STATUS_LED, LOW ); 531 | } 532 | sLedPattern = (sLedPattern >> 1) | (led << 15); 533 | } 534 | } 535 | else 536 | { 537 | // digitalWrite( D_STATUS_LED, LOW ); 538 | } 539 | 540 | if (sUpdateScreenInterval.check() == true) 541 | { 542 | updateScreen(); 543 | } 544 | } 545 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/MidiFunc.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | <ユーザ設定関数> 3 | 4 | 5 | 6 | catsin 7 | MIDIシーケンサユーザ設定関数 8 | ***********************************************************************/ 9 | #include "MidiFunc.h" 10 | #include "MidiPort.h" //MIDIポートアクセス関数定義 11 | 12 | int midiOutOpen(){ 13 | int Status; 14 | 15 | /* オプションポートをオープン */ 16 | /* オプションの値(下記コメントのかっこ内はその設定値)は適宜変更してください */ 17 | /* /B 転送速度 (31250bps) */ 18 | /* /P パリティ (なし) */ 19 | /* /L データ長 (8ビット) */ 20 | /* /S ストップビット (1ビット) */ 21 | /* /C ハードフロー (行わない) */ 22 | /* /X XON/XOFFフロー (行わない) */ 23 | /* 他のオプションはデフォルト値を使用 */ 24 | MidiPort_open(); 25 | 26 | /* エラー処理(アラートボックスの表示)*/ 27 | Status = MIDI_OK; 28 | 29 | return( Status ); 30 | } 31 | 32 | int midiOutClose(){ 33 | int Status; 34 | 35 | Status = MIDI_OK; 36 | 37 | /* オプションポートのクローズ */ 38 | MidiPort_close(); 39 | 40 | return( Status ); 41 | } 42 | 43 | int midiOutShortMsg( UCHAR status, UCHAR data1, UCHAR data2 ){ 44 | int Status; 45 | UCHAR Buf[3]; 46 | ULONG Len; 47 | 48 | Status = MIDI_OK; 49 | 50 | Buf[0] = status; 51 | Buf[1] = data1; 52 | Buf[2] = data2; 53 | 54 | Len = 3; 55 | MidiPort_writeBuffer( Buf, Len ); 56 | 57 | return( Status ); 58 | } 59 | 60 | int midiOutLongMsg( UCHAR *Buf, ULONG Len ){ 61 | int Status; 62 | 63 | Status = MIDI_OK; 64 | 65 | MidiPort_writeBuffer( Buf, Len ); 66 | 67 | return( Status ); 68 | } 69 | 70 | int midiOutGMReset(){ 71 | int Status; 72 | UCHAR GMResetData[] = {0xf0,0x7e,0x7f,0x09,0x01,0xf7}; 73 | 74 | //GMSystemON送信 75 | Status = midiOutLongMsg( (UCHAR *)GMResetData, (ULONG)sizeof(GMResetData) ); 76 | 77 | return( Status ); 78 | } 79 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/MidiFunc.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | <ユーザ設定用ヘッダファイル> 3 | 4 | 5 | 6 | catsin 7 | MIDIシーケンサユーザ設定用ヘッダファイル 8 | ***********************************************************************/ 9 | 10 | #ifndef DEF_MIDI_FUNC_H 11 | #define DEF_MIDI_FUNC_H 12 | 13 | #include "common.h" 14 | 15 | // 定数定義 16 | #define MIDI_OK 0 17 | #define MIDI_NG -1 18 | #define MIDI_ON 1 19 | #define MIDI_OFF 0 20 | 21 | #define MIDI_STAT_METAEVENT 0xff 22 | #define MIDI_META_SEQNO 0x00 23 | #define MIDI_META_TEXT 0x01 24 | #define MIDI_META_COPYRIGHT 0x02 25 | #define MIDI_META_SEQTRKNAME 0x03 26 | #define MIDI_META_INSTNAME 0x04 27 | #define MIDI_META_LYLIC 0x05 28 | #define MIDI_META_CHPREFIX 0x20 29 | #define MIDI_META_ENDOFTRACK 0x2f 30 | #define MIDI_META_SETTEMPO 0x51 31 | #define MIDI_META_SMPTEOFFSET 0x54 32 | #define MIDI_META_TEMPOSIG 0x58 33 | #define MIDI_META_KEYSIG 0x59 34 | #define MIDI_META_SEQSPECIFIC 0x7f 35 | 36 | 37 | #define MIDI_STATBIT_MASK 0x80 38 | #define MIDI_STAT_MASK 0xf0 39 | #define MIDI_CHANNEL_MASK 0x0f 40 | 41 | #define MIDI_STATCH_NOTEOFF 0x80 42 | #define MIDI_STATCH_NOTEON 0x90 43 | #define MIDI_STATCH_PKEYPRES 0xa0 44 | #define MIDI_STATCH_CTRLCHG 0xb0 45 | #define MIDI_STATCH_PROGCHG 0xc0 46 | #define MIDI_STATCH_CNPRES 0xd0 47 | #define MIDI_STATCH_PBNDCHG 0xe0 48 | 49 | #define MIDI_STATEX_SYSEXCL 0xf0 50 | #define MIDI_STATEX_MIDITCQF 0xf1 51 | #define MIDI_STATEX_SONGPOGP 0xf2 52 | #define MIDI_STATEX_SONGSEL 0xf3 53 | #define MIDI_STATEX_CHUNEREQ 0xf6 54 | #define MIDI_STATEX_EOEXCL 0xf7 55 | 56 | #define MIDI_STATRT_TMGCLOCK 0xf8 57 | #define MIDI_STATRT_START 0xfa 58 | #define MIDI_STATRT_CONTINUE 0xfb 59 | #define MIDI_STATRT_STOP 0xfc 60 | #define MIDI_STATRT_ACTSENC 0xfe 61 | #define MIDI_STATRT_SYSRESET 0xff 62 | 63 | #define MIDI_CNMD_ALLSNDOFF 0x78 64 | #define MIDI_CNMD_RSTALLCTL 0x79 65 | #define MIDI_CNMD_LOCALCTL 0x7a 66 | #define MIDI_CNMD_ALLNOTOFF 0x7b 67 | #define MIDI_CNMD_OMNIOFF 0x7c 68 | #define MIDI_CNMD_OMNION 0x7d 69 | #define MIDI_CNMD_MONOMODON 0x7e 70 | #define MIDI_CNMD_POLIMODON 0x7f 71 | 72 | #define MIDI_CH_EVF1_VOCAL 0 73 | #define MIDI_CH_GM_DRUM 9 74 | 75 | // 関数定義 76 | int midiOutOpen(); 77 | int midiOutClose(); 78 | int midiOutShortMsg( UCHAR status, UCHAR data1, UCHAR data2 ); 79 | int midiOutLongMsg( UCHAR *Buf, ULONG Len ); 80 | int midiOutGMReset(); 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/MidiPort.h: -------------------------------------------------------------------------------- 1 | #ifndef DEF_MIDIPORT_H 2 | #define DEF_MIDIPORT_H 3 | 4 | #include "common.h" 5 | 6 | //MIDIポートアクセス関数定義 7 | // MidiFunc内で使用するインタフェースを実装・リンクするための定義 8 | 9 | #define D_MIDI_PORT_BPS (31250) //標準MIDI 10 | //#define D_MIDI_PORT_BPS (38400) //PCシリアル 11 | //#define DUMPMIDI //MIDI送信データキャラクタダンプ 12 | 13 | //MIDIインタフェースオープン 14 | int MidiPort_open(); 15 | //MIDIインタフェースクローズ 16 | void MidiPort_close(); 17 | //MIDIインタフェース1バイト送信 18 | int MidiPort_write( UCHAR data ); 19 | //MIDIインタフェース複数バイト送信 20 | int MidiPort_writeBuffer( UCHAR * pData, ULONG Len ); 21 | 22 | #endif //DEF_MIDIPORT_H 23 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/SMF_for_test/playdat0.mid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/necobit/M5Stack-MIDI-Module/3e8d713115ec434e45967656a841eba9f527afba/Arduino-IDE-Sketch/M5Stack_SMF_Player/SMF_for_test/playdat0.mid -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/SMF_for_test/playdat1.mid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/necobit/M5Stack-MIDI-Module/3e8d713115ec434e45967656a841eba9f527afba/Arduino-IDE-Sketch/M5Stack_SMF_Player/SMF_for_test/playdat1.mid -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/SmfFileAccess.h: -------------------------------------------------------------------------------- 1 | #ifndef DEF_SMFFILEACCESS_H 2 | #define DEF_SMFFILEACCESS_H 3 | 4 | #include "common.h" 5 | 6 | //SMFファイルアクセス関数定義 7 | // SmfSeq内で使用するインタフェースを実装・リンクするための定義 8 | 9 | //SMFファイルオープン 10 | bool SmfFileAccessOpen( UCHAR * Filename ); 11 | //SMFファイルクローズ 12 | void SmfFileAccessClose(); 13 | //SMFファイル指定位置の1バイト取得 14 | bool SmfFileAccessRead( UCHAR * Buf, unsigned long Ptr ); 15 | //SMFファイル継続位置の1バイト取得 16 | bool SmfFileAccessReadNext( UCHAR * Buf ); 17 | //SMFファイル指定位置の複数バイト取得 18 | int SmfFileAccessReadBuf( UCHAR * Buf, unsigned long Ptr, int Lng ); 19 | //SMFファイルサイズ取得 20 | unsigned int SmfFileAccessSize(); 21 | 22 | #endif //DEF_SMFFILEACCESS_H 23 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/SmfSeq.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | <ユーザ設定関数> 3 | 4 | 5 | 6 | catsin 7 | MIDIシーケンサユーザ設定関数 8 | ***********************************************************************/ 9 | #include "SmfFileAccess.h" //SMFファイルアクセス関数定義 10 | #include "MidiFunc.h" //MIDIインタフェース 11 | #include "SmfSeq.h" 12 | 13 | #pragma GCC optimize ("O3") 14 | 15 | //----------------------------------------------------------------------------- 16 | //SMFシーケンス用テーブル 17 | //----------------------------------------------------------------------------- 18 | SMF_SEQ_TABLE seqTbl; //SMFシーケンステーブル 19 | SMF_TRACK_TABLE trkTbl[SMF_TRACKNUM]; //SMFトラックテーブル 20 | 21 | //----------------------------------------------------------------------------- 22 | // 初期化 全テーブル初期化、状態をファイル未読み込みに移行 23 | // 割り込み情報 24 | //----------------------------------------------------------------------------- 25 | SMF_SEQ_TABLE *SmfSeqInit(int Tick) 26 | { 27 | SMF_SEQ_TABLE *pseqTbl; 28 | int Ret; 29 | 30 | //ファイルに関係なくMIDIオープン 31 | Ret = midiOutOpen(); 32 | 33 | if (Ret != MIDI_NG) 34 | { 35 | pseqTbl = &seqTbl; 36 | //リセット 37 | //とりあえずバイパスしておく 38 | // Ret = SmfSeqGMReset(); 39 | if (Ret == SMF_OK) 40 | { 41 | SmfSeqClrarTbl(); 42 | SmfSeqInitSeqTbl(pseqTbl); 43 | //テンポ情報強制初期化 44 | Ret = SmfSeqSetTempo(pseqTbl, Tick, 480, 500000, 120); 45 | 46 | SmfSeqNoteClear(pseqTbl); 47 | } 48 | else 49 | { 50 | pseqTbl = (SMF_SEQ_TABLE *)NULL; 51 | } 52 | } 53 | else 54 | { 55 | pseqTbl = (SMF_SEQ_TABLE *)NULL; 56 | } 57 | 58 | return (pseqTbl); 59 | } 60 | 61 | void SmfSeqClrarTbl() 62 | { 63 | memset(&seqTbl, 0, sizeof(SMF_SEQ_TABLE)); 64 | memset(&trkTbl, 0, sizeof(SMF_TRACK_TABLE) * SMF_TRACKNUM); 65 | } 66 | 67 | void SmfSeqInitSeqTbl(SMF_SEQ_TABLE *pseqTbl) 68 | { 69 | int Cnt; 70 | 71 | if (pseqTbl == NULL) 72 | { 73 | return; 74 | } 75 | 76 | pseqTbl->PlayStatus = SMF_STAT_FILENOTREAD; //シーケンサ状態 77 | pseqTbl->DispRenewFlag = SMF_OFF; //表示情報更新フラグ 78 | 79 | //読込みMIDIファイル名 80 | //#LW# memset( pseqTbl->FileName, 0, sizeof( pseqTbl->FileName ) ); 81 | pseqTbl->FileSize = 0; //読込みMIDIファイルサイズ 82 | pseqTbl->chNoOffset = 0; 83 | pseqTbl->TrackNum = 0; //格納トラック数 84 | 85 | for (Cnt = 0; Cnt < SMF_TRACKNUM; Cnt++) 86 | { 87 | pseqTbl->ptrkTbl[Cnt] = &trkTbl[Cnt]; 88 | } 89 | } 90 | 91 | // テンポ設定 92 | int SmfSeqSetTempo(SMF_SEQ_TABLE *pseqTbl, int Tick, int TPQN, float TempoVal, int Tempo) 93 | { 94 | int Ret; 95 | 96 | if (pseqTbl == NULL) 97 | { 98 | return (SMF_NG); 99 | } 100 | 101 | pseqTbl->Tick = Tick; //仮想割り込み間隔(10ms):10ms 102 | pseqTbl->TPQN = TPQN; //4分音符の分解能 103 | pseqTbl->TempoVal = TempoVal; //4分音符の長さ(μs) 104 | pseqTbl->Tempo = Tempo; //1分に入る4分音符数:60000ms/500ms=120 105 | pseqTbl->SeqTickUnit = (int)((float)(((float)Tick * 1000) / ((float)TempoVal / (float)TPQN)) * 1000); 106 | 107 | if (pseqTbl->SeqTickUnit == 0) 108 | { //SeqTickUnitが0なのは致命的にエラーです 109 | Ret = SMF_NG; 110 | } 111 | else 112 | { 113 | Ret = SMF_OK; 114 | } 115 | return (Ret); 116 | } 117 | 118 | //----------------------------------------------------------------------------- 119 | // 一部初期化 トラックテーブル初期化、状態を停止に移行(現状態維持) 120 | //----------------------------------------------------------------------------- 121 | int SmfSeqInitTrkTbl(SMF_SEQ_TABLE *pseqTbl) 122 | { 123 | int Ret; 124 | int Cnt; 125 | UCHAR *pSmfData; 126 | SMF_HEADER *pSmfHeader; 127 | TRACK_HEADER *pTrkHeader; 128 | SMF_TRACK_TABLE *ptrkTbl; 129 | int TPQN; 130 | 131 | unsigned long fileAccessPtr = 0; 132 | int fileAccessReadSize = 0; 133 | 134 | if (pseqTbl == NULL) 135 | { 136 | return (SMF_NG); 137 | } 138 | 139 | Ret = SMF_OK; 140 | 141 | for (Cnt = 0; Cnt < SMF_TRACKNUM; Cnt++) 142 | { 143 | ptrkTbl = pseqTbl->ptrkTbl[Cnt]; 144 | 145 | //#LW# SmfSeqStrcpy( ptrkTbl->SeqTrkName, "", SMF_DISPSEQTRK_STRLEN ); 146 | pseqTbl->DispRenewFlag = SMF_ON; //表示情報更新あり 147 | 148 | ptrkTbl->TrkNo = Cnt; 149 | ptrkTbl->TrkBufOffset = 0; 150 | ptrkTbl->Size = 0; //トラックデータサイズ 151 | } 152 | 153 | if (pseqTbl->FileSize < sizeof(SMF_HEADER)) 154 | { 155 | Ret = SMF_NG; 156 | } 157 | else 158 | { 159 | SMF_HEADER smfHeader; 160 | fileAccessReadSize = SmfFileAccessReadBuf((UCHAR *)&smfHeader, fileAccessPtr, sizeof(SMF_HEADER)); 161 | pSmfHeader = &smfHeader; 162 | fileAccessPtr = fileAccessPtr + fileAccessReadSize; 163 | DPRINT(F("fileAccessPtr:")); 164 | DPRINTLN(fileAccessPtr); 165 | 166 | //収容トラック数取得 167 | pseqTbl->TrackNum = pSmfHeader->Tracks[0]; 168 | pseqTbl->TrackNum = (pseqTbl->TrackNum << 8) | pSmfHeader->Tracks[1]; 169 | 170 | //TPQN取得 171 | TPQN = pSmfHeader->Division[0]; 172 | TPQN = (TPQN << 8) | pSmfHeader->Division[1]; 173 | 174 | Ret = SmfSeqSetTempo(pseqTbl, pseqTbl->Tick, TPQN, pseqTbl->TempoVal, pseqTbl->Tempo); 175 | 176 | if (Ret == SMF_OK) 177 | { 178 | TRACK_HEADER trkHeader; 179 | for (Cnt = 0; Cnt < pseqTbl->TrackNum; Cnt++) 180 | { 181 | fileAccessReadSize = SmfFileAccessReadBuf((UCHAR *)&trkHeader, fileAccessPtr, sizeof(TRACK_HEADER)); 182 | if (fileAccessReadSize < sizeof(TRACK_HEADER)) 183 | { 184 | Ret = SMF_NG; 185 | break; 186 | } 187 | pTrkHeader = &trkHeader; 188 | fileAccessPtr = fileAccessPtr + fileAccessReadSize; 189 | ptrkTbl = pseqTbl->ptrkTbl[Cnt]; 190 | 191 | ptrkTbl->Size = pTrkHeader->Length[0]; 192 | ptrkTbl->Size = (ptrkTbl->Size << 8) | pTrkHeader->Length[1]; 193 | ptrkTbl->Size = (ptrkTbl->Size << 8) | pTrkHeader->Length[2]; 194 | ptrkTbl->Size = (ptrkTbl->Size << 8) | pTrkHeader->Length[3]; 195 | DPRINT(F("ptrkTbl->Size:")); 196 | DPRINTLN(ptrkTbl->Size); 197 | 198 | ptrkTbl->TrkBufOffset = fileAccessPtr; 199 | DPRINT(F("ptrkTbl->TrkBufOffset:")); 200 | DPRINTLN(ptrkTbl->TrkBufOffset); 201 | 202 | fileAccessPtr = fileAccessPtr + ptrkTbl->Size; 203 | } 204 | } 205 | } 206 | if (Ret == SMF_OK) 207 | { 208 | Ret = SmfSeqPlayResetTrkTbl(pseqTbl); 209 | } 210 | return (Ret); 211 | } 212 | 213 | int SmfSeqPlayResetTrkTbl(SMF_SEQ_TABLE *pseqTbl) 214 | { 215 | int Ret; 216 | int Cnt; 217 | SMF_TRACK_TABLE *ptrkTbl; 218 | 219 | if (pseqTbl == NULL) 220 | { 221 | return (SMF_NG); 222 | } 223 | 224 | DPRINTLN(F("SmfSeqPlayResetTrkTbl")); 225 | for (Cnt = 0; Cnt < pseqTbl->TrackNum; Cnt++) 226 | { 227 | ptrkTbl = pseqTbl->ptrkTbl[Cnt]; 228 | ptrkTbl->TrackStatus = SMF_TRKSTAT_ONTRACK; 229 | ptrkTbl->Ptr = 0; //データポインタ 230 | ptrkTbl->SeqWaitFlag = SMF_WAIT_OFF; //デルタタイム経過待ちフラグ 231 | ptrkTbl->PreStat = 0; 232 | ptrkTbl->TickCnt = 0; //シーケンスカウンタ 233 | } 234 | //テンポ情報強制初期化 235 | Ret = SmfSeqSetTempo(pseqTbl, pseqTbl->Tick, pseqTbl->TPQN, 500000, 120); 236 | 237 | return (Ret); 238 | } 239 | 240 | void SmfSeqNoteClear(SMF_SEQ_TABLE *pseqTbl) 241 | { 242 | int Cnt; 243 | 244 | if (pseqTbl == NULL) 245 | { 246 | return; 247 | } 248 | } 249 | 250 | // ノートオンをすべてノートオフ 251 | int SmfSeqAllNoteOff(SMF_SEQ_TABLE *pseqTbl) 252 | { 253 | int cnt; 254 | int ch; 255 | int Ret; 256 | 257 | if (pseqTbl == NULL) 258 | { 259 | return (SMF_NG); 260 | } 261 | 262 | Ret = SMF_OK; 263 | for (cnt = 0; cnt < SMF_NOTESTATLNG; cnt++) 264 | { 265 | //0縲鰀16bitの状態を順にチェック 266 | for (ch = 0; ch < SMF_CHMAX; ch++) 267 | { 268 | Ret = midiOutShortMsg( 269 | (UCHAR)(MIDI_STATCH_NOTEOFF | ch), 270 | (UCHAR)cnt, 271 | (UCHAR)SMF_NOTEOFF_VELOCITY); 272 | if (Ret == MIDI_NG) 273 | { 274 | Ret = SMF_NG; 275 | break; 276 | } 277 | } 278 | } 279 | return (Ret); 280 | } 281 | 282 | //----------------------------------------------------------------------------- 283 | // ファイル読み込み ファイル読み込み、全テーブル設定、トラックテーブル初期化、状態を停止状態に移行 284 | // ファイル名 285 | //----------------------------------------------------------------------------- 286 | void SmfSeqFileLoadWithChNoOffset(SMF_SEQ_TABLE *pseqTbl, char *FileName, int chNoOffset) 287 | { 288 | if (pseqTbl == NULL) 289 | { 290 | return; 291 | } 292 | 293 | SmfSeqFileLoad(pseqTbl, FileName); 294 | pseqTbl->chNoOffset = chNoOffset; 295 | } 296 | void SmfSeqFileLoad(SMF_SEQ_TABLE *pseqTbl, char *FileName) 297 | { 298 | // 299 | int Ret; 300 | int cnt; 301 | ULONG DataSize; 302 | 303 | if (pseqTbl == NULL) 304 | { 305 | return; 306 | } 307 | 308 | //ファイル情報クリア 309 | //#LW# SmfSeqStrcpy( pseqTbl->FileName, "", SMF_DISPFILE_STRLEN ); 310 | pseqTbl->DispRenewFlag = SMF_ON; //表示情報更新あり 311 | pseqTbl->FileSize = 0; 312 | 313 | SmfSeqInitSeqTbl(pseqTbl); //共通テーブル初期化 314 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "共通テーブル初期化", SMF_DISPSTATUS_STRLEN ); 315 | pseqTbl->DispRenewFlag = SMF_ON; //表示情報更新あり 316 | 317 | if (strlen((char *)FileName) > 0) 318 | { 319 | //#LW# SmfSeqStrcpy( pseqTbl->FileName, SmfSeqGetFileName( FileName ), SMF_DISPFILE_STRLEN ); 320 | pseqTbl->DispRenewFlag = SMF_ON; //表示情報更新あり 321 | 322 | /* コピー元ファイルをオープン */ 323 | //ファイルオープンは必ずバイナリだとさ 324 | bool openResult = SmfFileAccessOpen((UCHAR *)FileName); 325 | 326 | /* エラー処理(アラートボックスの表示)*/ 327 | if (openResult != true) 328 | { 329 | DPRINTLN(F("openResult != true")); 330 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "ファイルオープンエラー", SMF_DISPSTATUS_STRLEN ); 331 | pseqTbl->DispRenewFlag = SMF_ON; //表示情報更新あり 332 | } 333 | else 334 | { 335 | //いけてるね 336 | pseqTbl->FileSize = SmfFileAccessSize(); //SMFファイルデータ長設定 337 | DPRINT(F("pseqTbl->FileSize:")); 338 | DPRINTLN(pseqTbl->FileSize); 339 | //トラック毎テーブル初期化 340 | Ret = SmfSeqInitTrkTbl(pseqTbl); 341 | if (Ret == SMF_NG) 342 | { 343 | DPRINTLN(F("Ret == SMF_NG")); 344 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "ファイル内容エラー", SMF_DISPSTATUS_STRLEN ); 345 | //表示情報更新あり 346 | pseqTbl->DispRenewFlag = SMF_ON; 347 | } 348 | else 349 | { 350 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "初期化完了", SMF_DISPSTATUS_STRLEN ); 351 | //表示情報更新あり 352 | pseqTbl->DispRenewFlag = SMF_ON; 353 | //停止状態に初期化 354 | pseqTbl->PlayStatus = SMF_STAT_STOP; 355 | } 356 | } 357 | } 358 | } 359 | 360 | //----------------------------------------------------------------------------- 361 | // 再生開始 状態を再生中に移行 362 | //----------------------------------------------------------------------------- 363 | int SmfSeqStart(SMF_SEQ_TABLE *pseqTbl) 364 | { 365 | int Ret; 366 | 367 | if (pseqTbl == NULL) 368 | { 369 | return (SMF_NG); 370 | } 371 | 372 | switch (pseqTbl->PlayStatus) 373 | { 374 | case SMF_STAT_STOP: 375 | Ret = SMF_OK; 376 | 377 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "演奏中", SMF_DISPSTATUS_STRLEN ); 378 | pseqTbl->DispRenewFlag = SMF_ON; 379 | 380 | pseqTbl->PlayStatus = SMF_STAT_PLAY; 381 | break; 382 | default: 383 | Ret = SMF_NG; 384 | break; 385 | } 386 | 387 | return (Ret); 388 | } 389 | //----------------------------------------------------------------------------- 390 | // 一時停止 状態を一時停止中 391 | //----------------------------------------------------------------------------- 392 | int SmfSeqPauseSet(SMF_SEQ_TABLE *pseqTbl) 393 | { 394 | int Ret; 395 | 396 | if (pseqTbl == NULL) 397 | { 398 | return (SMF_NG); 399 | } 400 | 401 | switch (pseqTbl->PlayStatus) 402 | { 403 | case SMF_STAT_PLAY: 404 | Ret = SMF_OK; 405 | 406 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "一時停止中", SMF_DISPSTATUS_STRLEN ); 407 | pseqTbl->DispRenewFlag = SMF_ON; 408 | 409 | pseqTbl->PlayStatus = SMF_STAT_PAUSE; 410 | break; 411 | default: 412 | Ret = SMF_NG; 413 | break; 414 | } 415 | 416 | return (Ret); 417 | } 418 | //----------------------------------------------------------------------------- 419 | // 一時停止解除 状態を再生中に移行 420 | //----------------------------------------------------------------------------- 421 | int SmfSeqPauseRelease(SMF_SEQ_TABLE *pseqTbl) 422 | { 423 | int Ret; 424 | 425 | if (pseqTbl == NULL) 426 | { 427 | return (SMF_NG); 428 | } 429 | 430 | switch (pseqTbl->PlayStatus) 431 | { 432 | case SMF_STAT_PAUSE: 433 | Ret = SMF_OK; 434 | 435 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "演奏中", SMF_DISPSTATUS_STRLEN ); 436 | pseqTbl->DispRenewFlag = SMF_ON; 437 | 438 | pseqTbl->PlayStatus = SMF_STAT_PLAY; 439 | break; 440 | default: 441 | Ret = SMF_NG; 442 | break; 443 | } 444 | 445 | return (Ret); 446 | } 447 | //----------------------------------------------------------------------------- 448 | // 再生停止 状態を停止待ちに移行 449 | //----------------------------------------------------------------------------- 450 | int SmfSeqStop(SMF_SEQ_TABLE *pseqTbl) 451 | { 452 | int Ret; 453 | 454 | if (pseqTbl == NULL) 455 | { 456 | return (SMF_NG); 457 | } 458 | 459 | DPRINTLN(F("SmfSeqStop")); 460 | switch (pseqTbl->PlayStatus) 461 | { 462 | case SMF_STAT_PLAY: 463 | case SMF_STAT_PAUSE: 464 | Ret = SMF_OK; 465 | 466 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "演奏停止", SMF_DISPSTATUS_STRLEN ); 467 | pseqTbl->DispRenewFlag = SMF_ON; 468 | 469 | SmfSeqAllNoteOff(pseqTbl); //ノートオフ漏れ停止 470 | 471 | pseqTbl->PlayStatus = SMF_STAT_STOP; 472 | break; 473 | default: 474 | Ret = SMF_NG; 475 | break; 476 | } 477 | 478 | return (Ret); 479 | } 480 | //----------------------------------------------------------------------------- 481 | // 割り込み毎起動 割り込み毎処理 482 | //----------------------------------------------------------------------------- 483 | int SmfSeqTickProc(SMF_SEQ_TABLE *pseqTbl) 484 | { 485 | int PlayStat; 486 | int Cnt; 487 | int Ret; 488 | 489 | if (pseqTbl == NULL) 490 | { 491 | return (SMF_NG); 492 | } 493 | 494 | switch (pseqTbl->PlayStatus) 495 | { 496 | case SMF_STAT_PLAY: 497 | case SMF_STAT_STOPWAIT: 498 | Ret = SMF_OK; 499 | 500 | PlayStat = SMF_STAT_STOPWAIT; 501 | for (Cnt = 0; Cnt < pseqTbl->TrackNum; Cnt++) 502 | { 503 | Ret = SmfSeqEventProc(pseqTbl, pseqTbl->ptrkTbl[Cnt]); 504 | //異常発生しているか? 505 | if ((Ret == SMF_ENDOFSMF) || (Ret == SMF_NG)) 506 | { 507 | PlayStat = SMF_STAT_STOPWAIT; 508 | break; 509 | } 510 | //1トラックでも演奏中なら状態継続 511 | if (pseqTbl->ptrkTbl[Cnt]->TrackStatus == SMF_TRKSTAT_ONTRACK) 512 | { 513 | PlayStat = SMF_STAT_PLAY; 514 | } 515 | } 516 | //全トラックシーケンス終了か? 517 | if ((pseqTbl->PlayStatus == SMF_STAT_STOPWAIT) || 518 | (PlayStat == SMF_STAT_STOPWAIT)) 519 | { 520 | //#LW# SmfSeqStrcpy( pseqTbl->Status, "演奏終了", SMF_DISPSTATUS_STRLEN ); 521 | pseqTbl->DispRenewFlag = SMF_ON; 522 | 523 | SmfSeqAllNoteOff(pseqTbl); //ノートオフ漏れ停止 524 | 525 | pseqTbl->PlayStatus = SMF_STAT_STOP; 526 | } 527 | break; 528 | default: 529 | Ret = SMF_NG; 530 | break; 531 | } 532 | 533 | return (Ret); 534 | } 535 | 536 | inline void drawKey(int note, int ch, bool press) 537 | { 538 | static constexpr int note_x[] = {0, 2, 4, 6, 8, 12, 14, 16, 18, 20, 22, 24 }; 539 | static constexpr int note_y[] = {4, 0, 4, 0, 4, 4, 0, 4, 0, 4, 0, 4 }; 540 | 541 | int oct = note / 12; 542 | int idx = note - oct * 12; 543 | int dx = 18 + oct * 28 + note_x[idx]; 544 | int dy = 61 + ch * 10 + note_y[idx]; 545 | int color = TFT_WHITE; 546 | if (!press) color = note_y[idx] ? TFT_DARKGREY : TFT_BLACK; 547 | lcd.fillRect(dx, dy, 3, 2, color); 548 | } 549 | 550 | int SmfSeqEventProc(SMF_SEQ_TABLE *pseqTbl, SMF_TRACK_TABLE *ptrkTbl) 551 | { 552 | UCHAR ExBuff[SMF_EXBUFLNG]; 553 | UCHAR MidiData[2]; 554 | bool result; 555 | long MidiDeltaTime; 556 | int MidiStatus; 557 | int MidiData1; 558 | int MidiData2; 559 | int Ret; 560 | int Cnt; 561 | long TempTempo; 562 | 563 | if (pseqTbl == NULL) 564 | { 565 | return (SMF_NG); 566 | } 567 | 568 | Ret = SMF_OK; 569 | //トラックデータをシーケンス 570 | while (ptrkTbl->TrackStatus == SMF_TRKSTAT_ONTRACK) 571 | { 572 | //デルタ時間経過待ち? 573 | if (ptrkTbl->SeqWaitFlag == SMF_WAIT_OFF) 574 | { 575 | //待ってない 576 | MidiDeltaTime = SmfSeqGetNum(ptrkTbl); 577 | if (MidiDeltaTime == SMF_ENDOFSMF) 578 | { 579 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 580 | Ret = MidiDeltaTime; 581 | break; 582 | } 583 | ptrkTbl->TickCnt = ptrkTbl->TickCnt + MidiDeltaTime * 1000; 584 | } 585 | else 586 | { 587 | //待ってる 588 | MidiDeltaTime = 0; 589 | ptrkTbl->TickCnt = ptrkTbl->TickCnt - pseqTbl->SeqTickUnit; 590 | } 591 | if (ptrkTbl->TickCnt <= 0) 592 | { 593 | MidiStatus = SmfSeqGetByteData(ptrkTbl); 594 | if (MidiStatus == SMF_ENDOFSMF) 595 | { 596 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 597 | Ret = MidiStatus; 598 | break; 599 | } 600 | 601 | if (MidiStatus == MIDI_STAT_METAEVENT) 602 | { 603 | MidiData1 = SmfSeqGetByteData(ptrkTbl); 604 | if (MidiData1 == SMF_ENDOFSMF) 605 | { 606 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 607 | Ret = MidiData1; 608 | break; 609 | } 610 | MidiData2 = SmfSeqGetNum(ptrkTbl); 611 | if (MidiData2 == SMF_ENDOFSMF) 612 | { 613 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 614 | Ret = MidiData2; 615 | break; 616 | } 617 | Ret = SmfSeqGetData(ptrkTbl, ExBuff, MidiData2); 618 | if (Ret == SMF_ENDOFSMF) 619 | { 620 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 621 | Ret = MidiData2; 622 | break; 623 | } 624 | switch (MidiData1) 625 | { 626 | case MIDI_META_ENDOFTRACK: 627 | //トラックシーケンス状態をトラックエンドに 628 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 629 | break; 630 | case MIDI_META_SETTEMPO: 631 | TempTempo = 0; 632 | if (0 < MidiData2) 633 | { 634 | TempTempo = ExBuff[0]; 635 | for (Cnt = 1; Cnt < MidiData2; Cnt++) 636 | { 637 | TempTempo = TempTempo << 8 | ExBuff[Cnt]; 638 | } 639 | Ret = SmfSeqSetTempo(pseqTbl, pseqTbl->Tick, pseqTbl->TPQN, TempTempo, pseqTbl->Tempo); 640 | if (Ret == SMF_NG) 641 | { 642 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 643 | Ret = SMF_NG; 644 | break; 645 | } 646 | } 647 | break; 648 | case MIDI_META_SEQTRKNAME: 649 | ExBuff[MidiData2] = 0x00; //念の為デリミタ 650 | //#LW# SmfSeqStrcpy( ptrkTbl->SeqTrkName, (char *)ExBuff, SMF_DISPSEQTRK_STRLEN ); 651 | pseqTbl->DispRenewFlag = SMF_ON; //表示情報更新あり 652 | break; 653 | case MIDI_META_SEQNO: 654 | case MIDI_META_TEXT: 655 | case MIDI_META_COPYRIGHT: 656 | case MIDI_META_INSTNAME: 657 | case MIDI_META_LYLIC: 658 | case MIDI_META_CHPREFIX: 659 | case MIDI_META_SMPTEOFFSET: 660 | case MIDI_META_TEMPOSIG: 661 | case MIDI_META_KEYSIG: 662 | case MIDI_META_SEQSPECIFIC: 663 | default: 664 | break; 665 | } 666 | } 667 | else 668 | { 669 | //ステータスビットが立ってない場合は一つ前のイベントと同じ 670 | if ((MidiStatus & MIDI_STATBIT_MASK) != MIDI_STATBIT_MASK) 671 | { 672 | MidiStatus = ptrkTbl->PreStat; //無理矢理前と同じステータスにする 673 | Ret = SmfSeqStepBack(ptrkTbl); 674 | } 675 | else 676 | { 677 | ptrkTbl->PreStat = MidiStatus; 678 | } 679 | //eVF1ではch1がボーカル固定のためGM音源として使用する場合chをずらす 680 | if (pseqTbl->chNoOffset > 0) 681 | { 682 | int ch = MidiStatus & MIDI_CHANNEL_MASK; 683 | //ただしGM音源のCH10はドラム固定のためそのまま使用する 684 | if (ch != MIDI_CH_GM_DRUM) 685 | { 686 | ch = ch + pseqTbl->chNoOffset; 687 | if (ch == MIDI_CH_GM_DRUM) 688 | { 689 | ch++; 690 | } 691 | MidiStatus = (MidiStatus & MIDI_STAT_MASK) | ch; 692 | } 693 | } 694 | switch (MidiStatus & MIDI_STAT_MASK) 695 | { 696 | case MIDI_STATCH_NOTEOFF: 697 | result = SmfSeqGetData(ptrkTbl, MidiData, 2); 698 | if (result == false) 699 | { 700 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 701 | Ret = SMF_ENDOFSMF; 702 | break; 703 | } 704 | MidiData1 = (int)MidiData[0]; 705 | MidiData2 = (int)MidiData[1]; 706 | lcd.startWrite(); 707 | drawKey(MidiData1, MidiStatus & MIDI_CHANNEL_MASK, false); 708 | Ret = midiOutShortMsg((UCHAR)MidiStatus, 709 | (UCHAR)MidiData1, 710 | (UCHAR)MidiData2); 711 | lcd.endWrite(); 712 | if (Ret == MIDI_NG) 713 | { 714 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 715 | Ret = SMF_NG; 716 | break; 717 | } 718 | Ret = SMF_OK; 719 | break; 720 | case MIDI_STATCH_NOTEON: 721 | result = SmfSeqGetData(ptrkTbl, MidiData, 2); 722 | if (result == false) 723 | { 724 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 725 | Ret = SMF_ENDOFSMF; 726 | break; 727 | } 728 | MidiData1 = (int)MidiData[0]; 729 | MidiData2 = (int)MidiData[1]; 730 | lcd.startWrite(); 731 | drawKey(MidiData1, MidiStatus & MIDI_CHANNEL_MASK, MidiData2); 732 | //ノートオンによるノートオフ対策 733 | if (MidiData2 == 0) 734 | { 735 | //ベロシティ0のノートオンはノートオフに書換える 736 | MidiStatus = MIDI_STATCH_NOTEOFF | (MidiStatus & MIDI_CHANNEL_MASK); 737 | } 738 | Ret = midiOutShortMsg((UCHAR)MidiStatus, 739 | (UCHAR)MidiData1, 740 | (UCHAR)MidiData2); 741 | lcd.endWrite(); 742 | if (Ret == MIDI_NG) 743 | { 744 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 745 | Ret = SMF_NG; 746 | break; 747 | } 748 | Ret = SMF_OK; 749 | break; 750 | case MIDI_STATCH_PKEYPRES: 751 | case MIDI_STATCH_CTRLCHG: 752 | case MIDI_STATCH_PBNDCHG: 753 | result = SmfSeqGetData(ptrkTbl, MidiData, 2); 754 | if (result == false) 755 | { 756 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 757 | Ret = SMF_ENDOFSMF; 758 | break; 759 | } 760 | MidiData1 = (int)MidiData[0]; 761 | MidiData2 = (int)MidiData[1]; 762 | Ret = midiOutShortMsg((UCHAR)MidiStatus, 763 | (UCHAR)MidiData1, 764 | (UCHAR)MidiData2); 765 | if (Ret == MIDI_NG) 766 | { 767 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 768 | Ret = SMF_NG; 769 | break; 770 | } 771 | Ret = SMF_OK; 772 | break; 773 | case MIDI_STATCH_PROGCHG: 774 | case MIDI_STATCH_CNPRES: 775 | MidiData1 = SmfSeqGetNum(ptrkTbl); 776 | if (MidiData1 == SMF_ENDOFSMF) 777 | { 778 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 779 | Ret = MidiData1; 780 | break; 781 | } 782 | ExBuff[0] = MidiStatus; 783 | ExBuff[1] = MidiData1; 784 | 785 | Ret = midiOutLongMsg(ExBuff, 2); 786 | if (Ret == MIDI_NG) 787 | { 788 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 789 | Ret = SMF_NG; 790 | break; 791 | } 792 | Ret = SMF_OK; 793 | break; 794 | case MIDI_STATEX_SYSEXCL: 795 | MidiData1 = SmfSeqGetNum(ptrkTbl); 796 | if (MidiData1 == SMF_ENDOFSMF) 797 | { 798 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 799 | Ret = MidiData1; 800 | break; 801 | } 802 | ExBuff[0] = MidiStatus; 803 | Ret = SmfSeqGetData(ptrkTbl, &ExBuff[1], MidiData1); 804 | if (Ret == SMF_ENDOFSMF) 805 | { 806 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 807 | break; 808 | } 809 | Ret = midiOutLongMsg(ExBuff, MidiData1 + 1); 810 | if (Ret == MIDI_NG) 811 | { 812 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 813 | Ret = SMF_NG; 814 | break; 815 | } 816 | Ret = SMF_OK; 817 | break; 818 | case MIDI_STATEX_EOEXCL: 819 | MidiData1 = SmfSeqGetNum(ptrkTbl); 820 | if (MidiData1 == SMF_ENDOFSMF) 821 | { 822 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 823 | Ret = MidiData1; 824 | break; 825 | } 826 | Ret = SmfSeqGetData(ptrkTbl, ExBuff, MidiData1); 827 | if (Ret == SMF_ENDOFSMF) 828 | { 829 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 830 | break; 831 | } 832 | Ret = midiOutLongMsg(ExBuff, MidiData1); 833 | if (Ret == MIDI_NG) 834 | { 835 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 836 | Ret = SMF_NG; 837 | break; 838 | } 839 | Ret = SMF_OK; 840 | break; 841 | case MIDI_STATEX_MIDITCQF: 842 | case MIDI_STATEX_SONGPOGP: 843 | case MIDI_STATEX_SONGSEL: 844 | case MIDI_STATEX_CHUNEREQ: 845 | case MIDI_STATRT_TMGCLOCK: 846 | case MIDI_STATRT_START: 847 | case MIDI_STATRT_CONTINUE: 848 | case MIDI_STATRT_STOP: 849 | case MIDI_STATRT_ACTSENC: 850 | case MIDI_STATRT_SYSRESET: 851 | default: 852 | ptrkTbl->TrackStatus = SMF_TRKSTAT_TRACKEND; 853 | Ret = SMF_NG; 854 | break; 855 | } 856 | if ((Ret == SMF_NG) || (Ret == SMF_ENDOFSMF)) 857 | { 858 | break; 859 | } 860 | } 861 | ptrkTbl->SeqWaitFlag = SMF_WAIT_OFF; 862 | } 863 | else 864 | { 865 | ptrkTbl->SeqWaitFlag = SMF_WAIT_ON; 866 | break; 867 | } 868 | } 869 | return (Ret); 870 | } 871 | 872 | //----------------------------------------------------------------------------- 873 | // 終了 全テーブル初期化 874 | //----------------------------------------------------------------------------- 875 | int SmfSeqEnd(SMF_SEQ_TABLE *pseqTbl) 876 | { 877 | int Ret; 878 | 879 | if (pseqTbl == NULL) 880 | { 881 | return (SMF_NG); 882 | } 883 | 884 | //状態をファイル未読み込みに遷移 885 | pseqTbl->PlayStatus = SMF_STAT_FILENOTREAD; 886 | 887 | SmfSeqAllNoteOff(pseqTbl); //ノートオフ漏れ停止 888 | 889 | Ret = SmfSeqGMReset(); //音源リセット 890 | 891 | Ret = midiOutClose(); //MIDIクローズ 892 | 893 | SmfFileAccessClose(); //ファイルクローズ 894 | 895 | if (Ret == MIDI_NG) 896 | { 897 | Ret = SMF_NG; 898 | } 899 | else 900 | { 901 | Ret = SMF_OK; 902 | } 903 | return (Ret); 904 | } 905 | //------------------ 906 | //----------------------------------------------------------------------------- 907 | // 表示情報更新有無リセット 908 | //----------------------------------------------------------------------------- 909 | void SmfSeqDispRenewReset(SMF_SEQ_TABLE *pseqTbl) 910 | { 911 | 912 | if (pseqTbl == NULL) 913 | { 914 | return; 915 | } 916 | 917 | pseqTbl->DispRenewFlag = SMF_OFF; 918 | } 919 | //----------------------------------------------------------------------------- 920 | // 表示情報更新有無取得 921 | //----------------------------------------------------------------------------- 922 | int SmfSeqGetDispRenew(SMF_SEQ_TABLE *pseqTbl) 923 | { 924 | 925 | if (pseqTbl == NULL) 926 | { 927 | return (SMF_NG); 928 | } 929 | 930 | return (pseqTbl->DispRenewFlag); 931 | } 932 | //----------------------------------------------------------------------------- 933 | // 演奏状態取得 934 | //----------------------------------------------------------------------------- 935 | int SmfSeqGetStatus(SMF_SEQ_TABLE *pseqTbl) 936 | { 937 | 938 | if (pseqTbl == NULL) 939 | { 940 | return (SMF_NG); 941 | } 942 | 943 | return (pseqTbl->PlayStatus); 944 | } 945 | 946 | //----------------------------------------------------------------------------- 947 | // GMリセット 948 | //----------------------------------------------------------------------------- 949 | int SmfSeqGMReset() 950 | { 951 | int Ret; 952 | 953 | Ret = midiOutGMReset(); 954 | if (Ret == MIDI_NG) 955 | { 956 | Ret = SMF_NG; 957 | } 958 | else 959 | { 960 | Ret = SMF_OK; 961 | } 962 | return (Ret); 963 | } 964 | 965 | //----------------------------------------------------------------------------- 966 | int SmfSeqStepBack(SMF_TRACK_TABLE *ptrkTbl) 967 | { 968 | int Ret; 969 | 970 | if (ptrkTbl == NULL) 971 | { 972 | return (SMF_NG); 973 | } 974 | 975 | Ret = SMF_OK; 976 | 977 | if (ptrkTbl->Ptr != 0) 978 | { 979 | ptrkTbl->Ptr--; 980 | } 981 | 982 | return (Ret); 983 | } 984 | 985 | int SmfSeqGetByteData(SMF_TRACK_TABLE *ptrkTbl) 986 | { 987 | int Ret; 988 | 989 | if (ptrkTbl == NULL) 990 | { 991 | return (SMF_NG); 992 | } 993 | 994 | Ret = SMF_OK; 995 | 996 | //トラックサイズを越えてアクセスしようとしている 997 | if (ptrkTbl->Ptr >= ptrkTbl->Size) 998 | { 999 | Ret = SMF_ENDOFSMF; //エラーを返す 1000 | } 1001 | else 1002 | { 1003 | UCHAR data; 1004 | bool result = SmfFileAccessRead(&data, (ptrkTbl->TrkBufOffset + ptrkTbl->Ptr)); 1005 | ptrkTbl->Ptr++; 1006 | if (result == true) 1007 | { 1008 | Ret = (int)data; 1009 | } 1010 | else 1011 | { 1012 | Ret = SMF_ENDOFSMF; //エラーを返す 1013 | } 1014 | } 1015 | 1016 | return (Ret); 1017 | } 1018 | 1019 | int SmfSeqGetData(SMF_TRACK_TABLE *ptrkTbl, UCHAR *Data, int Length) 1020 | { 1021 | int Cnt = 0; 1022 | int Ret; 1023 | 1024 | if (ptrkTbl == NULL) 1025 | { 1026 | return (SMF_NG); 1027 | } 1028 | 1029 | Ret = SmfFileAccessReadBuf(Data, (ptrkTbl->TrkBufOffset + ptrkTbl->Ptr), Length); 1030 | ptrkTbl->Ptr = ptrkTbl->Ptr + Ret; 1031 | 1032 | if (Ret != Length) 1033 | { 1034 | Ret = SMF_ENDOFSMF; //エラーを返す 1035 | } 1036 | 1037 | return (Ret); 1038 | } 1039 | 1040 | int SmfSeqGetExData(SMF_TRACK_TABLE *ptrkTbl, UCHAR *Data, int MaxLength) 1041 | { 1042 | int Cnt; 1043 | int Ret; 1044 | int MidiData; 1045 | 1046 | if (ptrkTbl == NULL) 1047 | { 1048 | return (SMF_NG); 1049 | } 1050 | 1051 | Ret = SMF_OK; 1052 | 1053 | UCHAR data; 1054 | bool result; 1055 | for (Cnt = 0; Cnt < MaxLength; Cnt++) 1056 | { 1057 | if (ptrkTbl->Ptr >= ptrkTbl->Size) 1058 | { 1059 | MidiData = SMF_ENDOFSMF; //エラーを返す 1060 | } 1061 | else 1062 | { 1063 | if (Cnt == 0) 1064 | { 1065 | result = SmfFileAccessRead(&data, (ptrkTbl->TrkBufOffset + ptrkTbl->Ptr)); 1066 | ptrkTbl->Ptr++; 1067 | } 1068 | else 1069 | { 1070 | result = SmfFileAccessReadNext(&data); 1071 | ptrkTbl->Ptr++; 1072 | } 1073 | if (result == true) 1074 | { 1075 | MidiData = (int)data; 1076 | } 1077 | else 1078 | { 1079 | MidiData = SMF_ENDOFSMF; //エラーを返す 1080 | } 1081 | } 1082 | if (MidiData == SMF_ENDOFSMF) 1083 | { 1084 | Ret = MidiData; 1085 | break; 1086 | } 1087 | if (MidiData == MIDI_STATEX_EOEXCL) 1088 | { 1089 | Ret = Cnt; 1090 | break; 1091 | } 1092 | Data[Cnt] = MidiData; 1093 | } 1094 | 1095 | return (Ret); 1096 | } 1097 | 1098 | //数値取得、主にデルタタイム 1099 | long SmfSeqGetNum(SMF_TRACK_TABLE *ptrkTbl) 1100 | { 1101 | long Ret; 1102 | long DeltaTime; 1103 | long Cnt; 1104 | long MidiData; 1105 | 1106 | if (ptrkTbl == NULL) 1107 | { 1108 | return (SMF_NG); 1109 | } 1110 | 1111 | Ret = SMF_OK; 1112 | DeltaTime = 0; 1113 | 1114 | DPRINT(F("getNum")); 1115 | 1116 | UCHAR data; 1117 | bool result; 1118 | for (Cnt = 0; Cnt < SMF_DELTAMAXBYTE; Cnt++) 1119 | { 1120 | if (ptrkTbl->Ptr >= ptrkTbl->Size) 1121 | { 1122 | MidiData = SMF_ENDOFSMF; //エラーを返す 1123 | } 1124 | else 1125 | { 1126 | if (Cnt == 0) 1127 | { 1128 | result = SmfFileAccessRead(&data, (ptrkTbl->TrkBufOffset + ptrkTbl->Ptr)); 1129 | ptrkTbl->Ptr++; 1130 | } 1131 | else 1132 | { 1133 | result = SmfFileAccessReadNext(&data); 1134 | ptrkTbl->Ptr++; 1135 | } 1136 | if (result == true) 1137 | { 1138 | MidiData = (int)data; 1139 | } 1140 | else 1141 | { 1142 | MidiData = SMF_ENDOFSMF; //エラーを返す 1143 | } 1144 | } 1145 | DPRINT(F(":")); 1146 | DPRINT(MidiData, HEX); 1147 | if (MidiData == SMF_ENDOFSMF) 1148 | { 1149 | Ret = MidiData; 1150 | break; 1151 | } 1152 | DeltaTime = DeltaTime | (MidiData & SMF_DELTAMASK); 1153 | if ((MidiData & SMF_DELTACONTBIT) != SMF_DELTACONTBIT) 1154 | { 1155 | Ret = DeltaTime; 1156 | DPRINT(F("=>")); 1157 | DPRINT(Ret); 1158 | break; 1159 | } 1160 | DeltaTime = (DeltaTime << 7); 1161 | } 1162 | DPRINTLN(F("")); 1163 | 1164 | return (Ret); 1165 | } 1166 | 1167 | // サイズ制限付き文字列コピー 1168 | void SmfSeqStrcpy(char *DistStr, char *SrcStr, int MaxSize) 1169 | { 1170 | int Cnt; 1171 | 1172 | for (Cnt = 0; Cnt < MaxSize; Cnt++) 1173 | { 1174 | DistStr[Cnt] = SrcStr[Cnt]; 1175 | if (SrcStr[Cnt] == 0x00) 1176 | { 1177 | break; 1178 | } 1179 | } 1180 | } 1181 | 1182 | // フルパスファイル名からファイル名を取得 1183 | char *SmfSeqGetFileName(char *FullPathName) 1184 | { 1185 | int Cnt; 1186 | int Lng; 1187 | 1188 | Lng = strlen(FullPathName); 1189 | for (Cnt = 0; Cnt < Lng; Cnt++) 1190 | { 1191 | if (FullPathName[Lng - Cnt - 1] == '\\') 1192 | { 1193 | break; 1194 | } 1195 | } 1196 | 1197 | return (&FullPathName[Lng - Cnt]); 1198 | } 1199 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/SmfSeq.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | <ユーザ設定用ヘッダファイル> 3 | 4 | 5 | 6 | catsin 7 | MIDIシーケンサユーザ設定用ヘッダファイル 8 | ***********************************************************************/ 9 | 10 | #ifndef INC_SMF_SEQ_H 11 | #define INC_SMF_SEQ_H 12 | 13 | #include "common.h" 14 | 15 | #ifndef NULL 16 | #define NULL 0 17 | #endif 18 | 19 | // 定数定義 20 | #define SMF_OK 0 21 | #define SMF_NG -1 22 | #define SMF_ON 1 23 | #define SMF_OFF 0 24 | #define SMF_ENDOFSMF -1 25 | #define SMF_BUFFSIZEOVER -2 26 | #define SMF_DELTACONTBIT 0x80 27 | #define SMF_DELTAMASK 0x7F 28 | #define SMF_DELTAMAXBYTE 4 29 | 30 | #define SMF_CHMAX 16 31 | #define SMF_NOTEOFF_VELOCITY 64 32 | 33 | #define SMF_WAIT_ERROR -1 34 | #define SMF_WAIT_ON 1 35 | #define SMF_WAIT_OFF 0 36 | 37 | #define SMF_STAT_FILENOTREAD 0 //SMFファイル未読み込み(演奏不能) 38 | #define SMF_STAT_STOP 1 //演奏停止 39 | #define SMF_STAT_PLAY 2 //演奏中 40 | #define SMF_STAT_PAUSE 3 //演奏一時停止中 41 | #define SMF_STAT_STOPWAIT 4 //演奏停止待ち(演奏中) 42 | 43 | #define SMF_TRKSTAT_ONTRACK 0 44 | #define SMF_TRKSTAT_TRACKEND 1 45 | 46 | #define SMF_NOTESTAT_OFF -1 47 | 48 | 49 | //#LW##define SMF_EXBUFLNG 2048 //汎用処理バッファサイズ 50 | #define SMF_EXBUFLNG 128 //汎用処理バッファサイズ 51 | #define SMF_NOTESTATLNG 128 //発音記録バッファサイズ 52 | 53 | 54 | //#LW##define SMF_TRACKNUM 128 //最大トラック数 55 | #define SMF_TRACKNUM 32 //最大トラック数 56 | 57 | #define SMF_DISPSTATUS_STRLEN 25 58 | #define SMF_DISPFILE_STRLEN 25 59 | #define SMF_DISPSEQTRK_STRLEN 35 60 | 61 | //テーブル定義 62 | //トラックテーブル 63 | typedef struct { 64 | unsigned char TrkNo; 65 | char TrackStatus; //トラックシーケンス状態 66 | unsigned int Ptr; //データポインタ 67 | unsigned int Size; //トラックデータサイズ 68 | char SeqWaitFlag; //デルタタイム経過待ちフラグ 69 | char PreStat; 70 | 71 | long TickCnt; //シーケンスカウンタ 72 | 73 | //#LW# char SeqTrkName[SMF_DISPSEQTRK_STRLEN+1]; //トラック/シーケンス名 74 | 75 | unsigned long TrkBufOffset; 76 | } SMF_TRACK_TABLE; 77 | 78 | //シーケンステーブル 79 | typedef struct { 80 | char PlayStatus; //シーケンサ状態 81 | char DispRenewFlag; //表示情報更新フラグ 82 | 83 | int Tick; //仮想割り込み間隔(ms) 84 | 85 | //#LW# char Status[SMF_DISPSTATUS_STRLEN+1]; //状態文字列 86 | //#LW# char FileName[LONG_DFNAME_LEN+20]; //読込みMIDIファイル名 87 | unsigned int FileSize; //読込みMIDIファイルサイズ 88 | int chNoOffset; //CH番号オフセット 89 | 90 | int TrackNum; //格納トラック数 91 | 92 | int TPQN; //4分音符の分解能 93 | float TempoVal; //4分音符の長さ(ms):データ上はμs 94 | int Tempo; //1分に入る4分音符数:60000ms/500ms=120 95 | int SeqTickUnit; //1Tick毎の時間値 96 | 97 | SMF_TRACK_TABLE *ptrkTbl[ SMF_TRACKNUM ]; 98 | 99 | } SMF_SEQ_TABLE; 100 | 101 | //#SMF ファイルヘッダフォーマット 102 | typedef struct { 103 | UCHAR CyancType[4]; 104 | UCHAR Length[4]; 105 | UCHAR Format[2]; 106 | UCHAR Tracks[2]; 107 | UCHAR Division[2]; 108 | } SMF_HEADER; 109 | //#SMF トラックヘッダフォーマット 110 | typedef struct { 111 | UCHAR CyancType[4]; 112 | UCHAR Length[4]; 113 | } TRACK_HEADER; 114 | 115 | SMF_SEQ_TABLE *SmfSeqInit( int Tick ); 116 | void SmfSeqClrarTbl( ); 117 | void SmfSeqInitSeqTbl( SMF_SEQ_TABLE *pseqTbl ); 118 | int SmfSeqSetTempo( SMF_SEQ_TABLE *pseqTbl, int Tick, int TPQN, float TempoVal, int Tempo ); 119 | int SmfSeqInitTrkTbl( SMF_SEQ_TABLE *pseqTbl ); 120 | int SmfSeqPlayResetTrkTbl( SMF_SEQ_TABLE *pseqTbl ); 121 | void SmfSeqNoteClear( SMF_SEQ_TABLE *pseqTbl ); 122 | int SmfSeqAllNoteOff( SMF_SEQ_TABLE *pseqTbl ); 123 | void SmfSeqFileLoadWithChNoOffset( SMF_SEQ_TABLE *pseqTbl, char *FileName, int chNoOffset ); 124 | void SmfSeqFileLoad( SMF_SEQ_TABLE *pseqTbl, char *FileName ); 125 | int SmfSeqStart( SMF_SEQ_TABLE *pseqTbl ); 126 | int SmfSeqPauseSet( SMF_SEQ_TABLE *pseqTbl ); 127 | int SmfSeqPauseRelease( SMF_SEQ_TABLE *pseqTbl ); 128 | int SmfSeqStop( SMF_SEQ_TABLE *pseqTbl ); 129 | int SmfSeqTickProc( SMF_SEQ_TABLE *pseqTbl ); 130 | int SmfSeqEventProc( SMF_SEQ_TABLE *pseqTbl, SMF_TRACK_TABLE *ptrkTbl ); 131 | int SmfSeqEnd( SMF_SEQ_TABLE *pseqTbl ); 132 | void SmfSeqDispRenewReset( SMF_SEQ_TABLE *pseqTbl ); 133 | int SmfSeqGetDispRenew( SMF_SEQ_TABLE *pseqTbl ); 134 | int SmfSeqGetStatus( SMF_SEQ_TABLE *pseqTbl ); 135 | int SmfSeqGMReset(); 136 | int SmfSeqStepBack( SMF_TRACK_TABLE *ptrkTbl ); 137 | int SmfSeqGetByteData( SMF_TRACK_TABLE *ptrkTbl ); 138 | int SmfSeqGetData( SMF_TRACK_TABLE *ptrkTbl, UCHAR *Data, int Length ); 139 | int SmfSeqGetExData( SMF_TRACK_TABLE *ptrkTbl, UCHAR *Data, int MaxLength ); 140 | long SmfSeqGetNum( SMF_TRACK_TABLE *ptrkTbl ); 141 | void SmfSeqStrcpy( char *DistStr, char *SrcStr, int MaxSize ); 142 | char *SmfSeqGetFileName( char *FullPathName ); 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/M5Stack_SMF_Player/common.h: -------------------------------------------------------------------------------- 1 | #ifndef DEF_COMMON_H 2 | #define DEF_COMMON_H 3 | 4 | //システム依存する定義 5 | #include 6 | #include 7 | #define LGFX_M5STACK 8 | #include 9 | extern LGFX lcd; 10 | 11 | 12 | typedef unsigned char UCHAR; //1バイト符号なしデータ(SMFファイルアクセスに使用) 13 | typedef unsigned long ULONG; //4バイト符号なしデータ(SMFファイルアクセスに使用) 14 | 15 | typedef int ERRORCODE; 16 | 17 | #define SMF_FILENAME ("playdata.mid") //SMFファイル名 18 | #define ZTICK (30) //[ms]定期更新処理起動間隔 19 | 20 | #define LONG_DFNAME_LEN (128) 21 | #define FC_NO_DATA_ERROR (0) 22 | 23 | //#define DEBUG 24 | #ifdef DEBUG 25 | #define DPRINT Serial.print 26 | #define DPRINTLN Serial.println 27 | #else //DEBUG 28 | #define DPRINT 29 | #define DPRINTLN 30 | #endif //DEBUG 31 | 32 | #endif //DEF_COMMON_H 33 | -------------------------------------------------------------------------------- /Arduino-IDE-Sketch/sync_test/sync_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | void setup() { 3 | // init lcd, serial, but don't init sd card 4 | M5.begin(true, false, true); 5 | M5.Lcd.clear(BLACK); 6 | M5.Lcd.setTextColor(YELLOW); 7 | M5.Lcd.setTextSize(2); 8 | M5.Lcd.setCursor(65, 10); 9 | M5.Lcd.println("MIDI Module2"); 10 | M5.Lcd.setCursor(3, 35); 11 | M5.Lcd.println("Sync Test"); 12 | M5.Lcd.println("A:Slow B:Fast"); 13 | M5.Lcd.setTextColor(RED); 14 | pinMode(5, OUTPUT); 15 | pinMode(2, OUTPUT); 16 | digitalWrite(2, LOW); 17 | } 18 | 19 | int count; 20 | int tempo = 80000; 21 | int tempoo = tempo; 22 | 23 | void loop() { 24 | M5.update(); 25 | if (count > tempo) { 26 | digitalWrite(5, HIGH); 27 | M5.Lcd.setTextColor(RED); 28 | M5.Lcd.setCursor(150, 120); 29 | M5.Lcd.println("O"); 30 | M5.update(); 31 | 32 | delay(50); 33 | digitalWrite(5, LOW); 34 | M5.Lcd.setTextColor(BLACK); 35 | 36 | M5.Lcd.setCursor(150, 120); 37 | M5.Lcd.println("O"); 38 | M5.update(); 39 | 40 | count = 0; 41 | } 42 | else count ++; 43 | 44 | if (M5.BtnA.wasReleased()) { 45 | tempo = tempo + 10000; 46 | } 47 | else if (M5.BtnB.wasReleased()) { 48 | tempo = tempo - 10000; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Case-3Ddata/m5stackmidi1.6mm.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/necobit/M5Stack-MIDI-Module/3e8d713115ec434e45967656a841eba9f527afba/Case-3Ddata/m5stackmidi1.6mm.stl -------------------------------------------------------------------------------- /Case-3Ddata/m5stackmidiDIN5.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/necobit/M5Stack-MIDI-Module/3e8d713115ec434e45967656a841eba9f527afba/Case-3Ddata/m5stackmidiDIN5.stl -------------------------------------------------------------------------------- /Case-3Ddata/m5stackmidiDIN5top.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/necobit/M5Stack-MIDI-Module/3e8d713115ec434e45967656a841eba9f527afba/Case-3Ddata/m5stackmidiDIN5top.stl -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 necobit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # M5Stack-MIDI-Module 2 | M5Stack-MIDI-Module 3 | ArduinoのスケッチはMIDIライブラリが必要です。 4 | https://playground.arduino.cc/Main/MIDILibrary 5 | m5stackmidi1.6mm.stlとm5stackmidiDIN5.stlはY軸を180度反転させた方が綺麗にプリントできます。 6 | --------------------------------------------------------------------------------