Old 11-18-2022, 05:21 AM   #1
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default Script: Thin MIDI CC Events



This small suite of scripts will thin MIDI data (CCs, aftertouch, program change, pitchbend, polyAT). It currently supports selected points or a time selection, but will probably be extended to permit mouse-over-lane and a few other selectors.

The script uses the Ramer-Douglas-Peucker algorithm to perform data reduction. Due to REAPER limitations, it supports square and linear curves (other curves may follow). Square curves will cause points to be added (before reduction) in order to improve results.

Anyway, give it a try (from ReaPack, link below) and let me know how it works for you.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 11-18-2022, 05:52 AM   #2
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,955
Default

Quote:
The script uses the Ramer-Douglas-Peucker algorithm

I like this algorithm very much, it is in several of my premium scripts including one for track and takes envelopes smoothing.

I see you found the port I contributed to :P


Good to have it for CC now !
X-Raym is offline   Reply With Quote
Old 11-18-2022, 05:56 AM   #3
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by X-Raym View Post
I see you found the port I contributed to :P
Nice! Then a big bouquet of flowers for helping with the backbone here (btw I am using your addition, it was very handy):

Code:
         *
     *  *r*  *
  * *a* ^Y^ *i* *
 *m*^Y^*^\^*^Y^*s*
 ^Y^*\*e*/*l*/*^Y^
 *\*t*|Y^\^Y|*l*/*
*s*|Y^\\^/^//^Y|*a*
^Y^\\_^\\\//^_//^Y^
^\_^\_\_\//_/_/^_/^
 ^^\_^\_\\/_/^_/^^
   ^^\_ \// _/^^
       \_\_/
        /|\
       /\\/\
__________________
ReaPack Repository: right-click and copy index URL

Last edited by sockmonkey72; 11-18-2022 at 06:28 AM.
sockmonkey72 is online now   Reply With Quote
Old 11-18-2022, 07:00 AM   #4
daniellumertz
Human being with feelings
 
daniellumertz's Avatar
 
Join Date: Dec 2017
Location: Brazil
Posts: 2,023
Default

oh that cool, thanks sockmonkey!
daniellumertz is offline   Reply With Quote
Old 11-18-2022, 10:41 AM   #5
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,955
Default

Thanks for the ASCII art ��
X-Raym is offline   Reply With Quote
Old 11-19-2022, 01:41 PM   #6
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 523
Default

Looks useful. Thx!
reapero is offline   Reply With Quote
Old 11-20-2022, 01:55 AM   #7
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

I've pushed a few updates in the last couple of days, but 1.5.1 is up now:

- there is a 'main' context script to thin CCs in selected items (probably a Razor Edit script will follow)
- there are two MIDI Editor context scripts for time selections. The 'old' function ("sockmonkey72_Thin_CCs_In_Time_Selection") will reduce CCs on all channels regardless of ME filter status or visibility of lanes. The new function ("sockmonkey72_Thin_CCs_Visible_In_Time_Selection" ) will only reduce CCs for visible items in the time selection.
- (1.5.1) fix undo in a few places
__________________
ReaPack Repository: right-click and copy index URL

Last edited by sockmonkey72; 11-20-2022 at 06:37 AM.
sockmonkey72 is online now   Reply With Quote
Old 12-30-2023, 04:24 AM   #8
DavidBluecame
Human being with feelings
 
Join Date: May 2017
Posts: 3
Default

Hello.

I've just used this script with Reaper v7.07 and it is absolutely fantastic! Thank you so much!!

The only downside I've found so far is that the Pitch Bend reduction is not working well for me. I suspected the different scale in the pitch bend values compared with regular CC and I made the following change (just for my test) that helped me reduce the Pitch Bend correctly.

For your reference I changed the original "10" for CC to "100" for Pitch Bend and it seems to work for me. Of course it would be better make a change to do this automatically for pitch bend events.

ThinCCUtils.lua
----------------------------
function DoReduction(events)
local newevents = {}

local defaultReduction = "100" -- (Note David: using "100" for pitch bend)
----------------------------

In any case this is just great, thank you for this excellent script!!
DavidBluecame is offline   Reply With Quote
Old 12-30-2023, 05:36 AM   #9
DavidBluecame
Human being with feelings
 
Join Date: May 2017
Posts: 3
Default

FYI, I think this change could work:

------------------------------
function DoReduction(events)
local newevents = {}

local defaultReduction = "10" -- Note David: reverting to original value
if reaper.HasExtState("sockmonkey72_ThinCCs", "level") then
defaultReduction = reaper.GetExtState("sockmonkey72_ThinCCs", "level")
end
local reduction = tonumber(defaultReduction)

-- iterate, reduce points
for _, v in pairs(events) do
local status = v.status & 0xF0
local is3byte = status == 0xA0 or status == 0xB0
local is2byte = status == 0xC0 or status == 0xD0
local isPB = status == 0xE0

-- Note David: this is the change to use a larger reduction for pitch bend events:
if isPB then
reduction = reduction * 10
end
-----------------

See attached images showing before and after I run the script "Thin CC All" (with the Pitch Bend modification applied).

I hope it helps.
Attached Images
File Type: png Before Thin CC with Pitch Bend change.png (88.9 KB, 62 views)
File Type: png After Thin CC with Pitch Bend change.png (80.9 KB, 50 views)
DavidBluecame is offline   Reply With Quote
Old 12-30-2023, 11:35 AM   #10
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Thanks for your notes! I'll take a look at pitch events in the next day or so: I can imagine that the pitch-snapping changes made for v7 might also require some attention for this script.

I'm glad it's otherwise working for you, though -- I'll drop a note when I've had a chance to look at the script again.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 12-31-2023, 01:28 AM   #11
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

OK, pushed v1.7 to ReaPack -- you can now specify a pitch bend scale factor in the setup script. Default is 10.

When switching on 'snap', existing events aren't affected, so I'm not doing anything special in that case, either.

Enjoy, happy new year!

EDIT: if I had spent 2 more seconds, I would realize that this needs some better handling for 'snapped' events. Anyway, there's always a v1.7.1 in the future.

EDIT2: v1.7.1 pushed, hacks a fix for snapped PB lanes, which hopefully works as intended for most cases
__________________
ReaPack Repository: right-click and copy index URL

Last edited by sockmonkey72; 12-31-2023 at 02:44 AM.
sockmonkey72 is online now   Reply With Quote
Old 01-02-2024, 01:51 PM   #12
Coachz
Human being with feelings
 
Coachz's Avatar
 
Join Date: Oct 2010
Location: Charleston, SC
Posts: 12,871
Default

What is Reduction Level and Pitch Bend Scale please ?

Is there any way to make it so each time I run it, I can get further reduction ? Thanks for the cool script.
Coachz is online now   Reply With Quote
Old 01-02-2024, 02:03 PM   #13
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by Coachz View Post
What is Reduction Level and Pitch Bend Scale please ?

Is there any way to make it so each time I run it, I can get further reduction ? Thanks for the cool script.
Reduction Level is the strength of the thinning algorithm. Higher values mean more thinning.

Pitch Bend Scale is what we were talking about above: pitch bend has 14-bit values, rather than the typical 7-bit values, so you might want additional thinning. So it's a scale factor for the Reduction Level to increase it when thinning pitch bend messages.

At the moment, there's no way to do what you're asking for, at least not in any controllable way... I could probably improve the Setup script to make it easier to change levels and run the script over and over again, though.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 01-02-2024, 02:29 PM   #14
Coachz
Human being with feelings
 
Coachz's Avatar
 
Join Date: Oct 2010
Location: Charleston, SC
Posts: 12,871
Default

Thanks for the great reply. That would be a great addition I think.

Coachz is online now   Reply With Quote
Old 02-21-2024, 01:46 PM   #15
timzibrat
Human being with feelings
 
Join Date: Jan 2019
Location: Ljubljana, Slovenia
Posts: 53
Default

Great script, thanks.

Is it just me, or does the script work only in the midi editor? Any other type of way (inline midi editor, arrange view) etc. doesn't work. For example the action thin cc in selected items does nothing. Running Reaper 7.11.
timzibrat is offline   Reply With Quote
Old 02-22-2024, 01:37 AM   #16
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by timzibrat View Post
Great script, thanks.

Is it just me, or does the script work only in the midi editor? Any other type of way (inline midi editor, arrange view) etc. doesn't work. For example the action thin cc in selected items does nothing. Running Reaper 7.11.
Thanks for the report, fixed update for Thin CCs in Selected Items is now available.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 03-02-2024, 09:48 AM   #17
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,609
Default

Here's a version that operates on last clicked cc lanes (i needed it as i wanted to put the action in the context menu). Thought i'd put it here so you can maybe add it to the pack.

Thanks a lot for this, it's very good to have. Virtual hug.

EDIT: Realized i'm stupid, i could have chained "select all cc in last clicked lane" and "thin selected cc". Big d'oh moment. Please ignore me

Code:
--[[
   * Author: sockmonkey72
   * Licence: MIT
   * Version: 1.00
   * NoIndex: true
--]]

local reaper = reaper

package.path = debug.getinfo(1, "S").source:match [[^@?(.*[\/])[^\/]-$]] .. "?.lua;" -- GET DIRECTORY FOR REQUIRE
require "ThinCCs/ThinCCUtils"

local me = reaper.MIDIEditor_GetActive()
local take = reaper.MIDIEditor_GetTake(me)
if take then

  local ccLane = reaper.MIDIEditor_GetSetting_int(me, "last_clicked_cc_lane")

  local matchLane = 0
  if ccLane >= 0 and ccLane <= 127 then matchLane = 0xB0 -- CC
  elseif ccLane == 0x201 then matchLane = 0xE0 -- pitch bend
  elseif ccLane == 0x202 then matchLane = 0xC0 -- program change
  elseif ccLane == 0x203 then matchLane = 0xD0 -- channel pressure
  end
  if matchLane == 0 then return end

  local eventlist = GenerateEventListFromFilteredEvents(take, function (ev) return ev.chanmsg == matchLane end)

  local hasEvents = PrepareList(eventlist)
  if not hasEvents then return end

  reaper.Undo_BeginBlock2(0)
  PerformReduction(eventlist, take)
  reaper.MarkTrackItemsDirty(reaper.GetMediaItemTake_Track(take), reaper.GetMediaItemTake_Item(take))
  reaper.Undo_EndBlock2(0, "Thin CCs in last clicked lane", -1)
end
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 03-04-2024, 02:06 AM   #18
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by smandrap View Post
Here's a version that operates on last clicked cc lanes (i needed it as i wanted to put the action in the context menu). Thought i'd put it here so you can maybe add it to the pack.

Thanks a lot for this, it's very good to have. Virtual hug.

EDIT: Realized i'm stupid, i could have chained "select all cc in last clicked lane" and "thin selected cc". Big d'oh moment. Please ignore me

Code:
--[[
   * Author: sockmonkey72
   * Licence: MIT
   * Version: 1.00
   * NoIndex: true
--]]

local reaper = reaper

package.path = debug.getinfo(1, "S").source:match [[^@?(.*[\/])[^\/]-$]] .. "?.lua;" -- GET DIRECTORY FOR REQUIRE
require "ThinCCs/ThinCCUtils"

local me = reaper.MIDIEditor_GetActive()
local take = reaper.MIDIEditor_GetTake(me)
if take then

  local ccLane = reaper.MIDIEditor_GetSetting_int(me, "last_clicked_cc_lane")

  local matchLane = 0
  if ccLane >= 0 and ccLane <= 127 then matchLane = 0xB0 -- CC
  elseif ccLane == 0x201 then matchLane = 0xE0 -- pitch bend
  elseif ccLane == 0x202 then matchLane = 0xC0 -- program change
  elseif ccLane == 0x203 then matchLane = 0xD0 -- channel pressure
  end
  if matchLane == 0 then return end

  local eventlist = GenerateEventListFromFilteredEvents(take, function (ev) return ev.chanmsg == matchLane end)

  local hasEvents = PrepareList(eventlist)
  if not hasEvents then return end

  reaper.Undo_BeginBlock2(0)
  PerformReduction(eventlist, take)
  reaper.MarkTrackItemsDirty(reaper.GetMediaItemTake_Track(take), reaper.GetMediaItemTake_Item(take))
  reaper.Undo_EndBlock2(0, "Thin CCs in last clicked lane", -1)
end
Thanks smandrap!

Looks like this is going to thin _all_ CC lanes, not just the one you clicked on. It will properly distinguish between different event types, but all CCs (0-127 [119]) are treated as a single lane.

Happy to fix it, assuming that it was unintended. Thanks so much for the contribution.

Just saw your postscript -- if having an all-in-one script makes workflow easier, no objections here...

Something like this works great:

Code:
  local eventlist = GenerateEventListFromFilteredEvents(take, function (ev)
    if ev.chanmsg == matchLane then
        if matchLane == 0xB0 then
            return ev.msg2 == ccLane
        else return true
        end
    end
  end)
__________________
ReaPack Repository: right-click and copy index URL

Last edited by sockmonkey72; 03-04-2024 at 02:16 AM.
sockmonkey72 is online now   Reply With Quote
Old 03-04-2024, 02:29 AM   #19
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,609
Default

Quote:
assuming that it was unintended
Yeas it was... it has been a total brain fart time lol. Thanks for the fix

I second this:

Quote:
Originally Posted by Coachz View Post
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 03-04-2024, 02:33 AM   #20
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by smandrap View Post
Yeas it was... it has been a total brain fart time lol. Thanks for the fix

I second this:
I've been doing this every day of my life for 25 years, and I still feel like that much of the time. Thanks again -- just pushed the update.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 03-04-2024, 02:55 AM   #21
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,609
Default

Keep it up, you rock
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 04-17-2024, 12:51 AM   #22
paaltio
Human being with feelings
 
Join Date: Aug 2011
Location: Los Angeles, CA
Posts: 314
Default

This is a great script, thanks very much for making it! I had my own custom solution for this but this has a nicer algorithm and it's faster, so kudos for that.

It seems I did find a slight bug with sockmonkey72_Thin_CCs_In_Selected_Items.lua though. I can't quite tell what's going on, but the most minimal example I can come up with right now is that it seems to get somehow confused when there's a CC64 pedal on/off event and a single CC1 event in the same item. The CC1 event (lower lane in the image) will get deleted, and an extra pedal on event (upper lane) will be created at the boundary:

paaltio is offline   Reply With Quote
Old 04-17-2024, 01:06 AM   #23
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by paaltio View Post
This is a great script, thanks very much for making it! I had my own custom solution for this but this has a nicer algorithm and it's faster, so kudos for that.

It seems I did find a slight bug with sockmonkey72_Thin_CCs_In_Selected_Items.lua though. I can't quite tell what's going on, but the most minimal example I can come up with right now is that it seems to get somehow confused when there's a CC64 pedal on/off event and a single CC1 event in the same item. The CC1 event (lower lane in the image) will get deleted, and an extra pedal on event (upper lane) will be created at the boundary:
Thanks, I'm glad it's useful! And thanks for the report, I'll try to figure out what's going on.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 04-17-2024, 02:11 PM   #24
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by sockmonkey72 View Post
Thanks, I'm glad it's useful! And thanks for the report, I'll try to figure out what's going on.
Pushed a fix for the deletion of the CC1 lane. The addition of the point is deliberate (it's necessary for the case that you've enabled curve shapes in the CC64 lane). Thanks again.
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 04-17-2024, 11:12 PM   #25
paaltio
Human being with feelings
 
Join Date: Aug 2011
Location: Los Angeles, CA
Posts: 314
Default

Quote:
Originally Posted by sockmonkey72 View Post
Pushed a fix for the deletion of the CC1 lane. The addition of the point is deliberate (it's necessary for the case that you've enabled curve shapes in the CC64 lane). Thanks again.
Ah I did indeed have "Allow CC shape in sustain lane" on for some reason, which makes no sense for my use case so I did turn it off. For some reason I still get the extra event added at the boundary though. Is there something else that could be causing that?

The deletion fix seems to work great, thanks!
paaltio is offline   Reply With Quote
Old 04-17-2024, 11:16 PM   #26
sockmonkey72
Human being with feelings
 
sockmonkey72's Avatar
 
Join Date: Sep 2021
Location: Berlin
Posts: 1,994
Default

Quote:
Originally Posted by paaltio View Post
Ah I did indeed have "Allow CC shape in sustain lane" on for some reason, which makes no sense for my use case so I did turn it off. For some reason I still get the extra event added at the boundary though. Is there something else that could be causing that?

The deletion fix seems to work great, thanks!
I don't check that value in the script, but that's why an extra event is generated. Is the extra event causing a problem? If so, I could special case that lane, but I'd prefer not to!
__________________
ReaPack Repository: right-click and copy index URL
sockmonkey72 is online now   Reply With Quote
Old 04-18-2024, 01:34 AM   #27
paaltio
Human being with feelings
 
Join Date: Aug 2011
Location: Los Angeles, CA
Posts: 314
Default

Quote:
Originally Posted by sockmonkey72 View Post
I don't check that value in the script, but that's why an extra event is generated. Is the extra event causing a problem? If so, I could special case that lane, but I'd prefer not to!
Ah sorry I thought it was an option check you meant. I did have some issues editing busy sustain pedal lanes as I forgot to move the second on event at times, but I figured a workflow for my use case already so no worries from my part!
paaltio 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 12:24 PM.


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