Browse Source

cmDspSys.h/c:Added _cmDspSysAssignUniqueInstSymId() to automatically assign

unique symbol id's to all DSP instances which were not assigned explicit symbol
id's.

Also added cmDspSysPrintPgm() to print the currently loaded program
network as a JSON file.
master
kpl 11 years ago
parent
commit
78faec2960
2 changed files with 189 additions and 6 deletions
  1. 186
    6
      dsp/cmDspSys.c
  2. 3
    0
      dsp/cmDspSys.h

+ 186
- 6
dsp/cmDspSys.c View File

@@ -317,6 +317,87 @@ cmDspRC_t cmDspSysLastRC( cmDspSysH_t h )
317 317
   return cmErrLastRC(&p->err);
318 318
 }
319 319
 
320
+
321
+// This function is assigns a unique symbol id (cmDspInst_t.symId)
322
+// to all DSP instances whose symId is set to cmInvalidId.
323
+cmDspRC_t _cmDspSysAssignUniqueInstSymId( cmDsp_t* p )
324
+{
325
+  typedef struct class_str
326
+  {
327
+    const cmChar_t* label;
328
+    unsigned        cnt;
329
+    struct class_str* link;
330
+  } class_t;
331
+
332
+
333
+  cmDspRC_t rc = kOkDspRC;
334
+  class_t*  map = NULL;
335
+
336
+  _cmDspInst_t* dip = p->instList;
337
+
338
+  for(; dip!=NULL; dip=dip->linkPtr)
339
+  {
340
+    cmDspInst_t* ip = dip->instPtr;
341
+    
342
+    // if this instance does not have a valid symbol id then create one
343
+    if( ip->symId == cmInvalidId )
344
+    {
345
+      // look for the instance class in map[]
346
+      class_t* mp = map;
347
+      for(; mp!=NULL; mp=mp->link)
348
+        if( strcmp(ip->classPtr->labelStr,mp->label) == 0 )
349
+          break;
350
+
351
+      // if map[] does not yet have a recd for this class ....
352
+      if( mp == NULL )
353
+      {
354
+        mp = cmMemAllocZ(class_t,1);  // ... then make one
355
+        mp->label = ip->classPtr->labelStr;
356
+        mp->link  = map;
357
+        map = mp;
358
+      }
359
+     
360
+      // generate a new unique symbol
361
+      while( ip->symId==cmInvalidId && mp->cnt != cmInvalidId )
362
+      {
363
+        // increment the instance count for this class
364
+        mp->cnt += 1;
365
+
366
+        // form a symbol label
367
+        cmChar_t* idStr = cmTsPrintfP(NULL,"%s-%i",ip->classPtr->labelStr,mp->cnt);
368
+
369
+        // register the new symbol
370
+        unsigned symId = cmSymTblRegisterSymbol(p->ctx.stH,idStr); 
371
+
372
+        cmMemFree(idStr); // the symbol label is no longer need
373
+
374
+        // if the symbol has not yet been used then it must be unique 
375
+        if( _cmDspSysInstSymIdToPtr(p,symId) == NULL )
376
+          ip->symId = symId;
377
+        else
378
+          cmSymTblRemove(p->ctx.stH,symId);  // ... otherwise remove the symbol and try again
379
+
380
+      }
381
+
382
+      // check for the very unlikely case that no unique symbol could be generated
383
+      if(mp->cnt == cmInvalidId )
384
+        rc = cmErrMsg(&p->err,kSymNotFoundDspRC,"All DSP instances were not assigned a unique symbol id.");
385
+
386
+    }
387
+  }
388
+
389
+  // free the class list
390
+  class_t* mp = map;
391
+  while( mp != NULL )
392
+  {
393
+    class_t* np = mp->link;
394
+    cmMemFree(mp);
395
+    mp = np;
396
+  }
397
+
398
+  return rc;
399
+}
400
+
320 401
 cmDspRC_t cmDspSysLoad( cmDspSysH_t  h,  cmAudioSysCtx_t* asCtx, unsigned pgmIdx )
321 402
 {
322 403
   cmDspRC_t       rc;
@@ -428,6 +509,8 @@ cmDspRC_t cmDspSysLoad( cmDspSysH_t  h,  cmAudioSysCtx_t* asCtx, unsigned pgmIdx
428 509
     goto errLabel;
429 510
   }
430 511
 
512
+  rc = _cmDspSysAssignUniqueInstSymId(p);
513
+
431 514
  errLabel:
432 515
   if( rc != kOkDspRC )
433 516
     cmDspSysUnload(h);
@@ -673,6 +756,90 @@ unsigned cmDspSysSyncState( cmDspSysH_t h )
673 756
   return p->syncState;
674 757
 }
675 758
 
