Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 11-07-2019, 11:21 AM   #1
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default What voodoo is this script using??? Xenakios/SWS: Normalize selected takes to dB valu

This script "Xenakios/SWS: Normalize selected takes to dB value..." runs so incredibly fast... it made me wonder if it was really working... except that it is.

I have 3 hour files that I wanted to (for example) normalize to -2dB. Usually, I would use a script I wrote using the API function reaper.NF_GetMediaItemMaxPeak( item ) that would analyze the whole file for the peak. But somehow that Xenkios script is blazing fast.

Anyone have an idea why it's so fast?

Thanks.
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Old 11-07-2019, 11:40 AM   #2
pandabot
Human being with feelings
 
pandabot's Avatar
 
Join Date: Oct 2018
Posts: 56
Default

I think it's because you're using lua and it uses C/C++
pandabot is online now   Reply With Quote
Old 11-07-2019, 12:03 PM   #3
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

Quote:
Originally Posted by pandabot View Post
I think it's because you're using lua and it uses C/C++
But the API function "reaper.NF_GetMediaItemMaxPeak( item ) is surely written in C++... no?

I mean... it "scans" the file (if that's what it's really doing) 10x faster than it would take to copy the file in the OS. It doesn't feel like a C++ thing. It almost feels like there may be Max Peak info embedded in Reaper's media system that it's looking at.
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Old 11-07-2019, 12:29 PM   #4
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,109
Default

It uses Reaper's native action "Item properties: Normalize items" and then adds/substracts to reach the target value.
I'd guess the native action already 'knows' the peak from the generated .reapeak files and therefore doesn't have to scan the whole file.

Here's the code:
https://github.com/reaper-oss/sws/bl...pp#L1371-L1397
nofish is offline   Reply With Quote
Old 11-07-2019, 12:46 PM   #5
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

Quote:
Originally Posted by nofish View Post
It uses Reaper's native action "Item properties: Normalize items" and then adds/substracts to reach the target value.
I'd guess the native action already 'knows' the peak from the generated .reapeak files and therefore doesn't have to scan the whole file.

Here's the code:
https://github.com/reaper-oss/sws/bl...pp#L1371-L1397
Again... nofish to the rescue!!

Thanks so much. Yeah... it totally felt like it was looking up something rather than scanning the file Brilliant. Thanks for confirming my hunch!
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Old 11-07-2019, 12:54 PM   #6
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,109
Default

You're welcome.

Btw. you may know anyway, what's being done there could also be rewritten in a Lua script for your task (instead of using the 'slow' NF_GetMediaItemMaxPeak function).
nofish is offline   Reply With Quote
Old 11-07-2019, 12:59 PM   #7
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

Quote:
Originally Posted by nofish View Post
You're welcome.

Btw. you may know anyway, what's being done there could also be rewritten in a Lua script for your task (instead of using the 'slow' NF_GetMediaItemMaxPeak function).
Yeah... I was thinking of that... but my poor simpleton brain didn't catch the relevant line of code in the reference you posted.
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Old 11-07-2019, 01:49 PM   #8
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,109
Default

