-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
2 changed files
with
49 additions
and
52 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 |
---|---|---|
@@ -1,62 +1,59 @@ | ||
import { Midi } from "@tonejs/midi" | ||
import { Midi } from '@tonejs/midi' | ||
import fs from 'fs' | ||
import { Song, Track, Tracks, Bpm, SongNote } from '@/types' | ||
|
||
export function convertBase64MidiToSong(base64Midi: string) { | ||
const binaryMidi = Buffer.from(decodeURIComponent(base64Midi), 'base64') | ||
console.log(binaryMidi) | ||
const midi = new Midi(binaryMidi) | ||
console.log(midi) | ||
let tracks: Tracks = {} | ||
const bpms: Array<Bpm> = [] | ||
const notes: Array<SongNote> = [] | ||
|
||
let index: number = 0 | ||
for (const track of midi.tracks) { | ||
const newTrack: Track = { | ||
instrument: track.instrument.name.replace(/ /g,"_"), | ||
name: "Track" + index | ||
} | ||
tracks[index] = {...newTrack} | ||
for (const note of track.notes) { | ||
const newNote: SongNote = { | ||
type: "note", | ||
midiNote: note.midi, | ||
duration: note.duration, | ||
track: index, | ||
velocity: note.velocity, | ||
time: note.time, | ||
measure: note.bars // ?? | ||
} | ||
notes.push(newNote) | ||
} | ||
const binaryMidi = Buffer.from(decodeURIComponent(base64Midi), 'base64') | ||
const midi = new Midi(binaryMidi) | ||
let tracks: Tracks = {} | ||
const bpms: Array<Bpm> = [] | ||
const notes: Array<SongNote> = [] | ||
|
||
index++ | ||
let index: number = 0 | ||
for (const track of midi.tracks) { | ||
const newTrack: Track = { | ||
instrument: track.instrument.name.replace(/ /g, '_'), | ||
name: 'Track' + index, | ||
} | ||
tracks[index] = { ...newTrack } | ||
for (const note of track.notes) { | ||
const newNote: SongNote = { | ||
type: 'note', | ||
midiNote: note.midi, | ||
duration: note.duration, | ||
track: index, | ||
velocity: note.velocity, | ||
time: note.time, | ||
measure: note.bars, // ?? | ||
} | ||
notes.push(newNote) | ||
} | ||
|
||
index++ | ||
} | ||
|
||
for (const tempos of midi.header.tempos) { | ||
const newBpm: Bpm = { | ||
time: tempos.time? tempos.time : 0, | ||
bpm: tempos.bpm | ||
} | ||
bpms.push(newBpm) | ||
for (const tempos of midi.header.tempos) { | ||
const newBpm: Bpm = { | ||
time: tempos.time ? tempos.time : 0, | ||
bpm: tempos.bpm, | ||
} | ||
|
||
// Not enough info to fill everything | ||
const song: Song = { | ||
tracks: tracks, | ||
duration: midi.duration, | ||
measures: [], | ||
notes: notes, | ||
bpms: bpms, | ||
timeSignature: undefined, | ||
keySignature: 'C', | ||
items: notes, | ||
backing: undefined | ||
}; | ||
bpms.push(newBpm) | ||
} | ||
|
||
console.log(song) | ||
// Not enough info to fill everything | ||
const song: Song = { | ||
tracks: tracks, | ||
duration: midi.duration, | ||
measures: [], | ||
notes: notes, | ||
bpms: bpms, | ||
timeSignature: undefined, | ||
keySignature: 'C', | ||
items: notes, | ||
backing: undefined, | ||
} | ||
|
||
return song | ||
} | ||
console.log(song) | ||
|
||
return song | ||
} |
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