759
+cmDspRC_t cmDspSysPrintPgm( cmDspSysH_t h, const cmChar_t* outFn )
760
+{
761
+  cmDsp_t*  p = _cmDspHandleToPtr(h);
762
+  cmDspRC_t rc = kOkDspRC;
763
+  cmJsonH_t jsH = cmJsonNullHandle;
764
+
765
+  if( cmJsonInitialize(&jsH, &p->cmCtx ) != kOkJsRC )
766
+    return cmErrMsg(&p->err,kJsonFailDspRC,"JSON output object create failed.");
767
+
768
+
769
+  // create the root object
770
+  cmJsonNode_t* onp = cmJsonCreateObject(jsH,NULL);
771
+  assert( onp != NULL );
772
+
773
+  // create the instance array
774
+  cmJsonNode_t* iap = cmJsonInsertPairArray(jsH, cmJsonRoot(jsH), "inst_array" );
775
+  assert( iap != NULL );
776
+  
777
+  // create the connection array
778
+  cmJsonNode_t* cap = cmJsonInsertPairArray(jsH, cmJsonRoot(jsH), "conn_array" );
779
+  assert( cap != NULL );
780
+
781
+  _cmDspInst_t* dip = p->instList;
782
+
783
+  for(; dip!=NULL; dip=dip->linkPtr)
784
+  {
785
+    cmDspInst_t* ip = dip->instPtr;
786
+
787
+    onp   = cmJsonCreateObject(jsH,iap);
788
+    
789
+    if( cmJsonInsertPairs(jsH, onp, 
790
+        "class", kStringTId, ip->classPtr->labelStr,
791
+        "label", kStringTId, cmSymTblLabel(p->ctx.stH,ip->symId),
792
+        "id",    kIntTId,    ip->symId,
793
+        NULL ) != kOkJsRC )
794
+    {
795
+      rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON DSP instance create failed.");
796
+      goto errLabel;
797
+    }
798
+
799
+    unsigned i;
800
+    for(i=0; i<ip->varCnt; ++i)
801
+    {
802
+      const cmDspVar_t* v = ip->varArray + i;
803
+
804
+      cmDspCb_t* cp = v->cbList;
805
+
806
+      for(; cp!=NULL; cp=cp->linkPtr)
807
+      {
808
+        const cmDspVar_t* dvar = cmDspVarIdToCPtr(cp->dstInstPtr, cp->dstVarId );
809
+
810
+        onp   = cmJsonCreateObject(jsH,cap);
811
+
812
+        assert(dvar != NULL);
813
+
814
+        if(cmJsonInsertPairs(jsH,onp,
815
+            "sid",  kStringTId, cmSymTblLabel(p->ctx.stH,ip->symId),
816
+            "svar", kStringTId, cmSymTblLabel(p->ctx.stH,v->symId),
817
+            "did",  kStringTId, cmSymTblLabel(p->ctx.stH,cp->dstInstPtr->symId),
818
+            "dvar", kStringTId, cmSymTblLabel(p->ctx.stH,dvar->symId),
819
+            NULL ) != kOkJsRC )
820
+        {
821
+          rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON DSP connect create failed.");
822
+          goto errLabel;
823
+        }
824
+      }
825
+    }
826
+  }
827
+
828
+  if( cmJsonWrite(jsH,NULL,outFn) != kOkJsRC )
829
+  {
830
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON file write failed on '%s.",cmStringNullGuard(outFn));
831
+    goto errLabel;
832
+  }
833
+
834
+  if( cmJsonFinalize(&jsH) != kOkJsRC )
835
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON tree release failed.");
836
+
837
+ errLabel:
838
+
839
+  return rc;
840
+}
841
+
842
+
676 843
 unsigned        cmDspSysPresetGroupCount( cmDspSysH_t h )
