1 Sound in Java Summary: r Sound API Basics r MIDI JavaSound - Part of the UIT User Interface Toolkit.

Slides:



Advertisements
Similar presentations
Tamara Berg Advanced Multimedia
Advertisements

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.
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 Multimedia on the Web: Issues of Bandwidth Bandwidth is a measure of the amount of data that can be sent through a communication pipeline each second.
Sound, Part 3. Multiple Echoes Here is a recipe to create multiple echoes: def echoes(sndfile, delay, num): s1 = makeSound(sndfile) ends1 = getLength(s1)
From Sound to Music CSC 161: The Art of Programming Prof. Henry Kautz 11/16/2009.
Int 2 Multimedia Revision. Digitised Sound Analogue sound recorded from person, or real instruments.
SWE 423: Multimedia Systems Chapter 3: Audio Technology (2)
Java Sound API Medialogy, Semester 7, 2010 Aalborg University, Aalborg David Meredith
4.3. S OUND Use of sound within games. In lecture exploration of answers to frequently asked student questions.
EE2F2: Music Technology - Revision Two exam questions Music Recording Technology Mixing & multi-track recording Effects MIDI & Sequencers Virtual Studio.
1 Digital Audio Storage Formats. 2 Formats  There are many different formats for storing and communicating digital audio:  CD audio  Wav  Aiff  Au.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 L45 Multimedia: Applets and Applications. 2 OBJECTIVES  How to get and display images.  To create animations from sequences of images.  To create.
CSE466 Autumn ‘00- 1 System Functional Requirements  Children’s toy…comes with PC software. Child plays notes on the screen and the device makes corresponding.
Spring 2002EECS150 - Lec13-proj Page 1 EECS150 - Digital Design Lecture 13 - Final Project Description March 7, 2002 John Wawrzynek.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Audio Chapter 17 - Student. (c) 2005 by Elizabeth Sugar Boese Audio Files Java can play the following audio types: .au (Sun Audio) .wav (Windows Wave)
Java Audio.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
SMAF What Is SMAF? What Is Is SMAF? What Can Use SMAF? SMAF Structure How SMAF files are Created MIDI and SMAF Demo.
XP 1 Using Multimedia on the Web Enhancing a Web Site with Sound, Video, and Applets Tutorial 8.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Chapter 9 Audio.
Games and Simulations O-O Programming in Java The Walker School
Io package as Java’s basic I/O system continue’d.
Introduction to Interactive Media 10: Audio in Interactive Digital Media.
MIDI. A protocol that enables computers, synthesizers, keyboards, and other musical devices to communicate with each other. Instead of storing actual.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Audio and Video CGS Some Common Audio Formats Format Use Extension MIDI instrumental music.mid MPEG songs.mp3 RealAudio live broadcasts.ra Wave.
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
XP Tutorial 8New Perspectives on HTML and XHTML, Comprehensive 1 Using Multimedia on the Web Enhancing a Web Site with Sound, Video, and Applets Tutorial.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Sound or Audio, whichever you prefer –MIDI Files.midi or.mid (Musical Instrument Digital Interface) use for instrumental music. –This format is supported.
Sound on the Web. Using Sound on a Web Site Conveying information  pronounce a word or describe a product Set a mood  music to match the web page scene.
XP Tutorial 8New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Using Multimedia on the Web Enhancing a Web Site with Sound, Video, and.
Introduction to Java CS1316: Representing Structure and Behavior.
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
Timer Objects  regularly generates action events at programmer-specified time intervals  Timer class is in javax.swing package  Timer (int delay, ActionListener.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Game Art & Sound. Sound ● Midi files – Musical Instrument Digital Interface – Advantages ● small ● fast – Disadvantages ● synthesized ● rendered ● Wav.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
Chapter 16 Multimedia F Audio Files F Playing Audio F Running Audio on a Separate Thread F Displaying Images F Displaying a Sequence of Images  Using.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
PJV25 1 javax.sound.midi try { Sequencer seq = MidiSystem.getSequencer( ); seq.open( ); Sequence z = MidiSystem.getSequence( new File( "res/song.mid" )
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Lesson 28: More on the GUI button, frame and actions.
© 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.
MIDI Musical Instrument Digital Interface Musical sound can be generated, unlike other types of sounds. The Musical Instrument Digital Interface standard.
Chapter 14: Multimedia Audio Files Audio Files Playing Audio Playing Audio Running Audio on a Separate Thread Running Audio on a Separate Thread Displaying.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 15 Recording and Editing Sound
Using Multimedia on the Web
Creating Simple Music By Keith Lynn
Chapter 3: Using Methods, Classes, and Objects
Chapter 13: File Input and Output
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Use of sound within games
Digital Audio Application of Digital Audio - Selected Examples
Chapter 9 Audio.
Presentation transcript:

1 Sound in Java Summary: r Sound API Basics r MIDI JavaSound - Part of the UIT User Interface Toolkit

2

3 MIDI in JavaSound r Data is a MIDI file is a series of commands that defines a piece of music r Up to 16 MIDI channels are available (each instrument uses one channel) r A MIDI Synthesizer reproduces(synthesizes) sounds in response to MIDI commands m H/W part of the sound card m S/W as in JavaSound r A sequencer is a device that processes a MIDI sequence in order to play it on a synthesizer, or possible to edit it. m H/W or S/W A device conforming to the General MIDI spec. must provide: r A min. of 128 preset instruments + 47 percussive sounds r A min. of 16 simultaneous timbres (instruments) r A min. of 24 simultaneous voices, where each voice is a note of given velocity (loudness) for any of the available instruments and percussive sounds r 16 midi channels, where each channel is polyphonic(can play multiple simultaneous voices). Percussive sounds are always on channel 10

4 Midi Note Numbers r The Note Number ranges from r 60 = middle C r Number DIV 12 is the octave r 91 DIV 12 or octave number 7 r 127 DIV 12 or 10 octaves in all

5 Note Number to Class r Number MOD 12 is the note class

6 r Whole Note Names r CDEFGABC r Whole Note Classes r Wnc[]={0,2,4,5,7,9,11}

7 Note Class r Octave-NN matrix

8 r Note Number to Frequency public static double getMidiFrequency(int midiNoteNumber){ return 440. * Math.pow(2,((midiNote Number-69.)/12.)); }

9 MIDI Resources in JavaSound Sequencer Synthesizer MidiDevice Declares basic operations for a MIDI device. Inner class, MidDevice.Info can be used for specifying a device A sequencer object can play a sequence, which you can construct from a MIDI file A synthesizer object encapsumates a h/w or s/w MIDI synthesizer Declares operations specific to a synthesizer Declares operations specific to a sequencer. Innerclass Sequencer.SyncMode used for synchronizing with another device

10 Playing a MIDI file To play a MIDI file, you don’t need to access a synthesizer directly. All you need is a Sequencer reference and an object encapsulating the sequence that you want to play. Steps: 1. sequencer = MidiSystem.getSequencer(); // Get a sequencer 2. sequencer.open(); 3. sequence = MidiSystem.getSequence(midiFile) // Encapsulate the midi src (file here; URL possible) in a sequence obj. 4. sequencer.setSequence(sequence); // Hand the sequence over to the sequencer 5. sequencer.start(); // Play it. Stop it: sequencer.stop()

11

12 Java Sound Preliminaries File Formats Supported .au or.snd : usually stores 8-bit  -law encoded samples, but can also store 8 or 16 bit linear samples .aif : usually stores 8 or 16 bit linear encoded samples .wav :Can store 8 or 16 bit samples using linear or  -law encoded samples .midi : follows the midi data format Note: The file header indicates the actual format Frames and Frame Rates r Sample Frame m Stores all the samples taken at an instant of time m # of bytes in a frame = # of bytes in a sample X number of channels r Frame Rate m The number of frames per second of sound m In most cases frame rate is same as sample rate m In compressed sound, the frame rate will be less than sample rate.

13 Simple Sound Output import java.applet.*; import javax.swing.*; import java.awt.event.*; public class PlayIt extends JApplet { AudioClip clip; // The sound clip JButton button; final String play = "PLAY"; final String stop = "STOP"; public void init() { // Get the sound clip String fileName = getParameter("clip"); // Get the file name clip = getAudioClip(getDocumentBase(),fileName); // Create the clip // Rest of the applet initialization... button = new JButton(play); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals(play)) { clip.loop(); button.setText(stop); } else { clip.stop(); button.setText(play); } ); getContentPane().add(button); } Playit.html PlayIt.java

14 Sound in Applications Similar to an Applet  The Applet class defines a static method, newAudioClip() that retrieves an audio clip from a URL and returns a reference type AudioClip that encapsulates the audio clip.  This method being static, you don’t have to have an applet object to call it. It can be called from an application as well, like so: AudioClip clip = Applet.newAudioClip(URL location)  Take a look at example PlaySounds.java in the examples directory

15 Java Sound API System Resources r Audio Line: Any resource that is a source or destination of sampled sound data m A line can encapsulate several channels m Example: input/output ports on the sound card m Lines have controls (gain and pan control) r Mixer: Receives input from one or more source data lines and outputs the result of combining the input to an output line called a target data line Other Sound Sources r A file or more generally a URL Terminology: r A source data line is a source for a mixer, not a source for you; you write to it r A target data line is the output from the mixer; your read from it MIXER Sound i/p Sound o/p Target Data Lines Source Data Lines

16 Java Sound API (…contd) Packages: r javax.sound.sampled r javax.sound.midi r javax.sound.sampled.spi r javax.sound.sampled.midi The AudioSystem class r Establish whether a particular resource is available r Get a ref. to the object that encapsulates the resource r Call methods to operate the resource AudioStreamInput class r Represents a stream that is a source of sampled sound data with a specific format  You can create an AudioStreamInput object from a local sound file, from another input stream or a URL r You can m Read data from it m Write its contents to an output stream m Convert its format

17 Sampled Sound Interfaces TargetDataLine Something you read from. Data keeps coming Use it or loose it! Mixer DataLine Port (mic, dataline, etc) SourceDataLine Line (info on everything) Clip (not streaming) Audio in memory

18 Resource Descriptor Classes Mixer.Info DataLine.Info Port.Info Line.Info The Line, DataLine, Mixer and Port interface definitions each include an inner class with the name Info. Objects of these class types encapsulate data specifying an object of the corresponding type Specifies a Line object by its Class Specifies a Mixer object by its name, vendor version and description Specifies a DataLine by the audio formats to be supported, the buffer size limits, and the Class of the data line Specifies a Port object by its Class, its name, and whether it is a source or target line

19 Why Descriptor Class? In order to answer that question, we have to look at the steps involved in playing audio: 1. Create an instance of a Line that specifies the format of the sound data you wish to play (use descriptor class). Not all formats will be supported. 2. Get a reference to a line (a DataLine, Port, Mixer or Clip) that supports this format 1. May check if supported before requesting reference 3. Create an Audio Stream that encapsulates the sound data (file, URL, another stream) 1. Extract the format from the Audio Stream 4. Tie the source of the sound data to the reference (line) that will play it. I.e., open the source 5. Play it; loop; goto; quit.

20 Playing a Clip vs. Stream AudioInputStream source = AudioSystem.getAudioInputStream(file); // Step 3. DataLine.Info clipInfo = new DataLine.Info(Clip.class, source.getFormat()); // Step 1. if(AudioSystem.isLineSupported(clipInfo)) { Clip newClip = (Clip)AudioSystem.getLine(clipInfo); // Step 2. newClip.open(source);// Step 4. } clip.loop(clip.LOOP_CONTINUOUSLY); // loop clip.stop(); // stop clip.setFramePosition(0); Clip.close(); AudioInputStream newSource = AudioSystem.getAudioInputStream(file); // Step 3. AudioFormat format = newSource.getFormat(); DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format); // Step 1. if(AudioSystem.isLineSupported(sourceInfo)) { srcLine = (SourceDataLine)AudioSystem.getLine(sou rceInfo); // Step 2. bufferSize = (int)(format.getFrameSize()*format.getF rameRate()/2.0f); soundData = new byte[bufferSize]; srcLine.open(format, bufferSize); //4. } while(playing) { byteCount = source.read(soundData, 0, soundData.length); // Read the stream if(byteCount == -1) { sourceLine.drain(); // rest of buffer playing = false; break; } sourceLine.write(soundData,0, byteCount); // Write the array to the line }