something like this (to normalize first selected item's active take to TARGET_PEAK value):

Code:
-- from somewhere in the forum..
local LN10_OVER_TWENTY = 0.11512925464970228420089957273422
function DB2VAL(x) return math.exp(x*LN10_OVER_TWENTY) end

function VAL2DB(x)
  if x < 0.0000000298023223876953125 then
    return -150
  else
    return math.max(-150, math.log(x)* 8.6858896380650365530225783783321); 
  end
end

---------------------------------------------------
TARGET_PEAK = -2
---------------------------------------------------
item = reaper.GetSelectedMediaItem(0, 0)
if item == nil then return end
reaper.Undo_BeginBlock()
reaper.Main_OnCommand(40108,0) -- Normalize items

take = reaper.GetActiveTake(item)
-- take volume is negative if take polarity is flipped
curgain = math.abs(reaper.GetMediaItemTakeInfo_Value(take, "D_VOL"))
newdb = VAL2DB(curgain)+TARGET_PEAK

if reaper.GetMediaItemTakeInfo_Value(take, "D_VOL") < 0 then
 newgain = -DB2VAL(newdb)
else
  newgain = DB2VAL(newdb)
end
reaper.SetMediaItemTakeInfo_Value(take, "D_VOL", newgain)
reaper.Undo_EndBlock("Normalize to db", 0)

Last edited by nofish; 11-07-2019 at 01:57 PM.
nofish is offline   Reply With Quote
Old 11-07-2019, 06:07 PM   #9
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

OK... I get it now!

nofish... you surely know all this... but just in case it might help someone else....

The command in the script: reaper.Main_OnCommand(40108,0) is actually this script: Item properties: Normalize items

Item properties: Normalize items is really fast. So, you can use it to get the Max peak by seeing how much it raises the volume of the item to normalize it to 0dB. Then inverse the amount and you know what the Max Peak was.

Great to know! Thanks.
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Old 11-07-2019, 06:58 PM   #10
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,954
Default

edit : nevermind.
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is offline   Reply With Quote
Old 11-08-2019, 10:07 AM   #11
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 1,603
Default

Quote:
Originally Posted by Thonex View Post
"it "scans" the file (if that's what it's really doing) 10x faster than it would take to copy the file in the OS
Just for completeness: if it only scans, it only reads the file, if you copy it, you read AND write the file and writing is always way slower than reading, let alone reading and writing combined.
So copying would always be slower, unless you use extremly bad reading-file-code.

@nofish
Would using the peakfile an option for your functions too, assuming the mediafile has one? Just to speed up things? Or wouldn't it be any better than just using Reaper's native actions?
__________________
Ultraschall-API - a Lua-functions-library4Reaper: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs4Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine is offline   Reply With Quote
Old 11-08-2019, 10:31 AM   #12
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

Quote:
Originally Posted by mespotine View Post
Just for completeness: if it only scans, it only reads the file, if you copy it, you read AND write the file and writing is always way slower than reading, let alone reading and writing combined.
So copying would always be slower, unless you use extremly bad reading-file-code.

@nofish
Would using the peakfile an option for your functions too, assuming the mediafile has one? Just to speed up things? Or wouldn't it be any better than just using Reaper's native actions?
Thanks mespotine.

I wrote another script using the "Item properties: Normalize items" trick Xenakios did and it's very fast. ButI was chasing my tail for a while due to take and item differences. I settled on a solution, but noticed you have to zero out active takes is you want to get back to where you were. This whole active take vs item can be a PITA LOL
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Old 11-08-2019, 11:57 AM   #13
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

Following up on this, is there some hidden command to get item properties that would give Max Peak and similar? As opposed to using the "Item properties: Normalize items" and then doing a bunch of maths and then resetting the takes.

All I found was this... and the PCM source stuff that is time consuming IIRC:
retval, section, start, length, fade, reverse = reaper.BR_GetMediaSourceProperties( take )

Any ideas?
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB

Last edited by Thonex; 11-08-2019 at 06:53 PM.
Thonex is offline   Reply With Quote
Old 11-09-2019, 10:57 AM   #14
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,109
Default

@Thonex

I haven't looked into it but I think we could use GetMediaItemTake_Peaks() for this.

In this script one line says:

Code:
Peaks[p+1] = buf[i]             -- max peak
I think X-Raym and spk77 have recently used this for a waveform display so they may know better how it works.

@mespotine
Yeah would be nice (and probably considerably faster) to use this for NF_GetMediaItemMaxPeak() instead of file scanning but as said haven't looked into this (I've just exposed the SWS action 'SWS: Analyze and display item peak and RMS (entire item)' to ReaScript which means all the hard work was done already. :P).

Last edited by nofish; 11-09-2019 at 02:11 PM. Reason: low battery
nofish is offline   Reply With Quote
Old Yesterday, 10:36 AM   #15
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 774
Default

Quote:
Originally Posted by nofish View Post
@Thonex

I haven't looked into it but I think we could use GetMediaItemTake_Peaks() for this.

In this script one line says:

Code:
Peaks[p+1] = buf[i]             -- max peak
I think X-Raym and spk77 have recently used this for a waveform display so they may know better how it works.
Thanks again nofish!

Cheers!
__________________
Cheers,
Andrew K
v5.983+dev0922 - September 22 2019 • Mac 10.12.+ • i7 Quad 2.9GHz • 24GB
Thonex is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.