forked from libgdx/libgdx
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding iOS RoboVM AudioDevice implementation (libgdx#7371)
- Loading branch information
1 parent
a6dc60f
commit 7c6db62
Showing
10 changed files
with
209 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
backends/gdx-backend-robovm/src/com/badlogic/gdx/backends/iosrobovm/objectal/ALConsts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
package com.badlogic.gdx.backends.iosrobovm.objectal; | ||
|
||
public class ALConsts { | ||
public static int AL_FORMAT_MONO16 = 0x1101; | ||
public static int AL_FORMAT_STEREO16 = 0x1103; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
backends/gdx-backend-robovm/src/com/badlogic/gdx/backends/iosrobovm/objectal/ALWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
package com.badlogic.gdx.backends.iosrobovm.objectal; | ||
|
||
import org.robovm.apple.foundation.NSObject; | ||
import org.robovm.objc.ObjCRuntime; | ||
import org.robovm.objc.annotation.Method; | ||
import org.robovm.objc.annotation.NativeClass; | ||
import org.robovm.rt.bro.annotation.Library; | ||
import org.robovm.rt.bro.ptr.VoidPtr; | ||
|
||
/** @author Jile Gao */ | ||
@Library(Library.INTERNAL) | ||
@NativeClass | ||
public class ALWrapper extends NSObject { | ||
static { | ||
ObjCRuntime.bind(ALWrapper.class); | ||
} | ||
|
||
@Method(selector = "bufferData:format:data:size:frequency:") | ||
public static native boolean bufferData (int bufferId, int format, VoidPtr data, int size, int frequency); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...dx-backend-robovm/src/com/badlogic/gdx/backends/iosrobovm/objectal/OALIOSAudioDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
|
||
package com.badlogic.gdx.backends.iosrobovm.objectal; | ||
|
||
import com.badlogic.gdx.audio.AudioDevice; | ||
import org.robovm.rt.bro.Struct; | ||
import org.robovm.rt.bro.ptr.ShortPtr; | ||
import org.robovm.rt.bro.ptr.VoidPtr; | ||
|
||
import java.nio.ShortBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.badlogic.gdx.backends.iosrobovm.objectal.ALConsts.AL_FORMAT_MONO16; | ||
import static com.badlogic.gdx.backends.iosrobovm.objectal.ALConsts.AL_FORMAT_STEREO16; | ||
|
||
/** @author Jile Gao | ||
* @author Berstanio */ | ||
class OALIOSAudioDevice implements AudioDevice { | ||
private ALSource alSource; | ||
private List<ALBuffer> alBuffers = new ArrayList<>(); | ||
private List<ALBuffer> alBuffersFree = new ArrayList<>(); | ||
private final int samplingRate; | ||
private final boolean isMono; | ||
private final int format; | ||
private final ShortBuffer tmpBuffer; | ||
private final int minSize; | ||
private final int latency; | ||
|
||
OALIOSAudioDevice (int samplingRate, boolean isMono, int minSize, int bufferCount) { | ||
this.samplingRate = samplingRate; | ||
this.isMono = isMono; | ||
this.format = isMono ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; | ||
this.minSize = minSize; | ||
|
||
tmpBuffer = ShortBuffer.allocate(minSize); | ||
latency = minSize / (isMono ? 1 : 2) / bufferCount; | ||
alSource = new ALSource(); | ||
for (int i = 0; i < bufferCount; i++) { | ||
ALBuffer buffer = new ALBuffer().initWithNameDataSizeFormatFrequency("test", Struct.allocate(VoidPtr.class, 1), 2, | ||
format, samplingRate); | ||
alBuffersFree.add(buffer); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isMono () { | ||
return isMono; | ||
} | ||
|
||
@Override | ||
public void writeSamples (short[] samples, int offset, int numSamples) { | ||
if (numSamples < 0) throw new IllegalArgumentException("numSamples cannot be < 0."); | ||
|
||
ShortPtr shortPtr; | ||
if (numSamples + tmpBuffer.position() >= minSize) { | ||
shortPtr = Struct.allocate(ShortPtr.class, numSamples + tmpBuffer.position()); | ||
shortPtr.set(tmpBuffer.array(), 0, tmpBuffer.position()); | ||
shortPtr.next(tmpBuffer.position()).set(samples, offset, numSamples); | ||
numSamples += tmpBuffer.position(); | ||
tmpBuffer.position(0); | ||
} else { | ||
tmpBuffer.put(samples, offset, numSamples); | ||
return; | ||
} | ||
|
||
while (alBuffersFree.isEmpty()) { | ||
if (OALAudioSession.sharedInstance().interrupted()) { | ||
try { | ||
Thread.sleep(2); | ||
} catch (InterruptedException ignored) { | ||
} | ||
return; | ||
} | ||
int toFree = Math.min(alSource.buffersProcessed(), alBuffers.size()); | ||
for (int j = 0; j < toFree; j++) { | ||
ALBuffer alBuffer = alBuffers.get(0); | ||
if (alSource.unqueueBuffer(alBuffer)) { | ||
alBuffersFree.add(alBuffer); | ||
alBuffers.remove(alBuffer); | ||
} else { | ||
break; | ||
} | ||
} | ||
if (alBuffersFree.isEmpty()) { | ||
try { | ||
Thread.sleep(2); | ||
} catch (InterruptedException ignored) { | ||
} | ||
} | ||
} | ||
|
||
ALBuffer buffer = alBuffersFree.remove(0); | ||
ALWrapper.bufferData(buffer.bufferId(), format, shortPtr.as(VoidPtr.class), numSamples * 2, samplingRate); | ||
if (alSource.queueBuffer(buffer)) { | ||
alBuffers.add(buffer); | ||
} | ||
if (!alSource.playing()) { | ||
alSource.play(); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeSamples (float[] samples, int offset, int numSamples) { | ||
short[] shortSamples = new short[samples.length]; | ||
|
||
for (int i = offset, j = 0; i < samples.length; i++, j++) { | ||
float fValue = samples[i]; | ||
if (fValue > 1) fValue = 1; | ||
if (fValue < -1) fValue = -1; | ||
short value = (short)(fValue * Short.MAX_VALUE); | ||
shortSamples[j] = value; | ||
} | ||
writeSamples(shortSamples, offset, numSamples); | ||
} | ||
|
||
@Override | ||
public int getLatency () { | ||
return latency; | ||
} | ||
|
||
@Override | ||
public void dispose () { | ||
alSource.stop(); | ||
alBuffers = null; | ||
alBuffersFree = null; | ||
alSource = null; | ||
} | ||
|
||
@Override | ||
public void setVolume (float volume) { | ||
alSource.setVolume(volume); | ||
} | ||
|
||
@Override | ||
public void pause () { | ||
alSource.setPaused(true); | ||
} | ||
|
||
@Override | ||
public void resume () { | ||
alSource.setPaused(false); | ||
} | ||
} |