Ejemplo n.º 1
0
class MidiFileLink(TextFileCompareLink):
    def get_content(self, name):
        import midi

        try:
            f = open(name, 'rb')
        except IOError, e:
            if e.errno == errno.ENOENT:
                return None
            else:
                raise

        data = f.read()
        midi = midi.parse(data)
        tracks = midi[1]

        str = ''
        j = 0
        for t in tracks:
            str += 'track %d' % j
            j += 1

            for e in t:
                ev_str = repr(e)
                if re.search('LilyPond [0-9.]+', ev_str):
                    continue

                str += '  ev %s\n' % ` e `
        return str
Ejemplo n.º 2
0
    def get_content(self, name):
        try:
            f = open(name, 'rb')
        except IOError as e:
            if e.errno == errno.ENOENT:
                return None
            else:
                raise

        data = f.read()
        midi_data = midi.parse(data)
        tracks = midi_data[1]

        str = ''
        j = 0
        for t in tracks:
            str += 'track %d' % j
            j += 1

            for e in t:
                ev_str = repr(e)
                if re.search('LilyPond [0-9.]+', ev_str):
                    continue

                str += '  ev %s\n' % repr(e)
        return str
Ejemplo n.º 3
0
def convert_midi (in_file, out_file):
    global clocks_per_1, clocks_per_4, key
    global start_quant_clocks
    global  duration_quant_clocks
    global allowed_tuplet_clocks

    str = open (in_file).read ()
    midi_dump = midi.parse (str)
    
    clocks_per_1 = midi_dump[0][1]
    clocks_per_4 = clocks_per_1 / 4
    
    if global_options.start_quant:
        start_quant_clocks = clocks_per_1 / global_options.start_quant

    if global_options.duration_quant:
        duration_quant_clocks = clocks_per_1 / global_options.duration_quant

    allowed_tuplet_clocks = []
    for (dur, num, den) in global_options.allowed_tuplets:
        allowed_tuplet_clocks.append (clocks_per_1 / den)

    tracks = []
    for t in midi_dump[1]:
        global_options.key = Key (0, 0, 0)
        tracks.append (split_track (t))

    tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, in_file)

    
    s = ''
    s = tag + '\n\\version "2.7.18"\n\n'
    for i in range (len (tracks)):
        s = s + dump_track (tracks[i], i)

    s = s + '\n\\score {\n  <<\n'
    
    i = 0
    for t in tracks:
        track = track_name (i)
        item = track_first_item (t)
        
        if item and item.__class__ == Note:
            s = s + '    \\context Staff=%s \\%s\n' % (track, track)
        elif item and item.__class__ == Text:
            s = s + '    \\context Lyrics=%s \\%s\n' % (track, track)

        i += 1
    s = s + '  >>\n}\n'

    progress (_ ("%s output to `%s'...") % ('LY', out_file))

    if out_file == '-':
        handle = sys.stdout
    else:
        handle = open (out_file, 'w')

    handle.write (s)
    handle.close ()
Ejemplo n.º 4
0
    def get_content(self, oldnew):
        import midi

        data = FileCompareLink.get_content(self, oldnew)
        midi = midi.parse(data)
        tracks = midi[1]

        str = ''
        j = 0
        for t in tracks:
            str += 'track %d' % j
            j += 1

            for e in t:
                ev_str = repr(e)
                if re.search('LilyPond [0-9.]+', ev_str):
                    continue

                str += '  ev %s\n' % ` e `
        return str
Ejemplo n.º 5
0
    def get_content (self, oldnew):
        import midi
        
        data = FileCompareLink.get_content (self, oldnew)
        midi = midi.parse (data)
        tracks = midi[1]

        str = ''
        j = 0
        for t in tracks:
            str += 'track %d' % j
            j += 1

            for e in t:
                ev_str = repr (e)
                if re.search ('LilyPond [0-9.]+', ev_str):
                    continue
                
                str += '  ev %s\n' % `e`
        return str
Ejemplo n.º 6
0
def read_midi(file):
    import midi
    return midi.parse(open(file).read())
