Java Sound API Medialogy, Semester 7, 2010 Aalborg University, Aalborg David Meredith

Slides:



Advertisements
Similar presentations
Tamara Berg Advanced Multimedia
Advertisements

                      Digital Audio 1.
Audio Programming With Python Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Using Multimedia on the Web Enhancing a Web Site with Sound, Video, and Applets.
CNIT 132 – Week 9 Multimedia. Working with Multimedia Bandwidth is a measure of the amount of data that can be sent through a communication pipeline each.
Sound in multimedia How many of you like the use of audio in The Universal Machine? What about The Universal Computer? Why or why not? Does your preference.
Sound can make multimedia presentations dynamic and interesting.
4.1Different Audio Attributes 4.2Common Audio File Formats 4.3Balancing between File Size and Audio Quality 4.4Making Audio Elements Fit Our Needs.
1. Digitization of Sound What is Sound? Sound is a wave phenomenon like light, but is macroscopic and involves molecules of air being compressed and expanded.
4.2 Multimedia Elements Audio 1. Learning Outcomes: At the end of the lesson, students should be: a) describe the purpose of using audio in multimedia.
Int 2 Multimedia Revision. Digitised Sound Analogue sound recorded from person, or real instruments.
Audio 1 Subject:T0934 / Multimedia Programming Foundation Session:8 Tahun:2009 Versi:1/0.
Chapter 5-Sound.
I Power Higher Computing Multimedia technology Audio.
SWE 423: Multimedia Systems Chapter 3: Audio Technology (2)
Audio Programming in Java
Image and Sound Editing Raed S. Rasheed Sound What is sound? How is sound recorded? How is sound recorded digitally ? How does audio get digitized.
1 Digital Audio Storage Formats. 2 Formats  There are many different formats for storing and communicating digital audio:  CD audio  Wav  Aiff  Au.
Una DooneyMultimediaSlide 1 What is Multimedia? A combination of different media types such as text, graphics, audio, video and animation etc in a single.
1 PC Audio 2 Sound Card  An expansion board that enables a computer to receive, manipulate and output sounds.
Java Audio.
EET 450 Chapter 18 – Audio. Analog Audio Sound is analog Consists of air pressure that has a variety of characteristics  Frequencies  Amplitude (loudness)
5. Multimedia Data. 2 Multimedia Data Representation  Digital Audio  Sampling/Digitisation  Compression (Details of Compression algorithms – following.
Chapter 9 Audio.
Chapter 14 Recording and Editing Sound. Getting Started FAQs: − How does audio capability enhance my PC? − How does your PC record, store, and play digital.
Sound Chapter Types of Sound Waveforms MIDI Sound is related to many things in computers but only Wav and MIDI exist in PCs.
Digital Audio Multimedia Systems (Module 1 Lesson 1)
Seminar on Sound Card Presented by:- Guided by:-
Representation of Data in Computer Systems
Digital Sound and Video Chapter 10, Exploring the Digital Domain.
1 1 Multimedia. 2 What is SOUND? Sound comprises the spoken word, voices, music and even noise. It is a complex relationship involving: a vibrating object.
Chapter II The Multimedia Sysyem. What is multimedia? Multimedia means that computer information can be represented through audio, video, and animation.
Introduction to Interactive Media 10: Audio in Interactive Digital Media.
Multimedia Technology Digital Sound Krich Sintanakul Multimedia and Hypermedia Department of Computer Education KMITNB.
COMP Representing Sound in a ComputerSound Course book - pages
Topics Introduction Hardware and Software How Computers Store Data
CSCI-235 Micro-Computers in Science Hardware Part II.
1 Week 10: Audio Recording. 2 Overview  What is sound?  What does analogue mean?  Analogue-to-Digital conversion  Key terms in digital audio  Compression.
Audio. Why Audio Essential tool for – Interface – Narrative – Setting & Mood.
Translate the following message:
Video Conferencing-introduction --- IT Acumens. COM --- IT Acumens. COMIT Acumens. COMIT Acumens. COM.
CHAPTER SEVEN SOUND. CHAPTER HIGHLIGHTS Nature of sound – Sine waves, amplitude, frequency Traditional sound reproduction Digital sound – Sampled – Synthesized.
 Refers to sampling the gray/color level in the picture at MXN (M number of rows and N number of columns )array of points.  Once points are sampled,
3. Multimedia Systems Technology
Internet Addresses. Universal Identifiers Universal Communication Service - Communication system which allows any host to communicate with any other host.
Signal Digitization Analog vs Digital Signals An Analog Signal A Digital Signal What type of signal do we encounter in nature?
Modication by tuti 1 LECTURE 7 THE USES OF DIGITAL AUDIO IN MULTIMEDIA.
Overview of Multimedia A multimedia presentation might contain: –Text –Animation –Digital Sound Effects –Voices –Video Clips –Photographic Stills –Music.
Module 8 Review Questions 1.VGA stands for A. Video Graphic Association B. Video Gradient Array C. Video Graphic Array D. Video Graphic Arrangement.
Multimedia Technology and Applications Chapter 2. Digital Audio
Chapter 15 Recording and Editing Sound. 2Practical PC 5 th Edition Chapter 15 Getting Started In this Chapter, you will learn: − How sound capability.
CMSCDHN1114/CMSCD1011 Introduction to Computer Audio
MULTIMEDIA TECHNOLOGY SMM 3001 MEDIA - VIDEO. In this chapter How digital video differs from conventional analog video How digital video differs from.
Sound element Week - 11.
Audio / Sound INTRODUCTION TO MULTIMEDIA SYSTEMS Lect. No 3: AUDIO TECHNOLOGY.
© 2011 The McGraw-Hill Companies, Inc. All rights reserved Chapter 4: Sound.
Marwan Al-Namari 1 Digital Representations. Bits and Bytes Devices can only be in one of two states 0 or 1, yes or no, on or off, … Bit: a unit of data.
CSCI-100 Introduction to Computing Hardware Part II.
1 Sound in Java Summary: r Sound API Basics r MIDI JavaSound - Part of the UIT User Interface Toolkit.
COMP135/COMP535 Digital Multimedia, 2nd edition Nigel Chapman & Jenny Chapman Chapter 2 Lecture 2 – Digital Representations.
© M. Winter COSC 3P40 – Advanced Object-Oriented Programming 10.1 Sound effects and music Java Sound API: javax.sound.sampled 8- or 16-bit samples from.
© 2011 The McGraw-Hill Companies, Inc. All rights reserved Chapter 4: Sound.
XP Practical PC, 3e Chapter 14 1 Recording and Editing Sound.
Multimedia Systems Dr. Wissam Alkhadour.
Chapter 15 Recording and Editing Sound
Introduction to Multimedia
Chapter 13: File Input and Output
Data Representation Keywords Sound
Digital Audio Application of Digital Audio - Selected Examples
Chapter 9 Audio.
Presentation transcript:

Java Sound API Medialogy, Semester 7, 2010 Aalborg University, Aalborg David Meredith

Resources The Java Sound Resources web site The Java Sound Tutorial

Overview Java Sound API is a low-level API for controlling and manipulating sound data Can be used for both audio data and MIDI Provides the lowest level of sound support – Can install, access and manipulate mixers, synthesizers and other audio and MIDI devices – Other APIs provide a higher level sound API, e.g. Java Media Framework Does not provide any fancy graphical editors – You can build these using the Java Sound API!

Audio and MIDI Java Sound API provides support for both audio and MIDI processing – javax.sound.sampled Interfaces for capture, mixing and playback of digital, sampled audio – javax.sound.midi Interfaces for MIDI synthesis, sequencing and event transport Two other packages allow service providers to create custom software components that extend an implementation of the Java Sound API – javax.sound.sampled.spi – javax.sound.midi.spi

Sampled audio Sampled audio (digital audio data) is handled by the javax.sound.sampled package A sample is an instantaneous measurement of the pressure in a sound wave An analogue sound signal can be converted to a digital one by sampling the analogue sound wave many thousands of times per second (see figure)

Sampled audio A digital representation of a sound wave is just a sequence of numbers, where each number gives the air pressure (displacement or instantaneous amplitude) in the wave at a particular instant in time The temporal resolution depends on the sampling rate The resolution of the amplitude measurement depends on the quantization which is the number of bits in the number used to represent the amplitude CD quality sound is sampled at 44.1kHz, with each sample being represented by a 16 bit number – There are therefore different values that are used to represent the instantaneous amplitude

Sampled audio Java Sound API allows different sorts of audio components to be installed and accessed Supports – input and output from a sound card (for playback and recording) – mixing multiple streams of audio Mixer might receive sound data from a file, streamed from a network, from another application program, from a synthesizer or from the sound card

MIDI javax.sound.midi provides API for processing MIDI data – transmitting MIDI messages between devices and programs – sequencing MIDI events – synthesizing sound from MIDI events A MIDI event is an instruction to an instrument, telling it how to create a sound – A MIDI event is not raw audio data A MIDI event can be, for example – an instruction to start playing a particular note on a particular instrument – an instruction to change the tempo – an instruction to bend the pitch of a note – an instruction to start sustaining notes MIDI events can be sent to a synthesizer which then generates sound in response to the MIDI messages it receives

MIDI Synthesizers can be implemented entirely in software or they can take the form of hardware devices that a program can connect to via a MIDI output port on the system’s sound card Java Sound API provides interfaces that abstract synthesizers, sequencers, input and output ports

Service Provider Interfaces SPI interface packages – javax.sound.sampled.spi – javax.sound.midi.spi contain APIs that let software developers create new audio or MIDI resources that can be plugged into an implementation of the Java Sound API For example, a service provider could provide – an audio mixer – a midi synthesizer – a file parser that can read or write a new type of file – a converter that translates between different sound formats Services can be – software interfaces to hardware devices – pure software services

Overview of javax.sound.sampled Focused on problem of moving bytes of audio data into and out of the system and from one device to another Involves opening input and output devices and managing buffers that get filled with real-time sound data Also can involve mixing different audio streams together javax.sound.sampled provides – methods for converting between audio formats – methods for reading and writing sound files

Streaming and “in-memory” audio Sampled package provides classes for handling buffered audio data streams (e.g., writing and reading large amounts of audio data to and from a disk) Also provides classes for processing short audio clips loaded entirely into memory (e.g., to be looped, or started and stopped at random positions) To play or capture audio in Java Sound API, you need – formatted audio data – a mixer – a line

Formatted audio data Audio data is transported and stored in lots of different formats There are two types of format – Data formats Format of data as it is being transported – File formats Format of data as it is stored

Audio Data Formats Data format defines how a stream of bytes representing sampled sound should be interpeted – e.g., “raw” sampled audio data already read from a file or captured from a microphone Typically need to know – How many samples per second (sample rate) – How many bits per sample (quantization) – Number of channels – …

AudioFormat class An audio data format is represented in Java Sound by an AudioFormat object with following attributes – Encoding technique (usually PCM) – Number of channels (1 = mono, 2 = stereo) – Sample rate (CD quality is samples per second) – Number of bits per sample per channel (CD quality is 16) – Frame rate – Frame size in bytes – Byte order (big-endian or little-endian)

Encoding technique PCM encoding can be linear or non-linear In a linear encoding, the sample value is proportional to the instantaneous amplitude – The sample value can be represented by a signed or unsigned integer or a float In a non-linear encoding (e.g., µ-law or a-law), the amplitude resolution is higher at lower amplitudes – Used for companding speech in telephony

Frames In PCM, non-compressed formats, a frame contains the data in all channels for a particular sample – So the frame size in bytes would be the number of channels multiplied by the number bytes used to store a sample in single channel e.g., 2 bytes per channel if 16 bit quantization – Here, frame rate is same as sample rate In compressed formats (e.g., MP3), each frame might represent several samples and have extra header information – So here, frame size is not equal to sum of sample sizes for a given instant – And frame rate can be different from sample rate – Here, sample rate and sample size refer to PCM data produced by decoding the compressed format

File formats Audio file format specifies format in which sound is stored in a file (e.g., WAV, AIFF, AU) Represented in the Java Sound API by a AudioFileFormat object, which contains – The file type (e.g., WAVE, AIFF, etc.) – File’s length in bytes – Length in bytes of audio data stored in file – An AudioFormat object specifying data format AudioSystem class provides static methods for reading and writing sounds in different formats and converting between formats AudioSystem also let’s you get a special type of stream called an AudioInputStream on a file

AudioInputStream class AudioSystem has static method getAudioInputStream(audioFile) – Returns an AudioInputStream object that allows for reading from the audio file AudioInputStream is a subclass of InputStream that has an AudioFormat object – Gives direct access to samples without having to worry about the sound file’s structure

Mixers In Java Sound API, an audio device that has various audio inputs and outputs is represented by a Mixer object A Mixer object handles one or more streams of audio input and one or more streams of audio output – e.g., may mix several input streams into one output stream The mixing capabilities of a Mixer object may be implemented in software or in a hardware device

Ports on Mixers A microphone or a speaker is not considered a device – it is a port into or out of a Mixer object A port provides a single stream of data into or out of the mixer A Mixer object representing a sound card might have several input and output ports, e.g. – line-in, microphone – speaker, line-out, headphones A Java Sound Mixer has a source from which it gets audio data and a target to which the mixer sends audio data

Lines A line is a path for moving audio from one place to another Typically, a Line is a path into or out of a Mixer object – Though a Mixer is also a specialized Line object Input and output ports (e.g., speakers and microphones) are Lines A Line can also be a data path along which audio data is transmitted to and from a Mixer Audio data flowing through a Line can be mono or multi-channel – Not possible with analogue data flowing through one port of a physical mixer, which is always mono Each line has an AudioFormat object that specifies (among other things) the number of channels of data in it

An audio output system Figure represents a whole Mixer object in the Java Sound API This Mixer has 3 inputs and some output ports Inputs include – a Clip: a line into which you load a complete short sound – two SourceDataLines: lines that receive buffered, real-time audio input streams Each input line may (or may not) have its own controls (e.g., reverb, gain, pan) Mixer reads from all input lines and sends to output ports after possibly processing with its own controls

An audio input system Data flows into the mixer from the input ports (e.g., mic and line-in) Mixer has gain and pan controls that can modify the sound signal Sound sent on (e.g., to a program) through the TargetDataLine output A Mixer may have more than one TargetDataLine delivering the output sound data to various different targets simultaneously

The Line interface hierarchy Line has several subinterfaces A Line has – Controls including gain, pan, reverb and sample rate – Open or closed status: opening a line reserves it for use by the program; closing it makes it available to other programs – Events: generated when a line opens or closes; received by registered LineListener objects

Ports Simple Lines for input and output of audio to and from audio devices E.g., microphone, line input, CD-ROM drive, speaker, headphone and line output

Mixer Represents either a hardware or software device A Mixer can have various source and target lines Source lines feed audio into the Mixer Target lines take mixed audio away from the mixer Source lines can be input ports, Clips or SourceDataLines Target lines can be output ports or TargetDataLines Can synchronize two or more of a mixer’s lines so they can all be started, stopped or closed by sending a message to just one line in the group

DataLine Subinterface of Line that provides following additional features – Audio format – Media position – Buffer size (write to source, read from target) – Level – Start, stop, resume playback and capture – Flush and drain – Active status – START and STOP events

TargetDataLine Receives audio data from a Mixer object Adds methods for – reading data from its buffer – finding out how much data currently available for reading

SourceDataLine Receives audio data for playback Adds methods for – writing data to the buffer – finding out how much data the line can receive without blocking

Clip Line into which audio data can be loaded prior to playback Audio data is pre- loaded so can loop sounds and start and stop at any position But only short sounds can be loaded!

AudioSystem AudioSystem has static methods for learning what sampled-audio resources are available and obtaining the ones you need – e.g., can list available Mixers and choose the one that has the types of Line that you need AudioSystem can be used to obtain – Mixers – Lines every line is associated with a Mixer, but can be obtained directly using AudioSystem without first obtaining its Mixer – Audio format conversions – Files and streams specialized for audio data

Information objects Different types of Line object have Info objects that describe them e.g., – Mixer.Info objects describe Mixers – Line.Info objects describe Lines – Port.Info objects describe Ports – DataLine.Info objects describe DataLines including Clips, TargetDataLines and SourceDataLines)

