Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/src/openal/source.sml
ViewVC logotype

View of /trunk/sml3d/src/openal/source.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 882 - (download) (annotate)
Sat May 8 17:16:05 2010 UTC (9 years, 5 months ago) by jhr
File size: 4855 byte(s)
  Added Listener module to OpenAL implementation
(* source.sml
 *
 * COPYRIGHT (c) 2010 John Reppy (http://cs.uchicago.edu/~jhr)
 * All rights reserved.
 *
 * Sources represent individual sound objects in 3D-space.
 * Sources take the PCM data provided in the specified Buffer,
 * apply Source-specific modifications, and then
 * submit them to be mixed according to spatial arrangement etc.
 * 
 * Properties include:
 *
 * Gain                              AL_GAIN                 ALfloat
 * Min Gain                          AL_MIN_GAIN             ALfloat
 * Max Gain                          AL_MAX_GAIN             ALfloat
 * Position                          AL_POSITION             ALfloat[3]
 * Velocity                          AL_VELOCITY             ALfloat[3]
 * Direction                         AL_DIRECTION            ALfloat[3]
 * Head Relative Mode                AL_SOURCE_RELATIVE      ALint (AL_TRUE or AL_FALSE)
 * Reference Distance                AL_REFERENCE_DISTANCE   ALfloat
 * Max Distance                      AL_MAX_DISTANCE         ALfloat
 * RollOff Factor                    AL_ROLLOFF_FACTOR       ALfloat
 * Inner Angle                       AL_CONE_INNER_ANGLE     ALint or ALfloat
 * Outer Angle                       AL_CONE_OUTER_ANGLE     ALint or ALfloat
 * Cone Outer Gain                   AL_CONE_OUTER_GAIN      ALint or ALfloat
 * Pitch                             AL_PITCH                ALfloat
 * Looping                           AL_LOOPING              ALint (AL_TRUE or AL_FALSE)
 * MS Offset                         AL_MSEC_OFFSET          ALint or ALfloat
 * Byte Offset                       AL_BYTE_OFFSET          ALint or ALfloat
 * Sample Offset                     AL_SAMPLE_OFFSET        ALint or ALfloat
 * Attached Buffer                   AL_BUFFER               ALint
 * State (Query only)                AL_SOURCE_STATE         ALint
 * Buffers Queued (Query only)       AL_BUFFERS_QUEUED       ALint
 * Buffers Processed (Query only)    AL_BUFFERS_PROCESSED    ALint
 *)

structure Source =
  struct
    local
      open ALTypes
      open ALConsts
      open ALFuncs
    in

    type state = al_enum
    val INITIAL : state	= AL_INITIAL
    val PLAYING : state	= AL_PLAYING
    val PAUSED : state	= AL_PAUSED
    val STOPPED : state	= AL_STOPPED

    fun gen () = let
	  val id = ref 0w0
	  in
	    alGenSource (4, id);
	    !id
	  end

    fun genList n = let
	  val ids = Array.array (n, 0w0)
	  in
	    alGenSources (n, ids);
	    Array.foldr (op ::) [] ids
	  end

    fun delete [] = ()
      | delete ids = let
	  val ids = Vector.fromList ids
	  in
	    alDeleteSources (Vector.length ids, ids)
	  end

  (* playback controls *)
    local
      fun controlList f [] = ()
	| controlList f l = let
	    val v = Vector.fromList l
	    in
	      f (Vector.length v, v)
	    end
    in
    val play = alSourcePlay
    val playList = controlList alSourcePlayv
    val stop = alSourceStop
    val stopList = controlList alSourceStopv
    val pause = alSourcePause
    val pauseList = controlList alSourcePausev
    val rewind = alSourceRewind
    val rewindList = controlList alSourceRewindv
    end (* local *)

  (* integer/enum properties *)
    local
      fun get tag src = let
	    val i = ref 0
	    in
	      alGetSourcei (src, tag, i);
	      !i
	    end
      fun set tag (src, i) = alSourcei (src, tag, i)
    in
    val state = get AL_SOURCE_STATE
    end

    (* float-vector properties *)
    local
      fun get tag src = let
	    val arr = Array.array(3, 0.0)
	    in
	      alGetSourcefv (src, tag, arr);
	      {x = Array.sub(arr, 0), y = Array.sub(arr, 1), z = Array.sub(arr, 2)}
	    end
      fun set tag (src, {x, y, z}) = alSource3f (src, tag, x, y, z)
    in
    val position = get AL_POSITION
    val setPosition = set AL_POSITION
    val direction = get AL_DIRECTION
    val setDirection = set AL_DIRECTION
    val velocity = get AL_VELOCITY
    val setVelocity = set AL_VELOCITY
    end

  (* boolean properties *)
    local
      fun get tag src = let
	    val b = ref 0
	    in
	      alGetSourcei (src, tag, b);
	      (!b = AL_TRUE)
	    end
      fun set tag (src, b) = alSourcei (src, tag, if b then AL_TRUE else AL_FALSE)
    in
    val relative = get AL_SOURCE_RELATIVE
    val setRelative = set AL_SOURCE_RELATIVE
    val looping = get AL_LOOPING
    val setLooping = set AL_LOOPING
    end

  (* get the current buffer attached to the source, or NONE if there is no buffer *)
    fun buffer src = let
	  val buf = ref 0w0
	  in
	    alGetSourceui (src, AL_BUFFER, buf);
	    case !buf of 0w0 => NONE | bef => SOME buf
	  end

  (* set the given buffer to be the head of the source's queue *)
    fun setBuffer (src, buf) = alSourceui (src, AL_BUFFER, buf)

  (* clear all of the buffers from the source's queue *)
    fun clrBuffer src = alSourceui (src, AL_BUFFER, 0w0)

    end (* local *)
  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0