-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_utils.py
41 lines (28 loc) · 1.19 KB
/
midi_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
def download_midis(download_dir, genre='classic'):
import requests
from bs4 import BeautifulSoup
import re
"""
Download midi files from midiword.com
Code from: https://stackoverflow.com/questions/52124737/why-cant-i-download-a-midi-file-with-python-requests
"""
main_page = requests.get('http://www.midiworld.com/'+genre+'.htm')
parsed_page = BeautifulSoup(main_page.content, 'html.parser')
links = parsed_page.find_all('a', href=re.compile('mid$'))
def getFileName(link):
link = link['href']
filename = link.split('/')[::-1][0]
return filename
def downloadFile(link, filename):
mid_file = requests.get(link['href'], stream=True)
with open(download_dir+filename, 'wb') as saveMidFile:
saveMidFile.write(mid_file.content)
for link in links:
filename = getFileName(link)
downloadFile(link, filename)
def play_midi(midi_path):
from IPython.display import Audio
from pretty_midi import PrettyMIDI
midi_data = PrettyMIDI(midi_file=midi_path)
return Audio(midi_data.synthesize(fs=22050), rate=22050)