Getting a Mixer Typically start by getting a Mixer or a Line so that you can get sound into or out of your computer Can get an array of Mixer.Info objects for all installed Mixer objects using Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); Can use getName(), getVersion(), getVendor() and getDescription() methods to get information out of a Mixer.Info object Can get a specific Mixer as follows Mixer mixer = AudioSystem.getMixer(mixerInfo); – where mixerInfo is a Mixer.Info object See GetMixer.java

Getting a Line There are two ways to get a Line – Directly from the AudioSystem class – From a Mixer object that you’ve already obtained using the AudioSystem.getMixer(mixerInfo) method

Getting a Line from AudioSystem If you only need a Line and not a Mixer, then you can get a Line directly without first getting the Mixer object that it belongs to, e.g., SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); – where info is a subclass of Line.Info (Port.Info or DataLine.Info) See – line 22 in Example01Player.java – line 45 in Example02Recorder.java – line 24 in Example03ClipPlayer.java

Playing back audio Two types of Line you can use for playing sound – Clip All sound data loaded into memory before playback Can loop sound, start and stop anywhere Has to be short enough to be loaded into memory – SourceDataLine Buffer repeatedly loaded with data from a stream Use for long sounds or if length of sound cannot be known in advance of playback (e.g., monitoring sound during capture)

