Old 05-07-2020, 09:44 AM   #1
DavidOrd
Human being with feelings
 
Join Date: Jul 2019
Posts: 4
Default Midi reverse action lua part 1

I have a problem
A ‘Reverse MIDI’ action would be useful, so I’ve tried to write one
------
notSel = 0
local r = reaper; local function nothing() end; local function bla() r.defer(nothing) end
local take = r.MIDIEditor_GetTake(r.MIDIEditor_GetActive())
if not take then bla() return end
local _, notes = r.MIDI_CountEvts(take)
--get length of item; we need to know where the item we are in ends at
for i = 0, notes - 1 do
retval, sel, muted, startppq, endppq, chan, pitch, vel = r.MIDI_GetNote(take, i)
if sel then
--r.MIDI_InsertNote(take, sel, muted, startppq, endppq, chan, pitch + 24, vel)
EndsAt = endppq -- end of current note assuming we go through from begining to end
end
end
-- have found last note position?
r.Undo_BeginBlock() r.PreventUIRefresh(1)
--loop through notes - must be selected
for i = 0, notes do
local _,sel,muted,start_note,end_note,_,pitch = r.MIDI_GetNote(take, i)
if sel then
-- .MIDI_SetNote(take, i, notSel, nill, EndsAt - end_note, EndsAt - start_note, nil, pitch, nil, nil)
r.MIDI_SetNote(take, i, notSel, nill, EndsAt - end_note, EndsAt - start_note, nil, pitch, nil, nil)
end
end
r.PreventUIRefresh(-1) r.Undo_EndBlock('davidTest', -1)
--------
which sort of works.
Starting with (Fig_1)

And then running the script gets (Fig_2)

Showing that note 2 is not affected.
Then starting with (Fig_3)

And selecting the first 6 notes results in (Fig_4)

Which shows that only notes 1, 3,5 and 6 are affected – although they do go to the right place.
If I then just reverse 2 and 4 (Fig_5)

They reverse. Obviously I need to change the routine to use the start note position of the first note
Attached Images
File Type: jpg Fig_1.jpg (19.0 KB, 14 views)
File Type: jpg Fig_2.jpg (18.9 KB, 9 views)
File Type: jpg Fig_3.jpg (20.7 KB, 8 views)
File Type: jpg Fig_4.jpg (25.5 KB, 8 views)
File Type: jpg Fig_5.jpg (24.5 KB, 9 views)
DavidOrd is offline   Reply With Quote
Old 05-07-2020, 09:46 AM   #2
DavidOrd
Human being with feelings
 
Join Date: Jul 2019
Posts: 4
Default Midi reverse action lua part 2

Now with 7 notes selected (Fig_6)

Running it gives (Fig_7)

Again, 2 and 4 do not move.
If I was to pick the last 5 notes, they reverse, but are placed at the start of the item (need the start position to handle that) but correctly positioned!!!!!
Starting with (Fig_8)

and selecting all gives (Fig_9)

So 2 and 4 are a problem.
I took out the action part of the script and they are all still selected before it starts doing anything.
On a longer section (Fig_10)
Attached Images
File Type: jpg Fig_6.jpg (23.5 KB, 10 views)
File Type: jpg Fig_7.jpg (25.5 KB, 8 views)
File Type: jpg Fig_8.jpg (20.7 KB, 9 views)
File Type: jpg Fig_9.jpg (24.0 KB, 7 views)
File Type: jpg Fig_10.jpg (43.4 KB, 6 views)
DavidOrd is offline   Reply With Quote
Old 05-07-2020, 09:48 AM   #3
DavidOrd
Human being with feelings
 
Join Date: Jul 2019
Posts: 4
Default Midi reverse action lua part 3

it did this. (Fig_11)

Which I don’t understand. Why have the very long notes when no note lengths are being changed?

The other point of note is that although the changed notes are set to be not selected, the ones that don’t move are still selected. This tells me that they are not being processed at all

I got round the problem by ctrl-F2 and moving all the still selected notes down 2octaves, re-running the action (which worked perfectly!!!) then moving them to the right place and then changing the size of the very short note and the very long ones to the correct size (they were in the right starting place) then reselecting the moved notes and then moving them back up 2 octaves.

Amending the
r.MIDI_SetNote(take, i, notSel, nill, EndsAt - end_note, EndsAt - start_note, nil, pitch, nil, nil)
to
r.MIDI_SetNote(take, i, notSel, nill, (EndsAt + StartsAt) - end_note, (EndsAt + StartsAt) - start_note, nil, pitch, nil, nil)

Did fix the starting position impact, but still not all of the notes are selected and I don’t get why not all the notes are being processed.

Sorry about there being 3 parts but you can only upload 5 pictures on each one
Attached Images
File Type: jpg Fig_11.jpg (43.4 KB, 15 views)
DavidOrd is offline   Reply With Quote
Old 05-07-2020, 10:38 AM   #4
xpander
Human being with feelings
 
xpander's Avatar
 
Join Date: Jun 2007
Location: Terra incognita
Posts: 5,850
Default

I most probably can't answer your script questions. But a tip for bringing all these under the same topic: Copy your part 2 and part 3 posts as new replies into your first post.
xpander is offline   Reply With Quote
Old 05-11-2020, 05:29 AM   #5
DavidOrd
Human being with feelings
 
Join Date: Jul 2019
Posts: 4
Default Problem Solved

The answer was one of those things that is so obvious once you work it out.
The note number is not a property of the note but a property of the note list. Once you move a note it is now not in the note list in the place that it was.
The answer was to use a table containing the note properties, remove the notes to be moved, then insert new notes with the properties desired.
Simples!
DavidOrd 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 10:25 PM.


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