private void SetDelta(int ch, midiChip chip, long NewFrameCounter) { if (ch >= chip.oldFrameCounter.Length) { return; } long sub = NewFrameCounter - chip.oldFrameCounter[ch]; long step = (long)(sub / (double)Common.SampleRate * 960.0); chip.oldFrameCounter[ch] += (long)(step * (double)Common.SampleRate / 960.0); bool flg = true; for (int i = 0; i < 4; i++) { byte d = (byte)((step & (0x0fe00000 >> (7 * i))) >> (21 - 7 * i)); if (flg && d == 0 && i < 3) { continue; } flg = false; d |= (byte)((i != 3) ? 0x80 : 0x00); chip.data[ch].Add(d); } }
private void InitYM2151() { midi2151 = new midiChip(); midi2151.maxTrk = 8; midi2151.oldCode = new int[midi2151.maxTrk]; midi2151.oldFreq = new int[midi2151.maxTrk]; midi2151.data = new List <byte> [midi2151.maxTrk]; midi2151.oldFrameCounter = new long[midi2151.maxTrk]; for (int i = 0; i < midi2151.maxTrk; i++) { midi2151.oldCode[i] = -1; midi2151.oldFreq[i] = -1; midi2151.data[i] = new List <byte>(); midi2151.oldFrameCounter[i] = 0L; } for (int i = 0; i < midi2151.maxTrk; i++) { midi2151.data[i].Add(0x4d); //チャンクタイプ'MTrk' midi2151.data[i].Add(0x54); midi2151.data[i].Add(0x72); midi2151.data[i].Add(0x6b); midi2151.data[i].Add(0x00); //データ長 この時点では不明のためとりあえず0 midi2151.data[i].Add(0x00); midi2151.data[i].Add(0x00); midi2151.data[i].Add(0x00); midi2151.data[i].Add(0x00); //delta0 midi2151.data[i].Add(0xff); // メタイベントポート指定 midi2151.data[i].Add(0x21); midi2151.data[i].Add(0x01); midi2151.data[i].Add(0x00); //Port1 midi2151.data[i].Add(0x00); //delta0 midi2151.data[i].Add(0xff); // メタイベント トラック名 midi2151.data[i].Add(0x03); midi2151.data[i].Add(0x00); } if (!setting.midiExport.UseVOPMex) { return; } //VOPMex向け for (int i = 0; i < midi2151.maxTrk; i++) { //音色コントロールの動作を変更(全MIDIチャンネル) midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 121 127 midi2151.data[i].Add(121); midi2151.data[i].Add(127); midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 126 127 midi2151.data[i].Add(126); midi2151.data[i].Add(127); midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 123 127 midi2151.data[i].Add(123); midi2151.data[i].Add(127); midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 98 127 midi2151.data[i].Add(98); midi2151.data[i].Add(127); midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 99 126 midi2151.data[i].Add(99); midi2151.data[i].Add(126); midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 6 127 midi2151.data[i].Add(6); midi2151.data[i].Add(127); midi2151.data[i].Add(0x50); //Delta 0 midi2151.data[i].Add((byte)(0xb0 + i)); // CC 93 120 midi2151.data[i].Add(93); midi2151.data[i].Add(120); } }
public void Close() { if (!setting.midiExport.UseMIDIExport) { return; } if (setting.midiExport.ExportPath == "") { return; } if (cData == null) { return; } //if (midiData.Count < 23) return; int portNum = 0; int trkNum = 1; if (setting.midiExport.UseYM2151Export) { for (int ch = 0; ch < midi2151.maxTrk; ch++) { SetDelta(ch, midi2151, midi2151.oldFrameCounter[ch]); if (midi2151.oldCode[ch] >= 0) { midi2151.data[ch].Add((byte)(0x80 | ch)); midi2151.data[ch].Add((byte)midi2151.oldCode[ch]); midi2151.data[ch].Add(0x00); midi2151.oldCode[ch] = -1; midi2151.data[ch].Add(0x00);//Delta 0 } midi2151.data[ch].Add(0xff); //メタイベント midi2151.data[ch].Add(0x2f); midi2151.data[ch].Add(0x00); int MTrkLengthAdr = 0x04; midi2151.data[ch][MTrkLengthAdr + 0] = (byte)(((midi2151.data[ch].Count - (MTrkLengthAdr + 4)) & 0xff000000) >> 24); midi2151.data[ch][MTrkLengthAdr + 1] = (byte)(((midi2151.data[ch].Count - (MTrkLengthAdr + 4)) & 0x00ff0000) >> 16); midi2151.data[ch][MTrkLengthAdr + 2] = (byte)(((midi2151.data[ch].Count - (MTrkLengthAdr + 4)) & 0x0000ff00) >> 8); midi2151.data[ch][MTrkLengthAdr + 3] = (byte)(((midi2151.data[ch].Count - (MTrkLengthAdr + 4)) & 0x000000ff) >> 0); int PortAdr = 0x08; midi2151.data[ch][PortAdr + 4] = (byte)portNum; } portNum++; trkNum += midi2151.maxTrk; } if (setting.midiExport.UseYM2612Export) { for (int ch = 0; ch < midi2612.maxTrk; ch++) { SetDelta(ch, midi2612, midi2612.oldFrameCounter[ch]); if (midi2612.oldCode[ch] >= 0) { midi2612.data[ch].Add((byte)(0x80 | ch)); midi2612.data[ch].Add((byte)midi2612.oldCode[ch]); midi2612.data[ch].Add(0x00); midi2612.oldCode[ch] = -1; midi2612.data[ch].Add(0x00);//Delta 0 } midi2612.data[ch].Add(0xff); //メタイベント midi2612.data[ch].Add(0x2f); midi2612.data[ch].Add(0x00); int MTrkLengthAdr = 0x04; midi2612.data[ch][MTrkLengthAdr + 0] = (byte)(((midi2612.data[ch].Count - (MTrkLengthAdr + 4)) & 0xff000000) >> 24); midi2612.data[ch][MTrkLengthAdr + 1] = (byte)(((midi2612.data[ch].Count - (MTrkLengthAdr + 4)) & 0x00ff0000) >> 16); midi2612.data[ch][MTrkLengthAdr + 2] = (byte)(((midi2612.data[ch].Count - (MTrkLengthAdr + 4)) & 0x0000ff00) >> 8); midi2612.data[ch][MTrkLengthAdr + 3] = (byte)(((midi2612.data[ch].Count - (MTrkLengthAdr + 4)) & 0x000000ff) >> 0); int PortAdr = 0x08; midi2612.data[ch][PortAdr + 4] = (byte)portNum; } portNum++; trkNum += midi2612.maxTrk; } cData[0xb] = (byte)trkNum;//トラック数 try { string fn = PlayingFileName == "" ? "Temp.mid" : PlayingFileName; List <byte> buf = new List <byte>(); foreach (byte d in cData) { buf.Add(d); } if (setting.midiExport.UseYM2151Export) { foreach (List <byte> dat in midi2151.data) { foreach (byte d in dat) { buf.Add(d); } } } if (setting.midiExport.UseYM2612Export) { foreach (List <byte> dat in midi2612.data) { foreach (byte d in dat) { buf.Add(d); } } } File.WriteAllBytes(Path.Combine(setting.midiExport.ExportPath, Path.ChangeExtension(Path.GetFileName(fn), ".mid")), buf.ToArray()); } catch { } cData = null; midi2151 = null; midi2612 = null; }