rhythmbullet/core/src/zero1hd/rhythmbullet/audio/RhythmBulletAudioThread.java
2018-06-14 00:48:29 -05:00

75 lines
1.6 KiB
Java
Executable File

package zero1hd.rhythmbullet.audio;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music.OnCompletionListener;
import zero1hd.rhythmbullet.audio.processor.SampleProcessor;
public class RhythmBulletAudioThread extends Thread {
private AudioDevice ad;
private SampleProcessor sp;
private Music music;
private volatile OnCompletionListener ocl;
private short[] pcm;
private volatile boolean terminated;
private volatile boolean playing;
public RhythmBulletAudioThread(int windowSize, VisualizableMusic vm) {
music = vm;
this.sp = vm.getSampleProcessor();
pcm = new short[sp.getChannels()*windowSize];
this.ad = Gdx.audio.newAudioDevice(sp.getSampleRate(), (sp.getChannels() > 1 ? false : true));
}
@Override
public void run() {
while (!terminated && sp.readSamples(pcm) > 0) {
ad.writeSamples(pcm, 0, pcm.length);
}
if (ocl != null) {
ocl.onCompletion(music);
}
super.run();
}
public synchronized void play() {
playing = true;
notify();
}
public synchronized void pause() {
playing = false;
while (!playing) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized short[] getPcm() {
return pcm;
}
public void terminate() {
terminated = true;
}
public boolean isTerminated() {
return terminated;
}
public boolean isPlaying() {
return playing;
}
public void setOnCompletionListener(OnCompletionListener ocl) {
this.ocl = ocl;
}
}