OSC-CV Converter

Dependencies:   Bonjour OSCReceiver TextLCD mbed mbed-rpc BurstSPI DebouncedInterrupt FastIO MIDI OSC OSCtoCV ClockControl

OSC to CV Converter

http://gtbts.tumblr.com/post/125663817741/osc-to-cv-converter-ver2-mbed-osctocv

/media/uploads/casiotone401/tumblr_nsg7y4pkfg1qlle9fo1_540.png

Revision:
27:65e873bf8307
Parent:
26:8673d5254f7d
Child:
29:8b61b87b9676
--- a/main.cpp	Sun Jan 31 12:56:12 2016 +0000
+++ b/main.cpp	Tue Feb 16 11:31:57 2016 +0000
@@ -100,6 +100,7 @@
 int main() 
 { 
     float pot, _pot;
+    float duration, _duration = 0.6;
     int bpm;
         
     InitOSCCV();     
@@ -140,6 +141,18 @@
         
         bpm = CheckBPM(); // check current BPM
         
+        duration = gCtrl[6];
+        
+        if (_duration != duration) // check current gate duration
+        {
+            sendMes.setTopAddress("/ctrl7");
+            sendMes.setArgs("f", gCtrl[6]);
+    
+            osc.sendOsc(&sendMes);
+            
+            _duration = duration;
+        }
+        
         switch (gMode)
         {               
             case MODE_OSC: // OSCtoCV mode
@@ -153,11 +166,11 @@
                 
                 if (gCtrlSW[3])
                 {   // euclid sequencer auto offset
-                    ShiftCVSeq(EuclideanSeq(GateSeq(bpm, N16TH, SUBGATE, (gCtrl[6] * 12), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF, true), gCtrlSW[0], CV_CHANNEL8);
+                    ShiftCVSeq(EuclideanSeq(GateSeq(bpm, N16TH, SUBGATE, (duration * 12), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF, true), gCtrlSW[0], CV_CHANNEL8);
                 
                 } else {
                     
-                    ShiftCVSeq(BeatsSeq(GateSeq(bpm, N16TH, SUBGATE, (gCtrl[6] * 8), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF), gCtrlSW[0], CV_CHANNEL8);
+                    ShiftCVSeq(BeatsSeq(GateSeq(bpm, N16TH, SUBGATE, (duration * 8), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF), gCtrlSW[0], CV_CHANNEL8);
                 }
                 break;
                 
@@ -168,11 +181,11 @@
                 
                 if (gCtrlSW[3])
                 {   // euclid sequencer auto offset
-                    M185Seq(EuclideanSeq(GateSeq(bpm, N16TH, SUBGATE, (gCtrl[6] * 12), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF, true), gCtrlSW[0], CV_CHANNEL8);
+                    M185Seq(EuclideanSeq(GateSeq(bpm, N16TH, SUBGATE, (duration * 12), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF, true), gCtrlSW[0], CV_CHANNEL8);
                     
                 } else {
                     
-                    M185Seq(BeatsSeq(GateSeq(bpm, N16TH, SUBGATE, (gCtrl[6] * 8), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF), gCtrlSW[0], CV_CHANNEL8);
+                    M185Seq(BeatsSeq(GateSeq(bpm, N16TH, SUBGATE, (duration * 8), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF), gCtrlSW[0], CV_CHANNEL8);
                 }
                 break;
             
@@ -182,28 +195,28 @@
                 
                 if (gCtrlSW[3])
                 {   // euclid sequencer auto offset
-                    PolyCVSeq(EuclideanSeq(GateSeq(bpm, N16TH, SUBGATE, (gCtrl[6] * 12), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF, true), gCtrlSW[0]);
+                    PolyCVSeq(EuclideanSeq(GateSeq(bpm, N16TH, SUBGATE, (duration * 12), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF, true), gCtrlSW[0]);
                     
                 } else {
                     
-                    PolyCVSeq(BeatsSeq(GateSeq(bpm, N16TH, SUBGATE, (gCtrl[6] * 8), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF), gCtrlSW[0]);
+                    PolyCVSeq(BeatsSeq(GateSeq(bpm, N16TH, SUBGATE, (duration * 8), NON_INVERT, GATESOUT_OFF, SYNC_ON), gCtrlSW[0], GATESOUT_OFF), gCtrlSW[0]);
                 }
                 break;
                     
             case MODE_EUC: // Euclidean Sequencer mode
             
-                ShiftCVSeq(GateSeq(bpm, N1ST, SUBGATE, (gCtrl[6] * 5), NON_INVERT, GATESOUT_OFF, SYNC_OFF), gCtrlSW[0], CV_CHANNEL8); 
+                ShiftCVSeq(GateSeq(bpm, N1ST, SUBGATE, (duration * 5), NON_INVERT, GATESOUT_OFF, SYNC_OFF), gCtrlSW[0], CV_CHANNEL8); 
                 EuclideanSeq(GateSeq(bpm, N16TH, GATE1, 3, NON_INVERT, GATESOUT_OFF, SYNC_OFF), gCtrlSW[0], GATESOUT_ON, false);
                 break;
             
             case MODE_RND: // Random CV Generator mode
         
-                RandomCVGenerator(GateSeq(bpm, N32TH, GATE1, (gCtrl[6] * 5), NON_INVERT, GATESOUT_OFF, SYNC_ON));
+                RandomCVGenerator(GateSeq(bpm, N32TH, GATE1, (duration * 5), NON_INVERT, GATESOUT_OFF, SYNC_ON));
                 break;
 
             case MODE_LFO: // Stepped LFO mode
         
-                SteppedLFO(CV_CHANNEL1, true); // LFO out ch1 ~ ch8
+                SteppedLFO(CV_CHANNEL1, false); // LFO out ch1 ~ ch8
                 break;
 
             default:       // CV Calibration mode
@@ -258,7 +271,7 @@
     
 // Init  SPI
     gLDAC = _ENABLE;
-    gSPI.format(8,1);           // Data word length 8bit, Mode=1
+    gSPI.format(8, 1);           // Data word length 8bit, Mode=1
     gSPI.frequency(SPI_RATE);
     
     UpdateCV(CLR, 0, 0);        // Ignore CLR Pin
@@ -294,11 +307,11 @@
     sendMes.setIp(touchOSCAddress); 
     sendMes.setPort(touchOSCPort);
     
-    gSW.attach(&CheckModeSW,IRQ_RISE, 30);     // InterruptIn rising edge(ModeSW)
+    gSW.attach(&CheckModeSW,IRQ_RISE, 20);     // InterruptIn rising edge(ModeSW)
     wait(0.5);
     
     gPoller.attach_us(&NetPoll, POLLING_INTERVAL);  // Ticker Polling
-    wait(0.5);
+    wait(0.2);
 }
 
 //-------------------------------------------------------------
@@ -322,10 +335,10 @@
                 break;
         }
 
-        UpdateCVMeter(ch, &cv);
-        
-        ++ch;
-        ch &= 0x07;
+    UpdateCVMeter(ch, &cv);
+    
+    ++ch;
+    ch &= 0x07;
 }
 
 //-------------------------------------------------------------
@@ -333,17 +346,12 @@
 
 inline void SetCV()
 {
-    static int ch, qmode, amode, mcount;
+    static int ch, mcount;
     static float glidecv[8];
     unsigned int cv;
     static float qcv;
 
-    qmode = (gCtrl[1] * (SCALE_NUM - 1));
-    amode = SCALE_AOUT * qmode;
-    
-    gAOUT.write_u16(amode);
-
-    switch (qmode) 
+    switch (CheckQuantizeMode()) 
         {
             case Lin:
                 
@@ -460,6 +468,13 @@
             sendMes.setTopAddress("/bpm");
             sendMes.setArgs("i", bpm);
             osc.sendOsc(&sendMes);
+            
+            osc.sendOsc(&sendMes);
+            
+            sendMes.setTopAddress("/ctrl1");
+            sendMes.setArgs("f", gCtrl[0]);
+    
+            osc.sendOsc(&sendMes);
         }
     }
                         
@@ -591,7 +606,7 @@
             _mode = gMode;
     }
     
-    qmode = (gCtrl[1] * (SCALE_NUM - 1));
+    qmode = (gCtrl[1] * (SCALE_TOTAL - 1));
     
     if (_qmode != qmode)
     {
@@ -751,53 +766,54 @@
     {
         case UDPSOCKET_READABLE: // The only event for now
         
-        recvlen = gUdp.recvfrom(buf, 1024, &host);  // packet length    
-        
-        if (recvlen <= 0)  break;
-        
-        if (!bundleflag && buf[0] == '#') // #bundle
-        {
-            messagepos += 16;     // skip #bundle & timetag
-            recvlen -= 16;
-            
-            bundleflag = true;  
-        }
-        
-        do {
+            recvlen = gUdp.recvfrom(buf, 1024, &host);  // packet length    
+
+            if (recvlen <= 0)  break;
+
+            if (!bundleflag && buf[0] == '#') // #bundle
+            {
+                messagepos += 16;     // skip #bundle & timetag
+                recvlen -= 16;
+
+                bundleflag = true;  
+            }
+
+            do {
+
                 if (bundleflag)
                 {
                     messagepos += 4;
                     recvlen -= 4;
-                    
+
                     if (recvlen <= 8)
                     {
                         bundleflag = false;
                         break;
                     }
                 }
-                
+
                 if (getOSCmsg(buf + messagepos, msg) == -1)  continue;
-            
+
                 len = strlength(msg[0].address);
-        
+
                 if (isdigit(msg[0].address[len-1])) 
                 {       
                     num = msg[0].address[len-1] - '0' - 1;
-                    
+
                     offset = 1;
-                    
+
                     if (isdigit(msg[0].address[len-2])) 
                     {
                         offset = 2;
                         num += 10;
                     }
-                        
+
                 } else {
-            
+
                     num = -1;
                 }
-        
-            // address pattern SYNC & GATE (Type Tag int, float)
+
+                // address pattern SYNC & GATE (Type Tag int, float)
                 if (!strncmp(msg[0].address + (len - offset) - 4, "sync", 4)) 
                 { 
                     if (msg[2].i != 0) gCLOCKOUT = true;
@@ -809,7 +825,7 @@
                     if (msg[2].i != 0) gGATES[num] = true;
                     else              gGATES[num] = false;
                     continue;
-            // (touchOSC Control push, toggle)
+                    // (touchOSC Control push, toggle)
                 } else if (!strncmp(msg[0].address + (len - offset) - 5, "fader", 5) && (num != -1)) {
                     if (num > 7) continue;                                              
                     gOSC_cv[num] = msg[2].f * (SCALING_N);
@@ -820,183 +836,188 @@
                     if (msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
                     if (msg[1].typeTag[1] == 'f') gOSC_cv[++num] = msg[3].f * (SCALING_N);
                     continue;
-    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 12, "multifader1/", 12) && (num != -1)) {
                     if (num > 7) continue;
                     if (msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 5, "m185/", 5) && (num != -1)) {
                     if (num > 7) continue;
                     if (msg[1].typeTag[1] == 'f') g185_cv[num] = msg[2].f * (SCALING_N);
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) -10, "sequencer/", 10) && (num != -1)) {
                     if (num > 15) continue;
                     gSeq_cv[num] = msg[2].f * (SCALING_N);
                     continue;
-                            
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 6, "ctrlsw", 6) && (num != -1)) {
                     if (num > 7) continue;
                     if (msg[2].i != 0) gCtrlSW[num] = true;
                     else              gCtrlSW[num] = false;
                     continue;
-                                                
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 4, "ctrl", 4) && (num != -1)) {
                     if (num > 7) continue;                                             
                     gCtrl[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 9, "pulsecnt/", 9) && (num != -1)) {
                     if (num > 7) continue;
                     gPulseCount[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 9, "gatemode/", 9) && (num != -1)) {
                     if (num > 15) continue;
                     gGateMode[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 9, "g185mode/", 9) && (num != -1)) {
                     if (num > 7) continue;
                     gGateMode185[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 6, "slide/", 6) && (num != -1)) {
                     if (num > 15) continue;
                     gSlide[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 9, "slide185/", 9) && (num != -1)) {
                     if (num > 7) continue;
                     gSlide185[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset - 2) - 7, "accent/", 7) && (num != -1)) {
-                    
+
                     if (isdigit(msg[0].address[len - 3])) 
                     {       
                         num = msg[0].address[len - 3] - '0' - 1;
                     }
-                    
+
                     gAccent[num] = msg[2].i;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset - 3) - 7, "accent/", 7) && (num != -1)) {
-                    
+
                     if (isdigit(msg[0].address[len - 3]))
                     {       
                         num = msg[0].address[len - 3] - '0' - 1;
-                        
+
                         if (isdigit(msg[0].address[len - 4])) 
                         {
                             num += 10;
                         }
                     }
-                    
+
                     gAccent[num] = msg[2].i;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset - 3) - 6, "185acc/", 6) && (num != -1)) {
-                    
+
                     if (isdigit(msg[0].address[len-3])) 
                     {       
                         num = msg[0].address[len-3] - '0' - 1;
-                        
+
                         if (isdigit(msg[0].address[len - 4])) 
                         {
                             num += 10;
                         }
                     }
-                    
+
                     gAccent185[num] = msg[2].i;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset - 2) - 3, "bm/", 3) && (num != -1)) {
-                    
+
                     if (isdigit(msg[0].address[len - 3])) 
                     {       
                         num = msg[0].address[len - 3] - '0' - 1;
-                        
+
                         if (isdigit(msg[0].address[len - 4])) 
                         {
                             num += 10;
                         }
                     }
-                    
+
                     if (isdigit(msg[0].address[len - 1])) 
                     {       
                         numrow = msg[0].address[len - 1] - '0' - 1;
                     }
-                    
+
                     gBeatsMatrix[numrow][num] = msg[2].i;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset - 3) - 3, "bm/", 3) && (num != -1)) {
-                    
+
                     if (isdigit(msg[0].address[len - 3])) 
                     {       
                         num = msg[0].address[len - 3] - '0' - 1;
-                        
+
                         if (isdigit(msg[0].address[len - 4])) 
                         {
                             num += 10;
                         }
                     }
-                    
+
                     if (isdigit(msg[0].address[len - 1])) 
                     {       
                         numrow = msg[0].address[len - 1] - '0' - 1;
                     }
-                    
+
                     gBeatsMatrix[numrow][num] = msg[2].i;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 4, "bpc/", 4) && (num != -1)) {
                     if (num > 15) continue;
                     gPulseCountBeats[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 4, "blv/", 4) && (num != -1)) {
                     if (num > 7) continue;
                     gBeatsLevel[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 4, "bdc/", 4) && (num != -1)) {
                     if (num > 7) continue;
                     gBeatsDecay[num] = msg[2].f;
                     continue;
-                    
+
                 }  else if (!strncmp(msg[0].address + (len - offset) - 4, "euca", 4) && (num != -1)) {
                     if (num > 5) continue;                                             
                     gEucA[num] = msg[2].f;
                     continue;
-                    
+
                 } else if (!strncmp(msg[0].address + (len - offset) - 4, "eucb", 4) && (num != -1)) {
                     if (num > 5) continue;                                             
                     gEucB[num] = msg[2].f;
                     continue;
-                    
-                } else if (!strncmp(msg[0].address + (len - offset) - 3, "acv", 3) && (num != -1)) {
-                    if (num > 3) continue;                                             
-                    gArdCV[num] = msg[2].i;
-                    continue;
+
+                } else if (MODE_RND || MODE_LFO) {
                     
-                } else if (!strncmp(msg[0].address + (len - offset) - 3, "pot", 3) && (num != -1)) {
-                    if (num > 1) continue;                                             
-                    gArdPot[num] = msg[2].f;
-                    continue;
-                    
-                } else if (!strncmp(msg[0].address + (len - offset) - 2, "sw", 2) && (num != -1)) {
-                    if (num > 1) continue;
-                    if (msg[2].i != 0) gArdSW[num] = true;
-                    else               gArdSW[num] = false;                                       
-                    continue;
+                    if (!strncmp(msg[0].address + (len - offset) - 3, "acv", 3) && (num != -1)) {
+                        if (num > 3) continue;                                             
+                        gArdCV[num] = msg[2].i;
+                        continue;
+
+                    } else if (!strncmp(msg[0].address + (len - offset) - 3, "pot", 3) && (num != -1)) {
+                        if (num > 1) continue;                                             
+                        gArdPot[num] = msg[2].f;
+                        continue;
+
+                    } else if (!strncmp(msg[0].address + (len - offset) - 2, "sw", 2) && (num != -1)) {
+                        if (num > 1) continue;
+                        if (msg[2].i != 0) gArdSW[num] = true;
+                        else               gArdSW[num] = false;                                       
+                        continue;
+
+                    }
                     
                 } else {
+                    
                     continue;
                 }
-                
+
             } while (bundleflag);                   
     }
 }