0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.midのドラムのマッピング変換スクリプト

0
Posted at

.mid内のドラムのマッピングを変更するスクリプトを作成。

TuxGuitar等で作成した.midのドラムのマッピングをAddictive Drumsのmetal等に対応した番号へ変更するスクリプトを作成。

プロジェクトのフォルダにて下記のようなコマンドを実行。
権限変更
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
csv-parse追加
npm install @tonejs/midi csv-parse

使い方

node midi_mapping_converter.js mapping.csv input.mid output.mid

  • mapping.csv ... 変更前の番号、変更後の番号のcsv
  • input.mid ... 入力に使用する任意のmidiファイル
  • output.mid ... 出力名
midi_mapping_converter.js
// midi_mapping_converter.js
import { readFileSync, writeFileSync } from "fs";
import pkg from "@tonejs/midi";
import { parse } from "csv-parse/sync";

const { Midi } = pkg;

// ===== 引数 =====
const mappingFile = process.argv[2];
const inputFile   = process.argv[3];
const outputFile  = process.argv[4];

if (!mappingFile || !inputFile || !outputFile) {
  console.error("使い方: node midi_mapping_converter.js mapping.csv input.mid output.mid");
  process.exit(1);
}

// ===== CSV 読み込み =====
let csv = readFileSync(mappingFile, "utf8");

// --- BOM削除 ---
csv = csv.replace(/^\uFEFF/, "");

// --- コメント削除 (# 以降を削除) ---
csv = csv
  .split("\n")
  .map(line => line.split("#")[0].trim()) // ← コメント除去
  .filter(line => line.length > 0)        // ← 空行除去
  .join("\n");

// --- CSV パース ---
const rows = parse(csv, { columns: true });

// --- 変換テーブル作成 ---
const mapTable = {};
rows.forEach(r => {
  const from = Number(r.from.trim());
  const to   = Number(r.to.trim());
  mapTable[from] = to;
});

// ===== MIDI 読み込み =====
const midi = new Midi(readFileSync(inputFile));

// ===== 全トラックのノートを変換 =====
midi.tracks.forEach((track, ti) => {
  track.notes.forEach(note => {
    const before = note.midi;
    const after  = mapTable[before];

    if (after === 0) {
      note.midi = null; // 削除
    } else if (after !== undefined) {
      note.midi = after; // 変換
    }
  });

  track.notes = track.notes.filter(n => n.midi !== null);
});

// ===== 書き出し =====
writeFileSync(outputFile, Buffer.from(midi.toArray()));

console.log("変換完了 → " + outputFile);

サンプルcsv

Tux guitarで作成したドラムをAddictive drumsのMetalに変換するcsv

mapping.csv
from,to
33,36   # Kick
37,75   # Sticks
38,38   # Snare Open Hit
40,43   # Snare Shallow Hit
42,49   # Closed HH → Closed1 Tip
44,59   # Pedal HH → Foot Splash
46,55   # Open HH → Open B
51,60   # Ride → Ride Tip
53,61   # Ride Bell → Ride Bell
59,62   # Ride Bell → Ride Bell
49,77   # Crash → Cymbal 1
55,79   # Crash2 → Cymbal 2
57,81   # Crash2 → Cymbal 2
41,65   # Low Tom → Tom1
43,66   # Low Tom → Tom1
45,67   # Low Tom → Tom1
47,68   # Mid Tom → Tom2
48,69   # High Tom → Tom3
50,71   # Floor Tom → Tom4
24,0    # GM2 FX → 削除
25,0    # GM2 FX → 削除
26,38   # GM2 Snare → Snare Open Hit に寄せる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?