Old 08-01-2015, 07:08 AM   #1
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 2,650
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() 
__________________
SoundCloud | MPL Scripts discussion | ReaPack | Donate

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: 2,650
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).
__________________
SoundCloud | MPL Scripts discussion | ReaPack | Donate
mpl is offline   Reply With Quote
Old 03-28-2020, 11:41 AM   #4
alex_
Human being with feelings
 
Join Date: Mar 2015
Posts: 13
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
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 05:29 AM.


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