View Single Post
Old 01-07-2016, 03:47 PM   #18
swiiscompos
Human being with feelings
 
swiiscompos's Avatar
 
Join Date: Mar 2011
Location: London
Posts: 1,211
Default

Finished! I commented only what concerns you and put it at the top.

Code:
--[[
Compound meter mode:

if time_sig % 3 == 0 and time_sig_den == 8 then
    display tick box with text "Compound Meter". If ticked it will set compound_meter to 1 and enable the compound meter mode.
else
    compound_meter = 0
end
------------
Text output:
if a == 0 or a == 1 and inc_bar == 0 then
    print (n_bars_norm.." x "..time_sig.."/"..time_sig_den)
elseif b == 0 then
    print ("1 x "..inc_bar.."/"..time_sig_den.." + "..n_bars_norm.."x"..time_sig.."/"..time_sig_den)
elseif b == 1 then
    print (n_bars_norm.."x"..time_sig.."/"..time_sig_den.." + 1 x "..inc_bar.."/"..time_sig_den)

    
Thanks for your help!
]]

function init()
    t_sec = end_time-start_time  
    a = 0 -- Tick box. Allows creation of shorter bar. 1 is enabled, 0 disabled. Default is 0. When unticked, added_beats should be set to 0. 
    b = 0 -- Switch to set shorter bar position. 0 means short bar is at the beginning, 1 at the end
    compound_meter = 0 --see top.
    t_tempo = 120.00 --text input, target tempo. When modified, added_beats should be set to 0
    time_sig = 4 -- text input, time signature. Force intergers. Default is 4
    time_sig_den = 4 -- text input, time signature denominator. Default is 4
end

function addRemoveBeats()
    added_beats = 0 -- 2 buttons. 1 to add one beat, the other one to remove one beat
    if added_beats ~= 0 then
        total_beats = inc_bar + added_beats
        if total_beats >= 0 then
            inc_bar = total_beats % time_sig
            if inc_bar ~= 0 then
                n_bars_norm = n_bars_norm + math.floor(total_beats/time_sig)
            else
                n_bars_norm = n_bars_norm + total_beats/time_sig
            end
        elseif total_beats < 0 then
            inc_bar = (total_beats % time_sig) 
            if inc_bar ~= 0 then
                n_bars_norm = n_bars_norm - 1 + math.floor(total_beats/time_sig)
            else
                n_bars_norm = n_bars_norm + total_beats/time_sig
            end
        end
    end
end

function maths1()
    time_sig_den_b = 4 / time_sig_den
    time_sig_b = time_sig * time_sig_den_b
    t_min = t_sec/60
    if  compound_meter == 1 then
        t_tempo = t_tempo * 3 / 2
    end
    
    q = math.floor(t_tempo * t_min / time_sig_b)
    r = (t_tempo * t_min - q * time_sig_b) 
    inc_bar = math.floor(r / time_sig_den_b + 0.5)
    inc_bar_b = inc_bar * time_sig_den_b
    
    if a == 0 then   
        if r<(time_sig/2) then
            n_bars_norm = q
            else 
            n_bars_norm = q + 1
        end
        else
            n_bars_norm = q
    end

end

function maths2()
    if a == 0 then   
            tempo = n_bars_norm * time_sig_b / t_min
        else
            tempo = (n_bars_norm * time_sig_b + inc_bar_b) / t_min
    end
end

function deleteExistingTempoMarkers()
    delete_check = true
    while (delete_check == true)
    do
        exist_tempo_marker = reaper.FindTempoTimeSigMarker(0, end_time-1e-5)
        local retval, time_position, measureposOut, beatposOut, bpmOut, timesig_numOut, timesig_denomOut, lineartempoOut = reaper.GetTempoTimeSigMarker(0, exist_tempo_marker)
        if time_position >= start_time then
            delete_check = reaper.DeleteTempoTimeSigMarker(0, exist_tempo_marker)
        else
            delete_check = false
        end
    end
end

function insertNewTempoMarkersComplete()
    if a == 1 then
        if b == 0 then
            tempo_marker_pos = start_time + 60 / tempo*inc_bar_b
        else
            tempo_marker_pos = start_time
        end
    else
        tempo_marker_pos = start_time 
    end
    reaper.AddTempoTimeSigMarker(0, tempo_marker_pos, tempo, time_sig, time_sig_den, false)
end

function insertNewTempoMarkerShorter()
    if b == 0 then
        short_tempo_marker_pos = start_time
    else
        short_tempo_marker_pos = start_time + n_bars_norm*time_sig_b*60/tempo
    end
    reaper.AddTempoTimeSigMarker(0, short_tempo_marker_pos, tempo, inc_bar, time_sig_den, false)
end

function main()
--- GET TIME SELECTION EDGES
    start_time, end_time = reaper.GetSet_LoopTimeRange(false, false, 0, 0, false)

--- IF TIME SELECTION
    if start_time ~= end_time then
        init()
        maths1()
        deleteExistingTempoMarkers()
        if a == 1 then
            addRemoveBeats()
            maths2()
            if b == 0 then
                insertNewTempoMarkerShorter()
                insertNewTempoMarkersComplete()
            else
                insertNewTempoMarkersComplete()
                insertNewTempoMarkerShorter()
            end
        else
            maths2()
            insertNewTempoMarkersComplete()
        end
        reaper.UpdateTimeline()
    end
end

--RUN
main()
swiiscompos is offline   Reply With Quote