# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/", "height": 561} colab_type="code" id="tzAOpnZLGM5e" outputId="f1d11aee-49c7-4e3d-eca0-e3f35f41948c"
# !wget https://github.com/v3rm1/ml_bach_fugue/archive/midi_to_txt.zip
# !unzip midi_to_txt.zip

# + colab={"base_uri": "https://localhost:8080/", "height": 54} colab_type="code" id="_O2TVYQha-3K" outputId="42094b6c-9bc9-4573-883b-7f095f1dbcfc"
# !mv ml_bach_fugue-midi_to_txt/preprocessing/converters/ /content/
# !rm -rf ml_bach_fugue-midi_to_txt/

# + colab={"base_uri": "https://localhost:8080/", "height": 54} colab_type="code" id="6-mdLMd3W5iw" outputId="bd60fa97-3af3-45a2-a129-8262a55620cb"
from google.colab import drive
drive.mount('/content/gdrive')

# + colab={"base_uri": "https://localhost:8080/", "height": 153} colab_type="code" id="i4dWrNgCGiuR" outputId="bf9c5e41-e60a-4fe1-ef53-b1e8c1d35244"
from bs4 import BeautifulSoup
import requests
import os
import glob
import zipfile

url = 'http://www.stringquartets.org/composers.htm'
outpath = '/content/gdrive/My Drive/raw_data/'
os.makedirs(outpath, exist_ok=True)
r = requests.get(url)
soup = BeautifulSoup(r.text)
for link in soup.find_all('a', href=True):
  zipurl = link['href']
  if(zipurl.endswith('.zip')):
    outfname = outpath + zipurl.split('/')[-1]
    r = requests.get('http://www.stringquartets.org/' + zipurl, stream=True)
    if( r.status_code == requests.codes.ok ) :
        fsize = int(r.headers['content-length'])
        print('Downloading {}'.format(outfname))
        with open(outfname, 'wb') as fd:
            for chunk in r.iter_content(chunk_size=1024): # chuck size can be larger
                if chunk: # ignore keep-alive requests
                    fd.write(chunk)
            fd.close()

for files in glob.iglob('/content/gdrive/My Drive/raw_data/*.zip'): # Extract files
  f = zipfile.ZipFile(files)
  f.extractall('/content/gdrive/My Drive/raw_data')
  f.close()
  os.remove(files)

for files in glob.iglob('/content/gdrive/My Drive/raw_data/*.kpp'): # Remove .kpp files
  os.remove(files)

for files in glob.iglob('/content/gdrive/My Drive/raw_data/*.squ'): # Rename .squ files to midi
  name = files.split('/')[-1].split('.')[0]
  os.rename(files, '/content/gdrive/My Drive/raw_data/'+name+'.mid')

# + colab={"base_uri": "https://localhost:8080/", "height": 51} colab_type="code" id="LI6N_7FsncgD" outputId="a13cca14-a109-442b-c1c8-f908fdbb85f5"
# !pip install music21 mido

# + colab={} colab_type="code" id="hGSvA8pVnJOa"
""" Function taken from 
http://nickkellyresearch.com/python-script-transpose-midi-files-c-minor/
"""

def transpose_key(filename):
    """ converting everything into the key of C major or A minor
    """
    # major conversions
    majors = dict([('A-', 4),('G#', 4),('A', 3),('A#', 2),('B-', 2),('B', 1),('C', 0),('C#', -1),('D-', -1),('D', -2),
            ('D#', -3),('E-', -3),('E', -4),('F', -5),('F#', 6),('G-', 6),('G', 5)])
    minors = dict([('G#', 1), ('A-', 1),('A', 0),('A#', -1),('B-', -1),('B', -2),('C', -3),('C#', -4),('D-', -4),
            ('D', -5),('D#', 6),('E-', 6),('E', 5),('F', 4),('F#', 3),('G-', 3),('G', 2)])

    score = music21.converter.parse(filename)
    key = score.analyze('key')
    #    print key.tonic.name, key.mode
    if key.mode == "major":
        halfSteps = majors[key.tonic.name]
    elif key.mode == "minor":
        halfSteps = minors[key.tonic.name]

    newscore = score.transpose(halfSteps)
    newscore.write('midi', '/tmp/temp.mid')


# + colab={"base_uri": "https://localhost:8080/", "height": 1000} colab_type="code" id="23zifGNuWO9R" outputId="1eb79141-dd83-4395-d42c-e293367d53ad"
import converters.convert as cvt
from mido import MidiFile, MidiTrack, Message as MidiMessage
import music21
import tqdm

print('Total files found: ', len(glob.glob('/content/gdrive/My Drive/raw_data/*.mid')))

for file in tqdm.tqdm(glob.iglob('/content/gdrive/My Drive/raw_data/*.mid')):
    try:
      transpose_key(file)
    except:
      print('Error occured while reading file\n Deleting file')
      os.remove(file)
      continue
    
    mid = MidiFile('/tmp/temp.mid')
    tpb = mid.ticks_per_beat
    metamessages, insmessages = cvt.split_tracks(mid)  # Make sure that only 4 instruments are passed forward
    streams = cvt.notes_to_pitchstream(insmessages, tpb)
    cvt.pitch_stream_to_text(streams, file_name='/content/gdrive/My Drive/corpus.txt')
    os.remove(file)



# + colab={} colab_type="code" id="4VhdkOvhnsNo"
# !rm -rf /content/gdrive/My Drive/raw_data

# + colab={} colab_type="code" id="xLfw0jCl3u-y"
stream = cvt.text_to_pitch_stream(file_name='')
mid = cvt.pitchstream_to_midi(stream)
mid.save('txt.mid')

StarcoderdataJupyterScriptsDedupFiltered