Garry's Mod Wiki

sound.Generate

  sound.Generate( string identifier, number samplerate, number duration, function or table or string sampleData, number loopStart = nil )

Description

Creates a sound from raw audio sample data.

Arguments

1 string identifier
A unique identifier for the sound.

You cannot override existing identifiers.
2 number samplerate
The sample rate of the sound.

Must be 11025, 22050 or 44100.

3 number duration
The length in seconds of the sound to generate.
4 function or table or string sampleData
The raw sample data which will be turned into audio.

Can be a function which will be called to generate every sample on the sound.

Function argument(s):
1 number sampleIndex - The current sample number.
Function return value(s):
1 number sampleValue - The return value must be between -1.0 and 1.0.
Other values will wrap back to the -1 to 1 range and basically clip.
There are 65535 possible quantifiable values between -1 and 1.

Can be a table of numbers in the range -1 to 1

Can be a string of unsigned 16 bit (a "short") binary data.

5 number loopStart = nil
Sample ID of the loop start. If given, the sound will be looping and will restart playing at given position after reaching its end.

Example: Using a function to create sample data

Plays a 2000 Hz sine wave at maximum volume.

local frequency = 2000 -- Hz local duration = 2 -- Seconds local sampleRate = 44100 local function data( sampleIndex ) return math.sin( sampleIndex * math.pi * 2 / sampleRate * frequency ) end sound.Generate( "WikiFunctionSoundExample", sampleRate, duration, data ) surface.PlaySound( "WikiFunctionSoundExample" )