SML3d
3d graphics for Standard ML

Introduction

OpenAL (for "Open Audio Library") is a cross-platform interface to audio hardware. The interface consists of a number of functions that allow a programmer to specify the objects and operations in producing high-quality audio output, specifically multichannel output of 3D arrangements of sound sources around a listener.

Basic concepts

The AL structure

signature AL =
  sig

    type float = Real32.real
    type vec3f = (float * float * float)

    eqtype device
    eqtype context
    eqtype source
    eqtype buffer

    val vendorString : unit -> string
    val rendererString : unit -> string
    val versionString : unit -> string
    val extensionsString : unit -> string
    val extensions : unit -> string list

    val dopplerFactor    : float -> unit
    val getDopplerFactor : unit -> float
    val speedOfSound     : float -> unit
    val getSpeedOfSound  : unit -> float

    eqtype distance_model
    val INVERSE_DISTANCE           : distance_model
    val INVERSE_DISTANCE_CLAMPED   : distance_model
    val LINEAR_DISTANCE            : distance_model
    val LINEAR_DISTANCE_CLAMPED    : distance_model
    val EXPONENT_DISTANCE          : distance_model
    val EXPONENT_DISTANCE_CLAMPED  : distance_model
    val distanceModel : distance_model option -> unit

    structure Error : sig ... end
    structure Format : sig ... end
    structure Buffer : sig ... end
    structure Source : sig ... end
    structure Listener : sig ... end
    structure Context : sig ... end
    structure Device : sig ... end
    structure IO : sig ... end

  end

structure AL :> AL = struct ... end

Substructures

Most of the OpenAL structure is organized into a number of substructures

Types

  • device

    an abstract type representing an audio device.

  • context

    an abstract type representing an audio rendering context.

  • source

    an abstract type representing a source of audio in the simulated environment. A source object stores information about its location, direction, and other attributes.

  • buffer

    an abstract type representing a container of audio data.

  • distance_model

    This abstract type is used to specify the distance model used to compute sound attenuation.

Values

  • vendorString ()

    returns the name of the vendor.

  • rendererString ()

    returns information about the specific renderer.

  • versionString ()

    returns the OpenAL version string in the form "major.minor", followed by optional vendor version information.

  • extensionsString ()

    returns a string, which is a list of available extensions separated by spaces.

  • extensions ()

    returns a list of available extensions separated by spaces.

  • dopplerFactor f

    set the Doppler-effect scaling factor to f (see Section 3.5.2 of the OpenAL specification for details).

  • getDopplerFactor ()

    returns the current Doppler factor.

  • speedOfSound speed

    set the propagation speed of sound used to compute Doppler effects to speed (see Section 3.5.2 of the OpenAL specification for details).

  • getSpeedOfSound ()

    returns the current speed of sound propagation.

  • INVERSE_DISTANCE

    specifies the Inverse Distance Rolloff Model for sound attenuation (see Section 3.4.1 of the OpenAL specification for details).

  • INVERSE_DISTANCE_CLAMPED

    specifies the Inverse Distance Clamped Model for sound attenuation (see Section 3.4.2 of the OpenAL specification for details).

  • LINEAR_DISTANCE

    specifies the Linear Distance Rolloff Model for sound attenuation (see Section 3.4.3 of the OpenAL specification for details).

  • LINEAR_DISTANCE_CLAMPED

    specifies the Linear Distance Clamped Model for sound attenuation (see Section 3.4.4 of the OpenAL specification for details).

  • EXPONENT_DISTANCE

    specifies the Exponential Distance Rolloff Model for sound attenuation (see Section 3.4.5 of the OpenAL specification for details).

  • EXPONENT_DISTANCE_CLAMPED

    specifies the Exponential Distance Clamped Model for sound attenuation (see Section 3.4.6 of the OpenAL specification for details).

  • distanceModel (SOME model)

    set the current distance model used to compute sound attenuation.

  • distanceModel NONE

    disable distance-based sound attenuation.

  • getDistanceModel ()

    returns the current distance model used to compute sound attenuation, or NONE distance-based attenuation is currently disabled.

The Error structure

 structure Error : sig
     datatype t
   (* global errors *)
       = INVALID_NAME
       | INVALID_OPERATION
   (* device errors *)
       | INVALID_DEVICE
       | INVALID_CONTEXT
   (* general errors *)
       | INVALID_ENUM
       | INVALID_VALUE
       | OUT_OF_MEMORY

     val getError : unit -> t
     val getDevError : device -> t

     val toString : t -> string

   end

Types

  • t

    this datatype represents the various error codes that can be signaled by OpenAL operations. The values are

    • INVALID_NAME

    • INVALID_OPERATION

    • INVALID_DEVICE

    • INVALID_CONTEXT

    • INVALID_ENUM

    • INVALID_VALUE

    • OUT_OF_MEMORY

Values

  • getError ()

    get the current error condition; returns NONE if there are no errors. Calling this function resets the error condition.

  • getDevError dev

    get the device specific error condition; returns NONE if there are no errors. Calling this function resets the device’s error condition.

  • toString err

    Return a string representation of the error err.

The Format structure

structure Format : sig
    eqtype t

    val MONO8 : t
    val MONO16 : t
    val STEREO8 : t
    val STEREO16 : t

    val isMono : t -> bool
    val channels : t -> int
    val bytesPerChannel : t -> int
    val bytesPerSample : t -> int
  end

The Buffer structure

structure Buffer : sig
    type t = buffer

    val gen : unit -> t
    val genList : int -> t list
    val delete : t list -> unit

  (* Get Buffer parameters *)
    val frequency : t -> int
    val bitsPerSample : t -> int
    val channels : t -> int
    val size : t -> int

    val loadFromVector : (t * Word8Vector.vector * Format.t * int) -> unit
    val loadFromArray : (t * Word8Array.array * Format.t * int) -> unit
  end

