1

I've been trying to figure out for the life of me how to cut up a certain amount of milliseconds out of a sample. My first problem is when I insert this code into eclipse. It tells me AudioInputStream, AudioFileFormat and FileFormat are not available. If I can't use FileFormat then i can't pass methods into other methods to get the correct calculations to save the correct amount of music. I can't even import them. But more importantly, how can I change the parameter "secondstocopy" into "millisecondstocopy" without ruining the integrity of the algorithm? Still new to java, thank you for your help!

//edit// FileFormat fileFormat = AudioSystem.getAudioFileFormat(file); It seems I can't use this bit of code for android. The getAudioFileFormat doesn't work. Nor can I calculate the fram rate because of.. int bytesPerSecond = format. * (int)format.getFrameRate(); How do I properly get the format and frame rate with media player so that I can calculate the bytesPerSecond and secondsToCopy? AudioSystem is part of JavaSound, and JavaSound is part of the desktop JVM/SDK. JavaSound is NOT present in the Android JVM/SDK, so your old code will not compile on any current Android SDK. I get an exception using java on android (java.lang.NoClassDefFoundError), why?

How can I calculate the file format and fram rate and stuff without the javasound jvm/sdk. I can tell you now that the sounds are 44100 and 16 bit something. In the get info pane of the recording file, it says total bit rate: 128,000 with two audio channels. I'm not good with encoding stuff but I'm recording the audio with a program on mac called wiretap pro. It tells me the the parameters.

 import java.io.*;
import javax.sound.sampled.*;

class AudioFileProcessor {

  public static void main(String[] args) {
    copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", ƒ, 1);
  }

  public static void copyAudio(String sourceFileName, String destinationFileName, int startSecond, int secondsToCopy) {
    AudioInputStream inputStream = null;
    AudioInputStream shortenedStream = null;
    try {
      File file = new File(sourceFileName);
      AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
      AudioFormat format = fileFormat.getFormat();
      inputStream = AudioSystem.getAudioInputStream(file);
      int bytesPerSecond = format.getFrameSize() * (int)format.getFrameRate();
      inputStream.skip(startSecond * bytesPerSecond);
      long framesOfAudioToCopy = secondsToCopy * (int)format.getFrameRate();
      shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy);
      File destinationFile = new File(destinationFileName);
      AudioSystem.write(shortenedStream, fileFormat.getType(), destinationFile);
    } catch (Exception e) {
      println(e);
    } finally {
      if (inputStream != null) try { inputStream.close(); } catch (Exception e) { println(e); }
      if (shortenedStream != null) try { shortenedStream.close(); } catch (Exception e) { println(e); }
    }
  }

}

this code was taken from: cutting a wave file

Community
  • 1
  • 1
losethequit
  • 203
  • 3
  • 17
  • 1
    `public static void main(String[] args) { copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", ƒ, 1); }`. Do you call this method explicitly, or are you expecting it to be called when the class loads? (it's not). To change seconds to milliseconds - really? How about `int millis = secondsToCopy * 1000`? I think some basic tutorials are called for before you proceed. – Simon Feb 16 '14 at 18:33
  • I copied this public static void main(String[] args) { copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", ƒ, 1); } so that was a mistake, this code is ment for android – losethequit Feb 16 '14 at 20:38
  • long framesOfAudioToCopy = secondsToCopy * (int)format.getFrameRate(); if i change secondsToCopy with say a number like 1000 instead of 1, it will through off the calculation. @simon – losethequit Feb 16 '14 at 20:38
  • 1
    Ah, that's where you want to change it. That's simple maths! Just divide the milliseconds you want by 1000 instead. `long framesOfAudioToCopy = (long)((millis/1000f) * format.getFrameRate());` – Simon Feb 16 '14 at 20:43

0 Answers0