Skip to content

Commit

Permalink
Merge pull request #43 from grymmjack/sound-qb-keyword
Browse files Browse the repository at this point in the history
Added QB SOUND keyword
  • Loading branch information
boxgaming authored May 31, 2023
2 parents 565c136 + 3f6454c commit 2898f81
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tools/qb2js.exe
qbjs.zip
.vscode/launch.json
.vscode/settings.json
.vscode/tasks.json
2 changes: 1 addition & 1 deletion codemirror/qb-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions qb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
1 change: 1 addition & 0 deletions qb2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tools/qb2js.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2898f81

Please sign in to comment.