In [1]:
import os

In [2]:
notesDict = {}
with open('notes.tabs', 'rt') as f:
    for line in f:
        note, freq, wave = (line.replace('\n','').replace(' ', '').split('\t'))
        for note in note.split('/'):
            notesDict[note] = freq

In [3]:
def appendToFile(path, data):
    """
    appendToFile appends a line of data to specified file.  Then adds new line
    
    Args:
        path (string): the file path
        data (data): the data to be written
    
    Return:
        VOID
    """
    with open(path, 'a') as file:
        file.write(data + '\n')

In [32]:
def writeSong(path, song, notesDict, metronome):
    for note, time in song:
        time = int(float(1000 * 60 * 4 * time / 80))
        data = ("{},{}".format(int(float(notesDict[note])), time))
        appendToFile(path, data)

In [40]:
metronome = 140

In [41]:
pause = ['D2', 1/8]

In [47]:
twinkleTwinkle = [
                  ['C5', 1/4], ['C5', 1/4], ['G5', 1/4], ['G5', 1/4],
                  ['A5', 1/4], ['A5', 1/4], ['G5', 1/2],
                  ['F5', 1/4], ['F5', 1/4], ['E5', 1/4], ['E5', 1/4],
                  ['D5', 1/4], ['D5', 1/4], ['C5', 1/2],
                  
#                   ['G6', 1/4], ['G6', 1/4], ['F6', 1/4], ['F6', 1/4],
#                   ['E6', 1/4], ['E6', 1/4], ['D6', 1/2],
#                   ['G6', 1/4], ['G6', 1/4], ['F6', 1/4], ['F6', 1/4],
#                   ['E6', 1/4], ['E6', 1/4], ['D6', 1/2],
                  
                  ['G5', 1/4], ['G5', 1/4], ['F5', 1/4], ['F5', 1/4],
                  ['E5', 1/4], ['E5', 1/4], ['D5', 1/2],
                  ['G5', 1/4], ['G5', 1/4], ['F5', 1/4], ['F5', 1/4],
                  ['E5', 1/4], ['E5', 1/4], ['D5', 1/2],
                  
                  ['C5', 1/4], ['C5', 1/4], ['G5', 1/4], ['G5', 1/4],
                  ['A5', 1/4], ['A5', 1/4], ['G5', 1/2],
                  ['F5', 1/4], ['F5', 1/4], ['E5', 1/4], ['E5', 1/4],
                  ['D5', 1/4], ['D5', 1/4], ['C5', 1/2]
                 ]

In [48]:
# twinkleTwinkle = [['D2', 1/8],['D2', 1/8],['D2', 1/8],['D2', 1/8],['D2', 1/8],['D2', 1/8]
#                  ]

In [49]:
song = []
for note in twinkleTwinkle:
    song.append(note)
    song.append(pause)

In [50]:
os.remove('twinkleTwinkle.csv')

In [51]:
writeSong('twinkleTwinkle.csv', song, notesDict, metronome)


In [ ]: