Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
CursedPrograms committed Feb 11, 2024
1 parent a8a962e commit ab999e8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ __pycache__/
*$py.class

output/
input/

# C extensions
*.so
Expand Down
23 changes: 20 additions & 3 deletions scripts/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from tensorflow.keras.layers import LSTM, Dense
from music21 import stream, note, midi
from datetime import datetime
import json

# Assume 'dataset' is your generated or real music dataset
with open('settings.json') as f:
settings = json.load(f)

# Function to generate a synthetic dataset
def generate_dataset(num_samples=1000, sequence_length=50, num_classes=128):
Expand All @@ -24,6 +29,10 @@ def sequence_to_stream(sequence):
def generate_music(model, seed_sequence, length=1000, temperature=1.0):
generated_sequence = seed_sequence.copy()

# Get user input for the length and temperature
length = settings.get("length", input("Enter the length of the generated sequence: "))
temperature = settings.get("temperature", input("Enter the temperature for randomness (e.g., 0.5 for more randomness, 2.0 for less randomness): "))

for _ in range(length):
# Predict the next set of notes
next_notes_prob = model.predict(np.expand_dims(generated_sequence, axis=0))
Expand Down Expand Up @@ -66,6 +75,10 @@ def build_model(input_shape, output_shape):
dataset = generate_dataset()
X, y = dataset['input'], dataset['output']

# Get user input for the number of epochs and batch size if not provided in settings
epochs = settings.get("epochs", int(input("Enter the number of epochs: ")))
batch_size = settings.get("batch_size", int(input("Enter the batch size: ")))

# Assume 'model' is your trained LSTM model
# Make sure to replace 'input_shape' and 'output_shape' with the actual shapes from your dataset
input_shape = X.shape[1:]
Expand All @@ -75,7 +88,7 @@ def build_model(input_shape, output_shape):
model = build_model(input_shape, output_shape)

# Train the model
model.fit(X, y, epochs=50, batch_size=64)
model.fit(X, y, epochs=epochs, batch_size=batch_size)

# Save the trained model with a timestamp
model_timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
Expand All @@ -87,12 +100,16 @@ def build_model(input_shape, output_shape):
# Example usage
seed_sequence = X[np.random.randint(0, X.shape[0])] # Use a random sequence from your dataset as a seed

# Get user input for the length and temperature if not provided in settings
length = settings.get("length", int(input("Enter the length of the generated sequence: ")))
temperature = settings.get("temperature", float(input("Enter the temperature for randomness: ")))

# Generate music with the loaded model
generated_music_sequence = generate_music(loaded_model, seed_sequence, length=100)
generated_music_sequence = generate_music(loaded_model, seed_sequence, length=length, temperature=temperature)

# Convert the generated sequence to a music21 stream for visualization
generated_music_stream = sequence_to_stream(generated_music_sequence)

# Save the generated music to a MIDI file with a timestamp
midi_filename = save_to_midi(generated_music_stream, 'output/generated_output')
print(f'Generated music saved to: {midi_filename}')
print(f'Generated music saved to: {midi_filename}')
6 changes: 6 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"epochs": 50,
"batch_size": 64,
"length": 1000,
"temperature": 1.0
}

0 comments on commit ab999e8

Please sign in to comment.