All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lessvoid.nifty.sound.openal.slick.AudioLoader Maven / Gradle / Ivy

Go to download

This is an OpenAL/lwjgl based SoundDevice for Nifty GUI. It was extracted out of Slick2D to be independent of the whole Slick2D library and to be used with none Slick2D based renderes. All credits for the original code go to Kevin Glass and all Slick2D contributers.

The newest version!
package de.lessvoid.nifty.sound.openal.slick;

import de.lessvoid.nifty.tools.resourceloader.NiftyResourceLoader;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * A utility to provide a simple and rational interface to the
 * slick internals
 *
 * @author kevin
 */
public class AudioLoader {
  /**
   * AIF Format Indicator
   */
  private static final String AIF = "AIF";
  /**
   * WAV Format Indicator
   */
  private static final String WAV = "WAV";
  /**
   * OGG Format Indicator
   */
  private static final String OGG = "OGG";
  /**
   * MOD/XM Format Indicator
   */
  private static final String MOD = "MOD";
  /**
   * MOD/XM Format Indicator
   */
  private static final String XM = "XM";

  /**
   * True if the audio loader has be initialised
   */
  private static boolean inited = false;

  /**
   * Initialise the audio loader
   */
  private static void init() {
    if (!inited) {
      SoundStore.get().init();
      inited = true;
    }
  }

  /**
   * Get audio data in a playable state by loading the complete audio into
   * memory.
   *
   * @param format The format of the audio to be loaded (something like "XM" or "OGG")
   * @param in     The input stream from which to load the audio data
   * @return An object representing the audio data
   * @throws IOException Indicates a failure to access the audio data
   */
  @Nonnull
  public static Audio getAudio(@Nonnull String format, @Nonnull InputStream in) throws IOException {
    init();

    if (format.equals(AIF)) {
      return SoundStore.get().getAIF(in);
    }
    if (format.equals(WAV)) {
      return SoundStore.get().getWAV(in);
    }
    if (format.equals(OGG)) {
      return SoundStore.get().getOgg(in);
    }

    throw new IOException("Unsupported format for non-streaming Audio: " + format);
  }

  /**
   * Get audio data in a playable state by setting up a stream that can be piped into
   * OpenAL - i.e. streaming audio
   *
   * @param format The format of the audio to be loaded (something like "XM" or "OGG")
   * @param url    The location of the data that should be streamed
   * @return An object representing the audio data
   * @throws IOException Indicates a failure to access the audio data
   */
  @Nonnull
  public static Audio getStreamingAudio(
      @Nonnull String format,
      @Nonnull URL url,
      final NiftyResourceLoader resourceLoader) throws IOException {
    init();

    if (format.equals(OGG)) {
      return SoundStore.get().getOggStream(url, resourceLoader);
    }
    if (format.equals(MOD)) {
      return SoundStore.get().getMOD(url.openStream());
    }
    if (format.equals(XM)) {
      return SoundStore.get().getMOD(url.openStream());
    }

    throw new IOException("Unsupported format for streaming Audio: " + format);
  }

  /**
   * Allow the streaming system to update itself
   */
  public static void update() {
    init();

    SoundStore.get().poll(0);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy