IAudio
namespace Griff.Runtime;
public interface IAudioSound for gameplay code. Paths are relative to the project’s Assets folder, e.g. “Sounds/jump.wav”. WAV, MP3 and OGG are supported. Make sounds and music in the editor with Audio > Sound Maker.
Properties
Section titled “Properties”public virtual string? Music { get; }The music playing (or fading in), by name; null with none.
MusicLength
Section titled “MusicLength”public virtual double MusicLength { get; }How many seconds the music playing lasts before it loops; 0 with none, or until it’s known.
MusicTempo
Section titled “MusicTempo”public virtual float MusicTempo { get; }The music playing’s beats a minute, when it says: music made with Audio > New Music does. 0 when it doesn’t (or with no music).
MusicTime
Section titled “MusicTime”public virtual double MusicTime { get; }Seconds of the music playing that have been heard: how far into it the speakers are, allowing for the sound card’s delay, so something timed to it lands with what’s heard rather than what’s mixed. It counts on as the music loops (see MusicLength), is a moment below 0 just after the music starts, and is 0 with no music. BeatClock turns it into beats and bars.
MusicVolume
Section titled “MusicVolume”public abstract float MusicVolume { get; set; }Music volume, 0 to 1, on top of Volume.
public abstract bool Muted { get; set; }Silences everything without forgetting the volumes.
SoundVolume
Section titled “SoundVolume”public abstract float SoundVolume { get; set; }Sound effect volume, 0 to 1, on top of Volume.
Volume
Section titled “Volume”public abstract float Volume { get; set; }Overall volume, 0 to 1, for an options menu. Changes apply to the music straight away.
Methods
Section titled “Methods”Advance
Section titled “Advance”public void Advance(float seconds)Moves fades on by seconds of real time. The game calls it every frame.
CrossfadeMusic
Section titled “CrossfadeMusic”public void CrossfadeMusic(string music, float seconds, float volume = 0.6f, bool loop = true)Fades the music playing out over seconds while music fades in, for moving between areas. The same music already playing just carries on.
FadeOutMusic
Section titled “FadeOutMusic”public void FadeOutMusic(float seconds)Fades the music out over seconds, then stops it.
public ISoundLoop Loop(string sound, float volume = 1f)Starts a sound that plays over and over until it’s stopped, whose volume and pan can change while it plays: a waterfall, a humming machine, rain. SoundSource does this with no code.
public abstract void Play(string sound, float volume = 1f)Plays a one-shot sound effect. Overlapping plays of the same sound are fine.
public void Play(string sound, float volume, float pan)Plays a one-shot sound to one side: pan is -1 for the left speaker only, 1 for the right, 0 for both. Game.PlaySoundAt works it out from where something is on screen.
public void Play(string sound, float volume, float pan, float pitch)Plays a one-shot sound at a pitch: 1 is as recorded, 2 an octave higher (and twice as quick), 0.5 an octave lower. A little randomness keeps a sound heard over and over from getting tiring: Audio.Play("Sounds/coin.wav", 1, 0, Random.Shared.NextSingle() * 0.2f + 0.9f).
PlayMetronome
Section titled “PlayMetronome”public void PlayMetronome(float tempo, int beatsPerBar = 4, float volume = 1f)Plays a metronome as the music, in place of any music playing: a click on every beat at tempo beats a minute, a higher one on the first of each bar of beatsPerBar, until the music is stopped or changed. It’s timed as music is (MusicTime, and its MusicTempo says the tempo), so a BeatClock keeps time with it and a Calibration taps along to it. volume is on top of the sound effect volume rather than the music’s, so a player who has turned the music off still hears it; 0 plays it silently, keeping the time.
PlayMusic
Section titled “PlayMusic”public abstract void PlayMusic(string music, float volume = 0.6f, bool loop = true)Plays background music, replacing any music already playing.
PlayMusicFrom
Section titled “PlayMusicFrom”public void PlayMusicFrom(string music, double seconds, float volume = 0.6f, bool loop = true)Plays music as PlayMusic does, but from seconds into it rather than from the start: to carry a song on where it was (after a pause, or a rewind). MusicTime counts on from there.
StopAll
Section titled “StopAll”public abstract void StopAll()Stops everything (when the game stops).
StopMusic
Section titled “StopMusic”public abstract void StopMusic()Fields
Section titled “Fields”MetronomeMusic
Section titled “MetronomeMusic”public const string MetronomeMusicWhat Music says while PlayMetronome’s clicks play.