Contol Pitch in semitone By usb midi cc#

i am trying to control Master pitch by 1 semitone on each step of my plugin by cc#100 at channel 1, as you will
see here in my code .
as i am seeing a midi cc# value(0-127) = totel 128 units. so i tried it with 2 methods
-----------Method 1________
i set my knob range to 12.
when pitch knob at center pitch=0,
when fully rotated to left pitch= -1200cents,
when fully rotated to left pitch = +1200 cents .
so totel steps are 24 .
i think 1 step interval will be .... 128/24 =5.33.
so i declared variable (semi_key) multiply with 5.33 to get pitch inc/dec by 100cents/1semitone.
i thought when semi_key =12; , it will do knob to center & get pitch=0.
and chenge pitch in semitones , as i will inc or dec semi_key
and i use code this

float semi_key = 12.00;
float Pitch_cc = 63.00;
void loop() {
 if (c == 25)     {  if (semi_key > 0) { semi_key--;        
                              Pitch_cc = ((5.33*semi_key)-1);   
                              controlChange(1,100, Pitch_cc);  
                                MidiUSB.flush();    }}
  if (c == 26)     {  semi_key =12.00;                        
		Pitch_cc = ((5.33*semi_key)-1);   
		  controlChange(1,100, Pitch_cc);   
		MidiUSB.flush();   }  
   if (c == 27)     {  if (semi_key < 24) {semi_key++;   
		 Pitch_cc = ((5.33*semi_key)-1);   
		  controlChange(1,100, Pitch_cc);  
		 MidiUSB.flush();   }}
                      
}

but it got result ,,

when semi_key=0 , getting pitch = -28cents , instead of 0 ......(28 cents up)
when semi_key is increased from 0 . getting +85 . instead of +100 .....(15 cents less)
when decreased from 0 . getting pitch =-123 . instead of -100....(23 cent 0ver)

Method 2 ----

i set my knob range to 16
when pitch knob at center pitch=0,
when fully rotated to left pitch= -1600cents,
when fully rotated to left pitch = +1600 cents .
so totel steps are 32.
i think 1 step interval will be .... 128/32 =4.
so i declared variable (semi_key) multiply with 4 to get pitch inc/dec by 100cents/1semitone.
i thought when semi_key =16; , it will do knob to center & get pitch=0.
and chenge pitch in semitones , as i will inc or dec semi_key
and i use code this

float semi_key = 16.00;
float Pitch_cc = 63.00;
void loop() {
 if (c == 25)     {  if (semi_key > 0) { semi_key--;        
                              Pitch_cc = ((4*semi_key)-1);   
                              controlChange(1,100, Pitch_cc);  
                                MidiUSB.flush();    }}
  if (c == 26)     {  semi_key =16.00;                        
		Pitch_cc = ((4*semi_key)-1);   
		  controlChange(1,100, Pitch_cc);   
		MidiUSB.flush();   }  
   if (c == 27)     {  if (semi_key < 32) {semi_key++;   
		 Pitch_cc = ((4*semi_key)-1);   
		  controlChange(1,100, Pitch_cc);  
		 MidiUSB.flush();   }}
                      
}

but it got result ,,

when semi_key=0 , getting pitch = -13 cents , instead of 0 ......(13 cents up)
when semi_key is increased from 0 . getting +88 . instead of +100 .. (12 cents less)
when decreased from 0 . getting pitch =-113 instead of -100 ......(13cents over)

in both Metods am not getting accurate pitch changes . where am i doing mistake??????

It seems to be mostly a series of truncation errors. Rounding instead of truncating should help some. Using 5.333333 instead of 5.33 might also help.

Even better, let the compiler give you the best possible number, use

Pitch_cc = ((128.0/24.0*semi_key)-1); 

And since the controlChange function takes a byte (0-255) value, round it

Pitch_cc = ((128.0/24.0*semi_key)-1)+0.5; 

And you might as well change "Pitch_cc" to an integer type. I would use 'byte' since the allowed range is 0 to 127.

Thanks you all,
But reply became tuka , not working.
I seemed midi cc value not support decimal number. I tried 63.5 . But not support. It need Real numbers.
So thank you , I will use jugaad code as my sense(will have to accept +15 or -15 cents near correct pitch) like 212 as 200 ,. 788 as 800,. 508 as 500.

In which 0 will give -1200 , and 127 will give +1200 , 63 will give -9
.....

Don't use 7-bit Control Change messages to change the pitch, use 14-bit Pitch Bend messages for higher resolution.

1 Like

My slave pitch knob is from beat tracks. I can't assign pch Bender function. If use this, it Will also affect my playable sounds's or Selected plug ins pitchs . I was searching for cc for control separately from pitch Bender. Thank you Peter for help reply