GetSoundFFTData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(OOP)
(More sensible example)
Line 28: Line 28:


==Example==  
==Example==  
This code creates vertical spectrum analyzer with thin lines in center of screen from top to bottom. [http://imageshack.com/a/img543/79/i4oz.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">
local handl = nil
soundHandler = playSound ( "sound.wav" )
local sx,_ = guiGetScreenSize()
 
local colors = { tocolor(255,0,0),tocolor(0,255,0) }
function onSoundPlayRender ( )
     if ( soundHandler ) then
function clientRenderFunc()
         local soundFFT = getSoundFFTData ( soundHandler, 2048, 257 )
     if(handl) then
if ( soundFFT ) then
dxDrawRectangle(sx/2,0,1,256,tocolor(255,255,255,127))
            for i = 1, 256 do
         local bt = getSoundFFTData(handl,2048,257)
                dxDrawRectangle ( 0, i - 1, 1, math.sqrt ( soundFFT[i] ) * 256 )
if(not bt) then return end
             end
        for i=1,256 do
            bt[i] = math.sqrt(bt[i])*256 --scale it (sqrt to make low values more visible)
             dxDrawRectangle(sx/2-bt[i]/2,i-1,bt[i],1)
         end
         end
     end
     end
end
end
 
addEventHandler ( "onClientRender", getRootElement(), onSoundPlayRender )
function clientSoundStopFunc(_)
    if(source == handl) then
removeEventHandler("onClientRender",root,clientRenderFunc)
removeEventHandler("onClientSoundStopped",root,clientSoundStopFunc)
handl = nil
    end
end
function musicStartFunc()
    if(not handl) then
        handl = playSound('nya.mp3')
        addEventHandler("onClientRender",root,clientRenderFunc)
addEventHandler("onClientSoundStopped",root,clientSoundStopFunc)
    end
end
function musicStopFunc()
    if(handl) then
        stopSound(handl)
        handl = nil
        removeEventHandler("onClientRender",root,clientRenderFunc)
removeEventHandler("onClientSoundStopped",root,clientSoundStopFunc)
    end
end
bindKey("9","down",musicStartFunc)
bindKey("0","down",musicStopFunc)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 20:22, 9 July 2018

This function gets the fast fourier transform data for an audio stream which is a table of floats representing the current audio frame. This allows things like visualisations.

a fast fourier transform generates a table of all the frequencies of the current audio frame which starts at the bass end of the spectrum to mids to highs in that order

Should you have any problems there is an example resource located on the resource svn here: Visualiser

just type "startmusic mystreamurl" in your console and it will play on the cinema billboard near A51 If the element is a player, this function will use the players voice.

Syntax

table getSoundFFTData ( element sound, int iSamples [, int iBands = 0 ] )

OOP Syntax Help! I don't understand this!

Method: sound:getFFTData(...)


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.

Optional Arguments

  • iBands: post processing option allows you to split the samples into the desired amount of bands or bars so if you only need 5 bars this saves a lot of cpu power compared to trying to do it in Lua.

Returns

Returns a table of iSamples/2 (or iBands-1 if iBands is used) floats representing the current audio frame. Returns false if the sound is not playing yet or hasn't buffered in the case of streams.

Example

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

function onSoundPlayRender ( )
    if ( soundHandler ) then
        local soundFFT = getSoundFFTData ( soundHandler, 2048, 257 )
	if ( soundFFT ) then
            for i = 1, 256 do
                dxDrawRectangle ( 0, i - 1, 1, math.sqrt ( soundFFT[i] ) * 256 )
            end
        end
    end
end
addEventHandler ( "onClientRender", getRootElement(), onSoundPlayRender )

Changelog

Version Description
1.3.2 Added player element to use a players voice

See Also