Esempio n. 1
0
def convert(file):
    #Load the MIDI file and parse it into CSV format
    csv_string = pm.midi_to_csv(filepathin + "\\\\" + file)

    # load the data in csv
    str1 = ''.join(csv_string)
    array = []
    array1 = []
    array2 = []  # all the info in midi
    csv_array = []  # final array
    array = str1.split('\n')
    for item in array:
        array1 = item.split(', ')
        array2.append(array1)
    #print(array2)
    for row in array2:
        row[1] = int(row[1])
        if row[2] == 'Time_signature':  # time and time signature
            row[3] = int(row[3])
            row[4] = int(row[4])
            csv_array.append([row[1], row[3] * 300 + pow(2, row[4])])
        elif row[2] == 'Tempo':  # time and tempo
            row[3] = int(row[3])
            csv_array.append([row[1], row[3]])
        elif row[2] == 'Note_on_c':
            row[4] = int(row[4])
            csv_array.append([row[1], row[4]])
    #print(csv_array)
    with open(csvpath + "\\\\" + file + '.csv', 'w', newline="") as out:
        for row in csv_array:
            writer = csv.writer(out)
            writer.writerow(row)
Esempio n. 2
0
def process_midi(midiFile):
    if isinstance(midiFile, str):
        filename = midiFile.rsplit("/", 1)[-1]
    else:
        filename = midiFile.filename

    filename = filename.rsplit(".", 1)
    title = filename[0]
    extension = filename[1].lower() if 1 < len(filename) else ""
    if extension not in ["mid", "midi", "kar"]:
        raise ValueError("Oops! Your file seems to have the wrong extension!")

    try:
        csv = midi_to_csv(midiFile)
    except Exception:
        raise ValueError("Oh no! There was a problem processing your MIDI file!")

    song = Song(title)
    tempo_map = TempoMap()
    rows = "".join(csv).splitlines()
    for i, row in enumerate(rows):
        cells = row.split(", ")
        event = cells[2]
        if event == "Header":
            tempo_map.ticks_per_quarter_note = int(cells[5])
        elif event == "Title_t" and (track := int(cells[0])) == 1:
            song.title = cells[3][1:-1]
        elif event == "Tempo":
            tick = int(cells[1])
            tempo = int(cells[3])
            tempo_map.add_tempo(tick, tempo)
Esempio n. 3
0
def main():
    midiName = 'Despacito_Piano_Sheet_Music_Luis_Fonsi_ft_Daddy_Yankee.mid'
    midiLocation = './' + midiName

    csv_string = py_midicsv.midi_to_csv(midiLocation)
    # Mutate all the elements in csv_string
    for ind, line in enumerate(csv_string):
        line.strip('\n')
        csv_string[ind] = list(map(convertIfInt, line.split(',')))

    csv_string.sort(key=lambda x: x[1])
    csv_string = filter(lambda x: 'Note' in x[2], csv_string)

    partition = 0
    while True:
        with open(midiName[:-4] + str(partition) + '.csv.txt', 'w') as csvfile:
            csvfile.write('')
            for dummy in range(1500):
                try:
                    csvfile.write(str(next(csv_string)) + '\n')
                except:
                    csvfile.write('[\'EOF\']')
                    return
            csvfile.write('[\'EOF\']')
        partition += 1
Esempio n. 4
0
def midi_json(output):
    maintain = []
    notes = []
    csv_string = py_midicsv.midi_to_csv(output)
    for idx in range(len(csv_string)):
        current = (csv_string[idx]).strip().split(',')
        if 'Note' not in current[2]:
            continue
        current[4] = int(current[4]) % 12
        if 'on' in current[2]:
            maintain.append([current[4], float(int(current[1])/100)])
        if 'off' in current[2]:
            for idx,note in enumerate(maintain):
                if note[0] == current[4]:
                    d = {}
                    d["pitch"] = current[4]
                    d["start"] = note[1]
                    d["end"] = float(int(current[1])/100)
                    notes.append(d)
                    del maintain[idx]
                    break
    final = {}
    note = {}
    note["id"] = output
    note["notes"] = notes
    final["notes"] = note
    return final
