Repe [T3DU] Update - 31f26d171bba0355ce2a77031e3aad4c64dbc7e9
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package iron.object;
|
||||
|
||||
import kha.FastFloat;
|
||||
import kha.Sound;
|
||||
import kha.audio1.AudioChannel;
|
||||
import iron.data.Data;
|
||||
import iron.data.SceneFormat;
|
||||
@ -13,9 +14,10 @@ class SpeakerObject extends Object {
|
||||
|
||||
public var data: TSpeakerData;
|
||||
public var paused(default, null) = false;
|
||||
public var sound(default, null): kha.Sound = null;
|
||||
public var sound(default, null): Sound = null;
|
||||
public var channels(default, null): Array<AudioChannel> = [];
|
||||
public var volume(default, null) : FastFloat;
|
||||
public var sampleRate(default, null) : Int;
|
||||
|
||||
public function new(data: TSpeakerData) {
|
||||
super();
|
||||
@ -26,28 +28,32 @@ class SpeakerObject extends Object {
|
||||
|
||||
if (data.sound == "") return;
|
||||
|
||||
Data.getSound(data.sound, function(sound: kha.Sound) {
|
||||
this.sound = sound;
|
||||
Data.getSound(data.sound, function(sound: Sound) {
|
||||
this.sound = cloneSound(sound);
|
||||
App.notifyOnInit(init);
|
||||
});
|
||||
}
|
||||
|
||||
function init() {
|
||||
sampleRate = sound.sampleRate;
|
||||
if (data.pitch != 1.0)
|
||||
sound.sampleRate = Std.int(sampleRate * data.pitch);
|
||||
if (visible && data.play_on_start) play();
|
||||
}
|
||||
|
||||
public function play() {
|
||||
if (sound == null || data.muted) return;
|
||||
public function play(): AudioChannel {
|
||||
if (sound == null || data.muted) return null;
|
||||
if (paused) {
|
||||
for (c in channels) c.play();
|
||||
paused = false;
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
var channel = Audio.play(sound, data.loop, data.stream);
|
||||
if (channel != null) {
|
||||
channels.push(channel);
|
||||
if (data.attenuation > 0 && channels.length == 1) App.notifyOnUpdate(update);
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
public function pause() {
|
||||
@ -65,14 +71,24 @@ class SpeakerObject extends Object {
|
||||
|
||||
data.sound = sound;
|
||||
|
||||
Data.getSound(sound, function(sound: kha.Sound) {
|
||||
this.sound = sound;
|
||||
Data.getSound(sound, function(sound: Sound) {
|
||||
this.sound = cloneSound(sound);
|
||||
});
|
||||
|
||||
sampleRate = this.sound.sampleRate;
|
||||
if (data.pitch != 1.0)
|
||||
this.sound.sampleRate = Std.int(sampleRate * data.pitch);
|
||||
}
|
||||
|
||||
public function setVolume(volume: FastFloat) {
|
||||
data.volume = volume;
|
||||
}
|
||||
|
||||
public function setPosition(position: Float) {
|
||||
for (c in channels)
|
||||
if (position < c.length) c.position = position;
|
||||
}
|
||||
|
||||
function update() {
|
||||
if (paused) return;
|
||||
for (c in channels) if (c.finished) channels.remove(c);
|
||||
@ -103,6 +119,17 @@ class SpeakerObject extends Object {
|
||||
super.remove();
|
||||
}
|
||||
|
||||
function cloneSound(sound: Sound): Sound {
|
||||
if (sound == null) return null;
|
||||
var s = Type.createEmptyInstance(Sound);
|
||||
s.compressedData = sound.compressedData;
|
||||
s.uncompressedData = sound.uncompressedData;
|
||||
s.sampleRate = sound.sampleRate;
|
||||
s.length = sound.length;
|
||||
s.channels = sound.channels;
|
||||
return s;
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user