home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February / PCWorld_2008-02_cd.bin / audio-video / reaper / reaper2028-install.exe / Effects / IX / MIDI_KeySnap < prev    next >
Text File  |  2007-12-03  |  7KB  |  200 lines

  1. /*
  2. Copyright 2007, Philip S. Considine
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without modification, are permitted 
  6. provided that the following conditions are met:
  7.  
  8. Redistributions of source code must retain the above copyright notice, this list of conditions 
  9. and the following disclaimer. 
  10.  
  11. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
  12. and the following disclaimer in the documentation and/or other materials provided with the distribution. 
  13.  
  14. The name of Philip S. Considine may not be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16.  
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
  18. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
  19. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
  20. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26.  
  27. ////////////////////////////////////////////////////////////////////////////////////////////
  28. desc: MIDI Note Constrain.
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////////////////
  31.  
  32. //Input Channel
  33. slider1:0<0,15,1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}>Input Channel
  34.  
  35. /*----------- NOTE MIN/MAX You can change these if you want! --------------*/
  36. //Standard Sliders
  37. slider2:0<0,127,1>Note Min
  38. slider3:127<0,127,1>Note Max
  39.  
  40. //Drop-downs with note names (disabled)
  41. //slider2:0<0,127,1{C0,C#0,D0,D#0,E0,F0,F#0,G0,G#0,A0,A#0,B0,C1,C#1,D1,D#1,E1,F1,F#1,G1,G#1,A1,A#1,B1,C2,C#2,D2,D#2,E2,F2,F#2,G2,G#2,A2,A#2,B2,C3,C#3,D3,D#3,E3,F3,F#3,G3,G#3,A3,A#3,B3,C4,C#4,D4,D#4,E4,F4,F#4,G4,G#4,A4,A#4,B4,C5,C#5,D5,D#5,E5,F5,F#5,G5,G#5,A5,A#5,B5,C6,C#6,D6,D#6,E6,F6,F#6,G6,G#6,A6,A#6,B6,C7,C#7,D7,D#7,E7,F7,F#7,G7,G#7,A7,A#7,B7,C8,C#8,D8,D#8,E8,F8,F#8,G8,G#8,A8,A#8,B8,C9,C#9,D9,D#9,E9,F9,F#9,G9,G#9,A9,A#9,B9,C10,C#10,D10,D#10,E10,F10,F#10,G10}>Note Min
  42. //slider3:127<0,127,1{C0,C#0,D0,D#0,E0,F0,F#0,G0,G#0,A0,A#0,B0,C1,C#1,D1,D#1,E1,F1,F#1,G1,G#1,A1,A#1,B1,C2,C#2,D2,D#2,E2,F2,F#2,G2,G#2,A2,A#2,B2,C3,C#3,D3,D#3,E3,F3,F#3,G3,G#3,A3,A#3,B3,C4,C#4,D4,D#4,E4,F4,F#4,G4,G#4,A4,A#4,B4,C5,C#5,D5,D#5,E5,F5,F#5,G5,G#5,A5,A#5,B5,C6,C#6,D6,D#6,E6,F6,F#6,G6,G#6,A6,A#6,B6,C7,C#7,D7,D#7,E7,F7,F#7,G7,G#7,A7,A#7,B7,C8,C#8,D8,D#8,E8,F8,F#8,G8,G#8,A8,A#8,B8,C9,C#9,D9,D#9,E9,F9,F#9,G9,G#9,A9,A#9,B9,C10,C#10,D10,D#10,E10,F10,F#10,G10}>Note Max
  43. /*-------------------------------------------------------------------------*/
  44.  
  45. slider4:0<0,11,1{C,C#,D,Eb,E,F,F#,G,G#,A,Bb,B}>Root Note
  46. slider5:/ix_scales:none:Scale File
  47. slider6:1<0,1,1{Block,Remap}>Mode
  48. slider7:1<0,1,1{Off,On}>On/Off
  49.  
  50. ////////////////////////////////////////////////////////////////////////////////////////////
  51. @init
  52.  
  53. //Set memory offsets
  54. inputTracker = 0;
  55. noteMap = 128;
  56. scale = 1024;
  57.  
  58. //MIDI constants
  59. statNoteOn = 9 * 16;
  60. statNoteOff = 8 * 16;
  61.  
  62. //File change flag
  63. scaleFile = -1;
  64.  
  65. ////////////////////////////////////////////////////////////////////////////////////////////
  66. @slider
  67.  
  68. //Catch bad values
  69. slider2 = floor(slider2);
  70. slider3 = floor(slider3);
  71. slider2 < 0 ? slider2 = 0; slider2 > 127 ? slider2 = 127;
  72. slider3 < 0 ? slider3 = 0; slider3 > 127 ? slider3 = 127;
  73.  
  74. //Alias values
  75. inChannel = slider1;
  76. noteMin = slider2;
  77. noteMax = slider3;
  78. root = slider4;
  79. mode = slider6;
  80. on = slider7;
  81.  
  82. //Load scale
  83. scaleFile != slider5|0 ?
  84. (
  85.     scaleFile = slider5|0;
  86.     scaleSize = 0;
  87.     fileHandle = file_open(slider5);
  88.     fileHandle > 0 && file_text(fileHandle) ?
  89.     (
  90.         while
  91.         (
  92.             file_var(fileHandle, scale[scaleSize]);
  93.             file_avail(fileHandle) ? scaleSize += 1;
  94.         );
  95.         file_close(fileHandle);
  96.     );
  97. );
  98.  
  99. ////////////////////////////////////////////////////////////////////////////////////////////
  100. @block
  101.  
  102. while
  103. (
  104.     midirecv(offset,msg1,msg23) ?
  105.     (    
  106.         //Reset flags
  107.         passThru = 1;
  108.         
  109.         //Check status byte
  110.         status = msg1 & 240; //High four bits is message type (240 == 11110000)
  111.         channel = msg1 & 15; //Low four bits is channel number (15 == 00001111)
  112.  
  113.         //Is it on our channel
  114.         channel == inChannel && on == 1 ? 
  115.         (
  116.             //Is it a note event
  117.             (status == statNoteOn || status == statNoteOff) ?
  118.             (
  119.                 //get note number and velocity
  120.                 note = msg23 & 127;
  121.                 velocity = ((msg23/256) & 127);
  122.                 
  123.                 //Process if it's within the note range
  124.                 note >= noteMin && note <= noteMax ?
  125.                 (
  126.                 
  127.                     //What kind of note event?
  128.                     (status == statNoteOn && velocity > 0) ?
  129.                     (    
  130.                         //Note On code starts here
  131.                         inputTracker[note] == 0 ?
  132.                         (
  133.                             relNote = note % 12;
  134.                             octave = (note - relNote) / 12;
  135.                             relNote -= root;
  136.                             relNote < 0 ? relNote += 12;
  137.                     
  138.                             //Check if in key
  139.                             i = okay = 0;    
  140.                             while
  141.                             (
  142.                                 relNote == scale[i] ? okay = 1;
  143.                                 i += 1;
  144.                                 i <= scaleSize;
  145.                             );
  146.                                                 
  147.                             okay == 0 ? //Bad note
  148.                             (
  149.                                 mode == 1 ? //Remap
  150.                                 (
  151.                                     nearest = difference = 1000;
  152.                                     i = 0;
  153.  
  154.                                     while                
  155.                                     (
  156.                                         temp = abs(scale[i] - relNote);
  157.                                         temp < difference ?
  158.                                         (
  159.                                             difference = temp;
  160.                                             nearest = scale[i];
  161.                                         );
  162.                                         i += 1;
  163.                                         i <= scaleSize;
  164.                                     );
  165.                                     
  166.                                     inputTracker[note] = 2; //Flag as modified note
  167.                                     
  168.                                     //Map to nearest in key
  169.                                     nearest + root >= 11 ? octave -= 1;
  170.                                     noteMap[note] = (octave * 12) + nearest + root;                                    
  171.                                     msg23 = noteMap[note] |(velocity * 256); //Modify MIDI data
  172.                                     
  173.                                 ):passThru = 0; //Block
  174.                                 
  175.                             ): inputTracker[note] = 1; //Good note, flag as ordinary note
  176.                             
  177.                         ):passThru = 0; //Block duplicate notes
  178.                     )
  179.                     :
  180.                     (
  181.                         //Note Off code starts here
  182.                         inputTracker[note] > 0 ?            
  183.                         (
  184.                             //Check if it's a modified note
  185.                             inputTracker[note] == 2 ?
  186.                             (
  187.                                 msg23 = noteMap[note] |(velocity * 256); //Modify MIDI data
  188.                                 noteMap[note] = note; //Reset noteMap
  189.                             );
  190.                             
  191.                             inputTracker[note] = 0; //Flag this note as off    
  192.                         );
  193.                     );
  194.                 );
  195.             );
  196.         );
  197.         passThru == 1 ? midisend(offset,msg1,msg23);    //Pass if flagged
  198.     );
  199. );
  200.