fixed issue caused by misunderstanding(?) of samplerate
This commit is contained in:
parent
bb934de70c
commit
36eb547943
@ -1,6 +1,7 @@
|
|||||||
package zero1hd.rhythmbullet.audio.analyzer;
|
package zero1hd.rhythmbullet.audio.analyzer;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
import com.badlogic.gdx.utils.FloatArray;
|
import com.badlogic.gdx.utils.FloatArray;
|
||||||
|
|
||||||
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
|
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
|
||||||
@ -28,7 +29,7 @@ public class SpectralFluxAnalysisRunnable implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
progress = 0;
|
progress = 0;
|
||||||
int tasksDone = 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[] audioPCM = new float[musicManager.getReadWindowSize()];
|
||||||
float[] spectrum = new float[(musicManager.getReadWindowSize()/2)+1];
|
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());
|
FloatFFT_1D fft = new FloatFFT_1D(musicManager.getReadWindowSize());
|
||||||
int seedDigit = 0;
|
int seedDigit = 0;
|
||||||
|
|
||||||
while (musicManager.readSamples(audioPCM) > 0 && work) {
|
while (musicManager.readSampleFrames(audioPCM) > 0 && work) {
|
||||||
|
|
||||||
fft.realForward(audioPCM);
|
fft.realForward(audioPCM);
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ public class WAVSampleReader {
|
|||||||
return audioInputStream.getFrameLength();
|
return audioInputStream.getFrameLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readSamples(float[] samples) throws IOException {
|
public int readSamplesAsFrames(float[] samples) throws IOException {
|
||||||
int framesRead = 0;
|
int framesRead = 0;
|
||||||
for (int sampleID = 0; sampleID < samples.length; sampleID++) {
|
for (int sampleID = 0; sampleID < samples.length; sampleID++) {
|
||||||
if (audioInputStream.read(buffer) > 0) {
|
if (audioInputStream.read(buffer) > 0) {
|
||||||
@ -53,28 +53,6 @@ public class WAVSampleReader {
|
|||||||
return framesRead;
|
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() {
|
public AudioInputStream getAudioInputStream() {
|
||||||
return audioInputStream;
|
return audioInputStream;
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ public interface MusicManager extends Disposable {
|
|||||||
* @param samples the array that should contain said samples
|
* @param samples the array that should contain said samples
|
||||||
* @return amount read. Will return 0 if end of stream.
|
* @return amount read. Will return 0 if end of stream.
|
||||||
*/
|
*/
|
||||||
public int readSamples(float[] samples);
|
public int readSampleFrames(float[] samples);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns sample count.
|
* returns sample count.
|
||||||
|
@ -38,7 +38,7 @@ public class Mp3Manager implements MusicManager {
|
|||||||
private int sampleRate;
|
private int sampleRate;
|
||||||
private long sampleCount;
|
private long sampleCount;
|
||||||
private double durationInSeconds;
|
private double durationInSeconds;
|
||||||
private byte channels;
|
private int channels;
|
||||||
Bitstream bitstream;
|
Bitstream bitstream;
|
||||||
MP3Decoder decoder;
|
MP3Decoder decoder;
|
||||||
OutputBuffer sampleBuffer;
|
OutputBuffer sampleBuffer;
|
||||||
@ -63,10 +63,13 @@ public class Mp3Manager implements MusicManager {
|
|||||||
Logger.getLogger("org.jaudiotagger").setLevel(Level.OFF);
|
Logger.getLogger("org.jaudiotagger").setLevel(Level.OFF);
|
||||||
try {
|
try {
|
||||||
MP3File mp3File = new MP3File(audioFile.file());
|
MP3File mp3File = new MP3File(audioFile.file());
|
||||||
sampleCount = MathUtils.round(Float.valueOf((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())));
|
|
||||||
sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber();
|
sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber();
|
||||||
durationInSeconds = mp3File.getMP3AudioHeader().getPreciseTrackLength();
|
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) {
|
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -86,7 +89,7 @@ public class Mp3Manager implements MusicManager {
|
|||||||
} catch (BitstreamException e) {
|
} catch (BitstreamException e) {
|
||||||
throw new GdxRuntimeException("error while preloading mp3", e);
|
throw new GdxRuntimeException("error while preloading mp3", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
playbackMusic = Gdx.audio.newMusic(audioFile);
|
playbackMusic = Gdx.audio.newMusic(audioFile);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -118,7 +121,7 @@ public class Mp3Manager implements MusicManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readSamples(float[] samples) {
|
public int readSampleFrames(float[] samples) {
|
||||||
readIndex++;
|
readIndex++;
|
||||||
int framesRead = 0;
|
int framesRead = 0;
|
||||||
for (int sid = 0; sid < samples.length; sid++) {
|
for (int sid = 0; sid < samples.length; sid++) {
|
||||||
|
@ -63,10 +63,10 @@ public class WAVManager implements MusicManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readSamples(float[] samples) {
|
public int readSampleFrames(float[] samples) {
|
||||||
try {
|
try {
|
||||||
readIndex++;
|
readIndex++;
|
||||||
return d.readSamples(samples);
|
return d.readSamplesAsFrames(samples);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ public class WAVManager implements MusicManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getSampleCount() {
|
public long getSampleCount() {
|
||||||
return ais.getFrameLength();
|
return ais.getFrameLength()*d.getChannels();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user