From 53a76cb1ec5369769b007a03e18a87ef60f5754f Mon Sep 17 00:00:00 2001 From: Aoife Date: Fri, 2 Apr 2021 01:35:33 -0400 Subject: [PATCH] Correctly serialise the global part of patch --- src/features/io/convert/parse.js | 6 ++-- src/features/io/convert/saveStore.js | 2 +- src/features/io/convert/serialise.js | 53 ++++++++++++++++++++++++++-- src/features/io/utils/functions.js | 33 ++++++++++++++++- 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/features/io/convert/parse.js b/src/features/io/convert/parse.js index 527de11..63c7626 100644 --- a/src/features/io/convert/parse.js +++ b/src/features/io/convert/parse.js @@ -33,7 +33,7 @@ function build_patch(patch) { return { /* BYTES 0, 11 NOT 0, 12 */ program_name: String.fromCharCode(...patch.slice(0, 12)), - /* BYTES 13 / 14 SKIPPED */ + /* BYTES 12 / 13 SKIPPED */ arp_trigger_length: patch[14], arp_trigger_pattern: toBitArray(patch[15]), voice_mode: getBits(patch[16], 4, 5), @@ -52,11 +52,11 @@ function build_patch(patch) { eq_hi_gain: patch[27], eq_low_freq: patch[28], eq_low_gain: patch[29], - arp_tempo: patch.slice(30, 32).reduce((acc, curr) => acc + curr), + arp_tempo: (patch[30] << 8) + patch[31], arp_on_off: patch[32] & 0x80, arp_latch: patch[32] & 0x40, arp_target: getBits(patch[32], 4, 5), - arp_key_sync: patch[32] & 0x01, + arp_key_sync: patch[32] & 0x00, arp_type: getBits(patch[33], 0, 3), arp_range: getBits(patch[33], 4, 7), arp_gate_time: patch[34], diff --git a/src/features/io/convert/saveStore.js b/src/features/io/convert/saveStore.js index b67c3e6..d1d3b31 100644 --- a/src/features/io/convert/saveStore.js +++ b/src/features/io/convert/saveStore.js @@ -40,7 +40,7 @@ export default function saveStoreState(state) { { ...state_patch({ unused: { ...state.unusedPatch }, - patch: { ...state.Patch }, + patch: { ...state.patch }, arpeggA: { ...state.arpeggA }, arpeggB: { ...state.arpeggB }, delay: { ...state.delay }, diff --git a/src/features/io/convert/serialise.js b/src/features/io/convert/serialise.js index a033c7b..2e0c83d 100644 --- a/src/features/io/convert/serialise.js +++ b/src/features/io/convert/serialise.js @@ -1,5 +1,54 @@ +import { fromBitArray, buildByte } from '../utils/functions'; + export default function serialise(patch) { - serialise_patch(patch); + serialise_patch(patch[0]); } -function serialise_patch(patch) {} +function serialise_patch(patch) { + const patch_array = Array.from(Buffer.from(patch.program_name, 'ascii')); + patch_array.push( + 0, + 0, + patch.arp_trigger_length, + fromBitArray(patch.arp_trigger_pattern), + buildByte({ value: patch.voice_mode, start: 4 }), + buildByte( + { value: patch.scale_key, start: 4 }, + { value: patch.scale_type, start: 0 } + ), + 0, + buildByte( + { value: patch.delay_sync, start: 7 }, + { value: patch.delay_timebase, start: 0 } + ), + patch.delay_time, + patch.delay_depth, + patch.delay_type, + patch.mod_lfo_speed, + patch.mod_depth, + patch.mod_type, + patch.eq_hi_freq, + patch.eq_hi_gain, + patch.eq_low_freq, + patch.eq_low_gain, + (patch.arp_tempo >> 8) & 0xff, + patch.arp_tempo & 0xff, + buildByte( + { value: patch.arp_switch, start: 7 }, + { value: patch.arp_latch, start: 6 }, + { value: patch.arp_target, start: 4 }, + { value: patch.arp_key_sync, start: 0 } + ), + buildByte( + { value: patch.arp_type, start: 0 }, + { value: patch.arp_range, start: 4 } + ), + patch.arp_gate_time, + patch.arp_resolution, + patch.arp_swing, + patch.keyboard_octave + ); + console.log(patch_array); +} + +function serialise_timbre(timbre) {} diff --git a/src/features/io/utils/functions.js b/src/features/io/utils/functions.js index 5e1747d..72eb4cf 100644 --- a/src/features/io/utils/functions.js +++ b/src/features/io/utils/functions.js @@ -1,5 +1,19 @@ const toBitArray = (n, b = 8) => [...Array(b)].map((x, i) => (n >> i) & 1); +const fromBitArray = (array) => { + return ( + 0 | + (array[0] << 7) | + (array[1] << 6) | + (array[2] << 5) | + (array[3] << 4) | + (array[4] << 3) | + (array[5] << 2) | + (array[6] << 1) | + (array[7] << 0) + ); +}; + const getBits = (byte, indexStart, indexEnd = 7) => parseInt( toBitArray(byte) @@ -9,6 +23,15 @@ const getBits = (byte, indexStart, indexEnd = 7) => 2 ); +function buildByte(...args) { + // we take an objects, with the value, and the bits it takes up. + let byte = 0; + for (const element of arguments) { + byte = byte | (element.value << element.start); + } + return byte; +} + const signedNumber = (byte) => (byte > 63 ? (byte ^ 64) - 64 : byte); console.assert(1 === signedNumber(1)); @@ -29,4 +52,12 @@ function returnValue(setting, list) { } } -export { toBitArray, getBits, signedNumber, returnFromList, returnValue }; +export { + toBitArray, + fromBitArray, + getBits, + buildByte, + signedNumber, + returnFromList, + returnValue, +};