Skip to content

Commit

Permalink
SoundBufferLoaderParameter hack for the AssetManager
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias <[email protected]>
  • Loading branch information
Hangman committed Aug 22, 2023
1 parent 8b757dd commit 9dfe4d8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ public void detachAllEffects() {
}


SoundBuffer getBuffer() {
/**
* Returns the SoundBuffer that this source is currently using.
*
* @return the buffer
*/
public SoundBuffer getBuffer() {
return this.buffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file,

@Override
public void loadAsync(AssetManager manager, String fileName, FileHandle file, SoundBufferLoaderParameter parameter) {
file = parameter != null && parameter.file != null ? parameter.file : file;
final boolean reverse = parameter != null && parameter.reverse;
final String fileExtension = file.extension();
SoundFileType type = SoundFileType.getByFileEnding(fileExtension);
Expand Down Expand Up @@ -101,6 +102,14 @@ public static class SoundBufferLoaderParameter extends AssetLoaderParameters<Sou
* Loads the file for reversed playback.
*/
public boolean reverse = false;

/**
* A custom FileHandle object that can be set to specify an alternative file path for the asset.<br>
* If this is set, this path takes priority over the file name String given to the load function of the asset manager.<br>
* You can give the asset manager load method an arbitrary String that is just used to identify the asset, it must not point to the real file.<br>
* This is useful when multiple instances of the same asset need to be loaded with different configurations.
*/
public FileHandle file;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.pottgames.tuningfork.test;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;

import de.pottgames.tuningfork.Audio;
import de.pottgames.tuningfork.BufferedSoundSource;
import de.pottgames.tuningfork.SoundBuffer;
import de.pottgames.tuningfork.SoundBufferLoader;
import de.pottgames.tuningfork.SoundBufferLoader.SoundBufferLoaderParameter;

public class SoundBufferLoaderParameterTest extends ApplicationAdapter {
private AssetManager assetManager;
private Audio audio;
private SoundBuffer sound;
private SoundBuffer soundReverse;
private BufferedSoundSource source;


@Override
public void create() {
this.audio = Audio.init();
this.assetManager = new AssetManager();
final FileHandleResolver resolver = new InternalFileHandleResolver();
this.assetManager.setLoader(SoundBuffer.class, new SoundBufferLoader(resolver));

// queue fordward playback asset for loading as usual
this.assetManager.load("numbers.wav", SoundBuffer.class);

// queue reverse playback asset for loading
// it's the same file so we need to trick the AssetManager into thinking it's a different file
final SoundBufferLoaderParameter parameter = new SoundBufferLoaderParameter();
parameter.reverse = true;
parameter.file = Gdx.files.internal("numbers.wav");
this.assetManager.load("numerinos_wavus", SoundBuffer.class, parameter);

// we don't load asynchronously because it's just a test
this.assetManager.finishLoading();

// fetch assets
this.sound = this.assetManager.get("numbers.wav", SoundBuffer.class);
this.soundReverse = this.assetManager.get("numerinos_wavus", SoundBuffer.class);

this.source = this.audio.obtainSource(this.sound);
this.source.play();
}


@Override
public void render() {
if (!this.source.isPlaying()) {
final SoundBuffer playedSound = this.source.getBuffer();
this.source.free();
this.source = this.audio.obtainSource(playedSound == this.sound ? this.soundReverse : this.sound);
this.source.play();
}
}


@Override
public void dispose() {
this.assetManager.dispose();
this.audio.dispose();
}


public static void main(String[] args) {
final Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setTitle("SoundBufferLoaderParameterTest");
config.setWindowedMode(1000, 800);
config.useVsync(true);
config.disableAudio(true);
new Lwjgl3Application(new SoundBufferLoaderParameterTest(), config);
}

}

0 comments on commit 9dfe4d8

Please sign in to comment.