javax.sound.sampled

R

Roedy Green

What sound formats does javax.sound.sampled support?

It seems to refuse ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, which
I would have thought was pretty basic.
 
A

Andrew Thompson

What sound formats does javax.sound.sampled support?

It seems to refuse ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame,  which
I would have thought was pretty basic.

I have an output file that suggests it handles
the spacemusic.au supplied by Sun that is..
af 1: ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame,
....

Can you give us an URL to your sample?
 
K

Knute Johnson

Roedy said:
What sound formats does javax.sound.sampled support?

It seems to refuse ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, which
I would have thought was pretty basic.

You probably can't play a ULAW encoded file. Just convert it to PCM_SIGNED.

I don't really understand all the details but unless you have a card
that can play ULAW data directly it won't work. I know that they don't
work on my computer anyway.
 
A

Andrew Thompson

You probably can't play a ULAW encoded file.  Just convert it to PCM_SIGNED.

Interesting... I got that format I quoted for
the .au from a list of files I have been using
to test a little player, and even checked it
loaded and played OK. But my player also does
a rendering of the waveform, and for that I
needed PCM, so it had already been converted!
I don't really understand all the details but unless you have a card
that can play ULAW data directly it won't work.  I know that they don't
work on my computer anyway.

You cannot play the spacemusic.au on your box?
 
K

Knute Johnson

Andrew said:
You cannot play the spacemusic.au on your box?

Not with a simple Clip or SourceDataLine player. It works fine if I
convert it to PCM_SIGNED first though. I've never been able to play
ULAW files with Java on this computer. I think you need compatible
hardware, although I will admit that I am definitely not an expert on
this subject.
 
R

Roedy Green

Not with a simple Clip or SourceDataLine player. It works fine if I
convert it to PCM_SIGNED first though. I've never been able to play
ULAW files with Java on this computer. I think you need compatible
hardware, although I will admit that I am definitely not an expert on
this subject.

I have a horrible feeling that Java is not actually rendering
anything, just handing off to native sound drivers. So what will play
depends on the abilities of the native drivers -- hardly WORA.

I have been trying to figure out how to get a list of all the formats
supported. The way it works it you have to supply a format then find
out of it is supported.

You'd think Sun AU would be the safest imaginable.
 
R

Roedy Green

You probably can't play a ULAW encoded file. Just convert it to PCM_SIGNED.

That worked. Further AudioClip can now play PCM_SIGNED as well.
Originally it would eat only ULAW. Thanks. Goldwave makes this
conversion easy.

All I need do now is make the sound play in the background, something
AudioClip did by itself.
 
R

Roedy Green

Here is my class to play sounds in the background with
javax.sound.sampled.

package com.mindprod.vercheck;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;
import java.net.URL;

/**
* Plays a sound using the javax.sound.sampled classes.
* It plays the sound in the background. If you play another sound,
before the
* first has finished, it won't play the sounds on top of each other.
It will block,
* and not return until the first sound has finished at it has started
the second.
* Created by IntelliJ IDEA.
* version 1.0 2008-09-01 initial version
*
* @author Roedy Green
*/
class Play implements Runnable
{
// ------------------------------ FIELDS
------------------------------

/**
* true if want debugging output
*/
private static final boolean DEBUGGING = false;

/**
* information about the format of the sound data.
*/
private final AudioFormat af;

/**
* stream current background thread is playing.
*/
private final AudioInputStream ais;

/**
* information about the output sound rendering device
*/
private final DataLine.Info info;

/**
* size of buffer used to copy sound bytes across
*/
private final int buffSize;
// -------------------------- PUBLIC STATIC METHODS
--------------------------

/**
* play a sound file supported by javax.sound.sampled e.g. a
signed PCM 8-bit au resource.
*
* @param url usually a resource to play created with
Class.getResource.
* @throws UnsupportedAudioFileException if you select a sound
file type not supported on this platform.
* @throws IOException if problem retrieving the
URL
*/
public static void play( final URL url ) throws
UnsupportedAudioFileException, IOException
{
play( AudioSystem.getAudioInputStream( url ) );
}

/**
* play a sound file supported by javax.sound.sampled e.g. a
signed PCM 8-bit au resource.
*
* @param file a sound file to play
* @throws UnsupportedAudioFileException if you select a sound
file type not supported on this platform.
* @throws IOException if problem retrieving the
file.
*/
public static void play( final File file ) throws
UnsupportedAudioFileException, IOException
{
play( AudioSystem.getAudioInputStream( file ) );
}

// -------------------------- PUBLIC INSTANCE METHODS
--------------------------
/**
* background thread to feed bytes from stream to sound renderer
*/
public void run()
{
synchronized ( getClass() )
{
try
{
final SourceDataLine line = ( SourceDataLine )
AudioSystem.getLine( info );
line.open( af, buffSize );
// start streaming in from the resource
line.start();
final byte[] data = new byte[buffSize];
int bytesRead;
/* copy from stream to sound renderer */
while ( ( bytesRead = ais.read( data, 0, data.length )
) != -1 )
{
line.write( data, 0, bytesRead );
}
/* sound data stream is finished, wait for sound to
finish */
line.drain();
line.stop();
line.close();
}
catch ( LineUnavailableException e )
{
// throwing an exception in a background thread
probably won't be caught properly.
err.println( "Line unavailable to play a sound" );
}
catch ( IOException e )
{
err.println( "Problems fetching data to play a sound"
);
}
}
}

// -------------------------- STATIC METHODS
--------------------------

/**
* play a sound file supported by javax.sound.sampled e.g. a
signed PCM 8-bit au resource.
*
* @param ais AudioIunputStream to play
*/
private static synchronized void play( final AudioInputStream ais
)
{
// check out stream ahead of time so Exception will be
fielded.
final AudioFormat af = ais.getFormat();
if ( DEBUGGING )
{
System.out.println( "audio format: " + af.toString() );
}

final DataLine.Info info = new DataLine.Info(
SourceDataLine.class, af );
if ( !AudioSystem.isLineSupported( info ) )
{
throw new IllegalArgumentException( "Cannot play an
unsupported audio format" );
}

final int frameRate = ( int ) af.getFrameRate();
final int frameSize = af.getFrameSize();
final int buffSize = frameRate * frameSize / 10;
if ( DEBUGGING )
{
out.println( "Frame Rate: " + frameRate );
out.println( "Frame Size: " + frameSize );
out.println( "Buffer Size: " + buffSize );
}

// We can't start another thread until previous one has
terminated
// because this method and run are both synchronised on the
class object.
// Start up a background thread to play the sound.
new Thread( new Play( ais, af, info, buffSize ) ).start();
}

// --------------------------- CONSTRUCTORS
---------------------------

/**
* constructor to create a thread run object to play a sound in
the background.
*
* @param ais audion input stream to play
* @param af audio format of the stream
* @param info info about the line that renders the sound
* @param buffSize size of buffer when copying bytes to rendering
device.
*/
private Play( AudioInputStream ais, AudioFormat af, DataLine.Info
info, int buffSize )
{
this.ais = ais;
this.af = af;
this.info = info;
this.buffSize = buffSize;
}
}
 
K

Knute Johnson

Roedy said:
I have a horrible feeling that Java is not actually rendering
anything, just handing off to native sound drivers. So what will play
depends on the abilities of the native drivers -- hardly WORA.

I have been trying to figure out how to get a list of all the formats
supported. The way it works it you have to supply a format then find
out of it is supported.

You'd think Sun AU would be the safest imaginable.

Mixer.getSourceLineInfo()

AU format supports uLaw, ADPCM, PCM and a few others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top