Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > javax.sound.sampled

Reply
Thread Tools

javax.sound.sampled

 
 
Roedy Green
Guest
Posts: n/a
 
      09-01-2008
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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-01-2008
On Sep 1, 8:39*pm, Roedy Green <(E-Mail Removed)>
wrote:
> 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?

--
Andrew Thompson
http://pscode.org/
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      09-01-2008
Roedy Green wrote:
> 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.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-02-2008
On Sep 2, 9:43*am, Knute Johnson <(E-Mail Removed)>
wrote:
> Roedy Green wrote:
> > 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.


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?

--
Andrew Thompson
http://pscode.org/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      09-02-2008
Andrew Thompson wrote:
>
> 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.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2008
On Mon, 01 Sep 2008 23:11:24 -0700, Knute Johnson
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>
>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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2008
On Mon, 01 Sep 2008 16:43:49 -0700, Knute Johnson
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>
>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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2008
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;
}
}


--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      09-02-2008
Roedy Green wrote:
> On Mon, 01 Sep 2008 23:11:24 -0700, Knute Johnson
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
>> 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.


Mixer.getSourceLineInfo()

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

--

Knute Johnson
email s/nospam/knute2008/

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments