|
|
|
|
|
class MiamuyMidiModel { |
|
|
|
|
|
|
|
|
static async getInstance(progress_callback = null) { |
|
|
return new MiamuyMidiModel(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async generate(prompt, options = {}) { |
|
|
|
|
|
const noteNames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']; |
|
|
|
|
|
|
|
|
const startNote = this.noteToMidi(prompt); |
|
|
|
|
|
|
|
|
const sequenceLength = options.length || 8; |
|
|
|
|
|
let generatedNotes = [startNote]; |
|
|
|
|
|
|
|
|
for (let i = 1; i < sequenceLength; i++) { |
|
|
|
|
|
const nextNote = startNote + Math.floor(Math.random() * 11) - 5; |
|
|
generatedNotes.push(nextNote); |
|
|
} |
|
|
|
|
|
|
|
|
const formattedNotes = generatedNotes.map(note => { |
|
|
const octave = Math.floor(note / 12) - 1; |
|
|
const noteIndex = note % 12; |
|
|
return `${noteNames[noteIndex]}${octave}`; |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return [{ |
|
|
generated_text: formattedNotes.join(' '), |
|
|
midi_notes: generatedNotes |
|
|
}]; |
|
|
} |
|
|
|
|
|
|
|
|
noteToMidi(note) { |
|
|
const noteMap = { |
|
|
'C': 0, 'C#': 1, 'D': 2, 'D#': 3, 'E': 4, 'F': 5, |
|
|
'F#': 6, 'G': 7, 'G#': 8, 'A': 9, 'A#': 10, 'B': 11 |
|
|
}; |
|
|
|
|
|
const octave = parseInt(note.slice(-1)); |
|
|
const noteName = note.slice(0, -1); |
|
|
|
|
|
return noteMap[noteName] + (octave + 1) * 12; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export default MiamuyMidiModel; |