Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.sound.sampled.*;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- public class ReadMP3 {
- private ArrayList<Integer> bytes = new ArrayList<>();
- private AudioFormat decodedFormat;
- public ReadMP3() throws UnsupportedAudioFileException, IOException {
- String filename = new ReadFiles().getFile();
- File file = new File(filename);
- AudioInputStream in = AudioSystem.getAudioInputStream(file);
- AudioInputStream din = null;
- AudioFormat baseFormat = in.getFormat();
- AudioFormat decodedFormat = new
- AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
- baseFormat.getSampleRate(),
- 16,
- baseFormat.getChannels(),
- baseFormat.getChannels() * 2,
- baseFormat.getSampleRate(),
- false);
- din = AudioSystem.getAudioInputStream(decodedFormat, in);
- this.decodedFormat = decodedFormat;
- int i = 0;
- while(true){
- int currentByte = din.read();
- if (currentByte == -1) {break;}
- bytes.add(i, currentByte);
- i++;
- }
- din.close();
- in.close();
- }
- public class AnalyzeMP3 {
- //adds 4 bytes to offset[i][4], where each i represents 1hz,
- //and 44100hz=1sec
- public static int[][] calculate(ReadMP3 mp3) {
- //calculates and prints how long the song is
- double seconds = mp3.getBytes().size() /
- mp3.getDecodedFormat().getFrameRate() / 4;
- System.out.println("Length of song: " + (int)seconds + "s");
- //adds 4 values to i through the whole song
- int[][] offset = new int[mp3.getBytes().size()/4][4];
- for(int i = 0; i < mp3.getBytes().size()/4; i++) {
- for(int j = 0; j < 4; j++) {
- offset[i][j] = mp3.getBytes().get(i+j);
- }
- }
- int skip = (int)((seconds-(offset.length/44100))* 44100);
- return cleanUp(offset, skip);
- }
- //Removes the header from the mp3 array, the amount to remove from the
- //beginning is equal to skip
- private static int[][] cleanUp(int[][] array, int skip) {
- int[][] newArray = new int[array.length-skip][4];
- int counter = 0;
- for(int i = skip; i < array.length;i++) {
- newArray[counter][0] = array[i][0];
- newArray[counter][1] = array[i][1];
- newArray[counter][2] = array[i][2];
- newArray[counter][3] = array[i][3];
- counter++;
- }
- return newArray;
- }
- }
Add Comment
Please, Sign In to add comment