Java Audio - PowerPoint PPT Presentation

Loading...

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



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Java Audio

Description:

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: http://www.sussex.ac.uk
Category:
Tags: audio | java | streamed

less

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

Title: Java Audio


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

3
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)
    audio
  • javax.sound.midi
  • Provides interfaces for MIDI synthesis,
    sequencing and event transport

4
javax.sound.sampled
  • 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

5
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

6
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,
    etc.)
  • 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)

7
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

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

9
AudioInputStream
  • 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

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

11
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
    mat()
  • This information is needed to ask Java Sound for
    a suitable line for this audio file

12
Line
  • 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

13
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

14
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

15
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

16
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

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

18
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

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

20
Fin
  • Fin
About PowerShow.com