Esempio n. 5
0
def generate_data(filename):
    csv_string = pm.midi_to_csv(filename)
    notes = np.zeros((60000, 96))
    last = 0
    start = False
    start_time = 0
    for x in csv_string:
        note = x.split(',')
        if note[2] == ' Note_on_c':
            if not start:
                start_time = int(note[1]) // 60
            start = True
            time = int(note[1]) // 60 - start_time
            pitch = int(note[4]) - 16
            if pitch <= 0:
                print("ERROR")
            notes[time, pitch] = 1
            last = time
    notes = np.resize(notes, (last, 96))

    data = np.zeros((0, 48, 96))

    for i in range(0, notes.shape[0] - 47, 40):
        if np.sum(notes[i:i + 48, :]) > 15:
            data = np.append(data,
                             np.expand_dims(notes[i:i + 48, :], axis=0),
                             axis=0)
    return data
Esempio n. 6
0
    def __init__(self, midi_filepath, audio_filepath):
        self.midi_csv_strings = pm.midi_to_csv(midi_filepath)
        self.meta_data = self.midi_csv_strings[0:7]
        self.track_end = self.midi_csv_strings[-2:]
        self.midi_data = self.midi_csv_strings[5:-2]

        header = self.meta_data[0].split(',')
        tempo = self.meta_data[2].split(',')
        ticks_per_quarter = 384
        microsec_per_quarter = 500000
        microsec_per_tick = microsec_per_quarter / ticks_per_quarter
        seconds_per_tick = microsec_per_tick / 1000000
        self.tempo = microsec_per_quarter
        self.PPQ = ticks_per_quarter
        self.song_total_midi_ticks = int(self.track_end[0].split(', ')[1])
        self.midi_note_array = np.zeros((128, self.song_total_midi_ticks))
        self.midi_sus_array = np.zeros((1, self.song_total_midi_ticks))
        self.downsampled_tempo = None
        self.midi_array_splits = None

        self.audio_waveform, self.sample_rate = librosa.load(audio_filepath,
                                                             sr=None)
        self.raw_spectrogram = librosa.stft(self.audio_waveform)
        self.db_spectrogram = np.flipud(
            librosa.amplitude_to_db(abs(self.raw_spectrogram)))
        self.song_total_sample_ticks = self.db_spectrogram.shape[0]
        self.spectrogram_array_splits = None

        self.mel_spectrogram = None
        self.mel_windows = None
        self.midi_windows = None
        self.midi_slices = None
def convert_midi_to_csv(midi_path, csv_path, write_midi_cvs_to_file=False):
    # convert_midi_to_csv: Retrieves MIDI files at a folder and creates the corresponding csv converted files.
    # It uses py_midicsv, to obtain a csv type MIDI event file. Then it uses the midi_to_nmat to obtain a note 
    # matrix representation of the notes. The note matrix representation is saved as a csv file. 

    files_list = os.listdir(os.path.join(midi_path))
    
    for file_name in files_list:
        if file_name[-3:] == 'mid':
            print("Converting:" + file_name + "...", end="")
            midi_file_path = os.path.join(midi_path, file_name)
            csv_file_path = os.path.join(csv_path, file_name[:-3]+"csv")
            midi_csv_list = py_midicsv.midi_to_csv(midi_file_path)
            nmat = midi_to_nmat(midi_csv_list)# midi_csv to nmat_csv

            with open(csv_file_path, 'w', newline='') as myfile:
                wr = csv.writer(myfile, quoting=csv.QUOTE_NONE)
                wr.writerows(nmat)
                myfile.close
                
            if write_midi_cvs_to_file:
                midi_csv_file_path = os.path.join(csv_path, folder, file_name[:-4] + "_MIDI.csv") 
                csvfile = open(midi_csv_file_path,'w')
                csvfile.writelines(midi_csv_list)
                csvfile.close()

            print("Done!")
