GetSoundLevelData: Difference between revisions

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


===Returns===
===Returns===
Returns a 2 values with Left, Right level data from sound (range is [0,32768]), ''false'' otherwise.
Returns a two ''integers'' in range from 0 to 32767.


==Example==
==Example==
This code creates vertical lines of right and left level in center of screen from top to bottom. [http://imageshack.com/a/img716/8689/4ud1.png] Key '''9''' - start, key '''0''' - stop.
<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 clientRenderFunc()
    if(handl) then
local ls,rs = getSoundLevelData(handl)
if(ls ~= false) then
            dxDrawRectangle(sx/2-10,0,10,128*(ls/32768),colors[1])
            dxDrawRectangle(sx/2+10,0,10,128*(rs/32768),colors[2])
end
    end
end


function clientSoundStopFunc(_)
function onSoundPlayRender ( )
     if(source == handl) then
     if ( soundHandler ) then
removeEventHandler("onClientRender",root,clientRenderFunc)
        local leftData, rightData = getSoundLevelData ( soundHandler )
removeEventHandler("onClientSoundStopped",root,clientSoundStopFunc)
if ( leftData ) then
handl = nil
            dxDrawRectangle ( 0, 0, 64, leftData / 32767 * 256, tocolor ( 255, 0, 0 ) )
    end
            dxDrawRectangle ( 64, 0, 64, rightData / 32767 * 256, tocolor ( 0, 0, 255 ) )
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
end
end
addEventHandler ( "onClientRender", getRootElement(), onSoundPlayRender )
bindKey("9","down",musicStartFunc)
bindKey("0","down",musicStopFunc)
</syntaxhighlight>
</syntaxhighlight>
</section>


==Requirements==
==Requirements==

Revision as of 17:47, 10 July 2018

This function gets the left/right level from a sound element. If the element is a player, this function will use the players voice.

Syntax

int, int getSoundLevelData ( element theSound )

OOP Syntax Help! I don't understand this!

Method: sound:getLevelData(...)


Required Arguments

  • theSound: the sound element which level data you want to return.

Returns

Returns a two integers in range from 0 to 32767.

Example

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

function onSoundPlayRender ( )
    if ( soundHandler ) then
        local leftData, rightData = getSoundLevelData ( soundHandler )
	if ( leftData ) then
            dxDrawRectangle ( 0, 0, 64, leftData / 32767 * 256, tocolor ( 255, 0, 0 ) )
            dxDrawRectangle ( 64, 0, 64, rightData / 32767 * 256, tocolor ( 0, 0, 255 ) )
        end
    end
end
addEventHandler ( "onClientRender", getRootElement(), onSoundPlayRender )

Requirements

This template will be deleted.

Changelog

Version Description
1.3.2 Added player element to use a players voice

See Also