failed attempt since there is difficulty to seek using mp3

This commit is contained in:
Harrison Deng 2017-12-11 21:59:32 -06:00
parent 95c49602ca
commit 25142029af
5 changed files with 35 additions and 9 deletions

View File

@ -39,6 +39,7 @@ public class Mp3Manager implements MusicManager {
private Music playbackMusic;
private int playbackIndex;
private long bitrate;
private int sampleRate;
private long sampleCount;
private double durationInSeconds;
@ -70,7 +71,7 @@ public class Mp3Manager implements MusicManager {
sampleCount = MathUtils.round(Float.valueOf((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())));
sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber();
durationInSeconds = mp3File.getMP3AudioHeader().getPreciseTrackLength();
bitrate = mp3File.getAudioHeader().getBitRateAsNumber();
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace();
}
@ -102,9 +103,10 @@ public class Mp3Manager implements MusicManager {
@Override
public void setReadIndexToPlaybackIndex() {
try {
long pointer = (long) (getPositionInSeconds()*(long)sampleRate)-readWindowSize;
long pointer = (long) (getPositionInSeconds()*(bitrate*sampleRate/8f));
raf.seek(pointer);
readWindowIndex = (int) (pointer/readWindowSize);
System.out.println(pointer);
readWindowIndex = playbackIndex;
currentByteSet = null;
} catch (IOException e) {
e.printStackTrace();
@ -206,6 +208,11 @@ public class Mp3Manager implements MusicManager {
return sampleRate;
}
@Override
public long getBitrate() {
return bitrate;
}
@Override
public int getreadWindowIndex() {
return readWindowIndex;

View File

@ -50,6 +50,10 @@ public interface MusicManager extends Disposable {
*/
public float getSampleRate();
/**
* @return the bitrate of the audio.
*/
public long getBitrate();
/**
* The current numerical value of the last window read
* @return
@ -89,6 +93,7 @@ public interface MusicManager extends Disposable {
public FileHandle getMusicFile();
/**
* Synchronizes reading to current playback.
*/
public void setReadIndexToPlaybackIndex();
}

View File

@ -21,11 +21,12 @@ public class WAVManager implements MusicManager {
private int playbackIndex;
private int readWindowIndex;
private Music playbackMusic;
WavDecoder decoder;
private WavDecoder decoder;
private FileHandle file;
private String basicSongName;
private RandomAccessFile raf;
private int sampleSizeInBits;
public WAVManager(FileHandle file) {
this.file = file;
basicSongName = file.name();
@ -37,7 +38,8 @@ public class WAVManager implements MusicManager {
e.printStackTrace();
}
playbackMusic = Gdx.audio.newMusic(file);
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, (float) decoder.getSampleRate(), 16, decoder.getChannels(), decoder.getChannels()*2, (float) decoder.getSampleRate(), false);
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, (float) decoder.getSampleRate(), decoder.getSampleSizeInBits(), decoder.getChannels(), decoder.getChannels()*2, (float) decoder.getSampleRate(), false);
sampleSizeInBits = decoder.getSampleSizeInBits();
}
@Override
@ -58,9 +60,9 @@ public class WAVManager implements MusicManager {
@Override
public void setReadIndexToPlaybackIndex() {
try {
long pointer = (long) (getPositionInSeconds()*(long)getSampleRate())-readWindowSize;
long pointer = (long) (getPositionInSeconds()*(getBitrate()/8f));
raf.seek(pointer);
readWindowIndex = (int) (pointer/readWindowSize);
readWindowIndex = playbackIndex;
} catch (IOException e) {
e.printStackTrace();
}
@ -157,4 +159,9 @@ public class WAVManager implements MusicManager {
public FileHandle getMusicFile() {
return file;
}
@Override
public long getBitrate() {
return (long) (getSampleRate()*sampleSizeInBits);
}
}

View File

@ -11,6 +11,7 @@ public class WavDecoder {
private int channels;
private double sampleRate;
private byte[] buffer;
private int sampleSize;
private AudioInputStream audioInputStream;
public WavDecoder(BufferedInputStream inputStream) throws IOException {
@ -20,6 +21,7 @@ public class WavDecoder {
channels = audioInputStream.getFormat().getChannels();
sampleRate = audioInputStream.getFormat().getSampleRate();
sampleSize = audioInputStream.getFormat().getSampleSizeInBits();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
@ -64,6 +66,10 @@ public class WavDecoder {
return audioInputStream;
}
public int getSampleSizeInBits() {
return sampleSize;
}
public void cleanAndClose() {
try {
audioInputStream.close();

View File

@ -29,7 +29,8 @@ public class Visualizer extends Widget implements Disposable {
if (mm != null && mm.isFinishedLoading() && !mmSet) {
Gdx.app.debug("Visualizer", "\nsample count: " + mm.getSampleCount()
+ "\nDuration in seconds: " + mm.getDuration() +
"\nSample rate: " + mm.getSampleRate());
"\nSample rate: " + mm.getSampleRate()
+ "\nBit rate: " + mm.getBitrate());
vis.setMM(mm);
mmSet = true;
}