Ejemplo n.º 7
0
def convert_midi(in_file, out_file):
    global midi
    import midi

    global clocks_per_1, clocks_per_4, key
    global start_quant_clocks
    global duration_quant_clocks
    global allowed_tuplet_clocks
    global time

    str = open(in_file, 'rb').read()
    clocks_max = bar_max * clocks_per_1 * 2
    midi_dump = midi.parse(str, clocks_max)

    clocks_per_1 = midi_dump[0][1]
    clocks_per_4 = clocks_per_1 / 4
    time = Time(4, 4)

    if global_options.start_quant:
        start_quant_clocks = clocks_per_1 / global_options.start_quant

    if global_options.duration_quant:
        duration_quant_clocks = clocks_per_1 / global_options.duration_quant

    allowed_tuplet_clocks = []
    for (dur, num, den) in global_options.allowed_tuplets:
        allowed_tuplet_clocks.append(clocks_per_1 / dur * num / den)

    if global_options.verbose:
        print('allowed tuplet clocks:', allowed_tuplet_clocks)

    tracks = [create_track(t) for t in midi_dump[1]]
    # urg, parse all global track events, such as Key first
    # this fixes key in different voice/staff problem
    for t in tracks:
        t.music = t.parse()
    prev = None
    staves = []
    for t in tracks:
        voices = t.get_voices()
        if ((t.name and prev and prev.name)
                and t.name.split(':')[0] == prev.name.split(':')[0]):
            # staves[-1].voices += voices
            # all global track events first
            staves[-1].voices = ([staves[-1].voices[0]] + [voices[0]] +
                                 staves[-1].voices[1:] + voices[1:])
        else:
            staves.append(Staff(t))
        prev = t

    tag = '%% Lily was here -- automatically converted by %s from %s' % (
        program_name, in_file)

    s = tag
    s += r'''
\version "2.14.0"
'''

    s += r'''
\layout {
  \context {
    \Voice
    \remove "Note_heads_engraver"
    \consists "Completion_heads_engraver"
    \remove "Rest_engraver"
    \consists "Completion_rest_engraver"
  }
}
'''

    for i in global_options.include_header:
        s += '\n%% included from %(i)s\n' % locals()
        s += open(i).read()
        if s[-1] != '\n':
            s += '\n'
        s += '% end\n'

    for i, t in enumerate(staves):
        s += t.dump(i)

    s += '\n\\score {\n  <<\n'

    control_track = False
    i = 0
    for i, staff in enumerate(staves):
        track_name = get_track_name(i)
        item = track_first_item(staff.voices)
        staff_name = track_name
        context = None
        if not i and not item and len(staves) > 1:
            control_track = track_name
            continue
        elif (item and item.__class__ == Note):
            context = 'Staff'
            if control_track:
                s += '    \\context %(context)s=%(staff_name)s \\%(control_track)s\n' % locals(
                )
        elif item and item.__class__ == Text:
            context = 'Lyrics'
        if context:
            s += '    \\context %(context)s=%(staff_name)s \\%(track_name)s\n' % locals(
            )

    s = s + '''  >>
  \layout {}
  \midi {}
}
'''

    if not global_options.quiet:
        progress(_("%s output to `%s'...") % ('LY', out_file))

    if out_file == '-':
        handle = sys.stdout
    else:
        handle = open(out_file, 'w')

    handle.write(s)
    handle.close()
Ejemplo n.º 8
0
def read_midi (file):
    import midi
    return midi.parse (open (file).read ())
Ejemplo n.º 9
0
def convert_midi (in_file, out_file):
    global midi
    import midi

    global clocks_per_1, clocks_per_4, key
    global start_quant_clocks
    global duration_quant_clocks
    global allowed_tuplet_clocks
    global time

    str = open (in_file, 'rb').read ()
    clocks_max = bar_max * clocks_per_1 * 2
    midi_dump = midi.parse (str, clocks_max)

    clocks_per_1 = midi_dump[0][1]
    clocks_per_4 = clocks_per_1 / 4
    time = Time (4, 4)

    if global_options.start_quant:
        start_quant_clocks = clocks_per_1 / global_options.start_quant

    if global_options.duration_quant:
        duration_quant_clocks = clocks_per_1 / global_options.duration_quant

    allowed_tuplet_clocks = []
    for (dur, num, den) in global_options.allowed_tuplets:
        allowed_tuplet_clocks.append (clocks_per_1 / dur * num / den)

    if global_options.verbose:
        print 'allowed tuplet clocks:', allowed_tuplet_clocks

    tracks = [create_track (t) for t in midi_dump[1]]
    # urg, parse all global track events, such as Key first
    # this fixes key in different voice/staff problem
    for t in tracks:
        t.music = t.parse ()
    prev = None
    staves = []
    for t in tracks:
        voices = t.get_voices ()
        if ((t.name and prev and prev.name)
            and t.name.split (':')[0] == prev.name.split (':')[0]):
            # staves[-1].voices += voices
            # all global track events first
            staves[-1].voices = ([staves[-1].voices[0]]
                                 + [voices[0]]
                                 + staves[-1].voices[1:]
                                 + voices[1:])
        else:
            staves.append (Staff (t))
        prev = t

    tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, in_file)


    s = tag
    s += r'''
\version "2.14.0"
'''

    s += r'''
\layout {
  \context {
    \Voice
    \remove "Note_heads_engraver"
    \consists "Completion_heads_engraver"
    \remove "Rest_engraver"
    \consists "Completion_rest_engraver"
  }
}
'''

    for i in global_options.include_header:
        s += '\n%% included from %(i)s\n' % locals ()
        s += open (i).read ()
        if s[-1] != '\n':
            s += '\n'
        s += '% end\n'

    for i, t in enumerate (staves):
        s += t.dump (i)

    s += '\n\\score {\n  <<\n'

    control_track = False
    i = 0
    for i, staff in enumerate (staves):
        track_name = get_track_name (i)
        item = track_first_item (staff.voices)
        staff_name = track_name
        context = None
        if not i and not item and len (staves) > 1:
            control_track = track_name
            continue
        elif (item and item.__class__ == Note):
            context = 'Staff'
            if control_track:
                s += '    \\context %(context)s=%(staff_name)s \\%(control_track)s\n' % locals ()
        elif item and item.__class__ == Text:
            context = 'Lyrics'
        if context:
            s += '    \\context %(context)s=%(staff_name)s \\%(track_name)s\n' % locals ()

    s = s + '''  >>
  \layout {}
  \midi {}
}
'''

    if not global_options.quiet:
        progress (_ ("%s output to `%s'...") % ('LY', out_file))

    if out_file == '-':
        handle = sys.stdout
    else:
        handle = open (out_file, 'w')

    handle.write (s)
    handle.close ()
Ejemplo n.º 10
0
#!@PYTHON@

import sys
import midi

(h, tracks) = midi.parse(open(sys.argv[1]).read())

tracks = tracks[1:]

for t in tracks:
    for e in t:
        print e
Ejemplo n.º 11
0
#!@PYTHON@

import sys
import midi

(h,tracks) = midi.parse (open (sys.argv[1]).read ())

tracks = tracks[1:]

for t in tracks:
    for e in t:
        print e