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

Files at this revision

API Documentation at this revision

Comitter:
casiotone401
Date:
Sat Mar 23 11:24:13 2013 +0000
Parent:
9:1ac3d135d965
Child:
11:ef7610cd7ebe
Commit message:
minor update

Changed in this revision

OSCReceiver.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/OSCReceiver.lib	Sat Mar 02 00:14:57 2013 +0000
+++ b/OSCReceiver.lib	Sat Mar 23 11:24:13 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/casiotone401/code/OSCReceiver/#4ce7c0ce6a66
+http://mbed.org/users/casiotone401/code/OSCReceiver/#3d6dc580b565
--- a/main.cpp	Sat Mar 02 00:14:57 2013 +0000
+++ b/main.cpp	Sat Mar 23 11:24:13 2013 +0000
@@ -110,8 +110,8 @@
 inline void NetPoll(void);
 void InitOSCCV(void);
 inline void UpdateCV(int, int, const unsigned int*);
-int UpdateGate(int, int, int, int, int);
-void SetCV(void);
+int UpdateGate(int, int, int, int, bool);
+inline void SetCV(void);
 void SeqCV(int);
 void CheckModeSW(void);
 inline void CVMeter(int, const unsigned int*);
@@ -235,7 +235,7 @@
 // Variables for Control
 
 float gCtrl[2];
-volatile int gCtrlSW[4];
+volatile bool gCtrlSW[4] = {false};
 
 //-------------------------------------------------------------
 // mbed Functions
@@ -322,12 +322,12 @@
                     UpdateGate(bpm, NRESET, GATEALL, 3, 0);         // Reset (if bpm change)
                     _bpm = bpm;
                     
-                } else if (gCtrlSW[0] == 1) {                       // Stop (gCtrlSW[0])
+                } else if (gCtrlSW[0]) {                        // Stop (gCtrlSW[0])
                         
                     bpm = 0;
                 }
             