Esempio n. 8
0
    def all_midi_in_path_to_csv(self, midi_folder_path):
        now = datetime.datetime.now()
        current_timestamp = now.strftime("%Y-%m-%d %H:%M")
        destination_path = "CSVs%d" % current_timestamp

        # Create the new folder
        try:
            os.mkdir(destination_path)
        except OSError:
            print("Creation of the directory %s failed" % destination_path)
        else:
            print("Successfully created the directory %s " % destination_path)

        # Here iterate through folder and use midi_to_csv on each
        directory = os.fsencode(midi_folder_path)
        for file in os.listdir(directory):
            filename = os.fsdecode(file)
            if filename.endswith(".mid") or filename.endswith(".midi"):
                new_csv = midicsv.midi_to_csv(filename)
                save_name = destination_path + "/" + "weewee_%d.csv" % current_timestamp
                new_file_name = os.path.join(os.path.expanduser('~'),
                                             midi_folder_path, save_name)
                f = open(new_file_name, "x")
                f.write(new_csv)

                continue
            else:
                continue
Esempio n. 9
0
 def midi_to_csv(self, midi_path):
     file = os.fsencode(midi_path)
     filename = os.fsdecode(file)
     if filename.endswith(".mid") or filename.endswith(".midi"):
         csv = midicsv.midi_to_csv(filename)
         # lets write all of these to a new directory
         return csv
Esempio n. 10
0
def miditotxt(direc):
    csvText = py_midicsv.midi_to_csv(direc)
    outDirec = direc.split("\\")
    outval = outDirec[-1][0:-4]
    CreateText = open(outval + ".txt", 'w')
    for i in csvText:
        CreateText.write(i)
    CreateText.close()
Esempio n. 11
0
def load_to_csv(filepath):
    """
    Load midi to csv.
    :param filepath:
    :return:
    """
    s = midi.midi_to_csv(filepath)
    return s
Esempio n. 12
0
def midi_csv(filename):
    csv_string = py_midicsv.midi_to_csv(filename)
    count = 0
    for i in csv_string:
        line = i.strip().split(", ")
        if line[2] == 'Note_on_c' or line[2] == 'Note_off_c':
            count += 1
    return csv_string, count
Esempio n. 13
0
def RecMidiChatty(direc, outdirec):
    '''Give me a directory, I'll convert all the midi files into csv's, and put them into the outdirec!
    (I'll do my best to make it an exact copy, inner folders and all)
    This is also the Chatty version! I'll be printing out a bunch of stuff. If you don't want that just do RecMidi.'''
    listostuff = os.listdir(direc)
    print(listostuff)
    path_to_dir = (outdirec)
    garbage = []
    try:
        os.mkdir((path_to_dir))
    except (FileExistsError):
        print("file already exists")
    converts = 0
    for j in listostuff:
        currDir = direc + "\\" + j
        #(If this is another directory, do it again)
        if (os.path.isdir(currDir) and (j != ".git")):
            print(currDir)
            converts += RecMidiChatty(currDir, (path_to_dir + "\\" + j))
        elif (j[-4:] != '.mid'):
            continue
        else:
            print(j)
            try:
                csvText = pym.midi_to_csv(currDir)
            except (TypeError):
                print("ERROR ERROR ERROR\nType Error! " + j +
                      " won't be converted! File just sucks probably.")
                continue
            except:
                print("ERROR ERROR ERROR\n Something went wrong! " + j +
                      " won't be converted!!!!!")
                continue
            mani = j[:-4]
            mani += ".txt"
            CreateText = open(direc + "\\" + mani, 'w+')
            for i in csvText:
                try:
                    CreateText.write(i)
                except:
                    print(
                        "ERROR\n Something went wrong when writing. Adding to the pile..."
                    )
                    CreateText.close()
                    garbage.append(path_to_dir + "\\" + mani)
                    break
            CreateText.close()
            path_to_curr = direc + "\\" + mani
            try:
                sh.move(path_to_curr, path_to_dir)
            except:
                renameVal = path_to_curr[:-4] + str(converts) + ".txt"
                os.rename(path_to_curr, renameVal)
                sh.move(renameVal, path_to_dir)
            converts += 1
    for i in garbage:
        os.remove(i)
    return converts
