_,_,_,_,_,_,val=reaper.get_action_context()
  if val>0 then
  local take=reaper.MIDIEditor_GetTake(reaper.MIDIEditor_GetActive())
  local _,CountEvts=reaper.MIDI_CountEvts(take)
  if CountEvts==0 then no_undo() return end;
  local NewList={}
  if CountEvts>0 then
    note_data={}
    for i=0, CountEvts+1 do
      note_data[i]={}
      _,note_data[i].sel,note_data[i].muted,note_data[i].start,note_data[i].ending,note_data[i].chan,note_data[i].pitch,note_data[i].vel= reaper.MIDI_GetNote(take,i)
    end
    for i=0, #note_data do
      if note_data[i].sel==true then
        NewList[i*2]={} --list for duplicate notes
        NewList[i*2+1]={}
        reaper.MIDI_DeleteNote(take,i)
        
        --first dup note
        NewList[i*2].sel=note_data[i].sel
        NewList[i*2].muted=note_data[i].muted
        NewList[i*2].start=note_data[i].start
        NewList[i*2].ending=note_data[i].start+(note_data[i].ending-note_data[i].start)/2
        NewList[i*2].chan=note_data[i].chan
        NewList[i*2].pitch=note_data[i].pitch
        NewList[i*2].vel=note_data[i].vel
        
        --second dup note
        NewList[i*2+1].sel=note_data[i].sel
        NewList[i*2+1].muted=note_data[i].muted
        NewList[i*2+1].start=(note_data[i].ending-note_data[i].start)/2+note_data[i].start
        NewList[i*2+1].ending=note_data[i].ending
        NewList[i*2+1].chan=note_data[i].chan
        NewList[i*2+1].pitch=note_data[i].pitch
        NewList[i*2+1].vel=note_data[i].vel
      end
    end
    for x=0, #NewList do
      if NewList[x]~=nil then
        reaper.MIDI_InsertNote(take,NewList[x].sel,NewList[x].muted,NewList[x].start,NewList[x].ending,NewList[x].chan,NewList[x].pitch,NewList[x].vel)
      end
    end
  end
else
  local take=reaper.MIDIEditor_GetTake(reaper.MIDIEditor_GetActive())
  local _,CountEvts=reaper.MIDI_CountEvts(take)
  if CountEvts==0 then no_undo() return end;
  local NewList={}
  if CountEvts>0 then
    note_data={}
    for i=0, CountEvts+1 do
      note_data[i]={}
      _,note_data[i].sel,note_data[i].muted,note_data[i].start,note_data[i].ending,note_data[i].chan,note_data[i].pitch,note_data[i].vel= reaper.MIDI_GetNote(take,i)
    end
    for i=0,#note_data-1 do
      if note_data[i].sel==true and note_data[i+1].sel==true and note_data[i].pitch==note_data[i+1].pitch and note_data[i].ending==note_data[i+1].start and i%2==0 then
        NewList[i/2]={}
        NewList[i/2].sel=note_data[i].sel
        NewList[i/2].muted=note_data[i].muted
        NewList[i/2].start=note_data[i].start
        NewList[i/2].ending=note_data[i+1].ending
        NewList[i/2].chan=note_data[i].chan
        NewList[i/2].pitch=note_data[i].pitch
        NewList[i/2].vel=note_data[i].vel
        reaper.MIDI_DeleteNote(take,0)
        reaper.MIDI_DeleteNote(take,0)
        
      end
    end
    for x=0,#NewList do
      if NewList[x]~=nil then
        reaper.MIDI_InsertNote(take,NewList[x].sel,NewList[x].muted,NewList[x].start,NewList[x].ending,NewList[x].chan,NewList[x].pitch,NewList[x].vel)
      end
    end
  end
end