Page **1** of **1**

### Newbie-best method for processing CV pitch?

Posted: **Sat Jun 27, 2020 4:23 pm**

by **Adash**

I've gotten some basics working so far, but when it comes to working with CV pitch my code isnt quite working.

For example, trying to create a basic S&H plus quanitzer.

I can generate random cv values within a range based on a clock or sync input, but thenw working with the CV picth is proving difficult.

I cant seem to use 1/12 to assign the cv semitone value to a Double variable. Java just rounds it to 0.0 so I cant do any calculations with it.

I tried working in midi 128 note range 'internaly' then calculating out into cv by multiplying by 0.0833333333333333 but although its close, its slightly out of tune.

Could anyone please advise best practise for working with cv picth?

Thanks

### Re: Newbie-best method for processing CV pitch?

Posted: **Sat Jun 27, 2020 8:25 pm**

by **MRBarton**

Java shouldn't round anything with 15 digits of precision to work with. To help you out, a code fragment would be great. Just to be clear, I'm assuming you're working in the 1v/oct world and not the frequency world.

### Re: Newbie-best method for processing CV pitch?

Posted: **Sat Jun 27, 2020 9:58 pm**

by **Adash**

So below is where I try to create a random double within a -5 +5 range, then quantize the random to get a double that corresponds to a valid tuned semitone in CV. I then output that value to the output jack using SetValue().

If I hook up the 'mysinglecv' value to a text field it never gets beyond 0.0. If I do something like below but hard code the value 0.0833333333333333 its nearly there but a bit off tune so would rather use the full precision of a double.

TBH I'm not even sure if this is the correct/best approch to working with CV (1v/oct) values.

//get a random double value between -5 and +5. This part needs some work to get the range more accurate

//(roughly for now) Get a random double between 0.0 and 0.9. X by 10 and - 5 to get a rough -5 to +5 range

myinitialvalue = (java.lang.Math.random() * 10 -5);

//Store the value of a single semitone CV (1 volt per octave) into a double

mysinglecv = 1/12;

// Next to quantize the random values

//First I divide the random by the single cv value

myinitialvalue = myinitialvalue / mysinglecv;

//I round this off

myinitialvalue = java.lang.Math.round(myinitialvalue);

//Now I multiply it by the single cv value to get a quantized CV value

myinitialvalue = myinitialvalue * mysinglecv;

outputJack1.SetValue(myinitialvalue);

### Re: Newbie-best method for processing CV pitch?

Posted: **Sat Jun 27, 2020 11:46 pm**

by **MRBarton**

I'm not sure why you're getting values that are slightly off, but I can tell you why 1/12 is not working. In Java, 1/12 is an integer divide yielding zero. Use 1.0/12.0 instead. The value 0.0833333333333333 is just as good as that is full double precision (15 significant digits).

You're original idea of using "MIDI" or semitones makes things easier.

Try this (fancy all on one line):

double k = Math.round(Math.random() * 120.0 - 60.0) * 0.0833333333333333;

That should give you twelfths from -5 to +5. You don't need the java.lang. on the math calls.

You could divide by 12.0, but you should try to avoid division and things like transcendentals in ProcessSample() if you can. In 20 modules, I think I have used division twice when it was unavoidable. It might not make much difference, it's just me being a weenie. I hope I was of some help. Have fun. VM is a blast!

### Re: Newbie-best method for processing CV pitch?

Posted: **Sun Jun 28, 2020 8:56 am**

by **Adash**

Thank you very much for the feedback. Its been a great help.

I'll try keeping it midi for internal processing and then convert using 1.0/12.0 for the output.

### Re: Newbie-best method for processing CV pitch?

Posted: **Sun Jun 28, 2020 9:24 am**

by **Adash**

Yep this works perfectly.

Now I have a S&H with built in attenuator and offset and basic quantize. I can now start adding more features to the quantizer.

I test tuning by hooking up two oscs pitch input. One direct to my modules cv output and the other going via a quantizer module and they are both in tune with each other.

Thanks again for your help with this.