-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageDecoder.py
67 lines (52 loc) · 2.13 KB
/
imageDecoder.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
import sys
import pretty_midi
import math
import random
from PIL import Image, ImageDraw
# Settings ----------------
filename = 'images/testWil1.png'
if (len(sys.argv) > 1):
filename = sys.argv[1]
# The resolution and the tick scales are given in the default pretty midi file
# Unless resolution is specified in the Setting
resolution = -1
if (len(sys.argv) > 2):
resolution = int(sys.argv[2])
tick_duration = -1
if (len(sys.argv) > 3):
tick_duration = float(sys.argv[3])
pixel_res = 8
if (len(sys.argv) > 4):
pixel_res = int(sys.argv[4])
# Settings ----------------
midi_data = pretty_midi.PrettyMIDI()
resolution = midi_data.resolution if resolution == -1 else resolution# how many ticks in a beat
tick_duration = midi_data._tick_scales[0][1] if tick_duration == -1 else tick_duration# Get tick duration from the first tempo signature - we assume the tempo to be uniform
audio_res = resolution / pixel_res * tick_duration # how long in seconds does a pixel lasts
midi_data.resolution = resolution
midi_data._tick_scales[0] = (midi_data._tick_scales[0][0], tick_duration)
img = Image.open(filename)
x, y = img.size
for y_iter in range(y):
# Create Instrument
inst_prog = pretty_midi.instrument_name_to_program('Cello')
instm = pretty_midi.Instrument(program=inst_prog)
# Start reading the note image file
x_iter = 0
while (x_iter < x):
pix = img.getpixel((x_iter, y_iter))
pitch = pix[0] # TODO: change this scheme if note mapping changes
start_time = x_iter * audio_res # start time with pixel resolution
if (pitch != 0):
# Note found
# Loop through x_iter until the current pitch ends
while (x_iter < x and img.getpixel((x_iter, y_iter))[0] == pitch):
x_iter += 1
end_time = x_iter * audio_res
print((pitch, start_time, end_time))
note = pretty_midi.Note(velocity=100, pitch=(pitch - 24), start=start_time, end=end_time)
instm.notes.append(note)
x_iter -= 1
x_iter += 1
midi_data.instruments.append(instm)
midi_data.write("midi_outputs/output.mid")