javax.sound.sampled

Discussion in 'Java' started by Roedy Green, Sep 1, 2008.

  1. Roedy Green

    Roedy Green Guest

    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
     
    Roedy Green, Sep 1, 2008
    #1
    1. Advertising

  2. On Sep 1, 8:39 pm, 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.


    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/
     
    Andrew Thompson, Sep 1, 2008
    #2
    1. Advertising

  3. 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
     
    Knute Johnson, Sep 2, 2008
    #3
  4. On Sep 2, 9:43 am, Knute Johnson <>
    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/
     
    Andrew Thompson, Sep 2, 2008
    #4
  5. 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
     
    Knute Johnson, Sep 2, 2008
    #5
  6. Roedy Green

    Roedy Green Guest

    On Mon, 01 Sep 2008 23:11:24 -0700, Knute Johnson
    <> 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
     
    Roedy Green, Sep 2, 2008
    #6
  7. Roedy Green

    Roedy Green Guest

    On Mon, 01 Sep 2008 16:43:49 -0700, Knute Johnson
    <> 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
     
    Roedy Green, Sep 2, 2008
    #7
  8. Roedy Green

    Roedy Green Guest

    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
     
    Roedy Green, Sep 2, 2008
    #8
  9. Roedy Green wrote:
    > On Mon, 01 Sep 2008 23:11:24 -0700, Knute Johnson
    > <> 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
     
    Knute Johnson, Sep 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. viswanath

    mixing sampled sine waves

    viswanath, May 11, 2004, in forum: VHDL
    Replies:
    1
    Views:
    2,145
    Allan Herriman
    May 12, 2004
  2. Replies:
    1
    Views:
    784
    Esmond Pitt
    Mar 27, 2005
  3. picnanard
    Replies:
    0
    Views:
    609
    picnanard
    Sep 1, 2008
  4. Java Guy
    Replies:
    13
    Views:
    6,878
    Java Guy
    Mar 7, 2009
  5. Manuel Graune
    Replies:
    1
    Views:
    915
    Terry Reedy
    Aug 10, 2011
Loading...

Share This Page