Você está na página 1de 16

What is sound?

From a human perspective, sound is the sensation that we experience when pressure waves impinge upon the small parts contained within our ears. Normally, this is the result of pressure waves being transmitted in air. However, sound pressure waves are not limited to air. For example, if you are an underwater swimmer, sound pressure waves may reach your ear by way of water.
Post a comment Email Article Print Article Share Articles

From the perspective of the Java Sound API, the word Sound takes on a somewhat different meaning. However, it is probably fair to say that the ultimate purpose of the Sound A ! is to assist you in writing programs that will cause sound pressure waves impinge upon the ears of targeted individuals at specific times. What does Sun have to say about the API? Here is what Sun has to say about the "ava Sound A !# "The Java Sound API is a low-level API for effecting and controlling input and output of audio media. It provides explicit control over the capa ilities commonl! re"uired for audio input and output in a framewor# that promotes extensi ilit! and flexi ilit!." Sun also tells us# "Java Sound provides the lowest level of audio support on the Java platform. It provides a high degree of control over audio-specific functionalit!. ... It does not include sophisticated sound editors and $%I tools& rather, it provides a set of capa ilities upon which such applications can e uilt. It emphasi'es low-level control e!ond that commonl! expected ! the end user, who enefits from higher-level interfaces uilt on top of Java Sound." $hus, your mission as a "ava programmer is to use the Sound A ! to produce higher%level user interfaces built on top of "ava Sound. Not a trivial API $here are a number of fairly complex issues involved in the use of the Sound A !. $his tutorial lesson will provide a very brief introduction to some of those issues. Future lessons will explore many of them in detail. Viewing tip

&ou may find it useful to open another copy of this lesson in a separate browser window. $hat will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them. Supplementary material ! recommend that you also study the other lessons in my extensive collection of online "ava tutorials. &ou will find those lessons published at 'amelan.com. However, as of the date of this writing, 'amelan doesn(t maintain a consolidated index of my "ava tutorial lessons, and sometimes they are difficult to locate there. &ou will find a consolidated index at www.)ick*aldwin.com.

Preview
$his lesson provides a description of sound from both a physical and a programming viewpoint. $he lesson introduces the Java Sound API, which provides a high degree of control over audio% specific functionality in "ava programs. !t identifies the important packages incorporated in the Sound A ! and explains the difference between the sampled packages and the (I)I packages. $he lesson provides a description of sampled audio, and explains the typical steps used to capture sampled audio. !t also explains the typical steps used to render sampled audio. Finally, the lesson provides a program that you can use to first capture and then to play back audio sound.

Discussion and Sample


Pac!ages

ode

$wo significantly different types of audio *or sound+ data are supported by the A !# Sampled audio data +usical !nstrument )igital !nterface ,+!)!- data Sampled audio data Sampled audio data can be thought of as a series of digital values that represent the amplitude or intensity of sound pressure waves. $his will be the primary topic of the first several lessons in this miniseries. $his type of audio data is supported by the following two "ava packages# .avax.sound.sampled .avax.sound.sampled.spi

According to Sun, the first of these two packages "specifies interfaces for capture, mixing, and pla! ac# of digital *sampled+ audio." ! will have more to say about the second *spi+ package shortly. "IDI data +!)! data can be thought of as sound *usuall! musical sound or special sound effects+ created from a recipe. $his type of audio data is covered by the following two "ava packages# .avax.sound.midi .avax.sound.midi.spi According to Sun, the first of these two packages "provides interfaces for (I)I s!nthesis, se"uencing, and event transport." What about the spi pac!ages? According to Sun, each of the spi packages "permits service providers *as opposed to application developers+ to create custom components that can e installed on the s!stem" *ecause the two types of audio data are so significantly different, ! will defer any detailed discussion of +!)! audio data until later. What is sampled data? ! am going to refer you to another publication of mine entitled )igital Signal rocessing ,)S - in "ava, Sampled $ime Series for a discussion of sampled data in general. *ecause )S techni/ues are often used in the processing of sampled audio data, you may also be interested in my other publications on)S as well. What is sampled audio data? Sampled audio data is a special case of sampled data in general. For sampled audio data, a series of digital numeric values is used to represent the intensity of a sound pressure wave. !n other words, a set of numeric values is used to represent the actual waveform of a sound pressure wave. $ypically, the sound pressure wave *or an electronic representation thereof+ is sampled at a uniform series of points in time. An e#ample For example, the graph in Figure 0 might represent a set of sampled audio data values produced by a wide%band noise generator, such as the noise at an airport.

