Skip to content

Commit d3beab6

Browse files
committed
special handling for sample method in facetForWax build as it differs between facet and wax
1 parent 2c2dc1a commit d3beab6

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

js/wax/build_wax.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,32 @@ ast.body.forEach(node => {
1818
method.type === 'MethodDefinition' &&
1919
included_methods.includes(method.key.name)
2020
) {
21-
const methodStart = method.start;
22-
const methodEnd = method.end;
23-
let methodCode = facetPatternContent.slice(methodStart, methodEnd);
21+
let methodCode;
2422

25-
// replace instances of this.getWholeNoteNumSamples() with SAMPLE_RATE
26-
methodCode = methodCode.replace(/this\.getWholeNoteNumSamples\(\)/g, 'SAMPLE_RATE');
27-
// replace instances of Scale.get with Tonal.Scale.get
28-
methodCode = methodCode.replace(/\bScale\.get\b/g, 'Tonal.Scale.get');
23+
// special handling for sample() method which is different in wax
24+
if (method.key.name === 'sample') {
25+
methodCode = `sample(name) {
26+
if (!name.endsWith('.wav')) {
27+
name += '.wav';
28+
}
29+
let audioBuffer = audioBuffers[name];
30+
if (audioBuffer) {
31+
this.data = audioBuffer;
32+
} else {
33+
throw \`No buffer found with the name \${name}\`;
34+
}
35+
return this;
36+
}`;
37+
} else {
38+
const methodStart = method.start;
39+
const methodEnd = method.end;
40+
methodCode = facetPatternContent.slice(methodStart, methodEnd);
41+
42+
// replace instances of this.getWholeNoteNumSamples() with SAMPLE_RATE
43+
methodCode = methodCode.replace(/this\.getWholeNoteNumSamples\(\)/g, 'SAMPLE_RATE');
44+
// replace instances of Scale.get with Tonal.Scale.get
45+
methodCode = methodCode.replace(/\bScale\.get\b/g, 'Tonal.Scale.get');
46+
}
2947

3048
extractedMethods.push(methodCode);
3149
}

0 commit comments

Comments
 (0)