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

Reply
 
Thread Tools Display Modes
Old 08-01-2015, 07:08 AM   #1
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default Lua: Create Snap Offset at maximum peak in first second of take

Hi, guys.
I wrote a script to automatically create Snap Offset on maximum peak value on first second of take. Seem to be something wrong with audioaccessor (sometimes changing item selection does not affect to values, like REAPER leave last used accessor in the memory and didnt get new accessor). Sometimes it works, sometimes not, don`t know why. Can anyone confirm is it works or not? And if not, where I was wrong?

PHP Code:
item reaper.GetSelectedMediaItem(00)
take reaper.GetActiveTake(item)
source reaper.GetMediaItemTake_Source(take)

accessor reaper.CreateTakeAudioAccessor(take)
samplerate reaper.GetMediaSourceSampleRate(source)
channels reaper.GetMediaSourceNumChannels(source)
start_pos 0
samples 
samplerate
buf_size 
samples -- first 1 second
buffer 
reaper.new_array(buf_size*channels)

reaper.GetAudioAccessorSamples(accessorsampleratechannelsstart_possamplesbuffer)

sample_max 0
for 1buf_size do
 
sample math.abs(buffer[i]) 
 
sample_max math.max(samplesample_max)
 if 
sample_max ~= sample_max0 -- if max peak value is changed
    then max_peak_sample 
i/channels end
 sample_max0 
sample_max
end

snap_offset_position 
max_peak_sample samplerate
reaper
.DestroyAudioAccessor(accessor)
reaper.SetMediaItemInfo_Value(item"D_SNAPOFFSET"snap_offset_position)
reaper.UpdateArrange() 

Last edited by mpl; 08-01-2015 at 07:17 AM.
mpl is offline   Reply With Quote
Old 08-01-2015, 10:12 AM   #2
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Hi,
It seems to work here (if a take source has 1 channel).
spk77 is offline   Reply With Quote
Old 08-01-2015, 11:14 AM   #3
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

Strange... Sometimes buffer return extremely small values, even if i don't change code and just run script again.
I understood that samples going to buffer from same position one channel after another, so i put
PHP Code:
max_peak_sample i/channels 
.
But I have a feel something wrong here (or there is a deep bug in accessor api not reproducible simply).
mpl is offline   Reply With Quote
Old 03-28-2020, 11:41 AM   #4
alex_
Human being with feelings
 
Join Date: Mar 2015
Posts: 29
Default

Very useful script, thanks mpl!
Is it possible to set the snap offset at the peak of the entire file instead of the first second?

Also the script only sets the snap offset to the first of the selected files. It would be great to be able to do that for all the selected files at once.
alex_ is offline   Reply With Quote
Old 04-21-2020, 01:56 PM   #5
vala
Human being with feelings
 
Join Date: Mar 2013
Posts: 131
Default

Quote:
Originally Posted by alex_ View Post
Very useful script, thanks mpl!
Is it possible to set the snap offset at the peak of the entire file instead of the first second?

Also the script only sets the snap offset to the first of the selected files. It would be great to be able to do that for all the selected files at once.
I am interested as well in this behaviour, please!
vala is offline   Reply With Quote
Old 04-21-2020, 03:03 PM   #6
TabbyCat
Human being with feelings
 
TabbyCat's Avatar
 
Join Date: May 2019
Location: Los Angeles, CA
Posts: 161
Default

Quote:
Originally Posted by vala View Post
I am interested as well in this behaviour, please!
If you don't need the 1 sec time constraint, there are built-in Actions (via SWS) that will do this:

SWS: Move cursor to item peak sample
-then-
Item: Set snap offset to cursor

Combine those two into a Custom Action. I have one that I constantly use for this very application.

If you don't want the cursor to move, run these at the start/end of the Action, respectively, and it will appear that the cursor never moved:

SWS/BR: Save edit cursor position, slot 01
SWS/BR: Restore edit cursor position, slot 01
TabbyCat is offline   Reply With Quote
Old 04-21-2020, 03:44 PM   #7
vala
Human being with feelings
 
Join Date: Mar 2013
Posts: 131
Default

@TabbyCat:
oh yes, lovely! thank you!
vala is offline   Reply With Quote
Old 05-12-2020, 12:12 PM   #8
alex_
Human being with feelings
 
Join Date: Mar 2015
Posts: 29
Default

great, thanks
alex_ 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 03:55 PM.


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