Figure 0 Sampled audio data &ou can think of Figure 0 as the result of connecting a series of dots with short straight lines. $he vertical position of each dot relative to the red hori1ontal axis would represent the intensity of a sound wave at a particular point in time. $he location of each dot along the hori1ontal axis would represent the point in time at which the measurement of intensity was made. An audio compact dis! $ D% As another example, the data on an audio 2) is sampled audio data. As ! understand it, an electronic representation of the sound pressure waves produced by the artist is sampled 33,044 times per second. 5ach sample is represented as a 06%bit signed integer value. &ther sources o' sampled audio data Although sampled audio data most commonly results from actually sampling sound pressure waves, such data could be synthetically generated by a computer. For example, computeri1ed speech synthesi1ers can be used to produce sampled audio data. 7hen that data is converted to sound pressure waves *rendered+, which impinge on a human ear, the human experiences the sensation of sound representing human speech. According to Sun# "The term "sampled audio" refers to the t!pe of data, not its origin. Sampled audio can e thought of as the sound itself, whereas (I)I data can e thought of as a recipe for creating musical sound.+ " apturing sampled audio $ypically sampled audio data is captured in two steps# 8se a microphone to convert the sound pressure waves to electrical voltages that mimic the waveform of the sound pressure wave. 8se an analog%to%digital converter to measure the voltage at specific points in time and to convert that measurement to a digital value. (endering sampled audio

$he rendering of sampled audio data is also typically accomplished in two steps# 8se a digital%to%analog converter to convert a series of digital values into an analog voltage whose amplitude waveform reflects the digital values. Apply this voltage to a speaker, a set of headphones, or some other similar device that converts the analog voltage to sound pressure waves that mimic the waveform of the voltage.

(un the Program


At this point, you may find it useful to compile and run the program in 9isting 0 near the end of the lesson. *I have provided this sample program with ver! little in the wa! of an explanation as to how the program wor#s. I wanted to give !ou some code to get !ou started using the API. I will explain this program, or other ver! similar programs in future lessons.+ apture and playbac! audio data $his program demonstrates the ability to capture audio data from a microphone and to play it back through the speakers on your computer. $he usage instructions are simple# Start the program running. A simple '8! will appear on the screen. 2lick the apture button and speak into the microphone. 2lick the Stop button to terminate capturing data. 2lick the Playbac! button to play your captured voice back through the system speakers. !f you don(t hear anything during playback, you may need to increase your speaker volume. $his program saves the data that it captures in memory, so be careful. !f you attempt to save too much data, you may run out of memory.

Summary
!n this lesson, ! provided a description of sound from both a physical and a programming viewpoint. ! introduced the Java Sound API, which provides a high degree of control over audio%specific functionality in "ava programs. ! identified the important packages incorporated in the Sound A !, and explained the difference between the sampled packages and the (I)I packages.

! provided a description of sampled audio, and explained the typical steps used to capture sampled audio. ! also explained the typical steps used to render sampled audio into sound pressure waves. Finally, ! provided a relatively simple program that you can use to first capture and then to playback audio sound.

What)s Ne#t?
!n the next lesson, ! will explain the overall architecture of the Sound A !, introducing such terms and concepts as# 9ines $arget)ata9ine Source)ata9ine +ixers orts Audio format File format Audio stream 2lip 2ontrols

