Skip to content

Commit

Permalink
Move audio mixing to c
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Mar 27, 2023
1 parent 93b696f commit e7e52a4
Show file tree
Hide file tree
Showing 32 changed files with 124 additions and 4,986 deletions.
8 changes: 5 additions & 3 deletions Sources/Krom.hx
Expand Up @@ -73,12 +73,15 @@ extern class Krom {

static function loadImage(file: String, readable: Bool): Dynamic;
static function unloadImage(image: kha.Image): Void;
static function loadSound(file: String): Dynamic;
static function writeAudioBuffer(buffer: js.lib.ArrayBuffer, samples: Int): Void;
static function loadBlob(file: String): js.lib.ArrayBuffer;
static function loadUrl(url: String): Void;
static function copyToClipboard(text: String): Void;

static function loadSound(file: String): Dynamic;
static function unloadSound(sound: Dynamic): Void;
static function playSound(sound: Dynamic, loop: Bool): Void;
static function stopSound(sound: Dynamic): Void;

static function init(title: String, width: Int, height: Int, samplesPerPixel: Int, vSync: Bool, windowMode: Int, windowFeatures: Int, kromApi: Int, x: Int, y: Int, frequency: Int): Void;
static function setApplicationName(name: String): Void;
static function log(v: Dynamic, ?level: LogLevel): Void;
Expand Down Expand Up @@ -108,7 +111,6 @@ extern class Krom {
static function setMousePosition(windowId: Int, x: Int, y: Int): Void;
static function showMouse(show: Bool): Void;
static function showKeyboard(show: Bool): Void;
static function setAudioCallback(callback: Int->Void): Void;
static function getTime(): Float;
static function windowWidth(id: Int): Int;
static function windowHeight(id: Int): Int;
Expand Down
30 changes: 2 additions & 28 deletions Sources/iron/system/Audio.hx
@@ -1,37 +1,11 @@
package iron.system;

import kha.audio1.AudioChannel;

/**
Audio playback. This class wraps around `kha.audio1.Audio`.
**/
class Audio {

#if arm_audio

public function new() {}

/**
Plays the given sound.
If `stream` is `true` and the sound has compressed data, it is streamed
from disk instead of being fully loaded into memory. This is useful for
longer sounds such as background music.
This function returns `null` if:
- there is no unoccupied audio channel available for playback.
- the sound has compressed data only but `stream` is `false`. In this
case, call `kha.Sound.uncompress()` first.
**/
public static function play(sound: kha.Sound, loop = false, stream = false): Null<AudioChannel> {
if (stream && sound.compressedData != null) {
return kha.audio1.Audio.stream(sound, loop);
}
else if (sound.uncompressedData != null) {
return kha.audio1.Audio.play(sound, loop);
}
else return null;
public static function play(sound: kha.Sound, loop = false, stream = false): kha.audio1.AudioChannel {
return stream ? kha.audio1.Audio.stream(sound, loop) : kha.audio1.Audio.play(sound, loop);
}

#end
Expand Down
3 changes: 1 addition & 2 deletions Sources/kha/Assets.hx
Expand Up @@ -4,7 +4,6 @@ import kha.Blob;
import kha.Font;
import kha.Sound;
import haxe.io.Bytes;
import haxe.io.BytesData;

class Assets {

Expand All @@ -18,7 +17,7 @@ class Assets {
}

public static function loadSoundFromPath(path: String, done: Sound -> Void): Void {
done(new Sound(Bytes.ofData(Krom.loadSound(path))));
done(new Sound(Krom.loadSound(path)));
}

public static function loadFontFromPath(path: String, done: Font -> Void): Void {
Expand Down
26 changes: 4 additions & 22 deletions Sources/kha/Sound.hx
@@ -1,35 +1,17 @@
package kha;

import haxe.io.Bytes;
import haxe.io.BytesOutput;
import kha.audio2.ogg.vorbis.Reader;

/**
* Contains compressed or uncompressed audio data.
*/
class Sound {
public var compressedData: Bytes;
public var uncompressedData: kha.arrays.Float32Array;
public var length: Float = 0; // in seconds
public var channels: Int = 0;
public var sampleRate: Int = 0;

public function new(bytes: Bytes) {
var count = Std.int(bytes.length / 4);
uncompressedData = new kha.arrays.Float32Array(count);
for (i in 0...count) {
uncompressedData[i] = bytes.getFloat(i * 4);
}
public var sound_: Dynamic;

compressedData = null;
public function new(sound_: Dynamic) {
this.sound_ = sound_;
}

public function uncompress(done: Void->Void): Void {
done();
}

public function unload() {
compressedData = null;
uncompressedData = null;
Krom.unloadSound(sound_);
}
}
14 changes: 0 additions & 14 deletions Sources/kha/System.hx
Expand Up @@ -125,12 +125,6 @@ class System {
Krom.setGamepadAxisCallback(gamepadAxisCallback);
Krom.setGamepadButtonCallback(gamepadButtonCallback);

#if arm_audio
kha.audio2.Audio._init();
kha.audio1.Audio._init();
Krom.setAudioCallback(audioCallback);
#end

Scheduler.start();

callback(Window.get(0));
Expand Down Expand Up @@ -386,14 +380,6 @@ class System {
gamepads[gamepad].sendButtonEvent(button, value);
}

#if arm_audio
private static function audioCallback(samples: Int) : Void {
kha.audio2.Audio._callCallback(samples);
var buffer = @:privateAccess kha.audio2.Audio.buffer;
Krom.writeAudioBuffer(buffer.data.buffer, samples);
}
#end

public static function getVsync(): Bool {
return true;
}
Expand Down
13 changes: 12 additions & 1 deletion Sources/kha/audio1/Audio.hx
@@ -1,3 +1,14 @@
package kha.audio1;

typedef Audio = kha.audio2.Audio1;
class Audio {

public static function play(sound: Sound, loop: Bool = false): kha.audio1.AudioChannel {
var channel = new AudioChannel(sound, loop);
channel.play();
return channel;
}

public static function stream(sound: Sound, loop: Bool = false): kha.audio1.AudioChannel {
return play(sound, loop);
}
}
58 changes: 48 additions & 10 deletions Sources/kha/audio1/AudioChannel.hx
@@ -1,17 +1,55 @@
package kha.audio1;

interface AudioChannel {
public function play(): Void;
public function pause(): Void;
public function stop(): Void;
class AudioChannel {
var sound: kha.Sound;
var loop: Bool;

public function new(sound: kha.Sound, loop: Bool) {
this.sound = sound;
this.loop = loop;
}

public function play(): Void {
Krom.playSound(sound.sound_, loop);
}

public function pause(): Void {

}

public function stop(): Void {
Krom.stopSound(sound.sound_);
}

public var length(get, null): Float; // Seconds
private function get_length(): Float;

private function get_length(): Float {
return 0.0;
}

public var position(get, set): Float; // Seconds
private function get_position(): Float;
private function set_position(value: Float): Float;

private function get_position(): Float {
return 0.0;
}

private function set_position(value: Float): Float {
return 0.0;
}

public var volume(get, set): Float;
private function get_volume(): Float;
private function set_volume(value: Float): Float;

private function get_volume(): Float {
return 1.0;
}

private function set_volume(value: Float): Float {
return 1.0;
}

public var finished(get, null): Bool;
private function get_finished(): Bool;

private function get_finished(): Bool {
return false;
}
}
50 changes: 0 additions & 50 deletions Sources/kha/audio2/Audio.hx

This file was deleted.

0 comments on commit e7e52a4

Please sign in to comment.