javax.sound.sampled.UnsupportedAudioFileException: Stream of unsupported format - java

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.

Related

Detect Specific Frequency From Microphone Java

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.

How can I convert a wav file in java

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.

Why FrameLength is -1?

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

Process microphone data in real time in java

I'm using this instructions for get a signal audio of a microphone:
while(!stopCapture){
int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
if(cnt > 0){
byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
byteArrayOutputStream.close();
byte audio[] = byteArrayOutputStream.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
AudioInputStream ais = new AudioInputStream(input, audioFormat, audio.length / audioFormat.getFrameSize());
But these instructions wait the entire signal before elaborate it.
I need elaborate it in real time...is possible?
How can i do it?
thanks

Java ogg vorbis encoding

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

Categories