-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_abc.py
112 lines (86 loc) · 3.31 KB
/
generate_abc.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
# The make model and generate sequence functions are derived from the repo at
# https://github.com/gauravtheP/Music-Generation-Using-Deep-Learning
# The newer models independent of this original repo will consist of genre wise music generation
# And a model with a deeper layers
from tensorflow.keras.layers import LSTM, Dropout, Dense, Activation, Embedding
from tensorflow.keras.models import Sequential
from tensorflow.keras import backend
import numpy as np
import os
import sys
import json
sys.path.append('/home/runner/.site-packages/')
charIndex_json = "static/json/char_to_index.json"
BATCH_SIZE = 16
SEQ_LENGTH = 64
def make_model(unique_chars):
model = Sequential()
model.add(Embedding(input_dim=unique_chars,
output_dim=512, batch_input_shape=(1, 1)))
model.add(LSTM(256, return_sequences=True, stateful=True))
model.add(Dropout(0.2))
model.add(LSTM(256, return_sequences=True, stateful=True))
model.add(Dropout(0.2))
model.add(LSTM(256, stateful=True))
model.add(Dropout(0.2))
model.add((Dense(unique_chars)))
model.add(Activation("softmax"))
return model
def generate_sequence(initial_index, seq_length):
with open(os.path.join(charIndex_json)) as f:
char_to_index = json.load(f)
index_to_char = {i: ch for ch, i in char_to_index.items()}
unique_chars = len(index_to_char)
backend.clear_session()
model = make_model(unique_chars)
model.load_weights('static/weights/Weights_90.h5')
sequence_index = [initial_index]
for _ in range(seq_length):
batch = np.zeros((1, 1))
batch[0, 0] = sequence_index[-1]
predicted_probs = model.predict_on_batch(batch).ravel()
sample = np.random.choice(
range(unique_chars), size=1, p=predicted_probs)
sequence_index.append(sample[0])
print(len(sequence_index))
seq = ''.join(index_to_char[c] for c in sequence_index)
cnt = 0
for i in seq:
cnt += 1
if i == "\n":
break
seq1 = seq[cnt:]
cnt = 0
for i in seq1:
cnt += 1
if i == "\n" and seq1[cnt] == "\n":
break
seq2 = seq1[:cnt]
return seq2
# ar = Any number between 0 to 86 which will be given as initial charcter to model for generating sequence
# ln = The length of music sequence you want to generate. Typical number is between 300-600. Too small number will generate hardly generate any sequence
# instr = The instrument code is detailed in static/csv/abcmidi_instrument_name
def generate_abc_file(ar, ln, instr):
ar = max(ar % 87, 0)
ln = max(ln, 600)
instr = max(instr % 129, 1)
music = generate_sequence(ar, ln)
print("\nMUSIC SEQUENCE GENERATED: \n")
print(music)
with open('static/abc/generated.abc', 'w') as abc:
if music.find('X:') == -1:
abc.write('X:1\n')
if music.find('T:') == -1:
abc.write('T:RNN generated\n')
if music.find('M:') == -1:
abc.write('M:3/4\n')
if music.find('L:') == -1:
abc.write('L:1/8\n')
if music.find('Q:') == -1:
abc.write('Q:1/4=120\n')
if music.find('W:') == -1:
abc.write("W:Generated music\n")
if music.find('K:') == -1:
abc.write('K:C\n')
abc.write(f'%%MIDI program {instr}\n')
abc.write(music)