Old 03-28-2020, 08:54 PM   #1
LarrySeyer
Human being with feelings
 
LarrySeyer's Avatar
 
Join Date: Sep 2006
Location: Bastrop, Tx
Posts: 169
Default New Script: Change MIDI channel on Selected Items

I needed this functionality and couldn't find exactly what I was looking for, so I made this and am sharing it with everyone here.

Usage:

Select as many media MIDI items in your arrangement window across as many tracks as you want, and this function will change the selected item's MIDI note channels to the MIDI channel of your choosing.

Code:
-- LS Set MIDI Notes To Channel
-- @description Set all selected media item's MIDI notes to chosen MIDI channel
-- @version 0.1
-- @author Larry Seyer -- http://LarrySeyer.com
-- Free to use and share

midi_channel = 0 -- MIDI channels are ZERO based... 

-- this function does the actual changing of the MIDI channel
function change_the_channel(this_item, midi_channel)
	
	-- count the takes for this item
	local item_take_count = reaper.CountTakes(this_item)
	
	-- if we have at least one take then do some work
	if item_take_count > 0 then

		-- loop through takes
		for t = 0, item_take_count-1 do
			-- loop through takes
			local this_take = reaper.GetTake(this_item, t)
			
			-- is this take a MIDI take?
			local is_MIDI = reaper.TakeIsMIDI(this_take)
			
			-- only work on MIDI takes
			if is_MIDI then
				-- if it is MIDI, then change the channel
				
				-- count the notes in this take
				local retval, notecnt, ccevtcnt, textsyxevtcnt = reaper.MIDI_CountEvts(this_take)
				
				-- if there are notes in this take, work on them
				if notecnt > 0 then
					
					-- loop through each event
					for n = 0, notecnt-1 do
						-- set this note or cc to be the channel we want
						reaper.MIDI_SetNote(this_take, n, _, _, _ , _ , midi_channel, _ , _ , _)
						reaper.MIDI_SetCC(this_take, n, _, _, _ , _ , midi_channel, _ , _ , _)
					end
					
				end
				
			end
			
		end
		
	end
	
end


-- this function loops through our media items
-- and the calls the function that changes the MIDI channel on that media item
function loop_through_items(midi_channel)
	
	-- count the number of items in the project
	local items_count = reaper.CountMediaItems(0)

	-- if there are items 
	if items_count > 0 then
		
		-- loop through the items
		for i = 0, items_count-1 do
			
			-- get this media item
			local this_item = reaper.GetMediaItem(0, i)
			
			-- is this item selected?
			local is_selected = reaper.IsMediaItemSelected(this_item)

			-- if it is, then change the channel to 1
			if is_selected then
				change_the_channel(this_item, midi_channel)
			end

		end -- for i = 0, item_count-1
	
	end -- if item_count > 0 then

end


function main(midi_channel)
	-- pass the selected MIDI channel to the function that loops through the media items
	loop_through_items(midi_channel)
end --  Main()


-- Get the selected media items count
selected_media_items_count = reaper.CountSelectedMediaItems(0)

-- if there are media items selected then do something
if selected_media_items_count > 0 then
	
	retval_inputs, midi_channel = reaper.GetUserInputs( "Change Media Item's Channel: ", 1, "Channel (1-15): ", midi_channel + 1 )

	-- if they entered a MIDI channel then do something
	if retval_inputs then
	
		reaper.Undo_BeginBlock() 
		reaper.PreventUIRefresh(1)
		
		-- user enters ONE based MIDI channel, but our function wants to see ZERO based so...
		midi_channel = midi_channel - 1
		
		-- clamp our midi channel to be between 0-15
		if midi_channel > 15 then midi_channel = 15 end
		if midi_channel < 0 then midi_channel = 0 end
		
		-- do our main function sending it the selected MIDI channel
		main(midi_channel) --
	
		reaper.TrackList_AdjustWindows(true) 
		reaper.UpdateArrange()
	
		reaper.PreventUIRefresh(-1)
		reaper.Undo_EndBlock("LS Set MIDI Notes To Channel", -1) 
	
	end	
	
end
Edit:

I modified this script to include CC's on 2020-03-29... so you may need to re-download this if you downloaded the script before that date.
Attached Files
File Type: lua LS Set MIDI Notes To Channel.lua (3.2 KB, 82 views)

Last edited by LarrySeyer; 03-29-2020 at 11:39 AM. Reason: Fixed a CC bug
LarrySeyer is offline   Reply With Quote
Old 03-29-2020, 02:50 AM   #2
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
Default

Nice one.

There is also a widget in mpl_InteractiveToobar



It uses newer MIDI API, so it works faster if you change channel for lot of MIDI events.
mpl is offline   Reply With Quote
Old 03-29-2020, 06:25 AM   #3
LarrySeyer
Human being with feelings
 
LarrySeyer's Avatar
 
Join Date: Sep 2006
Location: Bastrop, Tx
Posts: 169
Default

Quote:
Originally Posted by mpl View Post
Nice one.

There is also a widget in mpl_InteractiveToobar

It uses newer MIDI API, so it works faster if you change channel for lot of MIDI events.
Thank you!

I’m curious. Where can I find this newer MIDI API you speak of?

Re: MPL.

I use your library. It’s great!

What I needed, however, was something that worked in the arrangement window directly on selected items that did not require me to open a MIDI editor window.

In my personalized version (which is different from this one) I don’t ask for the channel number. It simply changes all selected media items to MIDI channel 1 since that is all I ever need.

Thank you again!

L
LarrySeyer is offline   Reply With Quote
Old 03-29-2020, 08:36 AM   #4
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
Default

reaper.MIDI_SetAllEvts( take, buf )

You`d better to look at js_... scripts in ReaPack (they wriiten by Julian Sader) for well commented examples.
mpl is offline   Reply With Quote
Old 03-29-2020, 08:45 AM   #5
LarrySeyer
Human being with feelings
 
LarrySeyer's Avatar
 
Join Date: Sep 2006
Location: Bastrop, Tx
Posts: 169
Default

Quote:
Originally Posted by mpl View Post
reaper.MIDI_SetAllEvts( take, buf )

You`d better to look at js_... scripts in ReaPack (they wriiten by Julian Sader) for well commented examples.
OK. Thanks!

L
LarrySeyer 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:07 PM.


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