Guest User

Untitled

a guest
Oct 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import javax.sound.sampled.*;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5.  
  6.  
  7. public class ReadMP3 {
  8.  
  9.  
  10. private ArrayList<Integer> bytes = new ArrayList<>();
  11. private AudioFormat decodedFormat;
  12.  
  13. public ReadMP3() throws UnsupportedAudioFileException, IOException {
  14.  
  15. String filename = new ReadFiles().getFile();
  16. File file = new File(filename);
  17. AudioInputStream in = AudioSystem.getAudioInputStream(file);
  18. AudioInputStream din = null;
  19. AudioFormat baseFormat = in.getFormat();
  20. AudioFormat decodedFormat = new
  21. AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
  22. baseFormat.getSampleRate(),
  23. 16,
  24. baseFormat.getChannels(),
  25. baseFormat.getChannels() * 2,
  26. baseFormat.getSampleRate(),
  27. false);
  28. din = AudioSystem.getAudioInputStream(decodedFormat, in);
  29. this.decodedFormat = decodedFormat;
  30.  
  31. int i = 0;
  32. while(true){
  33. int currentByte = din.read();
  34. if (currentByte == -1) {break;}
  35. bytes.add(i, currentByte);
  36. i++;
  37. }
  38. din.close();
  39. in.close();
  40. }
  41.  
  42. public class AnalyzeMP3 {
  43.  
  44.  
  45. //adds 4 bytes to offset[i][4], where each i represents 1hz,
  46. //and 44100hz=1sec
  47.  
  48. public static int[][] calculate(ReadMP3 mp3) {
  49.  
  50. //calculates and prints how long the song is
  51. double seconds = mp3.getBytes().size() /
  52. mp3.getDecodedFormat().getFrameRate() / 4;
  53. System.out.println("Length of song: " + (int)seconds + "s");
  54.  
  55. //adds 4 values to i through the whole song
  56. int[][] offset = new int[mp3.getBytes().size()/4][4];
  57. for(int i = 0; i < mp3.getBytes().size()/4; i++) {
  58. for(int j = 0; j < 4; j++) {
  59. offset[i][j] = mp3.getBytes().get(i+j);
  60. }
  61. }
  62.  
  63.  
  64. int skip = (int)((seconds-(offset.length/44100))* 44100);
  65.  
  66. return cleanUp(offset, skip);
  67. }
  68.  
  69. //Removes the header from the mp3 array, the amount to remove from the
  70. //beginning is equal to skip
  71. private static int[][] cleanUp(int[][] array, int skip) {
  72. int[][] newArray = new int[array.length-skip][4];
  73. int counter = 0;
  74. for(int i = skip; i < array.length;i++) {
  75. newArray[counter][0] = array[i][0];
  76. newArray[counter][1] = array[i][1];
  77. newArray[counter][2] = array[i][2];
  78. newArray[counter][3] = array[i][3];
  79. counter++;
  80.  
  81. }
  82.  
  83. return newArray;
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment