Kha icon indicating copy to clipboard operation
Kha copied to clipboard

HTML% backend kha.capture.AudioCapture broken in Chrome

Open jakelewis3d opened this issue 7 years ago • 0 comments

According to https://bugs.chromium.org/p/chromium/issues/detail?id=327649 createScriptProcessor will not fire onaudioprocess in Chrome unless there is both 1 output channel, AND the node is connected to the audiocontext's destination. The updated the init method is below. Fortunatly, this does not actually play the microphone audio out thru the speakers although I'm not sure why not. I've tested this in Chrome and Firefox on Windows, but I suggest it is tested on other platforms prior to commiting.

public static function init(initialized: Void->Void, error: Void->Void): Void { if (kha.audio2.Audio._context == null) { error(); return; }

	var getUserMedia = untyped __js__("navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia");
	getUserMedia.call(js.Browser.navigator, {audio: true}, function (stream: Dynamic) {
		input = kha.audio2.Audio._context.createMediaStreamSource(stream);
		
		var bufferSize = 1024 * 2;
		buffer = new Buffer(bufferSize * 4, 2, Std.int(kha.audio2.Audio._context.sampleRate));
		
		processingNode = kha.audio2.Audio._context.createScriptProcessor(bufferSize, 1, 1);
		processingNode.onaudioprocess = function (e: AudioProcessingEvent) {
			if (audioCallback != null) {
				var input1 = e.inputBuffer.getChannelData(0);
				var input2 = e.inputBuffer.getChannelData(0);
				for (i in 0...e.inputBuffer.length) {
					buffer.data.set(buffer.writeLocation, input1[i]);
					buffer.writeLocation += 1;
					buffer.data.set(buffer.writeLocation, input2[i]);
					buffer.writeLocation += 1;
					if (buffer.writeLocation >= buffer.size) {
						buffer.writeLocation = 0;
					}
				}
				audioCallback(e.inputBuffer.length * 2, buffer);
			}
		}
		
		input.connect(processingNode);
                    processingNode.connect(kha.audio2.Audio._context.destination);  // connect to output
		//input.connect(kha.audio2.Audio._context.destination);
		initialized();
	}, function () {
		error();
	});
}

jakelewis3d avatar Jan 26 '18 22:01 jakelewis3d