Ver código fonte

cmDspBuiltIn.c : cmDspMidiIn now accepts a MIDI device/port to filter incoming MIDI messages.

master
kevin 7 anos atrás
pai
commit
b7ab42065b
1 arquivos alterados com 96 adições e 25 exclusões
  1. 96
    25
      dsp/cmDspBuiltIn.c

+ 96
- 25
dsp/cmDspBuiltIn.c Ver arquivo

@@ -602,6 +602,8 @@ cmDspClass_t*  cmSigGenClassCons( cmDspCtx_t* ctx )
602 602
 //( { label:cmDspMidiIn file_desc:"MIDI input port." kw:[sunit] }
603 603
 enum
604 604
 {
605
+  kInDeviceMiId,
606
+  kInPortMiId,
605 607
   kDeviceMiId,
606 608
   kPortMiId,
607 609
   kSmpIdxMiId,
@@ -617,16 +619,53 @@ cmDspClass_t _cmMidiInDC;
617 619
 
618 620
 typedef struct
619 621
 {
620
-  cmDspInst_t inst;
621
-  unsigned midiSymId;
622
-  unsigned prevSmpIdx;
622
+  cmDspInst_t  inst;
623
+  unsigned     midiSymId;
624
+  unsigned     prevSmpIdx;
623 625
   cmTimeSpec_t prevTimeStamp;
626
+  unsigned     inDevIdx;
627
+  unsigned     inPortIdx;
624 628
 } cmDspMidiIn_t;
625 629
 
630
+
631
+cmDspRC_t _cmDspMidiInSetDevice( cmDspCtx_t* ctx, cmDspMidiIn_t* p, const cmChar_t* deviceStr )
632
+{
633
+  cmDspRC_t rc = kOkDspRC;
634
+
635
+  if( deviceStr != NULL )
636
+  {
637
+    if((p->inDevIdx = cmMpDeviceNameToIndex(deviceStr)) == cmInvalidIdx )
638
+      rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI input device '%s' could not be found.",cmStringNullGuard(deviceStr));
639
+    p->inPortIdx = cmInvalidIdx; // set the port as invalid until it is explicitely set.
640
+  }
641
+  return rc;
642
+}
643
+
644
+cmDspRC_t _cmDspMidiInSetPort( cmDspCtx_t* ctx, cmDspMidiIn_t* p, const cmChar_t* portStr )
645
+{
646
+  cmDspRC_t rc = kOkDspRC;
647
+
648
+  if( portStr == NULL )
649
+    return rc;
650
+
651
+  if( p->inDevIdx == cmInvalidIdx )
652
+    rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI input port cannot be set until the MIDI input device is set.");
653
+  else
654
+  {
655
+    if((p->inPortIdx = cmMpDevicePortNameToIndex(p->inDevIdx,kOutMpFl,portStr)) == cmInvalidIdx )
656
+      rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI input port '%s' could not be found on device '%s'.",cmStringNullGuard(portStr),cmStringNullGuard(cmMpDeviceName(p->inDevIdx)));
657
+  }
658
+
659
+  return rc;
660
+}
661
+
626 662
 cmDspInst_t*  _cmDspMidiInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
627 663
 {
628 664
   cmDspVarArg_t args[] =
629 665
   {
666
+    { "indev",  kInDeviceMiId, 0,  0,  kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Listen only to this MIDI device"},
667
+    { "inport", kInPortMiId,   0,  0,  kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Listen only to this MIDI port"},
668
+    
630 669
     { "device", kDeviceMiId, 0,  0,  kOutDsvFl | kUIntDsvFl, "MIDI device" },
631 670
     { "port",   kPortMiId,   0,  0,  kOutDsvFl | kUIntDsvFl, "MIDI device port"},
632 671
     { "smpidx", kSmpIdxMiId, 0,  0,  kOutDsvFl | kUIntDsvFl, "Message time tag as sample index."},
@@ -640,6 +679,9 @@ cmDspInst_t*  _cmDspMidiInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigne
640 679
 
641 680
   cmDspMidiIn_t* p = cmDspInstAlloc(cmDspMidiIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
642 681
 
682
+  p->inDevIdx  = cmInvalidIdx;
683
+  p->inPortIdx = cmInvalidIdx;
684
+  
643 685
  p->midiSymId =  cmDspSysAssignInstAttrSymbolStr( ctx->dspH, &p->inst, "_midi" );
644 686
 
645 687
   return &p->inst;
@@ -649,43 +691,72 @@ cmDspRC_t _cmDspMidiInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t
649 691
 {
650 692
   cmDspRC_t      rc = kOkDspRC;
651 693
   cmDspMidiIn_t* p  = (cmDspMidiIn_t*)inst;
694
+  
652 695
   cmDspApplyAllDefaults(ctx,inst);
696
+
697
+  if(_cmDspMidiInSetDevice(ctx,p,cmDspStrcz(inst,kInDeviceMiId)) == kOkDspRC )
698
+    _cmDspMidiInSetPort(  ctx,p,cmDspStrcz(inst,kInPortMiId));
699
+  
653 700
   p->prevSmpIdx = 0;
654 701
   return rc;
655 702
 } 
656 703
 
704
+cmDspRC_t _cmDspMidiInRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
705
+{
706
+  cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
707
+
708
+  switch( evt->dstVarId )
709
+  {
710
+    case kInDeviceMiId:      
711
+      _cmDspMidiInSetDevice(ctx, p, cmDsvStrcz(evt->valuePtr));
712
+      break;
713
+
714
+    case kInPortMiId:
715
+      _cmDspMidiInSetPort(ctx, p, cmDsvStrcz(evt->valuePtr));
716
+      break;
717
+  }
718
+    
719
+  return kOkDspRC;
720
+
721
+}
722
+
657 723
 cmDspRC_t  _cmDspMidiInRecvFunc(   cmDspCtx_t* ctx, cmDspInst_t* inst,  unsigned attrSymId, const cmDspValue_t* value )
658 724
 {
659 725
   cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
660 726
 
727
+  
661 728
   if( attrSymId == p->midiSymId )
662 729
   {
663 730
     cmMidiPacket_t* pkt = (cmMidiPacket_t*)(value->u.m.u.vp);
664 731
     unsigned i;
665 732
 
666
-    cmDspSetUInt(ctx, inst, kDeviceMiId, pkt->devIdx);
667
-    cmDspSetUInt(ctx, inst, kPortMiId,   pkt->portIdx); 
668
-
669
-    for(i=0; i<pkt->msgCnt; ++i)
733
+    if( (p->inDevIdx==cmInvalidIdx || p->inDevIdx==pkt->devIdx) && (p->inPortIdx==cmInvalidIdx || p->inPortIdx==pkt->portIdx ) )
670 734
     {
671
-      cmMidiMsg* m = pkt->msgArray + i;
672
-      unsigned   deltaSmpCnt = 0;
673
-      if( p->prevTimeStamp.tv_sec!=0 && p->prevTimeStamp.tv_nsec!=0 )
674
-        deltaSmpCnt = floor(cmTimeElapsedMicros(&p->prevTimeStamp,&m->timeStamp) * cmDspSampleRate(ctx) / 1000000.0);
735
+    
736
+      cmDspSetUInt(ctx, inst, kDeviceMiId, pkt->devIdx);
737
+      cmDspSetUInt(ctx, inst, kPortMiId,   pkt->portIdx); 
675 738
 
676
-      if( p->prevSmpIdx == 0 )
677
-        p->prevSmpIdx = ctx->cycleCnt * cmDspSamplesPerCycle(ctx);
678
-      else
679
-        p->prevSmpIdx += deltaSmpCnt;
739
+      for(i=0; i<pkt->msgCnt; ++i)
740
+      {
741
+        cmMidiMsg* m = pkt->msgArray + i;
742
+        unsigned   deltaSmpCnt = 0;
743
+        if( p->prevTimeStamp.tv_sec!=0 && p->prevTimeStamp.tv_nsec!=0 )
744
+          deltaSmpCnt = floor(cmTimeElapsedMicros(&p->prevTimeStamp,&m->timeStamp) * cmDspSampleRate(ctx) / 1000000.0);
745
+
746
+        if( p->prevSmpIdx == 0 )
747
+          p->prevSmpIdx = ctx->cycleCnt * cmDspSamplesPerCycle(ctx);
748
+        else
749
+          p->prevSmpIdx += deltaSmpCnt;
680 750
 
681
-      cmDspSetUInt(ctx, inst, kSmpIdxMiId, p->prevSmpIdx );
682
-      cmDspSetUInt(ctx, inst, kSecMiId,    m->timeStamp.tv_sec);
683
-      cmDspSetUInt(ctx, inst, kNSecMiId,   m->timeStamp.tv_nsec);
684
-      cmDspSetUInt(ctx, inst, kD1MiId,     m->d1 );
685
-      cmDspSetUInt(ctx, inst, kD0MiId,     m->d0 );
686
-      cmDspSetUInt(ctx, inst, kStatusMiId, m->status );
751
+        cmDspSetUInt(ctx, inst, kSmpIdxMiId, p->prevSmpIdx );
752
+        cmDspSetUInt(ctx, inst, kSecMiId,    m->timeStamp.tv_sec);
753
+        cmDspSetUInt(ctx, inst, kNSecMiId,   m->timeStamp.tv_nsec);
754
+        cmDspSetUInt(ctx, inst, kD1MiId,     m->d1 );
755
+        cmDspSetUInt(ctx, inst, kD0MiId,     m->d0 );
756
+        cmDspSetUInt(ctx, inst, kStatusMiId, m->status );
687 757
 
688
-      p->prevTimeStamp = m->timeStamp;
758
+        p->prevTimeStamp = m->timeStamp;
759
+      }
689 760
     }
690 761
   }
691 762
 
@@ -700,7 +771,7 @@ cmDspClass_t*  cmMidiInClassCons( cmDspCtx_t* ctx )
700 771
     NULL,
701 772
     _cmDspMidiInReset,
702 773
     NULL,
703
-    NULL,
774
+    _cmDspMidiInRecv,
704 775
     NULL,
705 776
     _cmDspMidiInRecvFunc,
706 777
     "Midi input port");
@@ -737,7 +808,7 @@ cmDspRC_t _cmDspMidiOutSetDevice( cmDspCtx_t* ctx, cmDspMidiOut_t* p, const cmCh
737 808
 
738 809
   if( deviceStr != NULL )
739 810
     if((p->devIdx = cmMpDeviceNameToIndex(deviceStr)) == cmInvalidIdx )
740
-      rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI device '%s' could not be found.",cmStringNullGuard(deviceStr));
811
+      rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI output device '%s' could not be found.",cmStringNullGuard(deviceStr));
741 812
   return rc;
742 813
 }
743 814
 
@@ -753,7 +824,7 @@ cmDspRC_t _cmDspMidiOutSetPort( cmDspCtx_t* ctx, cmDspMidiOut_t* p, const cmChar
753 824
   else
754 825
   {
755 826
     if((p->portIdx = cmMpDevicePortNameToIndex(p->devIdx,kOutMpFl,portStr)) == cmInvalidIdx )
756
-      rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI port '%s' could not be found on device '%s'.",cmStringNullGuard(portStr),cmStringNullGuard(cmMpDeviceName(p->devIdx)));
827
+      rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI out port '%s' could not be found on device '%s'.",cmStringNullGuard(portStr),cmStringNullGuard(cmMpDeviceName(p->devIdx)));
757 828
   }
758 829
 
759 830
   return rc;

Carregando…
Cancelar
Salvar