Playback using a Clip Use AudioSystem.getAudioInputStream(clipFile) to get an AudioInputStream on a file Use AudioInputStream.getFormat() to find the AudioFormat of the audio file Construct a DataLine.Info object specifying the Clip.class Obtain a Clip using AudioSystem.getLine(info) Open the Clip and Loop it or start it Use setMicrosecondPosition to set start position See Example03ClipPlayer.java File clipFile = new File(clipFileName); audioInputStream = AudioSystem.getAudioInputStream(clipFile); AudioFormatformat = audioInputStream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format); clip = (Clip) AudioSystem.getLine(info); clip.addLineListener(this); clip.open(audioInputStream); clip.loop(nLoopCount);

Playback using a SourceDataLine Obtain a SourceDataLine directly from the AudioSystem Obtain AudioFormat from audio file Open the SourceDataLine to reserve it for your program – SourceDataLine.open(AudioFormat) takes an AudioFormat object as its argument cf. AudioInputStream argument when opening a Clip File soundFile = new File("resources/ChopinOp10No1Start.wav"); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile); AudioFormataudioFormat = audioInputStream.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(audioFormat);

Playback using a SourceDataLine Play back sound using a SourceDataLine by starting the Line and then writing data repeatedly to the line’s playback buffer Use int Line.write(byte[] byteArray, int offset, int length) to write data to the Line’s buffer Line starts sending data to its Mixer which delivers to its target When Mixer starts delivering to its target, the SourceDataLine emits a START event which can be caught by a LineListener, causing its update method to be run (see Example03ClipPlayer.java) write method returns as soon as it’s finished writing, not when buffer is empty, so there can be time to write more data before buffer becomes empty int nBytesRead = 0; byte[] abData = new byte[128000]; while (nBytesRead != -1) { nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) line.write(abData, 0, nBytesRead); }

Using SourceDataLine.drain() After writing the last buffer-full of data to the SourceDataLine, call drain() to make sure all the data is presented before continuing execution drain() blocks until buffer is empty Use stop() method to stop playback Use flush() method to empty the buffer without playing while (nBytesRead != -1) { nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) line.write(abData, 0, nBytesRead); } line.drain(); line.close(); line = null;

Recording Audio data See Example02Recorder.java