The Source structure

structure Source : sig
    type t = source

  (* the type *)
    datatype mode = UNDETERMINED | STATIC | STREAMING

    eqtype state
    val INITIAL : state
    val PLAYING : state
    val PAUSED : state
    val STOPPED : state

    val gen : unit -> t
    val genList : int -> t list
    val delete : t list -> unit

  (* playback controls *)
    val play : t -> unit
    val playList : t list -> unit
    val stop : t -> unit
    val stopList : t list -> unit
    val pause : t -> unit
    val pauseList : t list -> unit
    val rewind : t -> unit
    val rewindList : t list -> unit

  (* get the current mode of the source *)
    val mode : t -> mode

  (* get current state *)
    val state : t -> state

  (* source position *)
    val position : t -> vec3f
    val setPosition : (t * vec3f) -> unit

  (* is position relative to listener? *)
    val relative : t -> bool
    val setRelative : t * bool -> unit

  (* source velocity *)
    val velocity : t -> vec3f
    val setVelocity : (t * vec3f) -> unit

  (* source direction and cone *)
    val direction : t -> vec3f
    val setDirection : (t * vec3f) -> unit
    val coneInnerAngle : t -> float
    val setConeInnerAngle : t * float -> unit
    val coneOuterAngle : t -> float
    val setConeOuterAngle : t * float -> unit
    val coneOuterGain : t -> float
    val setConeOuterGain : t * float -> unit

  (* frequency shift *)
    val pitch : t -> float
    val setPitch : t * float -> unit

  (* looping playback *)
    val looping : t -> bool
    val setLooping : t * bool -> unit

  (* gain bounds *)
    val minGain : t -> float
    val setMinGain : t -> float
    val maxGain : t -> float
    val setMaxGain : t -> float

  (* distance-model attributes *)
    val referenceDistance : t -> float
    val setReferenceDistance : t * float -> unit
    val rolloffFactor : t -> float
    val setRolloffFactor : t * float -> unit
    val maxDistance : t -> float
    val setMaxDistance : t * float -> unit

  (* offset into the curent audio data *)
    val offset : t -> Time.time
    val setOffset : t * Time.time -> unit
    val sampleOffset : t -> int
    val setSampleOffset : t * int -> unit
    val byteOffset : t -> int
    val setByteOffset : t * int -> unit

  (* get the current buffer attached to the t, or NONE if there is no buffer *)
    val buffer : t -> buffer option
  (* set the given buffer to be the head of the t's queue *)
    val setBuffer : t * buffer -> unit
  (* clear all of the buffers from the t's queue *)
    val clrBuffer : t -> unit

  (* return the number of buffers in the t's queue *)
    val buffersQueued : t -> int
  (* return the number of buffers that have been completely played *)
    val buffersProcessed : t -> int
  (* add buffers to the end of the t's queue *)
    val queueBuffers : t * buffer list -> unit
  (* remove processed buffers from the front of the queue *)
    val unqueueBuffers : t * int -> buffer list

  end

The Listener structure

The listener object defines various properties that affect processing of the sound for the actual output. By controlling the listener, the application controls the way the user experiences the virtual world, as the listener defines the sampling/pick-up point and orientation, and other parameters that affect the output stream. The listener is unique for an OpenAL Context, and has no name. The Listener structure is used to control

structure Listener : sig
    val gain : float -> unit
    val getGain : unit -> float

    val position : vec3f -> unit
    val getPosition : unit -> vec3f

    val velocity : vec3f -> unit
    val getVelocity : unit -> vec3f

    val orientation : {dir : vec3f, up : vec3f} -> unit
    val getOrientation : unit -> {dir : vec3f, up : vec3f}
  end

Values

  • gain g

    set the gain of the current context’s listener to g; a value of 0.0 means silence and a value of 1.0 means no gain.

  • getGain ()

    get the current gain for the listener.

  • position pos

    set the position of the current context’s listener to pos.

  • getPosition ()

    get the listener’s current position.

  • velocity vel

    set the velocity of the current context’s listener to vel.

  • getVelocity ()

    get the listener’s current velocity.

  • orientation {dir, up}

    sets the orientation of the current context’s listener, where dir is the forward direction of the listener and up specifies the up direction. The vectors should be linearly independent, but do not have to be normalized.

  • getOrientation ()

    returns the current orientation.

The Context structure

structure Context : sig
    datatype context_attr
      = FREQUENCY of int        (* Frequency for mixing output buffer (Hz) *)
      | REFRESH of int          (* Refresh intervals (Hz) *)
      | SYNC of bool            (* Indicating a synchronous context *)
      | MONO_SOURCES of int     (* Number of requested mono sources *)
      | STEREO_SOURCES of int   (* Number of requested stereo sources *)

    val create : device * context_attr list -> context option
    val makeCurrent : context -> bool
    val process : context -> unit
    val suspend : context -> unit
    val destroy : context -> unit
    val getCurrent : unit -> context option
    val getDevice : context -> device option
  end

The Device structure

structure Device : sig
    val openDev : string option -> device option
    val close : device -> bool

    val defaultSpecifier : unit -> string
    val specifier : device -> string
    val specifiers : unit -> string list
    val attributes : device -> Context.context_attr list
  end

The IO structure

structure IO : sig
    type buffer_info = {
        frequency : int,
        format : Format.format,
        size : int
      }
    val loadFile : string -> buffer * buffer_info
    val loadFileIntoBuffer : string * buffer -> buffer_info
  end