Improve handling of disposed native audio source objects

This commit is contained in:
Joshua Granick
2018-04-03 17:04:28 -07:00
parent 25e8649c20
commit c34e11bb57

View File

@@ -57,7 +57,10 @@ class NativeAudioSource {
if (handle != null) {
stop ();
AL.sourcei (handle, AL.BUFFER, null);
AL.deleteSource (handle);
handle = null;
}
@@ -117,17 +120,27 @@ class NativeAudioSource {
if (parent.buffer.__srcBuffer == null) {
parent.buffer.__srcBuffer = AL.createBuffer ();
if (parent.buffer.__srcBuffer != null) {
AL.bufferData (parent.buffer.__srcBuffer, format, parent.buffer.data, parent.buffer.data.length, parent.buffer.sampleRate);
}
}
dataLength = parent.buffer.data.length;
handle = AL.createSource ();
if (handle != null) {
AL.sourcei (handle, AL.BUFFER, parent.buffer.__srcBuffer);
}
}
samples = Std.int ((dataLength * 8) / (parent.buffer.channels * parent.buffer.bitsPerSample));
}
@@ -196,6 +209,8 @@ class NativeAudioSource {
public function pause ():Void {
playing = false;
if (handle == null) return;
AL.sourcePause (handle);
if (streamTimer != null) {
@@ -324,9 +339,14 @@ class NativeAudioSource {
public function stop ():Void {
playing = false;
if (playing && handle != null && AL.getSourcei (handle, AL.SOURCE_STATE) == AL.PLAYING) {
AL.sourceStop (handle);
}
playing = false;
if (streamTimer != null) {
streamTimer.stop ();
@@ -358,10 +378,9 @@ class NativeAudioSource {
private function timer_onRun ():Void {
playing = false;
if (loops > 0) {
playing = false;
loops--;
setCurrentTime (0);
play ();
@@ -369,8 +388,7 @@ class NativeAudioSource {
} else {
AL.sourceStop (handle);
timer.stop ();
stop ();
}
@@ -393,7 +411,9 @@ class NativeAudioSource {
return getLength ();
} else if (stream) {
} else if (handle != null) {
if (stream) {
var time = (Std.int (parent.buffer.__srcVorbisFile.timeTell () * 1000) + Std.int (AL.getSourcef (handle, AL.SEC_OFFSET) * 1000)) - parent.offset;
if (time < 0) return 0;
@@ -415,9 +435,15 @@ class NativeAudioSource {
}
return 0;
}
public function setCurrentTime (value:Int):Int {
if (handle != null) {
if (stream) {
AL.sourceStop (handle);
@@ -447,6 +473,8 @@ class NativeAudioSource {
}
}
if (playing) {
if (timer != null) {
@@ -478,14 +506,27 @@ class NativeAudioSource {
public function getGain ():Float {
if (handle != null) {
return AL.getSourcef (handle, AL.GAIN);
} else {
return 1;
}
}
public function setGain (value:Float):Float {
if (handle != null) {
AL.sourcef (handle, AL.GAIN, value);
}
return value;
}
@@ -546,6 +587,8 @@ class NativeAudioSource {
public function getPosition ():Vector4 {
if (handle != null) {
#if !emscripten
var value = AL.getSource3f (handle, AL.POSITION);
position.x = value[0];
@@ -553,6 +596,8 @@ class NativeAudioSource {
position.z = value[2];
#end
}
return position;
}
@@ -565,9 +610,13 @@ class NativeAudioSource {
position.z = value.z;
position.w = value.w;
if (handle != null) {
AL.distanceModel (AL.NONE);
AL.source3f (handle, AL.POSITION, position.x, position.y, position.z);
}
return position;
}