Rename vidi-player.py to vidi-timeline.py
[vidi-player.git] / vidi-timeline.py
1 #!/usr/bin/env python3
2 #
3 # vidi-timeline - generate GStreamer Editing Services timelines from midi
4 #
5 # Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sys
22 import mido
23 import vidi
24
25 # TODO: turn that into a command line option
26 ADD_REST_BACKGROUND = True
27
28
29 def is_note(msg):
30     return msg.type == 'note_on' or msg.type == 'note_off'
31
32
33 def is_note_on(msg):
34     return msg.type == 'note_on' and msg.velocity > 0
35
36
37 def is_note_off(msg):
38     return ((msg.type == 'note_on' and msg.velocity == 0) or
39             (msg.type == 'note_off'))
40
41
42 def check_overlapping_notes(midi_file):
43     previous_note_on = False
44     for msg in midi_file:
45         if is_note_on(msg) and msg.channel == 0:
46             if previous_note_on:
47                 return True
48
49             previous_note_on = True
50         elif is_note_off(msg) and msg.channel == 0:
51             previous_note_on = False
52
53     return False
54
55
56 def timeline_from_midi(midi_file, video_font_path):
57     timeline = vidi.Timeline()
58
59     elapsed_time = 0
60     start_time = 0
61     for msg in midi_file:
62         elapsed_time += msg.time
63         if is_note_on(msg) and msg.channel == 0:
64             start_time = elapsed_time
65         elif is_note_off(msg) and msg.channel == 0:
66             note = vidi.MidiNote(msg.note)
67             duration = elapsed_time - start_time
68             print("Note name: %3s start_time: %f duration: %f" %
69                   (note.name, start_time, duration))
70
71             video_sample_path = "%s/sample_%s.webm" % (video_font_path, note.name)
72
73             timeline.add_clip(video_sample_path, start_time, duration)
74
75     if ADD_REST_BACKGROUND:
76         rest_sample_path = "%s/sample_rest.png" % video_font_path
77         timeline.add_layer_clip(rest_sample_path, 0, elapsed_time)
78
79     return timeline
80
81
82 def usage():
83     print("usage: %s <midi_file> <videofont_directory> [<destination_file>]"
84           % os.path.basename(sys.argv[0]))
85
86
87 def main():
88     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
89         usage()
90         return 0
91
92     if len(sys.argv) < 3:
93         usage()
94         return 1
95
96     if not os.path.isdir(sys.argv[2]):
97         sys.stderr.write("The second argument must be the path of the videofont directory\n")
98         usage()
99         return 1
100
101     if len(sys.argv) > 3 and os.path.exists(sys.argv[3]):
102         sys.stderr.write("File '%s' exists, exiting!\n" % sys.argv[3])
103         return 1
104
105     midi_file = mido.MidiFile(sys.argv[1])
106
107     overlapping_notes = check_overlapping_notes(midi_file)
108     if overlapping_notes:
109         sys.stderr.write("Sorry, supporting only midi file with no overlapping notes on channel 0\n")
110         return 1
111
112     video_font_path = os.path.realpath(sys.argv[2])
113
114     timeline = timeline_from_midi(midi_file, video_font_path)
115
116     if len(sys.argv) > 3:
117         timeline.save(sys.argv[3])
118     else:
119         try:
120             timeline.play()
121         except KeyboardInterrupt:
122             timeline.stop()
123             return 1
124
125
126 if __name__ == "__main__":
127     sys.exit(main())