I am trying to get the amplitude of one of the mp3 files. Following is the code:
AudioInputStream ain = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = ain.getFormat();
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels()*2,
baseFormat.getSampleRate(),
false);
//play(file,din,decodedFormat,ain);
ain = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
DecodedMpegAudioInputStream decodedStream = new DecodedMpegAudioInputStream(decodedFormat, ain);
System.out.println(ain.getFrameLength());
System.out.println(decodedStream.getFrameLength());
The problem is the last printlns are returning -1 in other words there is no information of frame length in the mp3 file or the audiostream is unable to read the frame length (is that possible?). I am trying to learn about mp3 audio file format. Am I doing this correct? OR is there any other way to get the frame length?
Verify if AudioSystem.getAudioFileFormat(file) is an MPEG format and run through its properties
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
if (audioFileFormat instanceof TAudioFileFormat) {
Map<String, Object> properties = ((TAudioFileFormat) audioFileFormat).properties();
// ...
}
There's properties like mp3.length.bytes and may help
Related
JukeBox
InputStream in = JukeBox.class.getResourceAsStream(s);
InputStream bin = new BufferedInputStream(in);
AudioInputStream ais = AudioSystem.getAudioInputStream(bin);
AudioFormat baseFormat = ais.getFormat();
PlayState
JukeBox.load("/Music/bgmusic.mp3", "music1");
JukeBox.setVolume("music1", -10);
JukeBox.loop("music1", 1000, 1000, JukeBox.getFrames("music1") - 1000);
*javax.sound.sampled.UnsupportedAudioFileException: Stream of unsupported format*
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1014)
at com.neet.DiamondHunter.Manager.JukeBox.load(JukeBox.java:37)
at com.neet.DiamondHunter.GameState.PlayState.init(PlayState.java:95)
at com.neet.DiamondHunter.Manager.GameStateManager.setState(GameStateManager.java:60)
I'm working on a JavaFX project and these codes cause the error and freezing the program.
I'm trying to capture audio that is coming from microphone and i wanted to check the frequency of sound. If I get a frequency greater then let's say : 1316.8 then I will start recording for 1 minute.
I am struggling with converting byte Data to Frequency.
I have used Javax.sound to capture audio that is coming from microphone and I have done the recording part as well.
AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);
try {
TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetLine.open(format);
targetLine.start();
SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(sourceInfo);
sourceLine.open(format);
sourceLine.start();
int numBytesRead;
byte[] targetData = new byte[targetLine.getBufferSize() / 5];
I expect the output to be like Frequency of every sound that is coming from microphone.
Trying to get the frame size of an audio file I am getting instead -1. I tried to look for the interpretation of of this result in the JavaDoc but it does not mention anything big. Here's the source code :
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
/*....*/
File file = new File("/home/songs/audio.mp3");
MpegAudioFileReader mpegAudioFileReader = new MpegAudioFileReader();
AudioInputStream audioInputStream = mpegAudioFileReader.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frameSize = format.getFrameSize();//frameSize = -1
float frameRate = format.getFrameRate();//frameRate = 38.28125
Inspecting he format object gives this : MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, 38.28125 frames/second,
I do not know why the frame size is unknown although it does appear on my audio file properties :
Any help is more than appreciated. Thanks.
getFormat() etc is implemented by the MPEG guys so it returns what they have - probably they left this blank or unable to extract;
If you put another .wav file you will probably get 2:
try {
audioInputStream=AudioSystem.getAudioInputStream(new File(".......wav"));
System.out.println(audioInputStream.getFormat().getFrameSize());
} catch (Exception e) {
e.printStackTrace();
}
Other notes: I dont see the Frame size in your display; it's rather the sample/bit rate so be sure to differentiate about that.
But for mp3 you have to live with that.
You can also create your own format if that helps - dont know your application
AudioFormat format = audioInputStream.getFormat();
newFormat=new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
16,
format.getChannels(),
format.getChannels() * 2,
format.getSampleRate(),
false);
How can I convert a wav file in java
AudioFormat targetFormat = new AudioFormat(
sourceFormat.getEncoding(),
fTargetFrameRate,
16,
sourceFormat.getChannels(),
sourceFormat.getFrameSize(),
fTargetFrameRate,
false);
in result Exception :
java.lang.IllegalArgumentException: Unsupported conversion:
ULAW 8000.0 Hz, **16 bit**, mono, 1 bytes/frame, **from** ULAW 8000.0 Hz, **8 bit**, mono, 1 bytes/frame
it is possible in java?
I need get wav file 16 bit, from 8
Here is a method that will convert an 8-bit uLaw encoded binary file into a 16-bit WAV file using built-in Java methods.
public static void convertULawFileToWav(String filename) {
File file = new File(filename);
if (!file.exists())
return;
try {
long fileSize = file.length();
int frameSize = 160;
long numFrames = fileSize / frameSize;
AudioFormat audioFormat = new AudioFormat(Encoding.ULAW, 8000, 8, 1, frameSize, 50, true);
AudioInputStream audioInputStream = new AudioInputStream(new FileInputStream(file), audioFormat, numFrames);
AudioSystem.write(audioInputStream, Type.WAVE, new File("C:\\file.wav"));
} catch (IOException e) {
e.printStackTrace();
}
}
Look at this one: Conversion of Audio Format it is similar to your issue suggesting looking at http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioSystem.html
You can always use FFMPEG, http://ffmpeg.org/, to do the conversion. Your Java program can call FFMPEG to do the conversion.
FFMPEG works on all OS.
i am using tritonous package for audio encoding in ogg-vorbis. I face a problem when i am giving the audio format.
Unsupported conversion: VORBIS 44100.0Hz, unknown bits per sample, mono, unknown frame size, from PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
This is my code where i am specifying the format
File outputFile = new File(userDir+"//San"+"_"+strFilename + ".spx");
// Using PCM 44.1 kHz, 16 bit signed,stereo.
if(osName.indexOf("win") >= 0){
System.out.println("windows");
audioFormat = getWindowsAudioFormat();
sampleRate = 44100.0F;
}else {
System.out.println("mac");
audioFormat = getMacAudioFormat();
sampleRate = 44100.0F;
}
AudioFormat vorbisFormat = new AudioFormat(VORBIS,
sampleRate,
AudioSystem.NOT_SPECIFIED,
1,
AudioSystem.NOT_SPECIFIED,
AudioSystem.NOT_SPECIFIED,
false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
AudioFileFormat.Type fileType = null;
File audioFile = null;
fileType = VORBIS;
try
{
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
}
catch (LineUnavailableException e)
{
System.out.println("unable to get a recording line");
e.printStackTrace();
System.exit(1);
}
AudioInputStream ais = new AudioInputStream(targetDataLine);
ais = AudioSystem.getAudioInputStream(vorbisFormat, ais);
final Recorder recorder = new Recorder(targetDataLine,ais,fileType,outputFile);
int number = 0;
System.out.println("Recording...");
recorder.start();
I wrote a utility class to encode OGG Vorbis audio files from Java, using the xiph Java ports of libogg and libvorbis.
https://github.com/xjmusic/java-vorbis-encoder/blob/master/VorbisEncoder.java