GetSoundWaveData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Client function}}
{{Client function}}
{{Needs Example}}
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 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.
This allows things like visualisations.
Line 12: Line 11:
===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 128, 256, 512, 1024, 2048, 4096, 8192 and 16384 and determine how many samples to return from the currently playing audio
*'''iSamples:''' Allowed samples are 256, 512, 1024, 2048, 4096, 8192 and 16384.


===Returns===
===Returns===
Returns a table of floats representing the current audio frame waveform.
Returns a table of '''iSamples'''/2 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.
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- not done yet
local handl = nil
local sx,_ = guiGetScreenSize()
local colors = { tocolor(255,0,0),tocolor(0,255,0) }
function clientRenderFunc()
    if(handl) then
        local bt = getSoundWaveData(handl,512)
dxDrawRectangle(sx/2,0,1,256,tocolor(255,255,255,127))
        for i=2,128 do
    dxDrawLine(sx/2+64*bt[2*i-1],2*i-2,sx/2+64*bt[2*i+1],2*i,colors[1])
            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
function musicStartFunc()
    if(not handl) then
        handl = playSound('btr.mp3')
        addEventHandler("onClientRender",root,clientRenderFunc)
    end
end
function musicStopFunc()
    if(handl) then
        stopSound(handl)
        handl = nil
        removeEventHandler("onClientRender",root,clientRenderFunc)
    end
end
bindKey("9","down",musicStartFunc)
bindKey("0","down",musicStopFunc)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 32: Line 65:
==See Also==
==See Also==
{{Client_audio_functions}}
{{Client_audio_functions}}
[[Category:Needs Example]]
[[AR:getSoundWaveData]]
[[AR:getSoundWaveData]]

Revision as of 18:43, 14 December 2013

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 )

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/2 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 code creates waveform of sound in center of screen from top to bottom. [1] Key 9 - start, key 0 - stop.

Click to collapse [-]
Client
local handl = nil
local sx,_ = guiGetScreenSize()
local colors = { tocolor(255,0,0),tocolor(0,255,0) }
 
function clientRenderFunc()
    if(handl) then
        local bt = getSoundWaveData(handl,512)
	dxDrawRectangle(sx/2,0,1,256,tocolor(255,255,255,127))
        for i=2,128 do
	    dxDrawLine(sx/2+64*bt[2*i-1],2*i-2,sx/2+64*bt[2*i+1],2*i,colors[1])
            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
 
function musicStartFunc()
    if(not handl) then
        handl = playSound('btr.mp3')
        addEventHandler("onClientRender",root,clientRenderFunc)
    end
end
 
function musicStopFunc()
    if(handl) then
        stopSound(handl)
        handl = nil
        removeEventHandler("onClientRender",root,clientRenderFunc)
    end
end
 
bindKey("9","down",musicStartFunc)
bindKey("0","down",musicStopFunc)

Changelog

Version Description
1.3.2 Added player element to use a players voice

See Also