28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 ■ Implementing Game Audio Assets: Using the <strong>Java</strong>FX AudioClip Class Audio Sequencing Engine<br />

<strong>Java</strong>FX AudioClip Class: A Digital Audio Sequencer<br />

The public final AudioClip class is part of the javafx.scene.media package, and is essentially a digital audio sample<br />

playback and audio sequencing engine that is designed to use short audio snippets or “samples” to create more<br />

complex audio performances. This is why this class is the perfect digital audio media playback class to use for your<br />

<strong>Java</strong> 8 game development. As you can see by the <strong>Java</strong> inheritance hierarchy shown below, the <strong>Java</strong>FX AudioClip<br />

class is a direct subclass of the java.lang.Object master class. This signifies that the AudioClip class has been “scratch<br />

coded” specifically for the purpose of being a digital audio sequencing engine.<br />

java.lang.Object<br />

> javafx.scene.media.AudioClip<br />

Your AudioClip objects will each reference one digital audio sample in memory. These can be triggered with<br />

virtually zero latency, which is what makes this class the perfect class to use for <strong>Java</strong> 8 games development. AudioClip<br />

objects are loaded similarly to Media (long-form audio or video) objects, using URL objects, but have a vastly different<br />

behavior. For example, a Media object cannot play itself; it will need a MediaPlayer object, and also the MediaView<br />

object, if it contains digital video. Media objects would be better suited for long-form digital audio assets (like<br />

music), which cannot fit in memory all at the same time, and must be streamed for optimal memory utilization. A<br />

MediaPlayer will only have enough decompressed digital audio data “pre-rolled” into memory to play for a short<br />

amount of time, so a MediaPlayer approach is much more memory efficient for long digital audio clips, especially if<br />

they are compressed.<br />

The AudioClip object is usable immediately upon instantiation, as you’ll see later on during this chapter, and<br />

this is an important attribute to have when it comes to <strong>Java</strong> 8 game development. AudioClip object playback behavior<br />

can be said to be “fire and forget,” which means that once one of your playiSound() method calls is invoked, your only<br />

operable control at that point over the digital audio sample is to call the AudioClip .stop() method.<br />

Interestingly, your AudioClip objects may also be triggered (played) multiple times, and AudioClips can even be<br />

triggered simultaneously, as you will see a bit later on during this chapter. To accomplish this same result by using a<br />

Media object, you would have to create a new MediaPlayer object for each sound that you want to play in parallel.<br />

The reason that the AudioClip object is so versatile (responsive) is because your AudioClip objects are stored in<br />

memory. The AudioClip object uses a raw, uncompressed digital audio sample representing the entire sound. This is<br />

placed into memory in its raw, uncompressed state, which is why in the next section of the chapter we’re going to use<br />

the WAVE audio file format. This audio format applies zero compression, and thus, the resulting file size for optimized<br />

digital audio samples will also represent the amount of system memory that each of these samples will utilize.<br />

What is really impressive about the AudioClip class, however, is the amount of power it gives developers via its<br />

properties and the three overloaded .play() method calls. You can set up sample playback priorities, shift the pitch<br />

(frequency of the sound) up to 8 times (higher octaves) or down to 1/8th (lower octaves), pan the sound anywhere in the<br />

spatial spectrum, control the left and right balance of the sound, control the volume (or amplitude) of the sound, and<br />

control the number of times the sound is played, from once to forever, using the AudioClip INDEFINITE constant.<br />

Besides the getter and setter methods for these AudioClip properties, there are .play() and .stop() methods as<br />

well as three (overloaded) .play() methods: one for simple (default) playback; one where you can specify volume; and<br />

one where you can specify volume, balance, frequency rate (pitch shifting factor), panning, and sample priority.<br />

The key to controlling the AudioClip object is to call one of the three .play() methods based upon how you want<br />

to control the sample playback. Use .play() for straightforward playback, as we will be doing during this chapter; or<br />

use .play(double volume) to play your AudioClip object at a relative volume level from 0.0 (off) to 1.0 (full); or use the<br />

.play(double volume, double balance, double rate, double pan, double priority) to play your AudioClip object at a<br />

relative volume (0.0 to 1.0), a relative balance (-1.0 left, 0.0 center, 1.0 right), pitch shift rate (0.125 to 8.0), relative pan<br />

(-1.0 left, 0.0 center, 1.0 right); and priority integer, which specifies which samples get played over others, which, due<br />

to low resources and low priority, may not get played. Let’s get into using Audacity to optimize our samples next!<br />

324<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!