mp3 audio processor complete; slow conversion to cleaner system

This commit is contained in:
2018-07-02 00:52:55 -05:00
parent bf94851e06
commit 8f70d72ed1
7 changed files with 253 additions and 435 deletions

View File

@@ -1,4 +1,31 @@
package zero1hd.rhythmbullet.audio.processor;
public interface AudioProcessor {
import com.badlogic.gdx.utils.Disposable;
public interface AudioProcessor extends Disposable {
/**
* Called once, contains the initiation to the stream, only called when play-back begins.
* Not thread safe as it should be the first thing to be called during read process.
*/
public void initiate();
/**
* @return number of channels
*/
public boolean isStereo();
/**
* @return sample rate
*/
public int getSampleRate();
/**
* <b>Thread safe</b>
* Reads samples (NOT FRAMES) with interwoven data for stereo.
* stored in 16 bit format (first 8 are the first byte of data while the second 8 are the second byte of data that composes a short value)
* @param pcm the array the samples should fill
* @param syncObj the object that this object should use to synchronize multiple threads.
* @return the amount of samples read.
*/
public int readSamples(short[] pcm, Object syncObj);
}

View File

@@ -0,0 +1,82 @@
package zero1hd.rhythmbullet.audio.processor;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import com.badlogic.gdx.files.FileHandle;
public class WAVAudioProcessor implements AudioProcessor {
private boolean stereo;
private int sampleRate;
private byte[] buffer;
private FileHandle fileHandle;
private AudioInputStream audioInputStream;
private boolean initiated;
public WAVAudioProcessor(FileHandle fileHandle, int windowSize) throws IOException, UnsupportedAudioFileException {
this.fileHandle = fileHandle;
AudioFormat format = AudioSystem.getAudioFileFormat(fileHandle.file()).getFormat();
stereo = format.getChannels() > 1 ? true : false;
sampleRate = (int) format.getSampleRate();
}
@Override
public void initiate() {
try {
audioInputStream = AudioSystem.getAudioInputStream(fileHandle.file());
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
buffer = new byte[audioInputStream.getFormat().getFrameSize()];
initiated = true;
}
public boolean isStereo() {
return stereo;
}
public int getSampleRate() {
return sampleRate;
}
@Override
public int readSamples(short[] pcm, Object syncObj) {
if (initiated) {
synchronized (syncObj) {
int framesRead = 0;
for (int sampleID = 0; sampleID < pcm.length; sampleID++) {
try {
if (audioInputStream.read(buffer) > 0) {
pcm[sampleID] = (short) ((buffer[1] << 8) + (buffer[0] & 0x00ff));
if (stereo) {
short secondChan = (short) ((buffer[3] << 8) + (buffer[2] & 0x00ff));
sampleID++;
pcm[sampleID] = secondChan;
}
framesRead++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return framesRead;
}
} else {
throw new IllegalStateException("Stream has not been initialized.");
}
}
@Override
public void dispose() {
try {
audioInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}