Esempio n. 14
0
def midi_to_csv(midi_filename: str) -> pd.DataFrame:
    csv_string = py_midicsv.midi_to_csv(midi_filename)

    data = []
    for row in csv_string:
        row = row.replace('\n', '')
        row = row.split(', ')
        data += [row]

    return pd.DataFrame(data)
Esempio n. 15
0
def midi2list(path):
    str_list = pm.midi_to_csv(path)
    for i in range(len(str_list)):
        str_list[i] = (str_list[i].replace(' ', '').replace('\n',
                                                            '')).split(',')
        for j in range(len(str_list[i])):
            if str_list[i][j].isdigit():
                str_list[i][j] = int(str_list[i][j])

    return str_list
Esempio n. 16
0
def midi_to_df(midi_file, save=False):
    """Converts a midi file to a list of csv then to a pandas df.

    Parameters
    ----------
    midi_file : str
        The directory pointing towards the midi file to be converted.
    save : bool, optional
        If specified, saves `midi_file` as a csv in the same directory
        and as the same name.

    Returns
    -------
    pandas.DataFrame
        A DataFrame that contains the timing of note events and note values,
        amongst other (irrelevant) information.

    Raises
    ------
    ValueError
        If `midi_file` is not pointing towards a .mid file.
    """

    if midi_file[-4:] != ".mid":
        raise ValueError("Pass in a .mid file!")

    csv_string = py_midicsv.midi_to_csv(midi_file)
    df = pd.DataFrame([ls.strip().split(",") for ls in csv_string])

    # strip all whitespaces
    df[2] = df[2].str.strip()
    df[4] = df[4].str.strip()

    # convert values to int
    for col in [0, 1, 4, 5]:
        df[col] = df[col].apply(pd.to_numeric, errors="coerce")
        df[col] = df[col].fillna(0).astype(int)

    # convert note velocity of 0 to 'Note_off_c' events
    # Key assumption that note velocity of 0 equals to a note off event
    df[2] = df.apply(_convert_to_off, axis=1)

    # rename df columns
    df = df.rename(
        columns={
            0: "Track_id",
            1: "Timings",
            2: "Events",
            3: "Time_signatures",
            4: "Note_values",
            5: "Velocity",
        })

    df.to_csv(midi_file[:-4] + ".csv") if save else None
    return df
Esempio n. 17
0
def miditocsvconvert(directory):
    for subdir, dirs, files in os.walk(directory):
        for filename in files:
            filepath = subdir + os.sep + filename 
            if filepath.endswith(".midi"):
                csv_string = py_midicsv.midi_to_csv(filepath)
                newfile = filepath.replace('.midi', '.csv')
                path = directory
                f = open(os.path.join(path, newfile), 'w+')
                for i in range(len(csv_string)):
                    f.write(csv_string[i])
Esempio n. 18
0
def MidiToCsv(file):
    import py_midicsv as pm
    import pandas as pd

    csv_file = pm.midi_to_csv(file)

    dataframe = pd.DataFrame([sub.split(",") for sub in csv_file])

    del csv_file, file

    return dataframe
def process_midi(midipath):
    """returns dataframe of midi note-on events.
    
    Arguments:
        midipath {pathto.mid} -- path to .mid file
    """
    midi_strings = py_midicsv.midi_to_csv(midipath)
    midi_csv = get_midi_csv(midi_strings)
    note_on_events = get_note_events(midi_csv)
    label, midi_start = generate_blank_label(note_on_events)
    insert_note_events(label, note_on_events, midi_start)

    return label
