Exemplo n.º 1
0
def midiwrite(filename, piano_roll, r=(21, 109), dt=0.2, patch=0):
  midi = MidiOutFile(filename)
  midi.header(division=100)
  midi.start_of_track() 
  midi.patch_change(channel=0, patch=patch)
  t = 0
  samples = [i.nonzero()[0] + r[0] for i in piano_roll]

  for i in xrange(len(samples)):
    for f in samples[i]:
      if i==0 or f not in samples[i-1]:
        midi.update_time(t)
        midi.note_on(channel=0, note=f, velocity=90)
        t = 0
    
    t += int(dt*200)

    for f in samples[i]:
      if i==len(samples)-1 or f not in samples[i+1]:
        midi.update_time(t)
        midi.note_off(channel=0, note=f, velocity=0)
        t = 0
      
  midi.update_time(0)
  midi.end_of_track()
  midi.eof()
Exemplo n.º 2
0
    def test_note_on_ShouldHaveCorrectStatusCode(self):
        expected_status_code = 0x90

        midi_msg = note_on('A', 2, 128)
        actual_status_code = midi_msg.status

        self.assertEqual(actual_status_code, expected_status_code)
Exemplo n.º 3
0
def main():
    print "starting...."
    sleep(3)

    packets = [midi.note_on(0, 65, 120),
               midi.note_on(0, 68, 120),
               midi.note_on(0, 80, 120)]
    coremidi.midi_send(h, packets)
    print "note on..."

    sleep(.5)

    packets = [midi.note_aftertouch(0, 65, 60)]
    coremidi.midi_send(h, packets)
    print "note aftertouch 1..."

    sleep(.5)

    packets = [midi.note_aftertouch(0, 65, 30),
               midi.note_aftertouch(0, 68, 60)]
    coremidi.midi_send(h, packets)
    print "note aftertouch 2..."

    sleep(.5)

    packets = [midi.note_aftertouch(0, 65, 10),
               midi.note_aftertouch(0, 68, 30),
               midi.note_aftertouch(0, 80, 60)]
    coremidi.midi_send(h, packets)
    print "note aftertouch 3..."

    sleep(.5)

    packets = [midi.note_off(0, 65, 120),
               midi.note_off(0, 68, 120),
               midi.note_off(0, 80, 120)]
    coremidi.midi_send(h, packets)
    print "note off..."
    sleep(3)
Exemplo n.º 4
0
    def event_to_midi(e, A, base_note_num=24):
        mes = []
        ename = e[0]

        # normalize and (maybe) filter parameters for output
        if ename.startswith('acc'):
            # update filter accelerometer state
            a = A[ename]
            ap = A[ename] = lpf(a, e[2][1])
            # modify event acc event with filtering & MIDI scale
            e = (e[0], (e[1][0], scaleShort(int(a))), (e[2][0], scaleShort(int(ap))))
        else:
            # normal event pressure/control value for MIDI scale
            e = (e[0], (e[1][0], scaleChar(e[1][1])), (e[2][0], scaleChar(e[2][1])))

        if prog_mode and abs(e[1][1] - e[2][1]) < 10:
            return (mes, A)

        if ename in note:
            event_note = base_note_num + note[ename]
            (was_on, prev_pressure) = e[1]
            (is_on, cur_pressure) = e[2]

            if is_on and not was_on:
                mes.append(m.note_on(channel, event_note, cur_pressure))
            elif was_on and not is_on:
                mes.append(m.note_off(channel, event_note, prev_pressure))
            else:
                mes.append(m.note_aftertouch(channel, event_note, cur_pressure))

        if ename in cc:
            (blank, ctrl_val) = e[2]
            mes.append(m.control_change(channel, cc[ename], invert(ctrl_val)))

        if ename in PITCH_WHEEL:
            PITCH_WHEEL[ename] = e[2][1]
            [ph, pl] = PITCH_WHEEL.values()
            if ph and pl:
                mes.append(m.pitch_wheel(channel, invert(ph) * 128 + invert(pl)))

        if ename in PITCH_TWO_SIDE:
            PITCH_TWO_SIDE[ename] = e[2][1]
            [pu, pd] = PITCH_TWO_SIDE.values()
            mes.append(m.pitch_wheel(channel, int(8192 + 32.125 * (pu if pu >= pd else -pd))))

        return (mes, A)