-                if(gCtrlSW[2] == 0 && gCtrlSW[3] == 0)              // Sequencer Mode1
+                if(!gCtrlSW[2] && !gCtrlSW[3])              // Sequencer Mode1
                 {
                     SeqCV((UpdateGate(bpm, N16TH, GATE1, 3, 0)));   // Shift Timming 16th note
                     UpdateGate(bpm, N8TH, GATE2, 3, 0);
@@ -336,7 +336,7 @@
 
                     break;
                 
-                } else if (gCtrlSW[2] == 1 && gCtrlSW[3] == 0) {    // Sequencer Mode2 (if gCtrlSW[2] ON)
+                } else if (gCtrlSW[2] && !gCtrlSW[3]) { // Sequencer Mode2 (if gCtrlSW[2] ON)
                 
                     SeqCV((UpdateGate(bpm, N16TH, GATE1, 3, 0)));  // Do shift ch 1~5
                     SeqCV((UpdateGate(bpm, N4TH, GATE2, 3, 0)));   // Do shift  ch 6
@@ -345,7 +345,7 @@
                     
                     break;
                 
-                } else if (gCtrlSW[3] == 1) {                       // Sequencer Mode3 (if gCtrlSW[3] ON)
+                } else if (gCtrlSW[3]) {                        // Sequencer Mode3 (if gCtrlSW[3] ON)
                                                                     // (ch6,7,8, short loop)
                     SeqCV((UpdateGate(bpm, N16TH, GATE1, 3, 0)));  // Do shift ch 1~5
                     SeqCV((UpdateGate(bpm, N8TH, GATE2, 3, 0)));   // Do shift  ch 6
@@ -451,13 +451,11 @@
 //-------------------------------------------------------------
 // GateOutSequence  beat(Note values) length(Gate time) invert(invert Gate)
 
-int UpdateGate(int bpm, int beat, int ch, int length, int invert)
+int UpdateGate(int bpm, int beat, int ch, int length, bool invert)
 {
-    static int gatetime[4];
-    static int oldgatetime[4];
-    static int bar;
-    static int sync24;
-    static int oldsynctime;
+    int i;
+    static int gatetime[4], oldgatetime[4];
+    static int bar, sync24, oldsynctime;
     
     int time = gTimer.read_us();
     
@@ -518,7 +516,7 @@
                 
             case NRESET:
                 
-                for(int i = 0; i < GATEALL; ++i)    // Reset
+                for(i = 0; i < GATEALL; ++i)    // Reset
                 {
                     gTimer.reset();
                     oldsynctime = oldgatetime[i] = gatetime[i] = NRESET;
@@ -544,27 +542,27 @@
     {
         return -1;
         
-    } else if (time > oldgatetime[ch] + gatetime[ch] && invert == 0) {
+    } else if (time > oldgatetime[ch] + gatetime[ch] && !invert) {
         
         oldgatetime[ch] = time;
         gLEDS[ch] = gGATES[ch] = 1;
         
         return ch + 1;
     
-    } else if (time > oldgatetime[ch] + gatetime[ch] && invert == 1) {
+    } else if (time > oldgatetime[ch] + gatetime[ch] && invert) {
         
         oldgatetime[ch] = time;
         gLEDS[ch] = gGATES[ch] = 0;
         
         return 0;
         
-    } else if (time > oldgatetime[ch] + (gatetime[ch] - gatetime[ch] / length) && invert == 0) {
+    } else if (time > oldgatetime[ch] + (gatetime[ch] - gatetime[ch] / length) && !invert) {
         
         gLEDS[ch] = gGATES[ch] = 0;
         
         return 0;
         
-    } else if (time > oldgatetime[ch] + (gatetime[ch] - gatetime[ch] / length) && invert == 1) {
+    } else if (time > oldgatetime[ch] + (gatetime[ch] - gatetime[ch] / length) && invert) {
         
         gLEDS[ch] = gGATES[ch] = 1;
         
@@ -579,13 +577,11 @@
 //-------------------------------------------------------------
 // Calculate CV
 
-void SetCV()
+inline void SetCV()
 {
-    static int ch;
-    float glidecv[8];
+    static int ch, quan;
+    float glidecv[8],  oldcv[8];
     unsigned int cv[8];
-    static float oldcv[8];
-    static unsigned int quan;
     float qcv;
 
     switch(gMode) 
@@ -682,18 +678,15 @@
 void SeqCV(int shift)
 {
     int i, j, k;
-    static int ch, SeqMode;
+    static int ch, quan, SeqMode;
     static int cnt1, cnt2, cnt3;
     static int cntloop1, cntloop2, cntloop3;
-    static float glidecv[8];
+    static float glidecv[8], shiftcv[8];
+    static float buffercv[9], loopcv[3];
     unsigned int cv[8];
-    static float shiftcv[8];
-    static float buffercv[9];
-    static float loopcv[3];
-    int quan;
     float qcv;
         
-    SeqMode = (unsigned int)(gCtrl[1] * (MODE_NUM - 3));    // Sequencer Quantize Mode (gCtrl[1])
+    SeqMode = gCtrl[1] * (MODE_NUM - 3);    // Sequencer Quantize Mode (gCtrl[1])
     
     switch(SeqMode) 
         {
@@ -809,7 +802,7 @@
                 break;
         }
         
-        if(gCtrlSW[2] == 0 && gCtrlSW[3] == 0)              // Sequencer Mode1
+        if(!gCtrlSW[2] && !gCtrlSW[3])              // Sequencer Mode1
         {
             for(i = 1; i < 8; ++i)
             {
@@ -832,7 +825,7 @@
             
             cnt1 = cnt2 = cnt3 = 0;
                     
-        } else if (gCtrlSW[2] == 1 && gCtrlSW[3] == 0) {    // Sequencer Mode2
+        } else if (gCtrlSW[2] && !gCtrlSW[3]) { // Sequencer Mode2
                 
             for(i = 1; i < 8; ++i)
             {
@@ -867,7 +860,7 @@
             
             cnt1 = cnt2 = cnt3 = 0;
                 
-        } else if (gCtrlSW[3] == 1) {                       // Sequencer Mode3
+        } else if (gCtrlSW[3]) {                        // Sequencer Mode3
                 
             for(i = 1; i < 5; ++i)
             {
@@ -944,7 +937,7 @@
                 }
             }
             
-            if(gCtrlSW[1] == 1)                 // Update loop buffer (if gCtrlSW[1] ON)
+            if(gCtrlSW[1])                  // Update loop buffer (if gCtrlSW[1] ON)
             {
                 cnt1 = cnt2 = cnt3 = 0;
             } 
@@ -996,7 +989,7 @@
 
 inline void CVMeter(int ch, const unsigned int *level)
 {
-    unsigned int cvmeter;
+    int cvmeter;
     
     cvmeter = *level / (SCALING_N / 7.9f);
         
@@ -1105,120 +1098,150 @@
 //-------------------------------------------------------------
 // Handller receive UDP Packet
 
+//-------------------------------------------------------------
+// Handller receive UDP Packet
+
 inline void onUDPSocketEvent(UDPSocketEvent e)
 {
     union OSCarg msg[10];
-    int num;
+    char buf[768] = {0};
+    int num, recvlen, len;
     unsigned int absv;
-        
+    int messagepos = 0;
+    bool bundleflag = false;
+    
+    Host host;
+    
+    recvlen = gUdp.recvfrom(buf, 768, &host);  // packet length
+            
     switch(e)
     {
         case UDPSOCKET_READABLE: // The only event for now
-        char buf[256] = {0};
-        Host host;
             
-        while( int len = gUdp.recvfrom( buf, 256, &host ))
+        if(recvlen <= 0)  break;
+        
+        if(!strncmp(buf, "#", 1)) // #bundle
         {
-            if(len <= 0)  break;
-            //  printf("\r\nFrom %d.%d.%d.%d:\r\n", 
-            //  host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]);
+            messagepos += 16;     // skip #bundle & timetag
+            recvlen -= 16;
             
-            getOSCmsg(buf,msg);
-            //  printf("OSCmsg: %s %s %f %i\r\n", 
-            //  msg[0].address, msg[1].typeTag, msg[2].f, msg[2].i);
+            bundleflag = true;  
+        }
+        
+        do {
+                if(bundleflag)
+                {
+                    messagepos += 4;
+                    recvlen -= 4;
                 
-            len = strlen(msg[0].address);
+                    if(recvlen <= 0)
+                    {
+                        bundleflag = false;
+                        break;
+                    }
+                }
+                
+                if(getOSCmsg(buf + messagepos, msg) == -1)  continue;
             
-            if(isdigit(msg[0].address[len-1]))
-                
-                num = msg[0].address[len-1] - '0' - 1;
-            else
-                num = -1;
+                len = strlen(msg[0].address);
+        
+                if(isdigit(msg[0].address[len-1])) 
+                {       
+                    num = msg[0].address[len-1] - '0' - 1;
             
-            absv = msg[2].f + 0; //convert -0 to 0
+                } else {
             
-         // address pattern SYNC & GATE (Type Tag int, float)
-            if(strncmp(msg[0].address+(len-1)-4, "sync", 4)==0) { 
-                if(absv >= 1 || msg[2].i >= 1) gCLOCKOUT = 1;
-                else                           gCLOCKOUT = 0;
-                break;
-    
-                } else if ((strncmp(msg[0].address+(len-1)-4, "gate", 4)==0) && (num != -1)) {
-                    if(num > 3) break;
+                    num = -1;
+                }
+        
+                absv = msg[2].f + 0; //convert -0 to 0
+        
+            // address pattern SYNC & GATE (Type Tag int, float)
+                if(!strncmp(msg[0].address+(len-1)-4, "sync", 4)) 
+                { 
+                    if(absv >= 1 || msg[2].i >= 1) gCLOCKOUT = 1;
+                    else                           gCLOCKOUT = 0;
+                    continue;
+
+                } else if (!strncmp(msg[0].address+(len-1)-4, "gate", 4) && (num != -1)) {
+                    if(num > 4) continue;
+                    if(absv >= 1 || msg[2].i >= 1) gLEDS[num] = gGATES[num] = 1;
+                    else                           gLEDS[num] = gGATES[num] = 0;
+                    continue;
+            // (touchOSC Control push, toggle)
+                } else if (!strncmp(msg[0].address+(len-1)-4, "push", 4) && (num != -1)) {     
+                    if(num > 4) continue;
+                    if(absv >= 1 || msg[2].i >= 1) gLEDS[num] = gGATES[num] = 1;
+                    else                           gLEDS[num] = gGATES[num] = 0;
+                    continue;
+                
+                } else if (!strncmp(msg[0].address+(len-1)-6, "toggle", 6) && (num != -1)) {
+                    if(num > 4) continue;
                     if(absv >= 1 || msg[2].i >= 1) gLEDS[num] = gGATES[num] = 1;
                     else                           gLEDS[num] = gGATES[num] = 0;
-                    break;
-                // (touchOSC Control push, toggle)
-                    } else if ((strncmp(msg[0].address+(len-1)-4, "push", 4)==0) && (num != -1)) {     
-                        if(num > 3) break;
-                        if(absv >= 1 || msg[2].i >= 1) gLEDS[num] = gGATES[num] = 1;
-                        else                           gLEDS[num] = gGATES[num] = 0;
-                        break;
-                    
-                        } else if ((strncmp(msg[0].address+(len-1)-6, "toggle", 6)==0) && (num != -1)) {
-                            if(num > 3) break;
-                            if(absv >= 1 || msg[2].i >= 1) gLEDS[num] = gGATES[num] = 1;
-                            else                           gLEDS[num] = gGATES[num] = 0;
-                            break;
-                            
-                            }
+                    continue;
+                        
+                } else if (!strncmp(msg[0].address,"/1/multipush",12) && (num != -1)) {
+                    if(num > 4) continue;
+                    if(absv >= 1 || msg[2].i >= 1) gLEDS[num] = gGATES[num] = 1;
+                    else                           gLEDS[num] = gGATES[num] = 0;
+                    continue;
+            // address pattern CV (Type Tag float)  
+                } else if(!strncmp(msg[0].address+(len-1)-2, "cv", 2) && (num != -1)) {
+                    if(num > 7) continue;
+                    if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
+                    continue;
+            // (touchOSC Control fader, rotary, xy, multixy, multifader)        
+                } else if (!strncmp(msg[0].address+(len-1)-5, "fader", 5) && (num != -1)) {
+                    if(num > 7) continue;                                              
+                    if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
+                    continue;
 
-                        // address pattern CV (Type Tag float)
-                            if((strncmp(msg[0].address+(len-1)-2, "cv", 2)==0) && (num != -1)) {
-                                if(num > 7) break;
-                                if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
-                                break;
-                            // (touchOSC Control fader, rotary, xy, multixy, multifader)
-                                } else if ((strncmp(msg[0].address+(len-1)-5, "fader", 5)==0) && (num != -1)) {
-                                    if(num > 7) break;                                                     
-                                    if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
-                                    break;
-
-                                    } else if ((strncmp(msg[0].address+(len-1)-6, "rotary", 6)==0) && (num != -1)) { 
-                                        if(num > 7) break;
-                                        if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
-                                        break;
-                    
-                                        } else if ((strncmp(msg[0].address+(len-1)-2, "xy", 2)==0) && (num != -1)) {
-                                            if(num > 7) break;
-                                            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);
-                                            break;
+                } else if (!strncmp(msg[0].address+(len-1)-6, "rotary", 6) && (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-1)-2, "xy", 2) && (num != -1)) {
+                    if(num > 7) continue;
+                    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-1)-9, "multixy1/", 9) && (num != -1)) {
+                    if(num > 7) continue;
+                    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-1)-12, "multifader1/", 12) && (num != -1)) {
+                    if(num > 7) continue;
+                    if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
+                    continue;
+            // (touchOSC multifader for Sequencer Mode)
+                } else if (!strncmp(msg[0].address+(len-1)-11, "sequencer1/", 11) && (num != -1)) {
+                    if(num > 7) continue;
+                    if(msg[1].typeTag[1] == 'f') gSeq_cv1[num] = msg[2].f * (SCALING_N);
+                    continue;
                             
-                                            } else if ((strncmp(msg[0].address+(len-1)-9, "multixy1/", 9)==0) && (num != -1)) {
-                                                if(num > 7) break;
-                                                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);
-                                                break;
-                                
-                                                } else if ((strncmp(msg[0].address+(len-1)-12, "multifader1/", 12)==0) && (num != -1)) {
-                                                    if(num > 7) break;
-                                                    if(msg[1].typeTag[1] == 'f') gOSC_cv[num] = msg[2].f * (SCALING_N);
-                                                    break;
-                                                // (touchOSC multifader for Sequencer Mode)
-                                                    } else if ((strncmp(msg[0].address+(len-1)-11, "sequencer1/", 11)==0) && (num != -1)) {
-                                                        if(num > 7) break;
-                                                        if(msg[1].typeTag[1] == 'f') gSeq_cv1[num] = msg[2].f * (SCALING_N);
-                                                        break;
-                                                    
-                                                        } else if ((strncmp(msg[0].address+(len-1)-11, "sequencer2/", 11)==0) && (num != -1)) {
-                                                            if(num > 7) break;
-                                                            if(msg[1].typeTag[1] == 'f') gSeq_cv2[num] = msg[2].f * (SCALING_N);
-                                                            break;
-                                                        }
+                } else if (!strncmp(msg[0].address+(len-1)-11, "sequencer2/", 11) && (num != -1)) {
+                    if(num > 7) continue;
+                    if(msg[1].typeTag[1] == 'f') gSeq_cv2[num] = msg[2].f * (SCALING_N);
+                    continue;
+            // address pattern for control
+                } else if (!strncmp(msg[0].address+(len-1)-6, "ctrlsw", 6) && (num != -1)) {
+                    if(num > 4) continue;
+                    if(absv >= 1 || msg[2].i >= 1) gCtrlSW[num] = true;
+                    else                           gCtrlSW[num] = false;
+                    continue;
                                                 
-                                                     // address pattern for control
-                                                        if ((strncmp(msg[0].address+(len-1)-6, "ctrlsw", 6)==0) && (num != -1)) {
-                                                            if(num > 4) break;
-                                                            if(absv >= 1 || msg[2].i >= 1) gCtrlSW[num] = 1;
-                                                            else                           gCtrlSW[num] = 0;
-                                                            break;
-                                                        
-                                                            } else if ((strncmp(msg[0].address+(len-1)-4, "ctrl", 4)==0) && (num != -1)) {
-                                                                if(num > 2) break;                                                     
-                                                                if(msg[1].typeTag[1] == 'f') gCtrl[num] = msg[2].f;
-                                                                break;
-                                                            }
-                                                        }                           
-                                                    }
-                                                }
+                } else if (!strncmp(msg[0].address+(len-1)-4, "ctrl", 4) && (num != -1)) {
+                    if(num > 3) continue;                                             
+                    if(msg[1].typeTag[1] == 'f') gCtrl[num] = msg[2].f;
+                    continue;
+                }
+                
+            } while(bundleflag);
+                    
+    }
+}