GetSoundWaveData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
("if ( waveData )" in for?)
 
(15 intermediate revisions by 8 users not shown)
Line 4: Line 4:
This allows things like visualisations.
This allows things like visualisations.


{{New feature/item|3.0132|1.3.2||
If the element is a player, this function will use the players voice.
If the element is a player, this function will use the players voice.
 
}}
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">table getSoundWaveData ( element sound, int iSamples )</syntaxhighlight>  
<syntaxhighlight lang="lua">table getSoundWaveData ( element sound, int iSamples )</syntaxhighlight>  
 
{{OOP||[[sound]]:getWaveData}}
===Required Arguments===  
===Required Arguments===  
*'''sound:''' A sound element that is created using [[playSound]] or [[playSound3D]]. Streams are also supported
*'''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.
*'''iSamples:''' allowed samples are 256, 512, 1024, 2048, 4096, 8192 and 16384.


===Returns===
===Returns===
Returns a table of '''iSamples'''/2 floats representing the current audio frame waveform.
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
Returns ''false'' if the sound is not playing yet or hasn't buffered in the
case of streams.
case of streams.


==Example==
==Example==
This code creates waveform of sound in center of screen from top to bottom. [http://imageshack.com/a/img547/9052/xbrp.png] Key '''9''' - start, key '''0''' - stop.
This example creates a sound visualizer on the top left corner of the screen.
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
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 )
</syntaxhighlight>
</section>
This example creates a sound viewer, but only with bars down.
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local handl = nil
soundHandler = playSound ("audio.mp3")
local sx,_ = guiGetScreenSize()
local samples = 256
local colors = { tocolor(255,0,0),tocolor(0,255,0) }
 
function renderWave ()
function clientRenderFunc()
     if (isElement (soundHandler)) then
     if(handl) then
         local waveData = getSoundWaveData (soundHandler, samples)
         local bt = getSoundWaveData(handl,512)
        if (waveData) then -- Avoid NaN values.
dxDrawRectangle(sx/2,0,1,256,tocolor(255,255,255,127))
            for i=0, samples-1 do
        for i=2,128 do
                dxDrawRectangle (i, 128, 1, math.abs (waveData[i]) * 128)
    dxDrawLine(sx/2+64*bt[2*i-1],2*i-2,sx/2+64*bt[2*i+1],2*i,colors[1])
             end
            dxDrawLine(sx/2+64*bt[2*(i-1)-1],2*(i-1)-2,sx/2+64*bt[2*(i-1)+1],2*(i-1),colors[1])
             dxDrawLine(sx/2+64*bt[2*i],2*i-1,sx/2+64*bt[2*i+2],2*i+1,colors[2])
            dxDrawLine(sx/2+64*bt[2*(i-1)],2*(i-1)-1,sx/2+64*bt[2*(i-1)+2],2*(i-1)+1,colors[2])
         end
         end
     end
     end
end
end
addEventHandler ("onClientRender", root, renderWave)
function musicStartFunc()
</syntaxhighlight>
     if(not handl) then
</section>
         handl = playSound('btr.mp3')
This example creates a sound viewer, but only with bars up.
         addEventHandler("onClientRender",root,clientRenderFunc)
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
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
end
end
addEventHandler ("onClientRender", root, renderWave)
function musicStopFunc()
</syntaxhighlight>
     if(handl) then
</section>
         stopSound(handl)
This example creates a sound viewer on the bottom right corner of the screen.
         handl = nil
<section name="Client" class="client" show="true">
        removeEventHandler("onClientRender",root,clientRenderFunc)
<syntaxhighlight lang="lua">
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
end
end
addEventHandler ("onClientRender", root, renderWave)
bindKey("9","down",musicStartFunc)
bindKey("0","down",musicStopFunc)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 65: Line 103:
==See Also==
==See Also==
{{Client_audio_functions}}
{{Client_audio_functions}}
[[AR:getSoundWaveData]]
 
[[hu: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