GetSoundWaveData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
("if ( waveData )" in for?)
 
(4 intermediate revisions by 4 users not shown)
Line 47: Line 47:
     if (isElement (soundHandler)) then
     if (isElement (soundHandler)) then
         local waveData = getSoundWaveData (soundHandler, samples)
         local waveData = getSoundWaveData (soundHandler, samples)
         for i=0, samples-1 do
         if (waveData) then -- Avoid NaN values.
            if (waveData[i] == waveData[i]) then -- Avoid NaN values.
            for i=0, samples-1 do
                 dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * 128)
                 dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * 128)
             end
             end
Line 66: Line 66:
     if (isElement (soundHandler)) then
     if (isElement (soundHandler)) then
         local waveData = getSoundWaveData (soundHandler, samples)
         local waveData = getSoundWaveData (soundHandler, samples)
         for i=0, samples-1 do
         if (waveData) then -- Avoid NaN values.
            if (waveData[i] == waveData[i]) then -- Avoid NaN values.
            for i=0, samples-1 do
                 dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * -128)
                 dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * -128)
             end
             end
Line 86: Line 86:
     if (isElement (soundHandler)) then
     if (isElement (soundHandler)) then
         local waveData = getSoundWaveData (soundHandler, samples)
         local waveData = getSoundWaveData (soundHandler, samples)
         for i=0, samples-1 do
         if (waveData) then -- Avoid NaN values.
            if (waveData[i] == waveData[i]) then -- Avoid NaN values.
            for i=0, samples-1 do
                 dxDrawRectangle ((x-samples)+i, y-128, 1, waveData[i] * 128)
                 dxDrawRectangle ((x-samples)+i, y-128, 1, waveData[i] * 128)
             end
             end
Line 106: Line 106:
[[hu:getSoundWaveData]]
[[hu:getSoundWaveData]]
[[ar:getSoundWaveData]]
[[ar:getSoundWaveData]]
[[pt-br:getSoundWaveData]]

Latest revision as of 06:09, 3 January 2023

This function gets the wave form data for an audio stream which is a table of floats representing the current audio frame as a wave. This allows things like visualisations.

If the element is a player, this function will use the players voice.

Syntax

table getSoundWaveData ( element sound, int iSamples )

OOP Syntax Help! I don't understand this!

Method: sound:getWaveData(...)


Required Arguments

  • sound: a sound element that is created using playSound or playSound3D. Streams are also supported
  • iSamples: allowed samples are 256, 512, 1024, 2048, 4096, 8192 and 16384.

Returns

Returns a table of iSamples floats representing the current audio frame waveform. Returns false if the sound is not playing yet or hasn't buffered in the case of streams.

Example

This example creates a sound visualizer on the top left corner of the screen.

Click to collapse [-]
Client
soundHandler = playSound ( "sound.wav" )

function onSoundPlayRender ( )
    if ( soundHandler ) then
        local waveData = getSoundWaveData ( soundHandler, 256 )
	if ( waveData ) then
            for i=0,255 do
                dxDrawRectangle ( i, 128, 1, waveData[i] * 128)
            end
        end
    end
end
addEventHandler ( "onClientRender", getRootElement(), onSoundPlayRender )

This example creates a sound viewer, but only with bars down.

Click to collapse [-]
Client
soundHandler = playSound ("audio.mp3")
local samples = 256

function renderWave ()
    if (isElement (soundHandler)) then
        local waveData = getSoundWaveData (soundHandler, samples)
        if (waveData) then -- Avoid NaN values.
            for i=0, samples-1 do
                dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * 128)
            end
        end
    end
end
addEventHandler ("onClientRender", root, renderWave)

This example creates a sound viewer, but only with bars up.

Click to collapse [-]
Client
soundHandler = playSound ("audio.mp3")
local samples = 256

function renderWave ()
    if (isElement (soundHandler)) then
        local waveData = getSoundWaveData (soundHandler, samples)
        if (waveData) then -- Avoid NaN values.
            for i=0, samples-1 do
                dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * -128)
            end
        end
    end
end
addEventHandler ("onClientRender", root, renderWave)

This example creates a sound viewer on the bottom right corner of the screen.

Click to collapse [-]
Client
soundHandler = playSound ("audio.mp3")
local x, y = guiGetScreenSize ()
local samples = 256

function renderWave ()
    if (isElement (soundHandler)) then
        local waveData = getSoundWaveData (soundHandler, samples)
        if (waveData) then -- Avoid NaN values.
            for i=0, samples-1 do
                dxDrawRectangle ((x-samples)+i, y-128, 1, waveData[i] * 128)
            end
        end
    end
end
addEventHandler ("onClientRender", root, renderWave)

Changelog

Version Description
1.3.2 Added player element to use a players voice

See Also