Esempio n. 20
0
def process_midi(midipath):
    """returns dataframe of midi note-on events.
    
    Arguments:
        midipath {pathto.mid} -- path to .mid file
    """
    midi_strings = py_midicsv.midi_to_csv(midipath)
    midi_csv = __get_midi_csv(midi_strings)
    note_on_events = __get_note_events(midi_csv)
    conversion_factor = __get_conversion_factor(midi_csv)
    note_on_events = __calc_absolute_times(note_on_events, conversion_factor)
    label, midi_start = __generate_blank_label(note_on_events)
    abs_label = __generate_absolute_label(note_on_events)
    __insert_abs_events(abs_label, note_on_events)
    __insert_note_events(label, note_on_events, midi_start)

    return abs_label
Esempio n. 21
0
def compress_midi(_midi_files):
    open("sorting/input_text.txt", "w").write("")
    for file in _midi_files:
        csv_string = pm.midi_to_csv(f"sorting/midi input/{file}")
        compressed_csv = []

        for i in range(len(csv_string)):
            compressed_string = csv_string[i].split(", ")
            if len(compressed_string) == 6 and "Note_off_c" not in compressed_string[2] \
                and "Control_c" not in compressed_string[2] and "Header" not in compressed_string[2] \
                    and not compressed_string[-1] == "0\n":
                compressed_csv.append([
                    int(compressed_string[1]),
                    tones_keys[int(compressed_string[4]) - 21],
                    int(compressed_string[4])
                ])

        compressed_to_string(sorted(compressed_csv), file)
Esempio n. 22
0
    def _midToTxt(self, create_file=True):
        '''
        Converts midi files to txt files
        returns True if successfully converted to txt file
        returns False otherwise

        Requires:
            midiFolder: The folder the midi files are held in relative to current directory
            fileName: The midifile name with extension
            txtOutFolder: The name of the folder the out txt file should be put in
            txtOutFileName: The name of the txt file that will be output by the converter

        Effect:
            Creates a new file in the txtOutFolder called [txtOutFileName].txt
            Contents is in list format:
              [channel, time note fires, midinotecommand, something, the midi note value, midi note volume]
              [int, int, str, int, int, int]
            Lists are sorted by time

            ***Visit https://glassarmonica.com/science/frequency_midi.php to convert midi note val to note                info***
        '''
        # Assert the Midis folder exists later
        midiLocation = './' + self._midDir + '/' + self._midFileName
        outLocation = './' + self._outDir + '/' + self._outFileName
        csv_string = py_midicsv.midi_to_csv(midiLocation)
        # Turn all the midi information into lists
        for ind, line in enumerate(csv_string):
            line.strip('\n')
            csv_string[ind] = list(map(self.__convertIfInt, line.split(',')))
        # Sort the midi file by time
        csv_string.sort(key=lambda x: x[1])
        # And only list the note sections of the file
        csv_string = filter(lambda x: 'Note' in x[2], csv_string)

        with open(outLocation, 'w') as txtfile:
            txtfile.write('')
            while True:
                try:
                    txtfile.write(str(next(csv_string)) + '\n')
                except:
                    txtfile.write('[\'EOF\']')
                    return True
            return False
def parse_midi_file(midi_filename, csv_filename=None):
    """
	Decompiles a MIDI file into human-readable text
	Inputs:
		midi_filename - name of MIDI file to convert
		csv_filename - name of csv file to output string to (optional), no file output if empty
	Returns:
		csv_string - string of all MIDI lines, according to the midicsv standard
	"""
    csv_string = py_midicsv.midi_to_csv(midi_filename)
    # print(csv_string)

    if csv_filename is not None:
        # output to CSV file
        with open(csv_filename, 'w') as csv_file:
            for line in csv_string:
                csv_file.write(line)

    return csv_string
