Skip to content

Commit

Permalink
Merge pull request #1498 from RblSb/webaudio-option
Browse files Browse the repository at this point in the history
Add webaudio option to System.start
  • Loading branch information
RobDangerous authored Sep 1, 2024
2 parents 977b972 + d603298 commit 345f1f3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Backends/HTML5/kha/SystemImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ class SystemImpl {
}
// canvas.getContext("2d").scale(transform, transform);

if (!mobile && kha.audio2.Audio._init()) {
if ((!mobile || options.audio.allowMobileWebAudio) && kha.audio2.Audio._init()) {
SystemImpl._hasWebAudio = true;
kha.audio2.Audio1._init();
}
Expand Down
6 changes: 3 additions & 3 deletions Backends/HTML5/kha/audio2/Audio.hx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package kha.audio2;

import js.Syntax;
import js.Browser;
import js.Syntax;
import js.html.URL;
import js.html.audio.AudioContext;
import js.html.audio.AudioProcessingEvent;
import js.html.audio.ScriptProcessorNode;
import kha.Sound;
import kha.internal.IntBox;
import kha.js.AEAudioChannel;
import kha.Sound;

class Audio {
public static var disableGcInteractions = false;
Expand Down Expand Up @@ -70,7 +70,7 @@ class Audio {

public static var samplesPerSecond: Int;

public static var audioCallback: kha.internal.IntBox->Buffer->Void;
public static var audioCallback: (outputBufferLength: IntBox, buffer: Buffer) -> Void;

static var virtualChannels: Array<VirtualStreamChannel> = [];

Expand Down
28 changes: 19 additions & 9 deletions Sources/kha/System.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@ package kha;

import kha.WindowOptions;

@:structInit
private class AudioOptions {
/** Allow `audio2.Audio` api initialization on mobile browsers (only for HTML5 target) **/
public var allowMobileWebAudio = false;
}

@:structInit
class SystemOptions {
@:optional public var title: String = "Kha";
@:optional public var width: Int = -1;
@:optional public var height: Int = -1;
@:optional public var window: WindowOptions = null;
@:optional public var framebuffer: FramebufferOptions = null;
public var title: String = "Kha";
public var width: Int = -1;
public var height: Int = -1;
public var window: WindowOptions = null;
public var framebuffer: FramebufferOptions = null;
public var audio: AudioOptions = null;

/**
* Used to provide parameters for System.start
* Used to provide parameters for `System.start`
* @param title The application title is the default window title (unless the window parameter provides a title of its own)
* and is used for various other purposes - for example for save data locations
* @param width Just a shortcut which overwrites window.width if set
* @param height Just a shortcut which overwrites window.height if set
* @param width Just a shortcut which overwrites `window.width` if set
* @param height Just a shortcut which overwrites `window.height` if set
* @param window Optionally provide window options
* @param framebuffer Optionally provide framebuffer options
* @param audio Optionally provide audio options
*/
public function new(title: String = "Kha", ?width: Int = -1, ?height: Int = -1, window: WindowOptions = null, framebuffer: FramebufferOptions = null) {
public function new(title: String = "Kha", ?width: Int = -1, ?height: Int = -1, ?window: WindowOptions, ?framebuffer: FramebufferOptions,
?audio: AudioOptions) {
this.title = title;
this.window = window == null ? {} : window;

Expand All @@ -44,6 +53,7 @@ class SystemOptions {
}

this.framebuffer = framebuffer == null ? {} : framebuffer;
this.audio = audio ?? {};
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/kha/audio2/Audio.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kha.audio2;

import kha.internal.IntBox;

extern class Audio {
/**
* The samples per second natively used by the target system.
Expand All @@ -10,8 +12,10 @@ extern class Audio {
* Requests additional audio data.
* Beware: This is called from a separate audio thread on some targets.
* See kha.audio2.Audio1 for sample code.
* This api is disabled on mobile for HTML5 target by default
* and can be enabled in `System.start` options.
*/
public static var audioCallback: Int->Buffer->Void;
public static var audioCallback: (outputBufferLength:IntBox, buffer:Buffer)->Void;

/**
* Similar to kha.audio1.Audio.stream, but only for hardware accelerated audio playback.
Expand Down

0 comments on commit 345f1f3

Please sign in to comment.