Skip to content

Instantly share code, notes, and snippets.

@OverlappingElvis
Last active December 23, 2020 22:51
Show Gist options
  • Save OverlappingElvis/09a6d402c2715c12489be5d3808533c8 to your computer and use it in GitHub Desktop.
Save OverlappingElvis/09a6d402c2715c12489be5d3808533c8 to your computer and use it in GitHub Desktop.
midi-to-blobs
const fs = require(`fs`)
const _ = require(`lodash`)
const { Player } = require(`midi-player-js`)
const VOWELS = [`a`, `e`, `i`, `o`, `u`]
const CONSONANTS = [`p`, `g`, `f`, `c`, `qu`, `h`, `j`, `n`, `l`, `m`, `t`, `d`, `ch`, `r`]
const player = new Player()
player.loadFile(`./test.mid`)
const songTime = player.getSongTime()
const allEvents = player.getEvents()
const MAX_TICKS = _.last(_.maxBy(allEvents, (events) => _.last(events).tick)).tick
const parsedEvents = allEvents.map((track) => {
return track.filter(event => event.name === `Note on`).map((event) => {
return {
timeSeconds: (event.tick / MAX_TICKS) * songTime,
midiPitch: event.noteNumber,
librettoChunk: {
vowel: {
name: _.sample(VOWELS),
duration: 0.20000000298023224
},
suffix: [
{
name: _.sample(CONSONANTS),
duration: 0.10000000149011612
}
]
}
}
})
}).filter(track => !_.isEmpty(track)).map((track) => {
return {
notes: track,
startSuffix: [
{
name: _.sample(CONSONANTS),
duration: 0.10000000149011612
}
]
}
})
const song = {
theme: 1,
parts: parsedEvents
}
fs.writeFile(`test.json`, JSON.stringify(song), () => console.log(`done`))
@OverlappingElvis
Copy link
Author

I think this might be getting out of hand
image

@The-King-of-Toasters
Copy link

I'm confused, how do you get the blobs to sing with the test.json file?

@OverlappingElvis
Copy link
Author

I'm confused, how do you get the blobs to sing with the test.json file?

If you set a breakpoint in the blob opera script where sample songs are loaded, you can sideload your custom json (see https://twitter.com/Overlapping/status/1338979945792466944)

@OverlappingElvis
Copy link
Author

This must now be called with a midi file argument: node index.js <song.mid>

@OverlappingElvis
Copy link
Author

Check out https://github.com/OverlappingElvis/blob-opera-midi, now a standalone package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment