Skip to content

Instantly share code, notes, and snippets.

@mmckegg
Created November 18, 2014 00:46
Show Gist options
  • Save mmckegg/d7485ddbca53b5fbddb8 to your computer and use it in GitHub Desktop.
Save mmckegg/d7485ddbca53b5fbddb8 to your computer and use it in GitHub Desktop.
Upgrade Loop Drop 0.x kits to 1.x chunks and setups
var ArrayGrid = require('array-grid')
var fs = require('fs')
var map = {
"Floating.json": [
{ "from": "Meowra-Floating A.json",
"chunks": {
"Spit on a stranger": [0,0, 3,3, "A"],
"Crunk Bass": [2,4, 3,6, "C"],
"Overdrum": [6,0, 7,3, "B"],
"Softdrum": [7,4, 7,7, "E"]
}
},
{ "from": "Meowra-Floating B.json",
"chunks": {
"Floating Lead": [0,0, 3,3, "A"],
"Floating Tinkle": [0,4, 3,7, "E"]
}
}
],
"Hold Me Up.json": [
{ "from": "Meowra-Hold Me Up A.json",
"chunks": {
"HMU Filterbass": [0,6, 0, 7, "A"],
"HMU Atmos": [1,0, 1,5, "B"],
"HMU Bongles": [2,0, 2,7, "C"],
"HMU Drums": [3,0, 3,2, "D"],
"HMU Bass": [4,4, 4,7, "E"]
}
},
{ "from": "Meowra-Hold Me Up B.json",
"chunks": {
"HMU 8bit": [0,0, 3,0, "F"],
"HMU 8bit FX": [0,1, 3,1],
"HMU Delay": [5,6, 5,7]
}
}
],
"Losing My Mind.json": [
{ "from": "Meowra-Losing My Mind A.json",
"chunks": {
"LMM Crashdrum": [0,0, 0,1, "A"],
"LMM Quickdrum": [0,2, 0,4, "B"],
"LMM Crashat": [3,3,3,3, "A"]
}
},
{ "from": "Meowra-Losing My Mind B.json",
"chunks": {
"LMM Wub": [2,0, 5,0, "C"],
"LMM Guilt": [0,2, 2,2, "A"],
"LMM Synth": [0,3,2,5, "B"]
}
}
],
"Replay.json": [
{ "from": "Meowra-Replay A.json",
"chunks": {
"RP Drums": [0,0, 3,3, "A"],
"RP Bass": [0,7, 3,7, "H"],
"RP Bass Alt": [0,6,2,6, "H"],
"RP Mashup": [6,0, 7,3],
"RP FX": [6,4, 6,7]
}
},
{ "from": "Meowra-Replay B.json",
"chunks": {
"RP Guit": [0,0, 0,3, "A"],
"RP Tinker": [2,0, 2,4, "B"],
"RP Cake": [0,5,4,5, "F"]
}
}
],
"Respect, D.json": [
{ "from": "Meowra-Respect, D A.json",
"chunks": {
"RD Drums": [0,0, 0,1, "A"],
"RD Guit": [0,4, 1,6, "B"],
"RD Vox": [2,4, 2,7, "A"],
"RD Bass": [4,4, 5,5, "C"]
}
},
{ "from": "Meowra-Respect, D B.json",
"chunks": {
"RD Synth": [0,0, 7,3, "A"]
}
}
],
"Show.json": [
{ "from": "Meowra-Show A.json",
"chunks": {
"S Drums": [4,4, 5,7, "A"],
"S FX": [0,4, 3,7]
}
},
{ "from": "Meowra-Show B.json",
"chunks": {
"S Vox": [0,0, 2,3, "A"],
"S Guit": [0,4, 2,4, "E"],
"S Synth": [0,6, 2,6],
"S Bit": [4,0, 5,7]
}
}
],
"Tongues and Glances.json": [
{ "from": "Meowra-Tongues A.json",
"chunks": {
"S Oldsko A": [4,2, 4,3, "A"],
"S Oldsko B": [7,1, 7,2, "A"],
"S Kick": [6,0, 6,0, "A"],
"S Hat": [6,3, 6,3, "A"],
"S Lowpipe": [0,3, 3,3, "B"],
"S Bass": [0,4, 3,7, "B"]
}
},
{ "from": "Meowra-Tongues B.json",
"chunks": {
"S Leader": [4,0, 7,3, "B"],
"S Bagpipes": [4,4, 7,7, "A"],
"S Sigits": [0,5, 3,5, "H"],
"S Tinkles": [0,7, 3,7, "H"]
}
}
],
"Wolves.json": [
{ "from": "Meowra-Wolves A.json",
"chunks": {
"W Drums": [0,0, 0,3, "A"],
"W Bass": [0,4, 3,4, "E"],
"W FX": [0,5, 5,5, "F"],
"Mute Input": [7,0, 7,0],
"Overdrive Input": [5,0, 5,0],
"W Funk": [0,6, 1,6]
}
},
{ "from": "Meowra-Wolves B.json",
"chunks": {
"W Lowguit": [0,4, 4,5, "E"],
"W Higuit": [1,0, 1,3, "B"],
"W Synth": [0,0, 0,3, "A"],
"W PSynth": [6,0, 6,3, "A"],
"W Mood": [0,6, 1,6, "G"],
"W Drop": [0,7, 1,7, "H"],
}
}
]
}
var samples = []
var root = "/Users/matt/Documents/Loop Drop/kits"
var sampleRoot = "/Users/matt/Documents/Loop Drop/samples"
var projectRoot = "/Users/matt/Projects/Meowra/Drop"
var midiPorts = ['Launchpad Mini', 'Launchpad Mini 2']
Object.keys(map).forEach(function(setupName){
var setup = {
node: 'setup',
controllers: [],
chunks: []
}
var controllers = map[setupName]
controllers.forEach(function(controllerMap, i){
var controller = {
node: 'controller/launchpad',
port: midiPorts[i] || null,
chunkPositions: {}
}
var kit = getKitLookup(controllerMap.from)
Object.keys(controllerMap.chunks).forEach(function(chunkName){
var range = controllerMap.chunks[chunkName]
var data = JSON.stringify(getChunk(kit, range))
fs.writeFileSync(projectRoot + '/chunks/' + chunkName + '.json', data)
controller.chunkPositions[chunkName] = [range[0], range[1]]
setup.chunks.push({
node: 'external',
src: 'chunks/' + chunkName + '.json',
id: chunkName
})
})
setup.controllers.push(controller)
})
var data = JSON.stringify(setup)
fs.writeFileSync(projectRoot + '/setups/' + setupName, data)
})
samples.forEach(function(sample){
fs.createReadStream(sampleRoot + '/' + sample)
.pipe(fs.createWriteStream(projectRoot + '/samples/' + sample))
})
function appendSamples(slots){
slots&&slots.forEach(function(slot){
if (slot.url && !~samples.indexOf(slot.url)){
samples.push(slot.url)
}
})
}
function getKitLookup(filePath){
var kit = JSON.parse(fs.readFileSync(root + '/' + filePath, 'utf8'))
var slots = []
var busses = {}
kit.slots.forEach(function(slot){
if (isFinite(slot.id)){
slots[parseInt(slot.id)] = slot
} else {
busses[slot.id] = slot
}
})
return {
slots: slots,
busses: busses
}
}
function getChunk(lookup, range){
var grid = ArrayGrid(lookup.slots, [8,8])
var slots = []
var triggerSlots = []
var chunk = {
node: 'chunk',
triggerSlots: triggerSlots,
shape: [range[2]-range[0]+1, range[3]-range[1]+1],
slots: slots,
outputs: ['output'],
color: randomColor()
}
var chunkGrid = ArrayGrid([], chunk.shape)
for (var r=range[0];r<=range[2];r++){
for (var c=range[1];c<=range[3];c++){
var slot = obtain(grid.get(r,c))
;delete slot.id
;delete slot.output
if (slot.from){
var coords = grid.coordsAt(parseInt(slot.from))
if (coords){
slot.from = String(chunkGrid.index(coords[0]-range[0], coords[1]-range[1]))
}
}
if (Object.keys(slot).length){
triggerSlots.push(slot)
} else {
triggerSlots.push(null)
}
if (slot.sources){
appendSamples(slot.sources)
}
}
}
if (lookup.busses[range[4]]){
var slot = obtain(lookup.busses[range[4]])
slot.id = 'output'
;delete slot.output
slots.push(slot)
} else {
slots.push({
id: 'output'
})
}
return chunk
}
function obtain(obj){
return JSON.parse(JSON.stringify(obj))
}
function randomColor(){
return [
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256)
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment