circle now spins left on preGameScreen.java

This commit is contained in:
Harrison Deng 2017-05-16 17:28:16 -05:00
parent 27f2e51613
commit 1be8e0cc9d
3 changed files with 52 additions and 34 deletions

View File

@ -42,7 +42,7 @@ project(":desktop") {
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop" compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
compile "com.googlecode.soundlibs:mp3spi:1.9.5-1" compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1'
compile "org.apache.commons:commons-math3:3.2" compile "org.apache.commons:commons-math3:3.2"
@ -72,7 +72,7 @@ project(":android") {
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
compile "com.googlecode.soundlibs:mp3spi:1.9.5-1" compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1'
compile "org.apache.commons:commons-math3:3.2" compile "org.apache.commons:commons-math3:3.2"
@ -90,7 +90,7 @@ project(":core") {
compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile "com.googlecode.soundlibs:mp3spi:1.9.5-1" compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1'
compile "org.apache.commons:commons-math3:3.2" compile "org.apache.commons:commons-math3:3.2"

View File

@ -1,6 +1,9 @@
package zero1hd.polyjet.audio; package zero1hd.polyjet.audio;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
@ -18,40 +21,45 @@ import com.badlogic.gdx.files.FileHandle;
public class Mp3AudioData implements AudioData { public class Mp3AudioData implements AudioData {
private int readWindowSize = 1024; private int readWindowSize = 1024;
private AudioInputStream audStream; private AudioInputStream din;
private AudioInputStream faudStream;
private AudioFormat decodedFormat;
private AudioFormat audioFormat;
private Music playbackMusic; private Music playbackMusic;
private int readIndex; private int readIndex;
private int sampleCount; private int sampleCount;
private AudioInputStream in;
public Mp3AudioData(FileHandle audioFile) { public Mp3AudioData(FileHandle audioFile) {
try { try {
sampleCount = (int) new MP3File(audioFile.file()).getMP3AudioHeader().getNumberOfFrames(); sampleCount = (int) new MP3File(audioFile.file()).getMP3AudioHeader().getNumberOfFrames();
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e1) { } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
reset(); reset();
try { try {
audStream = AudioSystem.getAudioInputStream(audioFile.file()); File file = audioFile.file();
AudioFormat bFormat = audStream.getFormat(); in = AudioSystem.getAudioInputStream(file);
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, bFormat.getSampleRate(), 16, bFormat.getChannels(), bFormat.getChannels()*2, bFormat.getSampleRate(), false); din = null;
faudStream = AudioSystem.getAudioInputStream(audioFormat, audStream); AudioFormat baseFormat = in.getFormat();
decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
} catch (UnsupportedAudioFileException | IOException e) { } catch (UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
playbackMusic = Gdx.audio.newMusic(audioFile); playbackMusic = Gdx.audio.newMusic(audioFile);
} }
@Override @Override
public void readIndexUpdate() { public void readIndexUpdate() {
readIndex = (int) (playbackMusic.getPosition() * audioFormat.getSampleRate() / readWindowSize); readIndex = (int) (playbackMusic.getPosition() * decodedFormat.getSampleRate() / readWindowSize);
} }
@Override @Override
@ -67,10 +75,10 @@ public class Mp3AudioData implements AudioData {
playbackMusic = null; playbackMusic = null;
} }
if (audStream != null) { if (din != null) {
try { try {
audStream.close(); din.close();
faudStream.close(); in.close();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -88,15 +96,27 @@ public class Mp3AudioData implements AudioData {
return playbackMusic; return playbackMusic;
} }
byte[] toBeConverted = new byte[2];
@Override @Override
public int readSamples(float[] samples) { public int readSamples(float[] samples) {
int samplesRead = 0; int samplesRead = 0;
int sampleAverage = 0; short sampleAverage = 0;
for (int currentSample = 0; currentSample < samples.length; currentSample++) { for (int currentSample = 0; currentSample < samples.length; currentSample++) {
for (int channel = 0; channel < audioFormat.getChannels(); channel++) { for (int channel = 0; channel < decodedFormat.getChannels(); channel++) {
try { try {
sampleAverage += audStream.read(); int readCount = din.read(toBeConverted, 0, toBeConverted.length);
if (sampleAverage == -1) {
ByteBuffer bBuffer = ByteBuffer.allocate(2);
bBuffer.order(ByteOrder.LITTLE_ENDIAN);
bBuffer.put(toBeConverted[0]);
bBuffer.put(toBeConverted[1]);
sampleAverage += bBuffer.getShort(0);
if (readCount == -1) {
break; break;
} }
} catch (IOException e) { } catch (IOException e) {
@ -104,22 +124,20 @@ public class Mp3AudioData implements AudioData {
e.printStackTrace(); e.printStackTrace();
} }
} }
sampleAverage /= audioFormat.getChannels()*Short.MAX_VALUE+1;
sampleAverage /= decodedFormat.getChannels() * Short.MAX_VALUE + 1;
samples[currentSample] = sampleAverage; samples[currentSample] = sampleAverage;
sampleAverage = 0;
samplesRead++; samplesRead++;
if (sampleAverage == -1) {
break;
}
} }
return samplesRead; return samplesRead;
} }
@Override @Override
public AudioFormat getFormat() { public AudioFormat getFormat() {
return audioFormat; return decodedFormat;
} }
@Override @Override
public int getSampleCount() { public int getSampleCount() {
return sampleCount; return sampleCount;

View File

@ -52,10 +52,10 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter {
stage.clear(); stage.clear();
Image cyberCircle1 = new Image(core.getAssetManager().get("cybercircle3B.png", Texture.class)); Image cyberCircle1 = new Image(core.getAssetManager().get("cybercircle3B.png", Texture.class));
cyberCircle1.setScale(0.7f); cyberCircle1.setScale(0.8f);
cyberCircle1.setOrigin(cyberCircle1.getWidth()/2, cyberCircle1.getHeight()/2); cyberCircle1.setOrigin(cyberCircle1.getWidth()/2, cyberCircle1.getHeight()/2);
cyberCircle1.setColor(0.8f,0.8f,0.8f,0.7f); cyberCircle1.setColor(0.8f,0.8f,0.8f,0.7f);
cyberCircle1.addAction(Actions.forever(Actions.rotateBy(-360f, 10f))); cyberCircle1.addAction(Actions.forever(Actions.rotateBy(360f, 10f)));
cyberCircle1.setPosition(Gdx.graphics.getWidth()-cyberCircle1.getWidth()/2-10, -cyberCircle1.getHeight()*2/4f); cyberCircle1.setPosition(Gdx.graphics.getWidth()-cyberCircle1.getWidth()/2-10, -cyberCircle1.getHeight()*2/4f);
stage.addActor(cyberCircle1); stage.addActor(cyberCircle1);