Esempio n. 24
0
def midi2txt(path):
    events_list = []
    # Go through each MIDI file within the path
    for file_name in os.listdir(path):
        if file_name.lower().endswith('.mid'):
            # Parse MIDI into a list where each element is a row in the CSV
            csv_list = pm.midi_to_csv(path + "/" + file_name)

            # Uncomment to save the csv file for each MIDI:
            with open(rf'./{path}/{file_name}.txt', "w") as txt:
                txt.writelines(csv_list)

            # Put each row of the csv into a list
            for i in range(len(csv_list)):
                csv_list[i] = csv_list[i].split(",")
                # Remove Spaces and \n's in each row element
                for j in range(len(csv_list[i])):
                    csv_list[i][j] = csv_list[i][j].replace(' ', '').replace(
                        '\n', '')

            str_list = csv2str(csv_list)
            if TRANSPOSE_COUNT > 0:
                rand_transpose_values = set()
                while len(rand_transpose_values) < TRANSPOSE_COUNT:
                    random_num = random.randrange(0, 12)
                    rand_transpose_values.add(random_num)

                # Transpose the original song and a transposed version of the song to events list
                events_list.extend(str_list)
                for number in rand_transpose_values:
                    events_list.extend(transpose(str_list, number))
                    print(len(events_list))
            else:
                events_list.extend(str_list)

    # At this point, events_list is a 2d list containing all MIDI events in the dataset.
    # Time unit is still in ticks

    # Events_list is converted to a list of MidiEvent Objects
    events_obj_list = str2objects(events_list)

    # Object list is converted to text
    objects2txt(events_obj_list)
 def checking_csv(self, only_file_name):
     csv_string = py_midicsv.midi_to_csv(self.output_file_dir +
                                         str(self.epsilon_folder) + "/" +
                                         "New_" + self.atype + "_" +
                                         self.orig_midi_name + "_" +
                                         only_file_name + ".mid")
     tmp_list = []
     for i in range(0, len(csv_string)):
         temp = np.array(csv_string[i].replace("\n",
                                               "").replace(" ",
                                                           "").split(","))
         tmp_list.append(temp)
     data = pd.DataFrame(tmp_list)
     data.to_csv(
         self.csv_output_dir + "New_" + self.atype + "_" +
         self.orig_midi_name + "_" + only_file_name + ".csv",
         header=False,
         index=False,
     )
     print(".csv saved!")
Esempio n. 26
0
def makesequences(file):
    csvarr = [(s.split(",")) for s in py_midicsv.midi_to_csv(file)]
    final = []
    last_tick = 0
    note = np.zeros(101)

    for i in range(len(csvarr)):
        line = csvarr[i]
        time = int(line[1])

        if line[2] == " Note_on_c":
            if time > last_tick + 400:
                last_tick = time
                final.append(note)
                note = np.zeros(101)
            note[int(line[4].strip())] = 1
    final.append(note)
    eof = np.zeros(101)
    eof[100] = 1
    final.append(eof)
    return final
Esempio n. 27
0
    def read_music(self, file: str, normalize: bool = False) -> str:
        file_path = os.path.join(self.midi_dir, file)
        csv_rows = pm.midi_to_csv(file_path)
        csv_rows = list(
            csv.reader(csv_rows, delimiter=',', skipinitialspace=True))
        csv_rows = self.normalize_tempo(csv_rows)

        note_offset = 0
        if normalize:
            note_offset = scarlatti_get_offset(filename=file)

        encoded = ''
        time_prev = 0
        velocity_prev = '0'
        for row in csv_rows:
            m_track, m_time, m_type = row[0], int(row[1]), row[2]

            wait_t = round((m_time - time_prev) / self.TIME_CONSTANT)
            time_prev = m_time

            if m_type == 'Note_on_c' or m_type == 'Note_off_c':
                channel, note, velocity = row[3], row[4], row[5]
                note = str(int(note) + note_offset)

                if wait_t != 0:
                    encoded += ' ' + self.time_to_waits(wait_t)

                if m_type == 'Note_off_c' or (m_type == 'Note_on_c'
                                              and velocity == '0'):
                    encoded += ' endp' + note
                elif m_type == 'Note_on_c':
                    if velocity != velocity_prev:
                        velocity_prev = velocity
                        velocity = int(velocity) // self.VELOCITIES
                        encoded += ' v' + str(velocity) + ' p' + note
                    else:
                        encoded += ' p' + note

        return encoded