677 844
 { 
678 845
   cmDsp_t* p = _cmDspHandleToPtr(h);
@@ -1567,12 +1734,25 @@ cmDspInst_t* cmDspSysAllocScalar(     cmDspSysH_t h, const cmChar_t* label, cmRe
1567 1734
 { return _cmDspSysAllocScalar(h,cmInvalidId,label,min,max,step,val,label); }
1568 1735
 
1569 1736
 
1570
-cmChar_t*  _cmDspSysFormLabel( cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label )
1571
-{ return cmTsPrintfH(cmDspSysLHeap(h),"%s-%s",prefixLabel,label);  }
1737
+cmChar_t*  _cmDspSysFormLabel( cmDspSysH_t h, unsigned presetGrpSymId, const cmChar_t* prefixLabel, const cmChar_t* label )
1738
+{ 
1739
+  cmDsp_t* p = _cmDspHandleToPtr(h);
1740
+
1741
+  if( prefixLabel == NULL )
1742
+  {
1743
+    if(( prefixLabel = cmSymTblLabel(p->ctx.stH,presetGrpSymId)) == NULL )
1744
+      cmErrMsg(&p->err,kInvalidArgDspRC,"The prefix label was not given for the DSP instance '%s'.",cmStringNullGuard(label));
1745
+  }
1746
+
1747
+  if( label == NULL )
1748
+    cmErrMsg(&p->err,kInvalidArgDspRC,"NULL was passed where a DSP instance label was expected.");
1749
+
1750
+  return cmTsPrintfH(cmDspSysLHeap(h),"%s-%s",cmStringNullGuard(prefixLabel),cmStringNullGuard(label));  
1751
+}
1572 1752
 
1573 1753
 cmDspInst_t* cmDspSysAllocScalarP(     cmDspSysH_t h, unsigned presetGrpSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val )
1574 1754
 {
1575
-  return _cmDspSysAllocScalar(h,presetGrpSymId,_cmDspSysFormLabel(h,prefixLabel,label),min,max,step,val,label);
1755
+  return _cmDspSysAllocScalar(h,presetGrpSymId,_cmDspSysFormLabel(h,presetGrpSymId,prefixLabel,label),min,max,step,val,label);
1576 1756
 }
1577 1757
 
1578 1758
 cmDspInst_t* cmDspSysAllocScalarRsrc( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, const cmChar_t* rsrcPath )
@@ -1604,7 +1784,7 @@ cmDspInst_t* _cmDspSysAllocButton(     cmDspSysH_t h, unsigned btnTypeId, unsign
1604 1784
 
1605 1785
 cmDspInst_t* cmDspSysAllocButtonP(    cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
1606 1786
 {
1607
-  return cmDspSysAllocButton(h,_cmDspSysFormLabel(h,prefixLabel,label),val);
1787
+  return cmDspSysAllocButton(h,_cmDspSysFormLabel(h,cmInvalidId,prefixLabel,label),val);
1608 1788
 }
1609 1789
 
1610 1790
 cmDspInst_t* _cmDspSysAllocButtonRsrc( cmDspSysH_t h, unsigned typeId, unsigned presetGroupSymId, const cmChar_t* label, const cmChar_t* rsrcPath )
@@ -1630,7 +1810,7 @@ cmDspInst_t* cmDspSysAllocCheck(     cmDspSysH_t h, const cmChar_t* label, cmRea
1630 1810
 { return  _cmDspSysAllocCheck(h,cmInvalidId,label,label,val); }
1631 1811
 
1632 1812
 cmDspInst_t* cmDspSysAllocCheckP(    cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
1633
-{ return _cmDspSysAllocCheck(h,presetGroupSymId,_cmDspSysFormLabel(h,prefixLabel,label),label,val); }
1813
+{ return _cmDspSysAllocCheck(h,presetGroupSymId,_cmDspSysFormLabel(h,presetGroupSymId,prefixLabel,label),label,val); }
1634 1814
 
1635 1815
 cmDspInst_t* cmDspSysAllocCheckRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
1636 1816
 { return _cmDspSysAllocButtonRsrc( h,kCheckDuiId, cmInvalidId, label,rsrcPath); }
@@ -1642,7 +1822,7 @@ cmDspInst_t* cmDspSysAllocMsgListP( cmDspSysH_t h, unsigned presetGroupSymId, co
1642 1822
 { 
1643 1823
   cmDspInst_t* inst;
1644 1824
   cmChar_t* lbl;
1645
-  if((inst = cmDspSysAllocInstS( h, "MsgList", presetGroupSymId,  lbl = _cmDspSysFormLabel(h,preLabel,label), 3, rsrcLabel, fn, initSelIdx)) == NULL )
1825
+  if((inst = cmDspSysAllocInstS( h, "MsgList", presetGroupSymId,  lbl = _cmDspSysFormLabel(h,presetGroupSymId,preLabel,label), 3, rsrcLabel, fn, initSelIdx)) == NULL )
1646 1826
   {
1647 1827
     cmDsp_t* p = _cmDspHandleToPtr(h);
1648 1828
     cmErrMsg(&p->err,kAllocInstFailDspRC,"Msg List UI control allocation failed for '%s'.", cmStringNullGuard(lbl));

+ 3
- 0
dsp/cmDspSys.h View File

@@ -46,6 +46,9 @@ extern "C" {
46 46
   enum { kSyncPreDspId, kSyncPendingDspId, kSyncSuccessDspId, kSyncFailDspId };
47 47
   unsigned cmDspSysSyncState( cmDspSysH_t h );
48 48
 
49
+  // Print the loaded program (instances and connections) to a JSON file.
50
+  cmDspRC_t cmDspSysPrintPgm( cmDspSysH_t h, const cmChar_t* outFn );
51
+
49 52
   //----------------------------------------------------------------------------------------------------
50 53
   // Preset function:
51 54
   //

Loading…
Cancel
Save