FAudioGMS/gamemaker/scripts/FAudioGMS_Scripts/FAudioGMS_Scripts.gml

380 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2021-10-29 23:00:03 +00:00
// working_directory on android is "assets/" which makes SDL freak out.
function GetPathPrepend()
{
if (os_type != os_android) return working_directory;
else return "";
}
2021-10-29 00:26:01 +00:00
// StaticSounds are usually short and intended to be played multiple times.
// All of the sound data lives in memory for as long as the StaticSound exists.
// Playing a StaticSound returns a SoundInstance.
function LoadStaticSound(filename)
{
2021-10-29 23:00:03 +00:00
var filePath = GetPathPrepend() + "audio/static/" + filename;
2021-10-29 00:26:01 +00:00
var staticSoundID = FAudioGMS_StaticSound_LoadWAV(filePath);
return new StaticSound(staticSoundID);
}
function StaticSound(_staticSoundID) constructor
{
staticSoundID = _staticSoundID;
// Create a sound instance from this static sound.
static CreateSoundInstance = function()
{
var instanceID = FAudioGMS_StaticSound_CreateSoundInstance(staticSoundID);
var instance = new SoundInstance(instanceID);
return instance;
}
// Plays and returns a sound instance!
2021-10-29 00:26:01 +00:00
// MUST be destroyed when you aren't referencing it any more or you will leak memory!
static Play = function(pan = 0, pitch = 1, volume = 1, loop = false, loopStartInMilliseconds = 0, loopEndInMilliseconds = 0)
2021-10-29 00:26:01 +00:00
{
var instanceID = FAudioGMS_StaticSound_CreateSoundInstance(staticSoundID);
var instance = new SoundInstance(instanceID);
instance.SetLoop(loop);
if (loop)
{
instance.SetPlayRegion(loopStartInMilliseconds, loopEndInMilliseconds);
}
2021-10-29 00:26:01 +00:00
instance.SetPan(pan);
instance.SetPitch(pitch);
instance.SetVolume(volume);
instance.Play();
2021-10-29 00:26:01 +00:00
return instance;
}
2021-10-29 00:26:01 +00:00
// Automatically destroys the SoundInstance when playback is finished.
// Does NOT return an instance!
static PlayOneOff = function(pan = 0, pitch = 1, volume = 1)
{
var instance = Play(pan, pitch, volume, false);
instance.DestroyWhenFinished();
}
// Plays and returns a sound instance!
2021-10-29 00:26:01 +00:00
// MUST be destroyed when you aren't referencing it any more or you will leak memory!
static PlaySpatial = function(xPosition, yPosition, zPosition, pitch = 1, volume = 1, loop = false)
{
var instanceID = FAudioGMS_StaticSound_CreateSoundInstance(staticSoundID);
var instance = new SoundInstance(instanceID);
instance.SetLoop(loop);
2021-10-29 00:26:01 +00:00
instance.Set3DPosition(xPosition, yPosition, zPosition);
instance.SetPitch(pitch);
instance.SetVolume(volume);
instance.Play(loop);
return instance;
}
2021-10-29 00:26:01 +00:00
// Automatically destroys the SoundInstance when playback is finished.
// Does NOT return an instance!
static PlaySpatialOneOff = function(xPosition, yPosition, zPosition, pitch = 1, volume = 1)
{
var instance = PlaySpatial(xPosition, yPosition, zPosition, pitch, volume, false);
instance.DestroyWhenFinished();
}
// Destroys the FAudioGMS static sound.
// If you use the StaticSound after calling this function, you are going to have a bad time.
// If you call this while there are still instances of the StaticSound, you are going to have a bad time.
static Destroy = function()
{
FAudioGMS_StaticSound_Destroy(staticSoundID);
2021-10-29 00:26:01 +00:00
}
}
// StreamingSounds are longer and usually only one copy is played at a time.
// The audio is streamed off the disk, so only a small amount of memory is used at a time.
// Good for things like music or voiceover playback.
// Note that StreamingSounds are SoundInstances.
function LoadStreamingSound(filename, bufferSizeInBytes = 0)
2021-10-29 00:26:01 +00:00
{
2021-10-29 23:00:03 +00:00
var filePath = GetPathPrepend() + "audio/streaming/" + filename;
soundInstanceID = FAudioGMS_StreamingSound_LoadOGG(filePath, bufferSizeInBytes);
2021-10-29 00:26:01 +00:00
return new SoundInstance(soundInstanceID);
}
function SoundInstance(_soundInstanceID) constructor
{
soundInstanceID = _soundInstanceID;
// Plays the sound or resumes from pause.
static Play = function()
2021-10-29 00:26:01 +00:00
{
FAudioGMS_SoundInstance_Play(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Pauses playback.
static Pause = function()
{
FAudioGMS_SoundInstance_Pause(soundInstanceID);
}
2021-10-29 00:26:01 +00:00
// Stops playback completely. If Play is called it will resume from the beginning of the sound.
static Stop = function()
{
FAudioGMS_SoundInstance_Stop(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Sets the 3-dimensional position of the sound. You probably want to use SetListenerPosition too.
static Set3DPosition = function(xPosition, yPosition, zPosition)
{
FAudioGMS_SoundInstance_Set3DPosition(soundInstanceID, xPosition, yPosition, zPosition);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Sets the 3-dimensional velocity of the sound. You probably want to use SetListenerVelocity too.
static Set3DVelocity = function(xVelocity, yVelocity, zVelocity)
{
FAudioGMS_SoundInstance_Set3DVelocity(soundInstanceID, xVelocity, yVelocity, zVelocity);
2021-10-29 00:26:01 +00:00
}
// Sets the 3-dimensional orientation of the sound.
static Set3DOrientation = function(xFront, yFront, zFront, xTop, yTop, zTop)
{
FAudioGMS_SoundInstance_Set3DOrientation(soundInstanceID, xFront, yFront, zFront, xTop, yTop, zTop);
}
// Sets whether the sound instance loops (true) or does not (false).
static SetLoop = function(loop)
{
FAudioGMS_SoundInstance_SetLoop(soundInstanceID, loop);
}
2021-10-29 00:26:01 +00:00
// Sets the panning value of the sound. -1 is farthest left, 1 is farthest right, 0 is center.
// NOTE: This is ignored if you have called Set3DPosition.
static SetPan = function(pan)
{
FAudioGMS_SoundInstance_SetPan(soundInstanceID, pan);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Sets the pitch of the sound. Default is 1. Lower than 1 is pitched down, higher than 1 is pitched up.
static SetPitch = function(pitch)
{
FAudioGMS_SoundInstance_SetPitch(soundInstanceID, pitch);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Sets the volume of the sound. Default is 1. Lower than 1 is quieter, greater than 1 is louder.
// If you set the milliseconds value then this will fade over time.
static SetVolume = function(volume, milliseconds = 0)
{
if (milliseconds > 0)
{
FAudioGMS_SoundInstance_SetVolumeOverTime(soundInstanceID, volume, milliseconds);
}
else
{
FAudioGMS_SoundInstance_SetVolume(soundInstanceID, volume);
2021-10-29 00:26:01 +00:00
}
}
// Sets the position of track playback.
2021-10-29 00:26:01 +00:00
static SetTrackPosition = function(seconds)
{
FAudioGMS_SoundInstance_SetTrackPositionInSeconds(soundInstanceID, seconds);
2021-10-29 00:26:01 +00:00
}
// Queues this sound instance for playing in sync.
static QueueSyncPlay = function()
{
FAudioGMS_SoundInstance_QueueSyncPlay(soundInstanceID);
}
// Sets the playback region for the sound instance.
static SetPlayRegion = function(loopStartInMilliseconds, loopEndInMilliseconds)
{
FAudioGMS_SoundInstance_SetPlayRegion(soundInstanceID, loopStartInMilliseconds, loopEndInMilliseconds);
}
2021-10-29 00:26:01 +00:00
// Sets a low pass filter on the sound.
// frequency: 0.0 <-> 1.0. 1.0 means all sound gets through.
// Q: set this to 1 unless you know what you're doing
static SetLowPassFilter = function(frequency, Q = 1.0)
{
FAudioGMS_SoundInstance_SetLowPassFilter(soundInstanceID, frequency, Q);
}
2021-10-29 00:26:01 +00:00
// Sets a high pass filter on the sound.
// frequency: 0.0 <-> 1.0. 0.0 means all sound gets through.
// Q: set this to 1 unless you know what you're doing
static SetHighPassFilter = function(frequency, Q = 1.0)
{
FAudioGMS_SoundInstance_SetHighPassFilter(soundInstanceID, frequency, Q);
}
2021-10-29 00:26:01 +00:00
// Sets a band pass filter ont he sound.
// frequency: 0.0 <-> 1.0
// Q: set this to 1 unless you know what you're doing
static SetBandPassFilter = function(frequency, Q = 1.0)
{
FAudioGMS_SoundInstance_SetBandPassFilter(soundInstanceID, frequency, Q);
}
2021-10-29 00:26:01 +00:00
// Sets an effect chain on the sound.
// Gain is how much the effect chain "affects" the sound. 1.0 is max.
// NOTE: Any changes to the effect chain will NOT apply automatically after this is set!
// You MUST call this function again if you want changed parameters to apply!
static SetEffectChain = function(effectChain, gain)
{
FAudioGMS_SoundInstance_SetEffectChain(soundInstanceID, effectChain.effectChainID, gain);
}
2021-10-29 00:26:01 +00:00
// Sets the effect gain of the current effect chain.
// Does nothing if no effect chain is set.
static SetEffectGain = function(gain)
{
FAudioGMS_SoundInstance_SetEffectGain(soundInstanceID, gain);
}
static QueueSoundInstance = function(queueSoundInstance)
{
FAudioGMS_SoundInstance_QueueSoundInstance(soundInstanceID, queueSoundInstance.soundInstanceID);
}
2021-10-29 00:26:01 +00:00
// Gets the pitch of the sound.
static GetPitch = function()
{
return FAudioGMS_SoundInstance_GetPitch(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Gets the volume of the sound.
static GetVolume = function()
{
return FAudioGMS_SoundInstance_GetVolume(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Gets the total length of the track.
static GetTrackLengthInSeconds = function()
{
return FAudioGMS_SoundInstance_GetTrackLengthInSeconds(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Gets the current track position.
static GetTrackPositionInSeconds = function()
{
return FAudioGMS_SoundInstance_GetTrackPositionInSeconds(soundInstanceID);
}
2021-10-29 00:26:01 +00:00
// Destroys the FAudioGMS sound instance.
// If you use the SoundInstance after calling this you are going to have a bad time.
static Destroy = function()
{
FAudioGMS_SoundInstance_Destroy(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
// Sets the FAudioGMS sound instance to destroy itself when playback is done.
// Calling this on a looping sound is not a good idea.
// If you use the SoundInstance after calling this you might have a bad time.
static DestroyWhenFinished = function()
{
FAudioGMS_SoundInstance_DestroyWhenFinished(soundInstanceID);
2021-10-29 00:26:01 +00:00
}
2021-10-29 00:26:01 +00:00
SetPan(0);
SetPitch(1);
SetVolume(1);
}
// Plays the sound instance queue.
function SyncPlay()
{
FAudioGMS_SoundInstance_SyncPlay();
}
2021-10-29 00:26:01 +00:00
// Effect chains allow you to modify sound playback using audio effects.
// Right now only reverb is implemented, but more effects will probably come later.
function EffectChain() constructor
{
effectChainID = FAudioGMS_EffectChain_Create();
2021-10-29 00:26:01 +00:00
// Adds a reverb effect to the effect chain.
static AddReverb = function(
wetDryMix,
reflectionsDelay,
reverbDelay,
earlyDiffusion,
lateDiffusion,
lowEQGain,
lowEQCutoff,
highEQGain,
highEQCutoff,
reflectionsGain,
reverbGain,
decayTime,
density,
roomSize
) {
FAudioGMS_EffectChain_AddReverb(
effectChainID,
wetDryMix,
reflectionsDelay,
reverbDelay,
earlyDiffusion,
lateDiffusion,
lowEQGain,
lowEQCutoff,
highEQGain,
highEQCutoff,
reflectionsGain,
reverbGain,
decayTime,
density,
roomSize
);
}
2021-10-29 00:26:01 +00:00
// Adds a default reverb effect to the effect chain.
// This is a good place to start if you don't know what all the reverb params do.
static AddDefaultReverb = function()
{
FAudioGMS_EffectChain_AddReverb(
effectChainID,
100,
7,
11,
15,
15,
8,
4,
8,
6,
-26,
10,
1.49,
100,
100
);
}
2021-10-29 00:26:01 +00:00
// Destroys the FAudioGMS effect chain.
// If you use the EffectChain after calling this you are going to have a bad time.
static Destroy = function()
{
FAudioGMS_EffectChain_Destroy(effectChainID);
2021-10-29 00:26:01 +00:00
}
}
// Sets the position of the listener for 3D audio.
function SetListenerPosition(xPosition, yPosition, zPosition)
{
FAudioGMS_SetListenerPosition(xPosition, yPosition, zPosition);
}
// Sets the velocity of the listener for 3D audio.
function SetListenerVelocity(xVelocity, yVelocity, zVelocity)
{
FAudioGMS_SetListenerVelocity(xVelocity, yVelocity, zVelocity);
2021-10-29 00:26:01 +00:00
}
// Sets the orientation of the listener for 3D audio.
function SetListenerOrientation(xFront, yFront, zFront, xTop, yTop, zTop)
{
FAudioGMS_SetListenerOrientation(xFront, yFront, zFront, xTop, yTop, zTop);
}
// Stops all audio playback.
2021-10-29 00:26:01 +00:00
function StopAllAudio()
{
FAudioGMS_StopAll();
2021-10-29 00:26:01 +00:00
}