-
Notifications
You must be signed in to change notification settings - Fork 116
/
playlist.py
executable file
·230 lines (211 loc) · 7.79 KB
/
playlist.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Code taken directly from:
# https://stackoverflow.com/questions/33417151/playing-mp3-in-a-folder-with-jupyter-notebook
# (I modified it only to sort the playlist and to play only .wav files)
# Refer also to:
# http://devblog.lastrose.com/html5-audio-video-playlist/
import os
def make_playlist(music_dir):
playlist_html=""
audio_html=""
count=0
for root, dirs, files in os.walk(music_dir):
for file in sorted(files):
if file[-3:]!="wav":
continue
if count==0:
playlist_html = u'''<li class="active"><a href="{0}">{1}</a>
</li>\n'''.format(os.path.join(root, file), file)
audio_html = u'''<audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
<source type="audio/mp3" src="{}">Sorry, your browser does not support HTML5 audio.
</audio>'''.format(os.path.join(root, file))
else:
playlist_html +=u'''<li><a href="{0}">{1}</a></li>\n'''.format(os.path.join(root, file), file)
count += 1
playlist_html = audio_html + u'''\n<ol id="playlist">\n{}</ol>'''.format(playlist_html)
playlist_css = """
<style>
#playlist .active a{color:#CC0000;text-decoration:none;}
#playlist li a:hover{text-decoration:none;}
</style>
"""
playlist_js = """
<script>
var audio;
var playlist;
var tracks;
var current;
init();
function init(){
current = 0;
audio = $('audio');
playlist = $('#playlist');
tracks = playlist.find('li a');
len = tracks.length - 1;
audio[0].volume = .90;
playlist.find('a').click(function(e){
e.preventDefault();
link = $(this);
current = link.parent().index();
run(link, audio[0]);
});
audio[0].addEventListener('ended',function(e){
current++;
if(current == len){
current = 0;
link = playlist.find('a')[0];
}else{
link = playlist.find('a')[current];
}
run($(link),audio[0]);
});
}
function run(link, player){
player.src = link.attr('href');
par = link.parent();
par.addClass('active').siblings().removeClass('active');
audio[0].load();
audio[0].play();
}
</script>
"""
return playlist_html, playlist_css, playlist_js
def make_http_playlist(music_dir):
# Website Colors: ffe74c-ff5964-ffffff-38618c-35a7ff
playlist_html=""
audio_html=""
head=""
end_html=""
image_html=""
count=0
for root, dirs, files in os.walk(music_dir):
for file in sorted(files):
if file[-3:]!="wav":
continue
if file[:4]=="prom":
continue
if count==0:
head=u'''<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head> <body><div class="jumbotron">
<h2 class="display-4" align="center">Clara: A Musical Neural Net Generator</h2>
<p align="center" class="lead">by Christine Payne</p>
</div>'''
image_html=u'''<div id="composing" class="center" text-align="center"><h2>Composing new pieces, please wait...</h2><br><img src="./composing.gif"></div>'''
audio_html = u'''<div id="view_playlist" class="center"><audio id="audio" preload="auto" tabindex="0" controls="" type="audio/wav">
<source type="audio/wav" src="{}">Sorry, your browser does not support HTML5 audio.
</audio>
'''.format(os.path.join(root, file))
playlist_html = u'''<li class="active"><a href="{0}">{1}</a>
</li>\n'''.format(os.path.join(root, file), file)
else:
playlist_html +=u'''<li><a href="{0}">{1}</a></li>\n'''.format(os.path.join(root, file), file)
count += 1
end_html=u'''<a class="btn btn-outline-secondary" href="./music" role="button" id="new">Generate New Songs (Takes
~1 Minute)</a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>'''
playlist_html = head + image_html + audio_html + u'''\n<ul id="playlist">\n{}</ul>'''.format(playlist_html) + end_html
playlist_css = """
<style>
hr {
border-top: 3px double #38618c;
width: 80%;
}
#playlist .active a{color:#FF5964;text-decoration:none;}
#playlist li a:hover{text-decoration:none;color:#38618c}
ul {
columns: 3;
-webkit-columns: 3;
-moz-columns: 3;
}
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 40%;
}
.center {
margin: auto;
width: 80%;
border: 3px solid #35a7ff;
padding: 10px;
}
</style>
"""
playlist_js = """
<script>
var audio;
var playlist;
var next_button;
var new_button;
var view_playlist;
var composing_image;
var tracks;
var current;
init();
function init(){
current = 0;
audio=$('audio');
playlist = $('#playlist');
next_button = $('#next');
new_button = $('#new');
view_playlist = $('#view_playlist');
composing_image = $('#composing');
composing_image.hide();
view_playlist.show();
tracks = playlist.find('li a');
len = tracks.length - 1;
audio[0].volume = .99;
playlist.find('a').click(function(e){
e.preventDefault();
link = $(this);
current = link.parent().index();
run(link, audio[0]);
});
next_button.click(function(){
console.log("Clicked button")
current++;
if(current == len){
current = 0;
link = playlist.find('a')[0];
}else{
link = playlist.find('a')[current];
}
run($(link),audio[0]);
});
new_button.click(function(){
console.log("Create new generations")
view_playlist.hide();
composing_image.show();
});
audio[0].addEventListener('ended',function(e){
current++;
if(current == len){
current = 0;
link = playlist.find('a')[0];
}else{
link = playlist.find('a')[current];
}
run($(link),audio[0]);
});
}
function run(link, player){
player.src = link.attr('href');
par = link.parent();
par.addClass('active').siblings().removeClass('active');
audio[0].load();
audio[0].play();
}
</script>
"""
return playlist_html, playlist_css, playlist_js