Garry's Mod Wiki

sound.Generate

sound.Generate( string indentifier, number samplerate, number length, function callback )

Description

Creates a sound from a function.

This function cannot generate sounds that have a duration of less than 1 second.

Issue Tracker: 3360
Sounds persist between disconnects.

Issue Tracker: 4082

Arguments

1 string indentifier
An unique identified for the sound.

You cannot override already existing ones.
2 number samplerate
The sample rate of the sound. Must be 11025, 22050 or 44100.
3 number length
The length in seconds of the sound to generate.
4 function callback
A function which will be called to generate every sample on the sound. This function gets the current sample number passed as the first argument. 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.

Example

Plays a 2000 Hz sine wave at maximum volume

local frequency = 2000 -- Hz local samplerate = 44100 local function data(t) return math.sin( t * math.pi * 2 / samplerate * frequency ) end test_sound_id = test_sound_id and test_sound_id + 1 or 10 sound.Generate ("testgen" .. test_sound_id, samplerate, 2, data) surface.PlaySound("testgen" .. test_sound_id)

Page Links


Special Pages


Wikis

?

Render Time: 40ms

DB GetPage 3
Generate Html 12
SaveChanges (1) 10
Render Body 0
Render Sidebar 12