Sound.Generate
static Sound Generate(AudioGenerator generator, float duration)
This function will generate a sound from a function you provide! The function is called once for each sample in the duration. As an example, it may be called 48,000 times for each second of duration.
| AudioGenerator generator | This function takes a time value as an argument, which will range from 0-duration, and should return a value from -1 - +1 representing the audio wave at that point in time. |
| float duration | In seconds, how long should the sound be? |
| RETURNS: Sound | Returns a generated sound effect! Or null if something went wrong. |
static Sound Generate(AudioBufferGenerator generator, float duration, SoundChannels channels)
This function generates a sound by asking your function to fill whole buffers of samples! This is far faster than the per-sample overload, one interop call instead of one per sample.
With a channel format, the buffer holds frames-x-channels interleaved samples: stereo alternates left/right, and Ambisonic1 packs W,Y,Z,X per frame in the ambiX convention - so procedural head-tracked sound fields are just a generator away.
| AudioBufferGenerator generator | Fills the provided buffer completely with interleaved audio sample values from -1 to +1. The second parameter is the index of the buffer’s first frame, at 48,000 frames per second. |
| float duration | In seconds, how long should the sound be? |
| SoundChannels channels | The channel format the generator fills. |
| RETURNS: Sound | Returns a generated sound effect! Or null if something went wrong. |
Examples
Generating a sound via generator
Making a procedural sound is pretty straightforward! Here’s an example of building a 500ms sound from two frequencies of sin wave.
Sound genSound = Sound.Generate((t) =>
{
float band1 = SKMath.Sin(t * 523.25f * SKMath.Tau); // a 'C' tone
float band2 = SKMath.Sin(t * 659.25f * SKMath.Tau); // an 'E' tone
const float volume = 0.1f;
return (band1*0.6f + band2*0.4f) * volume;
}, 0.5f);
genSound.Play(Vec3.Zero);
Found an issue with these docs, or have some additional questions? Create an Issue on Github!