Skip to content

Commit d91ce11

Browse files
committed
properly parse file values into display values, and fix some bits of logic with save and save as new
1 parent f01e0b0 commit d91ce11

File tree

8 files changed

+132
-48
lines changed

8 files changed

+132
-48
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"electron-is-dev": "^1.1.0",
2222
"electron-store": "^5.1.0",
2323
"immutability-helper": "^3.0.1",
24+
"node-sass-chokidar": "^1.4.0",
2425
"rc-slider": "^8.7.1",
2526
"react": "^16.12.0",
2627
"react-dnd": "^10.0.2",
@@ -38,7 +39,6 @@
3839
"electron": "7.1.8",
3940
"electron-builder": "^21.2.0",
4041
"eslint": "^6.8.0",
41-
"node-sass-chokidar": "^1.4.0",
4242
"wait-on": "^3.3.0",
4343
"yarn-run-all": "^3.1.1"
4444
},

public/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"short_name": "MY-APP",
3-
"name": "MY-APP",
2+
"short_name": "SAMPLEPAD-KIT-EDITOR",
3+
"name": "SAMPLEPAD-KIT-EDITOR",
44
"icons": [
55
{
66
"src": "assets/favicon.ico",

src/component/Pad.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const PadComponent = (props) => {
5252
onChange={(value) => props.updatePadIntProperty("tune", value)}/>
5353

5454
<SlideComponent
55-
min={0}
55+
min={1}
5656
max={8}
5757
tooltip={'Sensitivity: '}
5858
icon={'screenshot'}

src/component/PadControl/MuteGroup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class MuteGroupComponent extends React.Component {
7474
{
7575
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16].map((mgrp) => {
7676
return <span
77+
key={mgrp}
7778
className={"mgrpIcon " + this.getMgrpForegroundClass(mgrp)}
7879
onClick={(e) => {this.props.onChange(mgrp);this.handleClose();}}
7980
style={{backgroundColor: this.getMgrpBackgroundColor(mgrp)}}>

src/component/PadControl/Slide.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ const SlideComponent = (props) => {
1616

1717
const getColor = (value) => {
1818
let range = max - min
19-
let step = 200/range;
20-
21-
let color = (200 - (value * step)).toString(16);
19+
let step = 255/range;
20+
let color = Math.floor((255 - (value * step))).toString(16);
2221
return '#' + color + color + color;
2322
}
2423

src/redux/models.js

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,93 @@ export const KitModel = (filePath = "", fileName = "", isNew = false, isExisting
2222
};
2323
}
2424

25-
export const PadModel = (padType = "", location = "", level = 0, tune = 0, pan = 0, reverb = 0, midiNote = 0, mode = 0, sensitivity = 1, mgrp = 0, velocityMin = 0, velocityMax = 127, fileName = "", velocityMinB = 0, velocityMaxB = 127, fileNameB = "") => {
26-
return {
27-
id: uuidv1(),
28-
padType: padType,
29-
location: location,
30-
level: level,
31-
tune: tune,
32-
pan: pan,
33-
reverb: reverb,
34-
midiNote: midiNote,
35-
mode: mode,
36-
sensitivity: sensitivity,
37-
mgrp: mgrp,
38-
velocityMin: velocityMin,
39-
velocityMax: velocityMax,
40-
fileName: fileName,
41-
velocityMinB: velocityMinB,
42-
velocityMaxB: velocityMaxB,
43-
fileNameB: fileNameB
44-
};
25+
export class PadModel {
26+
static getPad(padType = "", location = 1, level = 0, tune = 0, pan = 0, reverb = 0, midiNote = 0, mode = 0, sensitivity = 1, mgrp = 0, velocityMin = 0, velocityMax = 127, fileName = "", velocityMinB = 0, velocityMaxB = 127, fileNameB = "") {
27+
return {
28+
id: uuidv1(),
29+
padType: padType,
30+
location: location,
31+
level: level,
32+
tune: tune,
33+
pan: pan,
34+
reverb: reverb,
35+
midiNote: midiNote,
36+
mode: mode,
37+
sensitivity: sensitivity,
38+
mgrp: mgrp,
39+
velocityMin: velocityMin,
40+
velocityMax: velocityMax,
41+
fileName: fileName,
42+
velocityMinB: velocityMinB,
43+
velocityMaxB: velocityMaxB,
44+
fileNameB: fileNameB
45+
};
46+
}
47+
48+
/**
49+
* Map file values to display values
50+
*/
51+
static fromFile(padType = "", location = "", level = 0, tune = 0, pan = 0, reverb = 0, midiNote = 0, mode = 0, sensitivity = 1, mgrp = 0, velocityMin = 0, velocityMax = 127, fileName = "", velocityMinB = 0, velocityMaxB = 127, fileNameB = "") {
52+
return this.getPad(
53+
padType,
54+
location,
55+
level,
56+
this.getUIntDisplayValue(tune),
57+
this.getUIntDisplayValue(pan),
58+
reverb,
59+
midiNote,
60+
mode,
61+
this.getSensitivityDisplayValue(sensitivity),
62+
mgrp,
63+
velocityMin,
64+
velocityMax,
65+
fileName,
66+
velocityMinB,
67+
velocityMaxB,
68+
fileNameB
69+
);
70+
}
71+
72+
/*
73+
* used for values: -4 to +4 (unsigned int: 252,253,254,255,0,1,2,3,4)
74+
* @returns {Number}
75+
*/
76+
static getUIntDisplayValue(value) {
77+
// todo: theres definitely a better way to convert uint8 to signed int here, right?
78+
if (value >= 252) {
79+
return (-1 * (256 - value));
80+
}
81+
82+
return value;
83+
}
84+
85+
/*
86+
* @param {Number} value
87+
*/
88+
static getUIntFileValue(value) {
89+
let uint = parseInt(value, 10);
90+
91+
if (uint < 0) {
92+
return (256 + uint);
93+
} else {
94+
return (uint);
95+
}
96+
}
97+
98+
/*
99+
* sensitivity display is 1 to 8
100+
* on file 20-27
101+
* @returns {Number}
102+
*/
103+
static getSensitivityDisplayValue(sensitivity) {
104+
return sensitivity - 19;
105+
}
106+
107+
/*
108+
* @param {Number} value
109+
*/
110+
static getSensitivityFileValue(sensitivity) {
111+
return sensitivity + 19;
112+
}
113+
45114
}

src/util/kitFile.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const getKitAndPadsFromFile = (kitFile) => {
4444
let kitPath = path.parse(kitFile);
4545
let buffer = fs.readFileSync(kitFile);
4646
let checksum = buffer.readUInt8(KitBuffer.CHECKSUM_BYTE);
47+
4748
if (checksum !== calculateChecksumFromBuffer(buffer)) {
4849
throw new Error("Invalid .kit file")
4950
}
@@ -185,7 +186,7 @@ const getPadFromBufferBlocks = (padType, block1, block2) => {
185186
fileNameB = block2.toString("utf-8", MEMLOC.fileNameB, MEMLOC.fileNameB + fileLength) + Drive.SAMPLE_EXTENSION;
186187
}
187188

188-
return PadModel(padType, location, level, tune, pan, reverb, midiNote, mode, sens, mgrp, velocityMin, velocityMax, fileName, velocityMinB, velocityMaxB, fileNameB);
189+
return PadModel.fromFile(padType, location, level, tune, pan, reverb, midiNote, mode, sens, mgrp, velocityMin, velocityMax, fileName, velocityMinB, velocityMaxB, fileNameB);
189190
}
190191

191192
/**
@@ -204,6 +205,11 @@ const getPadWithType = (kit, pads, padType) => {
204205
}
205206
})
206207

208+
if (!returnPad) {
209+
// for some reason we couldnt find a valid pad, return a default one
210+
return PadModel.getPad(padType);
211+
}
212+
207213
return returnPad;
208214
}
209215

@@ -272,11 +278,11 @@ const getPadBlock1 = (pad) => {
272278
block.splice(MEMLOC.midiNote, 1, pad.midiNote);
273279
block.splice(MEMLOC.location, 1, pad.location);
274280
block.splice(MEMLOC.level, 1, pad.level);
275-
block.splice(MEMLOC.tune, 1, pad.tune);
276-
block.splice(MEMLOC.pan, 1, pad.pan);
281+
block.splice(MEMLOC.tune, 1, PadModel.getUIntFileValue(pad.tune));
282+
block.splice(MEMLOC.pan, 1, PadModel.getUIntFileValue(pad.pan));
277283
block.splice(MEMLOC.reverb, 1, pad.reverb);
278284
block.splice(MEMLOC.mode, 1, pad.mode);
279-
block.splice(MEMLOC.sensitivity, 1, pad.sensitivity);
285+
block.splice(MEMLOC.sensitivity, 1, PadModel.getSensitivityFileValue(pad.sensitivity));
280286
block.splice(MEMLOC.mgrp, 1, pad.mgrp);
281287

282288
return block;
@@ -310,11 +316,11 @@ const getPadBlock2 = (pad) => {
310316
block.splice(MEMLOC.midiNote, 1, pad.midiNote);
311317
block.splice(MEMLOC.location, 1, pad.location);
312318
block.splice(MEMLOC.level, 1, pad.level);
313-
block.splice(MEMLOC.tune, 1, pad.tune);
314-
block.splice(MEMLOC.pan, 1, pad.pan);
319+
block.splice(MEMLOC.tune, 1, PadModel.getUIntFileValue(pad.tune));
320+
block.splice(MEMLOC.pan, 1, PadModel.getUIntFileValue(pad.pan));
315321
block.splice(MEMLOC.reverb, 1, pad.reverb);
316322
block.splice(MEMLOC.mode, 1, pad.mode);
317-
block.splice(MEMLOC.sensitivity, 1, pad.sensitivity);
323+
block.splice(MEMLOC.sensitivity, 1, PadModel.getSensitivityFileValue(pad.sensitivity));
318324
block.splice(MEMLOC.mgrp, 1, pad.mgrp);
319325
block.splice(MEMLOC.velocityMin, 1, pad.velocityMin);
320326
block.splice(MEMLOC.velocityMax, 1, pad.velocityMax);

src/util/storage.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,37 @@ export const saveKitToFile = (kit, pads, asNew = false) => {
5757
fs.mkdirSync(kit.filePath);
5858
}
5959

60-
let fileName = kit.fileName;
60+
let desiredFileName = kit.kitName + Drive.KIT_EXTENSION;
61+
let currentFileName = kit.fileName;
6162

62-
// if the kit hasnt been stored, or we want to store it as a new kit, use the kit name as the file name
63-
if (!fileName || asNew) {
64-
fileName = kit.kitName + Drive.KIT_EXTENSION;
63+
if (asNew) {
64+
// the kit is being stored as a new kit, drop the reference to the old file
65+
currentFileName = null;
6566
}
6667

67-
let kitFile = kit.filePath + "/" + fileName;
68+
let kitFile = kit.filePath + "/" + desiredFileName;
6869
try {
69-
// if(!fs.existsSync(kitFile)) {
70-
fs.open(kitFile, "w", (err, fd) => {
71-
if (err) throw err;
70+
// open the file
71+
fs.open(kitFile, "w", (err, fd) => {
72+
if (err) throw err;
7273

73-
// write the kit file
74-
let buffer = getKitFileBuffer(kit, pads);
75-
fs.writeSync(fd, buffer, 0, buffer.length);
76-
});
77-
// }
74+
// write the kit file
75+
let buffer = getKitFileBuffer(kit, pads);
76+
fs.writeSync(fd, buffer, 0, buffer.length);
77+
});
78+
79+
80+
if (currentFileName && desiredFileName !== currentFileName && !asNew) {
81+
// we need to rename the file, too
82+
fs.renameSync(
83+
kit.filePath + "/" + currentFileName,
84+
kit.filePath + "/" + desiredFileName
85+
);
86+
}
7887
} catch (err) {
7988
console.error(err);
8089
return;
8190
}
8291

83-
return fileName;
92+
return desiredFileName;
8493
}

0 commit comments

Comments
 (0)