Skip to content
zenith391 edited this page Dec 28, 2018 · 8 revisions

Audio

IMPORTANT: Audio in PowerHigh is currently beta. It can changes at any version! Although only slightly

Preparations

Audio in PowerHigh can be used via SimpleGame field "audio". If you're not using SimpleGame, it is usable via:

static Audio audio = new Audio();

How it works

The Audio class uses samples defined by AudioSource class and their childrens. Theses samples are defined as following:

public byte[] getNextSample() {
    // ...
}

public boolean hasNextSample() {
    // ...
}

Samples are always defined in an array of byte with the size of a sample (mostly 4 bytes for recents sound / music)

Of course, there is implementations of Sound class for reading files. Here for the example we will use WavSound from the Swing module (only reads WAV, AIFF(-C), SND, and some less known format made by Sun Microsystems [now Oracle]) This class is very good for some debugs/examples. Because it just asks a file and get the samples from it. But it only support the formats below, and since designed for debug, it is not optimized.

Playing Audio

So, here Audio in action:

WavSound sound = new WavSound(new File("path/to/sound"));

to play sound, add:

audio.playSound(sound);

Or you can do:

audio.playFromSource(sound) // Possible since WavSound extends Sound that extends AudioSource.

Others

There is of course Musics, which load on-the-fly the music file. Current decoder supported is WAV, AIFF-C, AIFF, SND, and some less known formats. That's why currently, the only decoder is named WavMusic. To play a music, you just need to change audio.playSound(sound) to audio.playMusic(music), or audio.playFromSource(source) which accept both entries.

The master volume can be set using audio.setMasterVolume(volume), the value is a float. It is used to control the mutliplier of sound/music volume. Example: 1.0f means volume is normal, 0 is mute, and 2 means 2x time higher volume than specified by sound/music

Clone this wiki locally