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

View File

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

View File

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

View File

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

View File

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