-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3819e95
commit 126c599
Showing
19 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Configuration for the music generation model | ||
BATCH_SIZE = 64 | ||
EPOCHS = 100 | ||
LEARNING_RATE = 0.001 | ||
MAX_SEQUENCE_LENGTH = 50 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import music21 | ||
from midiutil import MIDIFile | ||
|
||
def midi_to_notes(midi_file): | ||
notes = [] | ||
midi = music21.converter.parse(midi_file) | ||
for element in midi.flat.notes: | ||
if isinstance(element, music21.note.Note): | ||
notes.append(str(element.pitch)) # could convert to MIDI number here if needed | ||
return notes | ||
|
||
def create_midi_file(notes, filename): | ||
midi = MIDIFile(1) | ||
track = 0 | ||
time = 0 | ||
|
||
midi.addTrackName(track, time, "Generated Music") | ||
midi.addTempo(track, time, 60) | ||
|
||
for note in notes: | ||
midi.addNote(track, 0, int(note), time, 1, 100) # Ensure notes are in int format | ||
time += 1 | ||
|
||
with open(filename, "wb") as f: | ||
midi.writeFile(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import numpy as np | ||
from keras.models import load_model | ||
from midi_utils import create_midi_file | ||
|
||
def generate_music(model, start_seed, num_notes=100): | ||
generated = start_seed | ||
for _ in range(num_notes): | ||
input_sequence = np.array(generated[-50:]).reshape(1, 50) | ||
predicted = model.predict(input_sequence, verbose=0) | ||
next_note = np.argmax(predicted) | ||
generated.append(next_note) | ||
return generated | ||
|
||
if __name__ == "__main__": | ||
model = load_model('music_model.h5') | ||
start_seed = [60, 62, 64] # Example seed (MIDI note values) | ||
generated_notes = generate_music(model, start_seed) | ||
create_midi_file(generated_notes, 'generated_music.mid') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import music21 | ||
from midiutil import MIDIFile | ||
|
||
def midi_to_notes(midi_file): | ||
notes = [] | ||
midi = music21.converter.parse(midi_file) | ||
for element in midi.flat.notes: | ||
if isinstance(element, music21.note.Note): | ||
notes.append(str(element.pitch)) # could convert to MIDI number here if needed | ||
return notes | ||
|
||
def create_midi_file(notes, filename): | ||
midi = MIDIFile(1) | ||
track = 0 | ||
time = 0 | ||
|
||
midi.addTrackName(track, time, "Generated Music") | ||
midi.addTempo(track, time, 60) | ||
|
||
for note in notes: | ||
midi.addNote(track, 0, int(note), time, 1, 100) # Ensure notes are in int format | ||
time += 1 | ||
|
||
with open(filename, "wb") as f: | ||
midi.writeFile(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from keras.models import Sequential | ||
from keras.layers import LSTM, Dense, Embedding | ||
|
||
def create_model(vocab_size, max_sequence_length): | ||
model = Sequential() | ||
model.add(Embedding(input_dim=vocab_size, output_dim=256, input_length=max_sequence_length)) | ||
model.add(LSTM(256, return_sequences=True)) | ||
model.add(LSTM(256)) | ||
model.add(Dense(vocab_size, activation='softmax')) | ||
|
||
model.compile(loss='categorical_crossentropy', optimizer='adam') | ||
return model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
numpy | ||
tensorflow>=2.0.0 | ||
keras | ||
music21 | ||
midiutil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import numpy as np | ||
from keras.models import Sequential | ||
from keras.layers import LSTM, Dense, Embedding | ||
from keras.utils import to_categorical | ||
from midi_utils import midi_to_notes | ||
|
||
def prepare_data(notes): | ||
# Convert notes to integers or one-hot encoding | ||
# This function must be implemented. | ||
# Placeholder: Assume we return X, y from preprocessing | ||
pass | ||
|
||
def create_model(vocab_size, max_sequence_length): | ||
model = Sequential() | ||
model.add(Embedding(input_dim=vocab_size, output_dim=256, input_length=max_sequence_length)) | ||
model.add(LSTM(256, return_sequences=True)) | ||
model.add(LSTM(256)) | ||
model.add(Dense(vocab_size, activation='softmax')) | ||
model.compile(loss='categorical_crossentropy', optimizer='adam') | ||
return model | ||
|
||
def train_model(): | ||
notes = midi_to_notes('dataset/your_midi_file1.mid') # Load your dataset | ||
X, y = prepare_data(notes) # Process your data | ||
model = create_model(vocab_size=len(set(notes)), max_sequence_length=50) # Adjust variables as needed | ||
model.fit(X, to_categorical(y), epochs=100, batch_size=64) | ||
model.save('music_model.h5') | ||
|
||
if __name__ == "__main__": | ||
train_model() |