1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-04-28 17:20:13 +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:
grymmjack 2023-05-30 20:09:23 -04:00
parent 565c1369b9
commit 3f6454c134
5 changed files with 28 additions and 1 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ tools/qb2js.exe
qbjs.zip
.vscode/launch.json
.vscode/settings.json
.vscode/tasks.json

View file

@ -54,7 +54,7 @@ CodeMirror.defineMode("qbjs", function(conf, parserConf) {
'lbound', 'left', 'lcase', 'len', 'line', 'locate', 'log', 'ltrim', 'mid', 'mki', 'mkl',
'oct', 'paint', 'point', 'preset', 'print', 'pset',
'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',
'val', 'varptr', 'window',
'mkdir', 'chdir', 'rmdir', 'kill', 'name', 'files', 'open', 'close', 'lof', 'eof', 'put', 'get', 'freefile',

24
qb.js
View file

@ -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) {
return Math.sqrt(value);
};

View file

@ -3366,6 +3366,7 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "FUNCTION" , "Sgn" , False);
await sub_AddQBMethod( "FUNCTION" , "Sin" , False);
await sub_AddQBMethod( "SUB" , "Sleep" , True);
await sub_AddQBMethod( "SUB" , "Sound" , True);
await sub_AddQBMethod( "FUNCTION" , "Space" , False);
await sub_AddQBMethod( "FUNCTION" , "String" , False);
await sub_AddQBMethod( "FUNCTION" , "Sqr" , False);

View file

@ -3581,6 +3581,7 @@ Sub InitQBMethods
AddQBMethod "FUNCTION", "Sgn", False
AddQBMethod "FUNCTION", "Sin", False
AddQBMethod "SUB", "Sleep", True
AddQBMethod "SUB", "Sound", True
AddQBMethod "FUNCTION", "Space", False
AddQBMethod "FUNCTION", "String", False
AddQBMethod "FUNCTION", "Sqr", False