Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to download midi-files ot midi folder #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions download_midi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
__author__ = 'val'


from BeautifulSoup import BeautifulSoup
import urllib2
import re
import os

dict = set()

def download_midi_recursive(website, page, folder):
if page in dict:
return

dict.add(page)
print "Downloading page " + page

html_page = urllib2.urlopen(website + '/' + page)
soup = BeautifulSoup(html_page)
for link in soup.findAll('a'):
url = '{}'.format(link.get('href'))

if url.endswith('.mid'):
try:
filename = os.path.basename(url)
midiurl = urllib2.urlopen(website + '/' + url)
fullpath = folder + '/' + filename

if os.path.exists(fullpath):
print "Skipping " + filename
else:
print "Downloading " + filename
with open(fullpath, "wb") as local_file:
content = midiurl.read()
local_file.write(content)

except urllib2.HTTPError, e:
print "Http error" + e.code + url
except urllib2.URLError, e:
print "Url error" + e.reason + url
if url.endswith('.htm'):
try:
relativeurl = os.path.basename(url)
download_midi_recursive(website, relativeurl, folder)
except Exception, e:
print e.message




# website = "http://www.midiworld.com"
website = "http://www.piano-midi.de"
# page = "classic.htm"
page = "midi_files.htm"
folder = './music'
download_midi_recursive(website, page, folder)
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cPickle as pickle
import os.path
import gzip
import numpy
from midi_to_statematrix import *
Expand Down Expand Up @@ -37,12 +38,21 @@ def fetch_train_thoughts(m,pcs,batches,name="trainthoughts"):
all_thoughts.append((ipt,opt,thoughts))
pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

def create_output_directory():
outputdir = "output"
try:
os.stat(outputdir)
except:
os.mkdir(outputdir)

if __name__ == '__main__':

pcs = multi_training.loadPieces("music")

m = model.Model([300,300],[100,50], dropout=0.5)

create_output_directory()

multi_training.trainPiece(m, pcs, 10000)

pickle.dump( m.learned_config, open( "output/final_learned_config.p", "wb" ) )
15 changes: 9 additions & 6 deletions multi_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ def loadPieces(dirpath):

name = fname[:-4]

outMatrix = midiToNoteStateMatrix(os.path.join(dirpath, fname))
if len(outMatrix) < batch_len:
continue

pieces[name] = outMatrix
print "Loaded {}".format(name)
try:
outMatrix = midiToNoteStateMatrix(os.path.join(dirpath, fname))
if len(outMatrix) < batch_len:
continue

pieces[name] = outMatrix
print "Loaded {}".format(name)
except Exception, e:
print "Skipping to load {} due to error".format(name)

return pieces

Expand Down