Skip to content

Commit aadb367

Browse files
committed
lab1 audio files added
1 parent da3c840 commit aadb367

File tree

8 files changed

+75
-83
lines changed

8 files changed

+75
-83
lines changed

lab1/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

lab1/util.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

mitdeeplearning/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import mitdeeplearning.lab1
2+
import mitdeeplearning.util
File renamed without changes.

mitdeeplearning/lab1.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,57 @@
1-
def func():
2-
print("test package")
1+
import os
2+
import regex as re
3+
import subprocess
4+
import urllib
5+
6+
from IPython.display import Audio
7+
8+
9+
DATA_URL = 'https://raw.githubusercontent.com/aamini/introtodeeplearning_labs/2019/lab1/data/irish.abc'
10+
11+
def load_training_data():
12+
stream = urllib.request.urlopen(DATA_URL)
13+
text = stream.read().decode("utf-8")
14+
songs = extract_song_snippet(text)
15+
return songs
16+
17+
def extract_song_snippet(text):
18+
pattern = '\n\n(.*?)\n\n'
19+
search_results = re.findall(pattern, text, overlapped=True, flags=re.DOTALL)
20+
songs = [song for song in search_results]
21+
print("Found {} songs in text".format(len(songs)))
22+
return songs
23+
24+
def save_song_to_abc(song, filename="tmp"):
25+
save_name = "{}.abc".format(filename)
26+
with open(save_name, "w") as f:
27+
f.write(song)
28+
return filename
29+
30+
def abc2wav(abc_file):
31+
# path_to_tool = './introtodeeplearning_labs/lab1/abc2wav'
32+
path_to_tool = os.path.join(os.path.dirname(__file__), 'bin', 'abc2wav')
33+
34+
cmd = "{} {}".format(path_to_tool, abc_file)
35+
return os.system(cmd)
36+
37+
def play_wav(wav_file):
38+
return Audio(wav_file)
39+
40+
def play_song(song):
41+
basename = save_song_to_abc(song)
42+
ret = abc2wav(basename+'.abc')
43+
if ret == 0: #did not suceed
44+
return play_wav(basename+'.wav')
45+
return None
46+
47+
def play_generated_song(generated_text):
48+
songs = extract_song_snippet(generated_text)
49+
if len(songs) == 0:
50+
print("No valid songs found in generated text. Try training the \
51+
model longer or increasing the amount of generated music to \
52+
ensure complete songs are generated!")
53+
54+
for song in songs:
55+
play_song(song)
56+
print("None of the songs were valid, try training longer to improve \
57+
syntax.")

__init__.py renamed to mitdeeplearning/util.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
from .lab1 import *
2-
from .lab2 import *
3-
# from lab3 import *
4-
5-
61
import matplotlib.pyplot as plt
7-
import numpy as np
82
import tensorflow as tf
93
import time
4+
import progressbar
5+
106
from IPython import display as ipythondisplay
7+
from string import Formatter
8+
9+
1110

1211

1312
#####################################
1413
def custom_progress_text(message):
15-
import progressbar
16-
from string import Formatter
1714

1815
message_ = message.replace('(', '{')
1916
message_ = message_.replace(')', '}')
@@ -29,7 +26,6 @@ def custom_progress_text(message):
2926
return msg
3027

3128
def create_progress_bar(text=None):
32-
import progressbar
3329
if text is None:
3430
text = progressbar.FormatCustomText('')
3531
bar = progressbar.ProgressBar(widgets=[
@@ -44,23 +40,7 @@ def display_model(model):
4440
tf.keras.utils.plot_model(model,
4541
to_file='tmp.png',
4642
show_shapes=True)
47-
from IPython.display import Image
48-
return Image('tmp.png')
49-
50-
51-
def plot_sample(x,y,vae):
52-
plt.figure(figsize=(2,1))
53-
plt.subplot(1, 2, 1)
54-
55-
idx = np.where(y.numpy()==1)[0][0]
56-
plt.imshow(x[idx])
57-
plt.grid(False)
58-
59-
plt.subplot(1, 2, 2)
60-
plt.imshow(vae(x)[idx])
61-
plt.grid(False)
62-
63-
plt.show()
43+
return ipythondisplay.Image('tmp.png')
6444

6545

6646
class LossHistory:
@@ -74,9 +54,6 @@ def get(self):
7454

7555
class PeriodicPlotter:
7656
def __init__(self, sec, xlabel='', ylabel='', scale=None):
77-
from IPython import display as ipythondisplay
78-
import matplotlib.pyplot as plt
79-
import time
8057

8158
self.xlabel = xlabel
8259
self.ylabel = ylabel

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def get_dist(pkgname):
1111
install_deps = [
1212
'numpy',
1313
'regex',
14+
'progressbar',
15+
'tqdm',
16+
1417
]
1518
tf_ver = '2.0.0a'
1619
if get_dist('tensorflow>='+tf_ver) is None and get_dist('tensorflow_gpu>='+tf_ver) is None:

test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import mitdeeplearning as mdl
2+
3+
songs = mdl.lab1.load_training_data()
4+
5+
basename = mdl.lab1.save_song_to_abc(songs[0])
6+
ret = mdl.lab1.abc2wav(basename+'.abc')
7+
8+
import pdb; pdb.set_trace()

0 commit comments

Comments
 (0)