From 25142029af1e003ff49c1bfc15007fa2b11c545a Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 11 Dec 2017 21:59:32 -0600 Subject: [PATCH] failed attempt since there is difficulty to seek using mp3 --- .../zero1hd/rhythmbullet/audio/Mp3Manager.java | 15 +++++++++++---- .../zero1hd/rhythmbullet/audio/MusicManager.java | 5 +++++ .../zero1hd/rhythmbullet/audio/WAVManager.java | 15 +++++++++++---- .../audio/wavedecoder/WavDecoder.java | 6 ++++++ .../graphics/ui/components/Visualizer.java | 3 ++- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java b/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java index a7bbf91..ad41cb8 100755 --- a/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java +++ b/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java @@ -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(); @@ -205,7 +207,12 @@ public class Mp3Manager implements MusicManager { public float getSampleRate() { return sampleRate; } - + + @Override + public long getBitrate() { + return bitrate; + } + @Override public int getreadWindowIndex() { return readWindowIndex; diff --git a/core/src/zero1hd/rhythmbullet/audio/MusicManager.java b/core/src/zero1hd/rhythmbullet/audio/MusicManager.java index 4069578..7ae2a4f 100755 --- a/core/src/zero1hd/rhythmbullet/audio/MusicManager.java +++ b/core/src/zero1hd/rhythmbullet/audio/MusicManager.java @@ -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(); } diff --git a/core/src/zero1hd/rhythmbullet/audio/WAVManager.java b/core/src/zero1hd/rhythmbullet/audio/WAVManager.java index 089f625..2186d82 100755 --- a/core/src/zero1hd/rhythmbullet/audio/WAVManager.java +++ b/core/src/zero1hd/rhythmbullet/audio/WAVManager.java @@ -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); + } } diff --git a/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WavDecoder.java b/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WavDecoder.java index 0fff39e..f2e3fdf 100755 --- a/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WavDecoder.java +++ b/core/src/zero1hd/rhythmbullet/audio/wavedecoder/WavDecoder.java @@ -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(); diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java b/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java index 305fc33..6625dac 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java @@ -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; }