Spaces:
Running
on
Zero
Running
on
Zero
drscotthawley
commited on
Commit
β’
b1e308f
1
Parent(s):
288c3b4
dummy dir pom
Browse files- app.py +2 -2
- pom/__init__.py +1 -0
- pom/extract_chords.py +72 -0
- img2midi.py β pom/img2midi.py +0 -0
- midi2img.py β pom/midi2img.py +0 -0
- pianoroll.py β pom/pianoroll.py +0 -0
- rect_to_square.py β pom/rect_to_square.py +0 -0
- square_to_rect.py β pom/square_to_rect.py +0 -0
- pom/transpose_midi.py +64 -0
app.py
CHANGED
@@ -25,8 +25,8 @@ from torchvision import transforms
|
|
25 |
import k_diffusion as K
|
26 |
|
27 |
|
28 |
-
from .pianoroll import regroup_lines, img_file_2_midi_file, square_to_rect, rect_to_square
|
29 |
-
from .square_to_rect import square_to_rect
|
30 |
|
31 |
|
32 |
|
|
|
25 |
import k_diffusion as K
|
26 |
|
27 |
|
28 |
+
from pom.pianoroll import regroup_lines, img_file_2_midi_file, square_to_rect, rect_to_square
|
29 |
+
from pom.square_to_rect import square_to_rect
|
30 |
|
31 |
|
32 |
|
pom/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
pom/extract_chords.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#! /usr/bin/env python3
|
2 |
+
|
3 |
+
# this executes the chord extractor from polyffusion on a list of midi files
|
4 |
+
import sys
|
5 |
+
import os
|
6 |
+
import sys
|
7 |
+
import pretty_midi
|
8 |
+
from multiprocessing import Pool, cpu_count, set_start_method
|
9 |
+
from tqdm import tqdm
|
10 |
+
from control_toys.data import fast_scandir
|
11 |
+
from functools import partial
|
12 |
+
import argparse
|
13 |
+
|
14 |
+
chord_extractor_path = '/Users/shawley/github/polyffusion/polyffusion/chord_extractor'
|
15 |
+
# add chord extract path to python sys
|
16 |
+
sys.path.append(chord_extractor_path)
|
17 |
+
|
18 |
+
from chord_class import ChordClass
|
19 |
+
|
20 |
+
#try:
|
21 |
+
from chord_extractor.main import transcribe_cb1000_midi
|
22 |
+
chord_extractor_available = True
|
23 |
+
#except:
|
24 |
+
# chord_extractor_available = False
|
25 |
+
|
26 |
+
|
27 |
+
def extract_chords(args, midi_file):
|
28 |
+
"this will read in one midi file and estimate the chords"
|
29 |
+
chords_path = midi_file.replace('.mid', '_chords.txt')
|
30 |
+
try:
|
31 |
+
transcribe_cb1000_midi(midi_file, chords_path)
|
32 |
+
except Exception as e:
|
33 |
+
print(f"\nError processing {midi_file}: {e}. Skipping")
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
if __name__ == '__main__':
|
38 |
+
p = argparse.ArgumentParser(description=__doc__,
|
39 |
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
40 |
+
p.add_argument('--start-method', type=str, default='fork',
|
41 |
+
choices=['fork', 'forkserver', 'spawn'],
|
42 |
+
help='the multiprocessing start method')
|
43 |
+
p.add_argument('--skip-versions', default=True, help='skip extra versions of the same song')
|
44 |
+
p.add_argument("midi_dirs", nargs='+', help="directories containing MIDI files")
|
45 |
+
args = p.parse_args()
|
46 |
+
print("args = ",args)
|
47 |
+
|
48 |
+
if not chord_extractor_available:
|
49 |
+
print("Error: chord_extractor not available. Please install it, e.g. via 'ln -s ~/diffusion/polyffusion/polyffusion/chord_extractor .'")
|
50 |
+
sys.exit(1)
|
51 |
+
|
52 |
+
set_start_method(args.start_method)
|
53 |
+
midi_dirs = args.midi_dirs
|
54 |
+
|
55 |
+
if os.path.isdir(midi_dirs[0]):
|
56 |
+
midi_files = []
|
57 |
+
for mdir in midi_dirs:
|
58 |
+
m_subdirs, mf = fast_scandir(mdir, ['mid', 'midi'])
|
59 |
+
if mf != []: midi_files = midi_files + mf
|
60 |
+
elif os.path.isfile(midi_dirs[0]):
|
61 |
+
midi_files = midi_dirs
|
62 |
+
|
63 |
+
if args.skip_versions:
|
64 |
+
midi_files = [f for f in midi_files if '/versions/' not in f]
|
65 |
+
#print("midi_files = ",midi_files) # just a check for debugging
|
66 |
+
|
67 |
+
extract_one = partial(extract_chords, args)
|
68 |
+
with Pool(cpu_count()) as p:
|
69 |
+
# TODO: how to get functions called from p.imap to return values?
|
70 |
+
list(tqdm(p.imap(extract_one, midi_files), total=len(midi_files), desc='Extracting chords from MIDI files'))
|
71 |
+
|
72 |
+
print("Finished")
|
img2midi.py β pom/img2midi.py
RENAMED
File without changes
|
midi2img.py β pom/midi2img.py
RENAMED
File without changes
|
pianoroll.py β pom/pianoroll.py
RENAMED
File without changes
|
rect_to_square.py β pom/rect_to_square.py
RENAMED
File without changes
|
square_to_rect.py β pom/square_to_rect.py
RENAMED
File without changes
|
pom/transpose_midi.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#! /usr/bin/env python3
|
2 |
+
|
3 |
+
# This script takes a directory of MIDI files and transposes them between -max and +max semitones
|
4 |
+
# It was intended for the P909 dataset
|
5 |
+
# It will create a directory of images for each MIDI file, where each image is a frame of the MIDI file
|
6 |
+
|
7 |
+
import os
|
8 |
+
import sys
|
9 |
+
import pretty_midi
|
10 |
+
from multiprocessing import Pool, cpu_count, set_start_method
|
11 |
+
from tqdm import tqdm
|
12 |
+
from control_toys.data import fast_scandir
|
13 |
+
from functools import partial
|
14 |
+
import argparse
|
15 |
+
from copy import deepcopy
|
16 |
+
|
17 |
+
|
18 |
+
def transpose_midi(args, midi_file):
|
19 |
+
#print("midi_file = ",midi_file)
|
20 |
+
if '_transposed_' in midi_file: return # don't transpose files that have already been transposed
|
21 |
+
midi_save = pretty_midi.PrettyMIDI(midi_file)
|
22 |
+
for transpose_by in range(-args.transpose, args.transpose+1):
|
23 |
+
midi = deepcopy(midi_save)
|
24 |
+
if transpose_by == 0: continue
|
25 |
+
for instrument in midi.instruments:
|
26 |
+
for note in instrument.notes:
|
27 |
+
note.pitch += transpose_by
|
28 |
+
midi.write(midi_file.replace('.mid', f'_transposed_{transpose_by}.mid'))
|
29 |
+
return
|
30 |
+
|
31 |
+
if __name__ == '__main__':
|
32 |
+
p = argparse.ArgumentParser(description=__doc__,
|
33 |
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
34 |
+
p.add_argument('-t','--transpose', type=int, default=0, help='transpose by this maximum number of semitones (+/-)')
|
35 |
+
p.add_argument('--start-method', type=str, default='fork',
|
36 |
+
choices=['fork', 'forkserver', 'spawn'],
|
37 |
+
help='the multiprocessing start method')
|
38 |
+
p.add_argument('--skip-versions', default=True, help='skip extra versions of the same song')
|
39 |
+
p.add_argument("midi_dirs", nargs='+', help="directories containing MIDI files")
|
40 |
+
args = p.parse_args()
|
41 |
+
print("args = ",args)
|
42 |
+
|
43 |
+
set_start_method(args.start_method)
|
44 |
+
midi_dirs = args.midi_dirs
|
45 |
+
|
46 |
+
if os.path.isdir(midi_dirs[0]):
|
47 |
+
midi_files = []
|
48 |
+
for mdir in midi_dirs:
|
49 |
+
m_subdirs, mf = fast_scandir(mdir, ['mid', 'midi'])
|
50 |
+
if mf != []: midi_files = midi_files + mf
|
51 |
+
elif os.path.isfile(midi_dirs[0]):
|
52 |
+
midi_files = midi_dirs
|
53 |
+
|
54 |
+
if args.skip_versions:
|
55 |
+
midi_files = [f for f in midi_files if '/versions/' not in f]
|
56 |
+
#print("midi_files = ",midi_files) # just a check for debugging
|
57 |
+
|
58 |
+
# transpose all the midi files
|
59 |
+
if args.transpose != 0:
|
60 |
+
transpose_one = partial(transpose_midi, args)
|
61 |
+
with Pool(cpu_count()) as p:
|
62 |
+
list(tqdm(p.imap(transpose_one, midi_files), total=len(midi_files), desc='Transposing MIDI files'))
|
63 |
+
|
64 |
+
print("Finished")
|