def music_decrypt(file_name):
    csv_string = py_midicsv.midi_to_csv(file_name + ".mid")
    degrees = [60, 62, 64, 65, 67, 69, 71, 72]
    look_up_table = [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
                     ['i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
                     ['q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
                     ['y', 'z', '0', '1', '2', '3', '4', '5'],
                     ['6', '7', '8', '9', ',', '_', '.', ' ']]
    lis = []
    for i in csv_string:
        s = i.split()
        l = len(s)
        if (l > 3):
            lis.append([s[1], s[l - 2]])
    temp = []
    for i in lis:
        i[0] = (i[0].split(',')[0])
        i[1] = (i[1].split(',')[0])
        if (i[1] != 'Tempo'):
            if (i[1] >= '60'):
                temp.append([int(i[0]), int(i[1])])
    for n in range(1, len(temp)):
        temp[n][0] = temp[n][0] - temp[n - 1][0]

    print(temp)
    max = 0
    for n in range(len(temp)):
        temp[n][1] = degrees.index(temp[n][1])
        max = temp[n][0]
        if (temp[n][0 > max]):
            max = temp[n][0]
    for n in range(len(temp)):
        temp[n][0] = int(math.log(float(temp[n][0] / max), 2))
    dec_txt = []
    for n in range(len[temp]):
        dec_txt.append(look_up_table[temp[n][0][temp[n][1]]])
    print(dec_txt)
Esempio n. 29
0
def read_midi_file(file_name,frequency):
    try:
        csv_string = py_midicsv.midi_to_csv(file_name)
    except:
        return []

    beats_per_second = 0

    heap_array = []

    for row in csv_string.getvalue().split('\n'):
        array_row = [x.strip() for x in row.split(',')]
        if array_row[2] == 'Header':
            beats_per_second = float(array_row[5])/frequency

        if array_row[2] == 'Note_on_c' and array_row[5] != 0:
            heap_element = [float(array_row[1])/beats_per_second, True, int(array_row[4])]
            heap_array.append(heap_element)

        if array_row[2] == 'Note_off_c' or (array_row[2] == 'Note_on_c' and array_row[5] == 0):
            heap_element = [float(array_row[1])/beats_per_second, False, int(array_row[4])]
            heap_array.append(heap_element)
    heap_array.sort()
    return heap_array
"""	Main: Archivo que muestra el uso basico de la clase Comparator	    """

# Se utiliza el modulo py_midicsv para extraer las notas desde un archivo MIDI.
import py_midicsv as pm
# Sin embargo, lo unico necesario para utilizar esta clase es tener un lista de notas con la notacion en numero de las notas musicales.
from Comparator import Comparator

# Se cambia el formato del archivo de MIDI a CSV
OnRepeat = pm.midi_to_csv(r"dummy.mid")

# Extraccion de Notas de Archivo MIDI
notes = []
for l in OnRepeat:
    line = l.split(",")
    if len(line) >= 5:
        if line[2] == " Note_on_c":
            m = int(line[4])
            notes.append(m)

# Se inicializa el objeto de la clase Comparator con los intervalos de la Escala Mayor
majorComp = Comparator([0, 2, 4, 5, 7, 9, 11])
notesStr = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]

indiceMayor = majorComp.analyze(notes)
print("Indices Escala Mayor", indiceMayor)
print("\nEscala:", notesStr[indiceMayor.index(max(indiceMayor))], "mayor\n")