omplete Program *isting


A complete listing of the program is shown in 9isting 0.

/*File AudioCapture01.java This program demonstrates the capture and subsequent playbac o! audio data.

A "#$ appears on the screen containing the !ollo%ing buttons& Capture 'top

(laybac

$nput data !rom a microphone is captured and saved in a )yteArray*utput'tream object %hen the user clic s the Capture button.

+ata capture stops %hen the user clic s the 'top button.

(laybac

begins %hen the user clic s button.

the (laybac

Tested using '+, 1.-.0 under .in/000 **************************************/

import java0.s%ing.*1 import java.a%t.*1 import java.a%t.event.*1 import java.io.*1 import java0.sound.sampled.*1

public class AudioCapture01 e0tends 2Frame3

boolean stopCapture 4 !alse1 )yteArray*utput'tream byteArray*utput'tream1 AudioFormat audioFormat1 Target+ata5ine target+ata5ine1 Audio$nput'tream audio$nput'tream1

'ource+ata5ine source+ata5ine1

public static void main6 'tring args7893 ne% AudioCapture01691 ://end main

public AudioCapture01693//constructor !inal 2)utton capture)tn 4 ne% 2)utton6;Capture;91 !inal 2)utton stop)tn 4 ne% 2)utton6;'top;91 !inal 2)utton play)tn 4 ne% 2)utton6;(laybac ;91

capture)tn.set<nabled6true91 stop)tn.set<nabled6!alse91 play)tn.set<nabled6!alse91

//=egister anonymous listeners capture)tn.addAction5istener6 ne% Action5istener693 public void action(er!ormed6 Action<vent e93 capture)tn.set<nabled6!alse91 stop)tn.set<nabled6true91 play)tn.set<nabled6!alse91 //Capture input data !rom the // microphone until the 'top // button is clic ed. captureAudio691

://end action(er!ormed ://end Action5istener 91//end addAction5istener69 getContent(ane69.add6capture)tn91

stop)tn.addAction5istener6 ne% Action5istener693 public void action(er!ormed6 Action<vent e93 capture)tn.set<nabled6true91 stop)tn.set<nabled6!alse91 play)tn.set<nabled6true91 //Terminate the capturing o! // input data !rom the // microphone. stopCapture 4 true1 ://end action(er!ormed ://end Action5istener 91//end addAction5istener69 getContent(ane69.add6stop)tn91

