mirror of
https://github.com/boxgaming/qbjs.git
synced 2025-01-15 12:21:17 +00:00
Added QB SOUND keyword with validation and support for additional shapes as provded by the WebAudio API OscillatorNode object.
This commit is contained in:
parent
565c1369b9
commit
3f6454c134
5 changed files with 28 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ tools/qb2js.exe
|
||||||
qbjs.zip
|
qbjs.zip
|
||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
|
.vscode/tasks.json
|
||||||
|
|
|
@ -54,7 +54,7 @@ CodeMirror.defineMode("qbjs", function(conf, parserConf) {
|
||||||
'lbound', 'left', 'lcase', 'len', 'line', 'locate', 'log', 'ltrim', 'mid', 'mki', 'mkl',
|
'lbound', 'left', 'lcase', 'len', 'line', 'locate', 'log', 'ltrim', 'mid', 'mki', 'mkl',
|
||||||
'oct', 'paint', 'point', 'preset', 'print', 'pset',
|
'oct', 'paint', 'point', 'preset', 'print', 'pset',
|
||||||
'right', 'rtrim', 'randomize', 'read', 'restore', 'rnd',
|
'right', 'rtrim', 'randomize', 'read', 'restore', 'rnd',
|
||||||
'screen', 'shared', 'sgn', 'sin', 'sleep', 'space', 'sqr',
|
'screen', 'shared', 'sgn', 'sin', 'sleep', 'sound', 'space', 'sqr',
|
||||||
'str', 'swap', 'tan', 'time', 'timer', 'ubound', 'ucase',
|
'str', 'swap', 'tan', 'time', 'timer', 'ubound', 'ucase',
|
||||||
'val', 'varptr', 'window',
|
'val', 'varptr', 'window',
|
||||||
'mkdir', 'chdir', 'rmdir', 'kill', 'name', 'files', 'open', 'close', 'lof', 'eof', 'put', 'get', 'freefile',
|
'mkdir', 'chdir', 'rmdir', 'kill', 'name', 'files', 'open', 'close', 'lof', 'eof', 'put', 'get', 'freefile',
|
||||||
|
|
24
qb.js
24
qb.js
|
@ -2815,6 +2815,30 @@ var QB = new function() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.sub_Sound = async function(freq, duration, shape) {
|
||||||
|
if (shape == undefined) { shape = "square"; }
|
||||||
|
if (!(freq == 0 || (freq >= 32 && freq <= 32767))) {
|
||||||
|
throw new Error("Frequency invalid - valid: 0 (delay), 32 to 32767");
|
||||||
|
}
|
||||||
|
var valid_shapes = ["sine", "square", "sawtooth", "triangle"];
|
||||||
|
if (!valid_shapes.includes(shape.toLowerCase())) {
|
||||||
|
throw new Error("Shape invalid - valid: " + valid_shapes.join(', '));
|
||||||
|
}
|
||||||
|
if (freq == 0) {
|
||||||
|
await GX.sleep(duration);
|
||||||
|
} else {
|
||||||
|
var context = new AudioContext();
|
||||||
|
var oscillator = context.createOscillator();
|
||||||
|
oscillator.type = shape;
|
||||||
|
oscillator.frequency.value = freq;
|
||||||
|
oscillator.connect(context.destination);
|
||||||
|
oscillator.start();
|
||||||
|
setTimeout(await function () {
|
||||||
|
oscillator.stop();
|
||||||
|
}, duration);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
this.func_Sqr = function(value) {
|
this.func_Sqr = function(value) {
|
||||||
return Math.sqrt(value);
|
return Math.sqrt(value);
|
||||||
};
|
};
|
||||||
|
|
1
qb2js.js
1
qb2js.js
|
@ -3366,6 +3366,7 @@ if (QB.halted()) { return; }
|
||||||
await sub_AddQBMethod( "FUNCTION" , "Sgn" , False);
|
await sub_AddQBMethod( "FUNCTION" , "Sgn" , False);
|
||||||
await sub_AddQBMethod( "FUNCTION" , "Sin" , False);
|
await sub_AddQBMethod( "FUNCTION" , "Sin" , False);
|
||||||
await sub_AddQBMethod( "SUB" , "Sleep" , True);
|
await sub_AddQBMethod( "SUB" , "Sleep" , True);
|
||||||
|
await sub_AddQBMethod( "SUB" , "Sound" , True);
|
||||||
await sub_AddQBMethod( "FUNCTION" , "Space" , False);
|
await sub_AddQBMethod( "FUNCTION" , "Space" , False);
|
||||||
await sub_AddQBMethod( "FUNCTION" , "String" , False);
|
await sub_AddQBMethod( "FUNCTION" , "String" , False);
|
||||||
await sub_AddQBMethod( "FUNCTION" , "Sqr" , False);
|
await sub_AddQBMethod( "FUNCTION" , "Sqr" , False);
|
||||||
|
|
|
@ -3581,6 +3581,7 @@ Sub InitQBMethods
|
||||||
AddQBMethod "FUNCTION", "Sgn", False
|
AddQBMethod "FUNCTION", "Sgn", False
|
||||||
AddQBMethod "FUNCTION", "Sin", False
|
AddQBMethod "FUNCTION", "Sin", False
|
||||||
AddQBMethod "SUB", "Sleep", True
|
AddQBMethod "SUB", "Sleep", True
|
||||||
|
AddQBMethod "SUB", "Sound", True
|
||||||
AddQBMethod "FUNCTION", "Space", False
|
AddQBMethod "FUNCTION", "Space", False
|
||||||
AddQBMethod "FUNCTION", "String", False
|
AddQBMethod "FUNCTION", "String", False
|
||||||
AddQBMethod "FUNCTION", "Sqr", False
|
AddQBMethod "FUNCTION", "Sqr", False
|
||||||
|
|
Loading…
Reference in a new issue