issue when opening wav file using (Java) AudioInputStream - java

I am using JDK7 and trying to run a wav file - I tried the following test but got the error copied below:
Error:
line with format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.
Sample Code:
import javax.sound.sampled.*;
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
new File("C://Users//xyz//Desktop//centerClosed.wav"));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
Any ideas on how I go about handling this case? Thanks in advance

Your wav file seems to be in ULAW format, sampled at 8kHz, a format the clip apparently does not understand.
Try converting the audio to 44.1kHz PCM like this:
import javax.sound.sampled.*;
try {
Clip clip = AudioSystem.getClip();
AudioInputStream ulawIn = AudioSystem.getAudioInputStream(
new File("C://Users//xyz//Desktop//centerClosed.wav"));
// define a target AudioFormat that is likely to be supported by your audio hardware,
// i.e. 44.1kHz sampling rate and 16 bit samples.
AudioInputStream pcmIn = AudioSystem.getAudioInputStream(
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100f, 16, 1, 2, 44100f, true)
ulawIn);
clip.open(pcmIn);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}

Related

Unknown frame size

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);

Why is the spectrum different when it's captured with Java Sound and the Adobe Software?

I'm capturing guitar audio notes (same format and conditions in both cases), with Java Sound API and the Adobe Audition Software, same parameters in both. As a result, they (the recording) should be also the same in both ones, but, I'm getting a difference in the spectrum form.
The audio format values are:
sample rate = 8000
sample size in bits = 16
channel = 1
This is basically the code I have used with the API: (replacing the corresponding AudioFormat values)
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class TestMic {
public static void main(String[] args) {
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];
while (true) {
numBytesRead = targetLine.read(targetData, 0, targetData.length);
if (numBytesRead == -1) break;
sourceLine.write(targetData, 0, numBytesRead);
}
}
catch (Exception e) {
System.err.println(e);
}
}
}
Then, when I applied the fourier transform, I'm obtaining next values, this is the array generated with the Adobe Software:
graphics are also different:
With the Adobe Audition:
graphic adobe audition
With the Java Sound API:
graphic java sound
Why is this happening?

Getting the current master volume in Java

I have Clip plaiong on my app and I'd like to get either the master volume or the volume of the Clip that is playing... I am fairly new to this.
Here's the code where i play the audio:
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("src/music/music.wav"));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
clip.loop(-1);
Float volume = clip.getControl(FloatControl.Type.VOLUME).getValue();
You should probably look at Java's API entry for FloatControl.

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.

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