play)tn.addAction5istener6 ne% Action5istener693 public void action(er!ormed6 Action<vent e93 //(lay bac all o! the data

// that %as saved during // capture. playAudio691 ://end action(er!ormed ://end Action5istener

91//end addAction5istener69 getContent(ane69.add6play)tn91

getContent(ane69.set5ayout6 ne% Flo%5ayout6991 setTitle6;Capture/(laybac set+e!aultClose*peration6 <>$T?*@?C5*'<91 set'iAe6/B0CD091 setEisible6true91 ://end constructor +emo;91

//This method captures audio input // !rom a microphone and saves it in // a )yteArray*utput'tream object. private void captureAudio693 try3 //"et everything set up !or // capture audioFormat 4 getAudioFormat691 +ata5ine.$n!o data5ine$n!o 4 ne% +ata5ine.$n!o6 Target+ata5ine.classC audioFormat91 target+ata5ine 4 6Target+ata5ine9 Audio'ystem.get5ine6 data5ine$n!o91 target+ata5ine.open6audioFormat91 target+ata5ine.start691

//Create a thread to capture the

// microphone data and start it // running. $t %ill run until

// the 'top button is clic ed. Thread captureThread 4 ne% Thread6 ne% CaptureThread6991 captureThread.start691 : catch 6<0ception e9 3 'ystem.out.println6e91 'ystem.e0it6091 ://end catch ://end captureAudio method

//This method plays bac

the audio

// data that has been saved in the // )yteArray*utput'tream private void playAudio69 3 try3 //"et everything set up !or // playbac . //"et the previouslyFsaved data // into a byte array object. byte audio+ata78 4 byteArray*utput'tream. to)yteArray691 //"et an input stream on the // byte array containing the data $nput'tream byteArray$nput'tream 4 ne% )yteArray$nput'tream6 audio+ata91 AudioFormat audioFormat 4

getAudioFormat691 audio$nput'tream 4 ne% Audio$nput'tream6 byteArray$nput'treamC audioFormatC audio+ata.length/audioFormat. getFrame'iAe6991 +ata5ine.$n!o data5ine$n!o 4 ne% +ata5ine.$n!o6 'ource+ata5ine.classC audioFormat91 source+ata5ine 4 6'ource+ata5ine9 Audio'ystem.get5ine6 data5ine$n!o91 source+ata5ine.open6audioFormat91 source+ata5ine.start691

//Create a thread to play bac // the data and start it // running. $t %ill run until

// all the data has been played // bac . Thread playThread 4 ne% Thread6ne% (layThread6991 playThread.start691 : catch 6<0ception e9 3 'ystem.out.println6e91 'ystem.e0it6091 ://end catch ://end playAudio

//This method creates and returns an // AudioFormat object !or a given set // o! !ormat parameters. // parameters donGt %or $! these %ell !or

// youC try some o! the other // allo%able parameter valuesC %hich // are sho%n in comments !ollo%ing // the declarations. private AudioFormat getAudioFormat693 !loat sample=ate 4 H000.0F1 //H000C110/BC1I000C//0B0C--100 int sample'iAe$n)its 4 1I1 //HC1I int channels 4 11 //1C/ boolean signed 4 true1 //trueC!alse boolean big<ndian 4 !alse1 //trueC!alse return ne% AudioFormat6 sample=ateC sample'iAe$n)itsC channelsC signedC big<ndian91 ://end getAudioFormat //44444444444444444444444444444444444//

//$nner class to capture data !rom // microphone class CaptureThread e0tends Thread3

//An arbitraryFsiAe temporary holding // bu!!er byte temp)u!!er78 4 ne% byte71000081 public void run693 byteArray*utput'tream 4 ne% )yteArray*utput'tream691 stopCapture 4 !alse1 try3//5oop until stopCapture is set // by another thread that // services the 'top button. %hile6JstopCapture93 //=ead data !rom the internal // bu!!er o! the data line. int cnt 4 target+ata5ine.read6 temp)u!!erC 0C temp)u!!er.length91 i!6cnt K 093 //'ave data in output stream // object. byteArray*utput'tream.%rite6 temp)u!!erC 0C cnt91 ://end i! ://end %hile byteArray*utput'tream.close691 :catch 6<0ception e9 3 'ystem.out.println6e91 'ystem.e0it6091 ://end catch ://end run ://end inner class CaptureThread

//44444444444444444444444444444444444// //$nner class to play bac // that %as saved. class (layThread e0tends Thread3 byte temp)u!!er78 4 ne% byte71000081 the data

public void run693 try3 int cnt1 //,eep looping until the input // read method returns F1 !or // empty stream. %hile66cnt 4 audio$nput'tream. read6temp)u!!erC 0C temp)u!!er.length99 J4 F193 i!6cnt K 093 //.rite data to the internal // bu!!er o! the data line // %here it %ill be delivered // to the spea er. source+ata5ine.%rite6 temp)u!!erC 0C cnt91 ://end i! ://end %hile //)loc and %ait !or internal

// bu!!er o! the data line to // empty. source+ata5ine.drain691 source+ata5ine.close691 :catch 6<0ception e9 3 'ystem.out.println6e91

'ystem.e0it6091 ://end catch ://end run ://end inner class (layThread //44444444444444444444444444444444444//

://end outer class AudioCapture01.java

Listing 1

Você também pode gostar