<commit_before>import time
import rtmidi
import music21
import threading


class ReaderThread(threading.Thread):
    def __init__(self, device, port, stream):
        threading.Thread.__init__(self)
        self.stream = stream
        self.setDaemon(True)
        self.port = port
        self.portName = device.getPortName(port)
        self.device = device
        self.quit = False

    def run(self):
        self.device.openPort(self.port)
        self.device.ignoreTypes(True, False, True)
        while True:
            if self.quit:
                return
            msg = self.device.getMessage()
            if msg:
                self.append(msg)
            else:
                time.sleep(0.001)

    def append(self, msg):
        if msg.isNoteOn():
            n = music21.note.Note()
            n.midi = msg.getNoteNumber()
            print n.midi
            n.duration = music21.duration.Duration('half')
            self.stream.insert(0, n)
            theChords = stream.chordify()
            for thisChord in theChords.flat:
                if 'Chord' not in thisChord.classes:  # not a chord
                    continue
                if thisChord.isDominantSeventh():
                    print "this is dominant"
                else:
                    print thisChord.pitchedCommonName


class Reader():
    def __init__(self, stream):
        if stream is not None:
            self.stream = stream
        else:
            stream = music21.stream.Stream()

    def __enter__(self):
        dev = rtmidi.RtMidiIn()
        self.collectors = []
        for i in range(dev.getPortCount()):
            device = rtmidi.RtMidiIn()
            print 'OPENING', dev.getPortName(i)
            collector = ReaderThread(device, i, self.stream)
            collector.start()<commit_msg>Timestamping implemented<commit_after>from __future__ import division
import time
import rtmidi
import music21
import threading


class ReaderThread(threading.Thread):
    def __init__(self, device, port, stream):
        threading.Thread.__init__(self)
        self.stream = stream
        self.setDaemon(True)
        self.port = port
        self.portName = device.getPortName(port)
        self.device = device
        self.quit = False

    def run(self):
        self.device.openPort(self.port)
        self.device.ignoreTypes(True, False, True)
        self.start = time.time() * 1000
        while True:
            if self.quit:
                return
            msg = self.device.getMessage()
            if msg:
                self.append(msg)
            else:
                time.sleep(0.001)

    def append(self, msg):
        if msg.isNoteOn():
            n = music21.note.Note()
            n.midi = msg.getNoteNumber()
            n.duration = music21.duration.Duration('breve')
            timestamp = self.ms_to_samples()
            self.stream.insert(timestamp, n)
            theChords = stream.chordify()
            for thisChord in theChords.flat:
                if 'Chord' not in thisChord.classes:  # not a chord
                    continue
                if thisChord.isDominantSeventh():
                    print "this is dominant"
                else:
                    print thisChord.pitchedCommonName

    def ms_to_samples(self):
        diff = (time.time() * 1000) - self.start
        return diff * 480 / (60 * 1000)


class Reader():
    def __init__(self, stream):
        if stream is not None:
            self.stream = stream
        else:
            stream = music21.stream.Stream()

    def __enter__(self):
        dev = rtmidi.RtMidiIn()
        self.collectors = []
        for i in range(dev.getPortCount()):
            device = rtmidi.RtMidiIn()
            print 'OPENING', dev.getPortName(i)
            collector = ReaderThread(device, i, self.stream)
            collector.start()
{'fraction_non_alphanumeric': 0.09324009324009325, 'fraction_numerical': 0.010878010878010878, 'mean_word_length': 1.6452054794520548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3657002', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1072, 'n_words': 254}