From 36eb547943a67e4ac32ddfd0415eb0330c055f3d Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 15 Jan 2018 13:50:30 -0600 Subject: [PATCH] fixed issue caused by misunderstanding(?) of samplerate --- .../SpectralFluxAnalysisRunnable.java | 5 ++-- .../audio/wavedecoder/WAVSampleReader.java | 24 +------------------ .../rhythmbullet/util/MusicManager.java | 2 +- .../desktop/audio/Mp3Manager.java | 13 ++++++---- .../desktop/audio/WAVManager.java | 6 ++--- 5 files changed, 16 insertions(+), 34 deletions(-) diff --git a/core/src/zero1hd/rhythmbullet/audio/analyzer/SpectralFluxAnalysisRunnable.java b/core/src/zero1hd/rhythmbullet/audio/analyzer/SpectralFluxAnalysisRunnable.java index aa6e30d..b2c5dce 100755 --- a/core/src/zero1hd/rhythmbullet/audio/analyzer/SpectralFluxAnalysisRunnable.java +++ b/core/src/zero1hd/rhythmbullet/audio/analyzer/SpectralFluxAnalysisRunnable.java @@ -1,6 +1,7 @@ package zero1hd.rhythmbullet.audio.analyzer; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.FloatArray; import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D; @@ -28,7 +29,7 @@ public class SpectralFluxAnalysisRunnable implements Runnable { public void run() { progress = 0; int tasksDone = 0; - long totalTasks = musicManager.getSampleCount()/musicManager.getReadWindowSize(); + long totalTasks = MathUtils.round((float)musicManager.getSampleCount()/musicManager.getChannelCount()/musicManager.getReadWindowSize()); float[] audioPCM = new float[musicManager.getReadWindowSize()]; float[] spectrum = new float[(musicManager.getReadWindowSize()/2)+1]; @@ -52,7 +53,7 @@ public class SpectralFluxAnalysisRunnable implements Runnable { FloatFFT_1D fft = new FloatFFT_1D(musicManager.getReadWindowSize()); int seedDigit = 0; - while (musicManager.readSamples(audioPCM) > 0 && work) { + while (musicManager.readSampleFrames(audioPCM) > 0 && work) { fft.realForward(audioPCM); diff --git a/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WAVSampleReader.java b/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WAVSampleReader.java index 1bb0ed4..708d738 100755 --- a/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WAVSampleReader.java +++ b/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WAVSampleReader.java @@ -31,7 +31,7 @@ public class WAVSampleReader { return audioInputStream.getFrameLength(); } - public int readSamples(float[] samples) throws IOException { + public int readSamplesAsFrames(float[] samples) throws IOException { int framesRead = 0; for (int sampleID = 0; sampleID < samples.length; sampleID++) { if (audioInputStream.read(buffer) > 0) { @@ -53,28 +53,6 @@ public class WAVSampleReader { return framesRead; } - public int readSamples(short[] samples) throws IOException { - int framesRead = 0; - - for (int sampleID = 0; sampleID < samples.length; sampleID++) { - if (audioInputStream.read(buffer) > 0) { - samples[sampleID] += (buffer[1] << 8) + (buffer[0] & 0x00ff); - if (audioInputStream.getFormat().getChannels() > 1) { - short altChan = (short) ((buffer[3] << 8) + (buffer[2] & 0x00ff)); - if (mergeChannels) { - samples[sampleID] = altChan > samples[sampleID] ? altChan : samples[sampleID]; - } else { - sampleID++; - samples[sampleID] = altChan; - } - } - framesRead ++; - } - - } - return framesRead; - } - public AudioInputStream getAudioInputStream() { return audioInputStream; } diff --git a/core/src/zero1hd/rhythmbullet/util/MusicManager.java b/core/src/zero1hd/rhythmbullet/util/MusicManager.java index 4f16a35..551b599 100755 --- a/core/src/zero1hd/rhythmbullet/util/MusicManager.java +++ b/core/src/zero1hd/rhythmbullet/util/MusicManager.java @@ -31,7 +31,7 @@ public interface MusicManager extends Disposable { * @param samples the array that should contain said samples * @return amount read. Will return 0 if end of stream. */ - public int readSamples(float[] samples); + public int readSampleFrames(float[] samples); /** * returns sample count. diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java index f672af8..8601a2b 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java @@ -38,7 +38,7 @@ public class Mp3Manager implements MusicManager { private int sampleRate; private long sampleCount; private double durationInSeconds; - private byte channels; + private int channels; Bitstream bitstream; MP3Decoder decoder; OutputBuffer sampleBuffer; @@ -63,10 +63,13 @@ public class Mp3Manager implements MusicManager { Logger.getLogger("org.jaudiotagger").setLevel(Level.OFF); try { MP3File mp3File = new MP3File(audioFile.file()); - sampleCount = MathUtils.round(Float.valueOf((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength()))); sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber(); durationInSeconds = mp3File.getMP3AudioHeader().getPreciseTrackLength(); - + channels = mp3File.getAudioHeader().getChannels().equals("Mono") ? 1:2; + sampleCount = MathUtils.round(Float.valueOf((float) (sampleRate*durationInSeconds))) * channels; + + //Rounding error probably? + sampleCount += readWindowSize*2; } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { e.printStackTrace(); } @@ -86,7 +89,7 @@ public class Mp3Manager implements MusicManager { } catch (BitstreamException e) { throw new GdxRuntimeException("error while preloading mp3", e); } - + playbackMusic = Gdx.audio.newMusic(audioFile); } @@ -118,7 +121,7 @@ public class Mp3Manager implements MusicManager { } @Override - public int readSamples(float[] samples) { + public int readSampleFrames(float[] samples) { readIndex++; int framesRead = 0; for (int sid = 0; sid < samples.length; sid++) { diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java index 9a3cb1f..590e5c3 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java @@ -63,10 +63,10 @@ public class WAVManager implements MusicManager { } @Override - public int readSamples(float[] samples) { + public int readSampleFrames(float[] samples) { try { readIndex++; - return d.readSamples(samples); + return d.readSamplesAsFrames(samples); } catch (IOException e) { e.printStackTrace(); } @@ -75,7 +75,7 @@ public class WAVManager implements MusicManager { @Override public long getSampleCount() { - return ais.getFrameLength(); + return ais.getFrameLength()*d.getChannels(); } @Override