nimble install slappy
Big thanks to yglukhov's sound library!
This library provides higher level interface to the OpenAL library.
Slappy provides the standard features of:
- 3d positions of sounds.
- 3d position of listener.
- Doppler shift.
- Acoustic attenuation.
Slappy also provides some extra features such as:
- A much more nim-like api.
.wav
and.ogg
loading.- Sound priority. (in progress)
- Max number of the same sound played. (in progress)
- Fade in and fade out. (in progress)
- Ability to queued sounds. (in progress)
# rotate sound in 3d
let sound = newSound("tests/drums.mono.wav")
var source = sound.play()
source.looping = true
echo "rotating sound in 3d, 1 rotation"
for i in 0..360:
let a = float(i) / 180 * PI
source.pos = vec3(sin(a), cos(a), 0)
sleep(20)
source.stop()
sleep(500)
See test.nim for more details.
Listener is the main ear of abstract person in the 3d world.
Listener has the following properties:
- gain - Volume on which the listener hears.
- pos - Position
- vel - Velocity
- mat - Orientation matrix
You get one global Listener.
Sound the recording of the sound that can be played.
Sound can be loaded with:
sound = newSound("path/to/wav.or.ogg")
Sound has the following functions:
- play() - Creates a
Source
objects that has the sound playing.
Sound has the following properties, that are read only:
- bits - Bit rate or number of bits per sample.
- size - Number of byte the sound takes up.
- freq - Frequency or the samples per second rate.
- channels - Number of channels, only 1 or 2 supported.
WARNING
: 2 channel sounds can't be positioned in 3d - samples - Number of samples, a sample is a single integer that sets the position of the speaker membrane.
- duration - duration of the sound in seconds.
Source represents the sound playing in a 3d world, kind of like an abstract speaker.
Source has the following functions:
- stop() - Stop the sound.
- play() - Start playing the sound (if it was stopped before).
Source has the following properties:
- pitch - How fast the sound plays, or how low or high it sounds.
- gain - Volume of the sound.
- maxDistance - Inverse Clamped Distance Model, where sound will not longer be played.
- rolloffFactor - Set roll off rate for the source.
- halfDistance - The distance under which the volume for the source would normally drop by half.
- minGain - The minimum gain for this source.
- maxGain - The minimum gain for this source.
- playing - Is the sound playing.
- looping - Should the sound loop.
- pos - Position
- vel - Velocity
- mat - Orientation matrix
- playback - playback position in seconds (offset)
import slappy
Listener = object
Sound = ref object
id: ALuint
Source = ref object
id: ALuint
listener = Listener()
Set Master gain. Value should be positive.
proc gain=(listener: Listener; v: float32)
Get master gain.
proc gain(listener: Listener): float32
Set position of the main listener.
proc pos=(listener: Listener; pos: Vec3)
Get position of the main listener.
proc pos(listener: Listener): Vec3
Set velocity of the main listener.
proc vel=(listener: Listener; vel: Vec3)
Get velocity of the main listener.
proc vel(listener: Listener): Vec3
Set orientation of the main listener.
proc mat=(listener: Listener; mat: Mat4)
Get orientation of the main listener.
proc mat(listener: Listener): Mat4
proc playing(source: Source): bool {.inline.}
proc stop(source: Source)
proc play(source: Source)
proc pitch=(source: Source; v: float32)
proc pitch(source: Source): float32
proc gain=(source: Source; v: float32)
proc gain(source: Source): float32
Set the Inverse Clamped Distance Model to set the distance where there will no longer be any attenuation of the source.
proc maxDistance=(source: Source; v: float32)
proc maxDistance(source: Source): float32
Set rolloff rate for the source. Default is 1.0.
proc rolloffFactor=(source: Source; v: float32)
proc rolloffFactor(source: Source): float32
The distance under which the volume for the source would normally drop by half (before being influenced by rolloff factor or maxDistance).
proc halfDistance=(source: Source; v: float32)
proc halfDistance(source: Source): float32
The minimum gain for this source.
proc minGain=(source: Source; v: float32)
proc minGain(source: Source): float32
The minimum gain for this source.
proc maxGain=(source: Source; v: float32)
proc maxGain(source: Source): float32
The gain when outside the oriented cone.
proc coneOuterGain=(source: Source; v: float32)
proc coneOuterGain(source: Source): float32
Inner angle of the sound cone, in degrees. Default is 360.
proc coneInnerAngle=(source: Source; v: float32)
proc coneInnerAngle(source: Source): float32
Outer angle of the sound cone, in degrees. Default is 360.
proc coneOuterAngle=(source: Source; v: float32)
proc coneOuterAngle(source: Source): float32
proc looping=(source: Source; v: bool)
proc looping(source: Source): bool
Set playback position in seconds (offset).
proc playback=(source: Source; v: float32)
Get the playback position in seconds (offset).
proc playback(source: Source): float32
Set source position.
proc pos=(source: Source; pos: Vec3)
proc pos(source: Source): Vec3
proc vel=(source: Source; vel: Vec3)
proc vel(source: Source): Vec3
proc mat=(source: Source; mat: Mat4)
proc mat(source: Source): Mat4
Call this on start of your program.
proc slappyInit()
Call this on exit.
proc slappyClose()
Updates all sources and sounds.
proc slappyTick()
proc newSound(): Sound
proc newSound(filePath: string): Sound {.raises: [Defect, IOError, OSError, Exception, ValueError], tags: [ReadIOEffect, WriteIOEffect].}
Gets the bit rate or bits per sample, only 8bits and 16bits per sample supported.
proc bits(sound: Sound): int {.inline.}
Gets the size of the sound buffer in bytes.
proc size(sound: Sound): int {.inline.}
Gets the frequency or the samples per second rate.
proc freq(sound: Sound): int {.inline.}
Gets number of channels, only 1 or 2 are supported. WARNING: 2 channel sounds can't be positioned in 3d.
proc channels(sound: Sound): int {.inline.}
Gets number of samples.
proc samples(sound: Sound): int {.inline.}
Gets duration of the sound in seconds.
proc duration(sound: Sound): float32 {.inline.}
proc play(sound: Sound): Source