Java Audio - PowerPoint PPT Presentation


PPT – Java Audio PowerPoint presentation | free to download - id: dcd58-ZDc1Z


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Java Audio


write an external file from an audio input stream ... data is pre-loaded rather than streamed, thus the clip's duration is known before playback ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 21
Provided by: drpauln
Learn more at:
Tags: audio | java | streamed


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Java Audio

Java Audio
Java Sound API 1.0
  • Specifies a software layer that allows
    application programs to communicate with an audio
    and MIDI engine

Java Sound API 1.0
  • Includes support for both digital and MIDI data,
    provided in two separate packages
  • javax.sound.sampled
  • This package specifies interfaces for the
    capture, mixing and playback of digital (sampled)
  • javax.sound.midi
  • Provides interfaces for MIDI synthesis,
    sequencing and event transport

  • The central task that the java sound API
    addresses is how to move bytes of formatted audio
    data into and out of the system
  • To play or capture audio using Java sound API you
    need at least two things
  • Formatted audio data
  • A line

Formatted Audio Data
  • Formatted audio data refers to sound in any of a
    number of standard formats
  • The Java Sound API distinguishes between data
    formats and file formats

Data Formats
  • A data format tells you how to interpret a series
    of bytes of raw sampled audio data
  • In the Java Sound API, a data format is
    represented by an AudioFormat object, which
    includes the following attributes
  • Encoding technique, usually Pulse Code Modulation
  • Number of channels (1 for mono, 2 for stereo,
  • Sample rate (samples per second, per channel)
  • Number of bits per sample (per channel)
  • Frame size in bytes
  • Frame rate
  • Byte order (big-endian or little-endian)

File Formats
  • Specifies the structure of a sound file,
    including the format of the raw audio data in the
    file, and other information that can be stored in
    the file
  • File format is represented by an AudioFileFormat
    object, which contains
  • The file type (WAVE, AIFF, etc.)
  • The file's length in bytes
  • The length, in frames, of the audio data
    contained in the file
  • An AudioFormat object that specifies the data
    format of the audio data contained in the file

File Formats
  • The AudioSystem class provides methods for
    reading and writing sounds in different file
    formats, and for converting between different
    data formats
  • By reading a sound file as an AudioInputStream,
    you get immediate access to the samples, without
    having to worry about the sound file's structure
    (its header, chunks, etc.)

  • An audio input stream is an input stream with a
    specified audio format and length that enables
    you to
  • obtain an audio input stream from an external
    audio file, stream, or URL
  • write an external file from an audio input stream
  • convert an audio input stream to a different
    audio format

Reading from an Audio File
  • File soundFile new File(String filename)
  • AudioInputStream audioInputStream null
  • audioInputStream AudioSystem.getAudioInputStream

Finding Data Format
  • From the AudioInputStream, i.e. from the sound
    file we fetch information about the format of the
    audio data
  • AudioFormat audioFormat audioInputStream.getFor
  • This information is needed to ask Java Sound for
    a suitable line for this audio file

  • A line is an element of the digital audio
    pipeline - that is, a path for moving audio
    into or out of the system
  • A Clip is a kind of line into which you can load
    audio data prior to playback
  • A SourceDataLine is an input that accepts a
    real-time stream of audio data

Line Interface Hierarchy
  • The base interface, Line, describes the minimal
    functionality common to all lines
  • Controls - Data lines and ports often have a set
    of controls that affect the audio signal passing
    through the line
  • Open and Closed status
  • Events

Line Types
  • Ports are simple lines for input or output of
    audio to or from audio devices
  • some common types of ports are the microphone,
    line input, CD-ROM drive, speaker, headphone, and
    line output
  • A TargetDataLine receives audio data from a mixer
  • The TargetDataLine interface provides methods for
    reading the data from the target data line's
    buffer and for determining how much data is
    currently available for reading

Line Types
  • A SourceDataLine receives audio data for playback
  • Provides methods for writing data to the source
    data line's buffer for playback, and for
    determining how much data the line is prepared to
    receive without blocking
  • A Clip is a data line into which audio data can
    be loaded prior to playback.
  • The data is pre-loaded rather than streamed, thus
    the clip's duration is known before playback
  • Any starting position in the media can be chosen
  • Clips can be looped

Getting a Line
  • Asking for a line is a rather tricky thing.
  • We have to construct an Info object that
    specifies the desired properties for the line.
  • First, we have to say which kind of line we want
  • SourceDataLine (for playback), Clip (for repeated
    playback) and TargetDataLine (for recording)
  • Then, we have to pass an AudioFormat object, so
    that the Line knows which format the data passed
    to it will have

Getting a Clip
  • Clip soundclip null
  • DataLine.Info clipinfo new DataLine.Info(
    ass, audioFormat)
  • try
  • soundclip (Clip) AudioSystem.getLine(clipinfo
  • catch exceptions
  • soundclip.start()

Getting a SourceDataLine
  • This is the same process as getting a Clip
  • However, the SourceDataLine does not store all
    the audio data, rather it buffers it in real-time
  • So to output the audio we have to read it from
    the AudioInputStream in real-time

Getting a SourceDataLine
  • byte buffer new byteEXTERNAL_BUFFER_SIZE
  • while (nBytesRead ! -1)
  • nBytesRead, 0,
  • if (nBytesRead gt 0)
  • nBytesWritten line.write(buffer, 0,

  • Fin