fixed issue caused by misunderstanding(?) of samplerate

This commit is contained in:
Harrison Deng 2018-01-15 13:50:30 -06:00
parent bb934de70c
commit 36eb547943
5 changed files with 16 additions and 34 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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.

View File

@ -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++) {

View File

@ -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