1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// -----------------------------------------------------------------------------
// File: audio_source.ss
// Description: spatial audio source
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
using SurgeEngine.Level;
using SurgeEngine.Player;
using SurgeEngine.Transform;
using SurgeEngine.Audio.Sound;
using SurgeEngine.Video.Screen;
//
// Audio Source
//
// This is a spatial audio source. The sound is attenuated according to the
// distance between the active player and the audio source. In this version,
// a linear falloff is used.
//
// Properties:
// - sound: string. Path to a .wav file in the samples/ folder.
// - type: string. Either "line" or "point" ("line" plays nicely on a platformer).
// - mindist: number. Within a distance of mindist pixels, the sound will stay the loudest.
// - maxdist: number. Outside the region of maxdist pixels, the sound will be silent.
// - volume: number. Base volume: a number between 0 and 1. Usually set to 1.
// - enabled: boolean. Whether the audio source is enabled or not.
//
object "Audio Source" is "entity", "special"
{
public sound = null;
public type = "line";
public mindist = 256;
public maxdist = 512;
mixer = Level.child("Audio Mixer") || Level.spawn("Audio Mixer");
transform = Transform();
distance = null;
snd = null;
vol = 0;
basevol = 1;
// get the sound effect
state "main"
{
if(sound != null) {
if(typeof(sound) == "string") {
distance = distance || ((type == "line") ?
spawn("Audio Source - Horizontal Distance") :
spawn("Audio Source - Euclidean Distance")
);
snd = sound;
state = "playing";
}
}
}
// play the sound effect
state "playing"
{
vol = volumeAt(Player.active.transform.position);
mixer.notify(snd, vol);
}
// this audio source is disabled
state "disabled"
{
if(snd !== null)
mixer.notify(snd, 0.0);
}
// stop the sound
fun onReset()
{
if(snd !== null)
mixer.notify(snd, 0.0);
}
// compute the volume of the audio source at a certain position
fun volumeAt(position)
{
dist = distance(transform.position, position);
dist = Math.clamp(dist, mindist, maxdist);
return basevol * (maxdist - dist) / (maxdist - mindist);
}
// get the base volume
fun get_volume()
{
return basevol;
}
// set the base volume
fun set_volume(value)
{
basevol = Math.clamp(value, 0, 1);
}
// is the audio source enabled?
fun get_enabled()
{
return state != "disabled";
}
// enable/disable the audio source
fun set_enabled(enabled)
{
if(enabled) {
if(state == "disabled")
state = "main";
}
else
state = "disabled";
}
}
// Mixes multiple audio sources
object "Audio Mixer"
{
channel = {};
fun notify(snd, vol)
{
if((c = channel[snd]) === null)
channel[snd] = spawn("Audio Mixer - Channel").setSound(snd).increaseVolume(vol);
else
c.increaseVolume(vol);
}
}
object "Audio Mixer - Channel"
{
sound = null;
volume = 0.0;
state "main"
{
assert(sound !== null);
if(volume == 0.0) {
sound.volume = 0.0;
//sound.stop(); // will restart the sound if the player moves back and forth
return;
}
sound.volume = volume;
if(!sound.playing)
sound.play();
volume = 0.0; // need to increaseVolume() every frame
}
fun setSound(filepath)
{
sound = Sound(filepath);
return this;
}
fun increaseVolume(vol)
{
vol = Math.clamp(vol, 0.0, 1.0);
volume = Math.max(volume, vol);
return this;
}
fun destructor()
{
assert(sound !== null);
sound.stop();
}
}
// Computes the horizontal distance between
// of a and b, both Vector2 objects
object "Audio Source - Horizontal Distance"
{
margin = Screen.height / 2;
fun call(a, b)
{
dy = Math.abs(a.y - b.y) - margin;
if(dy < 0)
return Math.abs(a.x - b.x);
else
return Math.abs(a.x - b.x) + dy * 2;
}
fun setLineHeight(value)
{
margin = Math.max(value, 0) / 2;
return this;
}
}
// Computes the Euclidean distance between
// a and b, both Vector2 objects
object "Audio Source - Euclidean Distance"
{
fun call(a, b)
{
return a.distanceTo(b);
}
}
|