Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

javax.sound.sampled supported formats?

2,176 views
Skip to first unread message

David Lee Lambert

unread,
Jan 5, 2009, 7:12:12 AM1/5/09
to
I'm trying to add some mouse-over sound-effects to a small Java Swing
application. I've converted the audio-files to .au (8 Khz, μ-law)
format because I found a tip somewhere ( http://www.chami.com/tips/Internet/060698I.html
) that says that's the default format for applets, and to save space.
I put together the following code to actually play the file based on
an example I found somewhere else:

AudioInputStream aIn
= AudioSystem.getAudioInputStream(
Program.class.getResource(key+".au"));

AudioFormat fmt = aIn.getFormat();
System.err.println("Playing clip, format="+fmt);
SourceDataLine aLine = (SourceDataLine) // (line 102)
AudioSystem.getLine(
new DataLine.Info(SourceDataLine.class,fmt));
aLine.open(fmt);
aLine.start();

int n;
byte[] buf = new byte[8200];
do {
n = aIn.read(buf, 0, buf.length);
if (n>0)
aLine.write(buf, 0, n);
} while (n>0);

aLine.drain();
aLine.close();

However, on Linux with Sun JRE 1.5 (the only platform I care about
for this particular program), I get an exception when that code gets
called:

java.lang.IllegalArgumentException: No line matching interface
SourceDataLine supporting format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/
frame, is supported.
at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:
459)
at com.lmert.kidmenu.Program$2.run(Program.java:102)
at java.lang.Thread.run(Thread.java:595)

Any clues on what I'm doing wrong? Is there an even simpler way to do
this, outside an applet?

--
DLL

Andrew Thompson

unread,
Jan 5, 2009, 7:39:57 AM1/5/09
to
On Jan 5, 11:12 pm, David Lee Lambert <dav...@lmert.com> wrote:
> I'm trying to add some mouse-over sound-effects to a small Java Swing
> application.  I've converted the audio-files to .au (8 Khz, ì-law)

What is 'i-law'? The law of 'me'?

> format because I found a tip somewhere (http://www.chami.com/tips/Internet/060698I.html)

The internet is a big place, filled with
clueless morons. Take it with a grain of salt
(be skeptical).

>..that says that's the default format for applets, and to save space.


> I put together the following code to actually play the file based on
> an example I found somewhere else:

Where? (Again, the internet is big - try to narrow
it down with an URL).

> ..I get an exception when that code gets


> called:
>
> java.lang.IllegalArgumentException: No line matching interface
> SourceDataLine supporting format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes

...


> Any clues on what I'm doing wrong?  

Java sound generally only deals with 44.1KHz,
stereo, 16 bit sound. I generally have to
*convert* any sound format to the above mentioned
parameters before JavaSound will deal with it.

This can be done, entirely within the code,
regardless of the original source format.

>..Is there an even simpler way to do
> this,  

The 'simple way' within an applet, is to
hand control over to the Applet.getAudioClip()
method, which dumbs things down to a level most developers (even
incompetent ones) can understand.

To do it within an application, convert the
InputStream to a format that the underlying sound
system can deal with.

--
Andrew Thompson
http://pscode.org/

John B. Matthews

unread,
Jan 5, 2009, 8:36:21 AM1/5/09
to
In article
<79ab7d13-b003-4135...@w1g2000prk.googlegroups.com>,

David Lee Lambert <dav...@lmert.com> wrote:

> [...] I've converted the audio-files to .au (8 Khz, ì-law) [...]

I'm guessing that was supposed to be µ-law, with a \u03BC:

<http://en.wikipedia.org/wiki/M-law_algorithm>

> <http://www.chami.com/tips/Internet/060698I.html>

This might be more dispositive:

<http://java.sun.com/docs/books/tutorial/deployment/applet/sound.html>

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

Knute Johnson

unread,
Jan 5, 2009, 1:53:33 PM1/5/09
to

Applets can play 8 bit 8khz u-law files in au format.

JavaSound has a lot more options but one of the problems is there are
thousands of audio formats and they can be in any number of file
formats. What formats can be played is very dependent on your sound
card and there are thousands of those too. My card for example, cannot
play 8 bit 8khz u-law audio.

One option that was mentioned was to convert the audio format that you
read from the file to an audio format that can be played. See my code
below, it converts the format to PCM_SIGNED that can be played by most
cards. Andrew's Clip idea is a good one too, see the second code
example. The AudioClip and Clip are two different classes.

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

public class Play {
public static void main(String[] args) {
class MyLineListener implements LineListener {
public void update(LineEvent le) {
LineEvent.Type type = le.getType();
System.out.println(type);
}
};

try {
AudioInputStream fis =
AudioSystem.getAudioInputStream(new File(args[0]));
System.out.println("File AudioFormat: " + fis.getFormat());
AudioInputStream ais = AudioSystem.getAudioInputStream(
AudioFormat.Encoding.PCM_SIGNED,fis);
AudioFormat af = ais.getFormat();
System.out.println("AudioFormat: " + af.toString());

int frameRate = (int)af.getFrameRate();
System.out.println("Frame Rate: " + frameRate);
int frameSize = af.getFrameSize();
System.out.println("Frame Size: " + frameSize);

SourceDataLine line = AudioSystem.getSourceDataLine(af);
line.addLineListener(new MyLineListener());

line.open(af);
int bufSize = line.getBufferSize();
System.out.println("Buffer Size: " + bufSize);

line.start();

byte[] data = new byte[bufSize];
int bytesRead;

while ((bytesRead = ais.read(data,0,data.length)) != -1)
line.write(data,0,bytesRead);

line.drain();
line.stop();
line.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

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

public class PlayClip {
public static void main(String[] args) {
try {
AudioInputStream ais =
AudioSystem.getAudioInputStream(new File(args[0]));
AudioFormat af = ais.getFormat();
Clip line = AudioSystem.getClip();
line.open(ais);
line.start();
Thread.sleep(1);
line.drain();
line.stop();
line.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

David Lee Lambert

unread,
Jan 5, 2009, 9:44:27 PM1/5/09
to
On 5 ene, 13:53, Knute Johnson <nos...@rabbitbrush.frazmtn.com> wrote:
> David Lee Lambert wrote:
> > I'm trying to add some mouse-over sound-effects to a small Java Swing
> > application. [...]

Thanks! The "PlayClip" example was exactly what I needed. In this
case, the simpler, the better.

--
δαυιδ (\u03b4\u03b1\u03c5\u03b9\u03b4)
(&#948;&#945;&#965;&#953;&#948;)

Roedy Green

unread,
Jan 15, 2009, 12:09:08 AM1/15/09
to
On Mon, 5 Jan 2009 04:12:12 -0800 (PST), David Lee Lambert
<dav...@lmert.com> wrote, quoted or indirectly quoted someone who said
:

>However, on Linux with Sun JRE 1.5 (the only platform I care about
>for this particular program), I get an exception when that code gets
>called:
>
>java.lang.IllegalArgumentException: No line matching interface
>SourceDataLine supporting format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/
>frame, is supported.
> at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:
>459)
> at com.lmert.kidmenu.Program$2.run(Program.java:102)
> at java.lang.Thread.run(Thread.java:595)
>
>Any clues on what I'm doing wrong? Is there an even simpler way to do
>this, outside an applet?

see http://mindprod.com/jgloss/sound.html

You can find out what your current platform support with Mixer.
getSourceLineInfo. That gets you Line.Info[] which you can display
with Line.Info.toString. You can’t get at the individual properties.

In Applets you can use the slightly simpler java.applet.AudioClip
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP

0 new messages