This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidi-to-dtx.py
executable file
·80 lines (61 loc) · 2.38 KB
/
midi-to-dtx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# coding=utf-8
import argparse
from pathlib import Path
from utils.misc import suffixFilename
import mido
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="*")
parser.add_argument("-t", "--track", dest="tracknumber", metavar="TRACKNUMBER", type=int, help="the number (from 1) of the track to copy")
parser.add_argument("-tn", "--trackname", dest="trackname", metavar="TRACKNAME", help="set the track name")
parser.add_argument("-an", "--artistname", dest="artistname", metavar="ARTISTNAME", help="set the artist name")
args = parser.parse_args()
multiple_files = len(args.files) > 1
track_number = (args.tracknumber - 1) if args.tracknumber else 0
if track_number < 0: track_number = 0
#track_name = args.trackname if args.trackname else None
artist_name = args.artistname if args.artistname else ''
for f in args.files:
mid = mido.MidiFile(f)
if track_number > len(mid.tracks):
print("Invalid track number: file %s has only %d track(s)" % (f, len(mid.tracks)))
continue
# tries to find time signature and tempo:
time_signature = None
set_tempo = None
for m in mid.tracks[0]:
if m.is_meta:
if m.type == 'time_signature':
time_signature = m
break
for m in mid.tracks[0]:
if m.is_meta:
if m.type == 'set_tempo':
set_tempo = m
break
if args.trackname:
track_name = args.trackname
else:
for m in mid.tracks[0]:
if m.is_meta:
if m.type == 'track_name':
track_name = m.name
break
if not track_name:
track_name = 'no name'
new_track = mid.tracks[track_number]
if time_signature:
new_track.insert(0, time_signature)
if set_tempo:
new_track.insert(0, set_tempo)
new_track.name = track_name
# XF track name and artist name
new_track.insert(0, mido.MetaMessage('text', text='XFln:L1:' + track_name + ':Composer:::' + artist_name + ':'))
# XF Version ID
new_track.insert(0, mido.MetaMessage('sequencer_specific', data=[0x43, 0x7B, 0, 0x58, 70, 48, 50, 0, 1]))
new_mid = mido.MidiFile(type=0)
new_mid.tracks.append(new_track)
#nf = Path(f).parent.joinpath(Path(f).stem + '_dtx' + Path(f).suffix)
nf = suffixFilename(f, '_dtx')
print(f, '-->', nf)
new_mid.save(nf)