Exemplo n.º 5
0
def listen():
    'listen to beat (keystrokes for now) from user, learn beats / timing'
    beats = []
    beat = None
    which_beat_guess, beats_per_measure = 0, 4
    rolling_beat_len_guess = None
    prev_note = None

    adjusted_pitches = midi.close_big_intervals_interactive()
    adjusted_pitches.next()  # prime the coroutine
    note_chars = []

    with NonBlockingGetch():
        while True:
            prev_beat = beat

            # waits for user tap -

            # non-blocking version:
            while True:
                #time.sleep(.002)
                ch = nb_getch()
                if ch: break
            # blocking version:
            #ch = getch()

            if ch == 'q':
                print('Quitting...')
                note_strn = ''.join(note_chars)
                return (beats, note_strn)

            # timing
            if len(
                    beats
            ) > 5:  # only feed it the rolling avg once we're confident in it
                beat = Beat(prev_beat, rolling_beat_len_guess)
            else:
                beat = Beat(prev_beat)

            # record the space before the note if any
            if prev_beat and beat.delta_len_guess:
                subdivision_factor = 4
                remaining_time_slots = int(
                    beat.delta_len_guess * subdivision_factor) - 1
                note_chars.append('-' * remaining_time_slots)
            # record the note itself
            note_chars.append(ch)

            # play music
            midi.note_off(prev_note)
            if ch in midi.note_numbers:
                raw_pitch = midi.note_numbers[ch]
                # get next adjusted pitch from coroutine
                note = adjusted_pitches.send(raw_pitch)
            else:
                note = None
                print("No note for key", ch)
            midi.note_on(note)
            prev_note = note

            # add to beat log
            beats.append(beat)
            # rolling avg beat length guess
            deltas = beats[-10:]
            rolling_beat_len_guess = rolling_avg_beat_len(deltas)

            # display
            #print(beat.delta, '-', beat)
            #print('delta len guess:', beat.delta_len_guess)
            #print('rolling beat len guess:', rolling_beat_len_guess)
            beat.print_which_beat()
            print(midi.show_notes_spatially((note, ), None, offset=30))
Exemplo n.º 6
0
    scale = range(20)
    scale = [0, 2, 5, 7, 9, 12, 14, 17, 19, 21, 24, 26, 29]
    scale = [0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19]

    dev = open('/dev/input/js0')
    out = midi.MidiOut(open('/dev/midi1', 'w'))

    # out.send(midi.program_change(0, 19))
    out.send(midi.program_change(0, 16))

    while 1:
        e = read_event(dev)
        if e.type == JS_EVENT_BUTTON:
            note = 62 + 12 + scale[e.number]
            if e.value:
                out.send(midi.note_on(0, note, 64))
            else:
                out.send(midi.note_off(0, note, 64))

        if e.type == JS_EVENT_AXIS:
            #if e.number == 1:
            #    if e.value <= 0:
            #        value = int((-e.value) * 127)
            #        out.send(midi.control_change(0, 7, value))
            #
            #        print e.value, value

            if e.number == 0:
                bend = int(((e.value + 1) / 2) * 16383)
                hi = bend >> 7
                lo = bend & 0x7f
Exemplo n.º 7
0
def listen():
    'listen to beat (keystrokes for now) from user, learn beats / timing'
    beats = []
    beat = None
    which_beat_guess, beats_per_measure = 0, 4
    rolling_beat_len_guess = None
    prev_note = None

    adjusted_pitches = midi.close_big_intervals_interactive()
    adjusted_pitches.next() # prime the coroutine
    note_chars = []

    with NonBlockingGetch():
        while True:
            prev_beat = beat

            # waits for user tap -

            # non-blocking version:
            while True:
                #time.sleep(.002)
                ch = nb_getch()
                if ch: break
            # blocking version:
            #ch = getch()

            if ch == 'q':
                print('Quitting...')
                note_strn = ''.join(note_chars)
                return (beats, note_strn)

            # timing
            if len(beats) > 5: # only feed it the rolling avg once we're confident in it
                beat = Beat(prev_beat, rolling_beat_len_guess)
            else:
                beat = Beat(prev_beat)

            # record the space before the note if any
            if prev_beat and beat.delta_len_guess:
                subdivision_factor = 2
                remaining_time_slots = int(beat.delta_len_guess * subdivision_factor) - 1
                note_chars.append('-' * remaining_time_slots)
            # record the note itself
            note_chars.append(ch)

            # play music
            midi.note_off(prev_note)
            if ch in midi.note_numbers:
                raw_pitch = midi.note_numbers[ch]
                # get next adjusted pitch from coroutine
                note = adjusted_pitches.send(raw_pitch)
            else:
                note = None
                print("No note for key", ch)
            midi.note_on(note)
            prev_note = note

            # add to beat log
            beats.append(beat)
            # rolling avg beat length guess
            deltas = beats[-10:]
            rolling_beat_len_guess = rolling_avg_beat_len(deltas)

            # display
            #print(beat.delta, '-', beat)
            #print('delta len guess:', beat.delta_len_guess)
            #print('rolling beat len guess:', rolling_beat_len_guess)
            beat.print_which_beat()
            print(midi.show_notes_spatially((note,), None, offset=30))
Exemplo n.º 8
0
    scale = range(20)
    scale = [0, 2, 5, 7, 9, 12, 14, 17, 19, 21, 24, 26, 29]
    scale = [0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19]

    dev = open('/dev/input/js0')
    out = midi.MidiOut(open('/dev/midi1', 'w'))

    # out.send(midi.program_change(0, 19))
    out.send(midi.program_change(0, 16))

    while 1:
        e = read_event(dev)
        if e.type == JS_EVENT_BUTTON:
            note = 62 + 12 + scale[e.number]
            if e.value:
                out.send(midi.note_on(0, note, 64))
            else:
                out.send(midi.note_off(0, note, 64))

        if e.type == JS_EVENT_AXIS:
            #if e.number == 1:
            #    if e.value <= 0:
            #        value = int((-e.value) * 127)
            #        out.send(midi.control_change(0, 7, value))
            #
            #        print e.value, value

            if e.number == 0:
                bend = int(((e.value + 1)/2) * 16383)
                hi = bend >> 7
                lo = bend & 0x7f