Browse Source

cmMidiFile: Debug Note duration calculation

master
kpl 12 years ago
parent
commit
b7191065bb
1 changed files with 28 additions and 26 deletions
  1. 28
    26
      cmMidiFile.c

+ 28
- 26
cmMidiFile.c View File

828
   struct _cmMidiVoice_str*  link;
828
   struct _cmMidiVoice_str*  link;
829
 } _cmMidiVoice_t;
829
 } _cmMidiVoice_t;
830
 
830
 
831
+
831
 void _cmMidFileCalcNoteDurationReleaseNote( _cmMidiVoice_t** listPtrPtr, _cmMidiVoice_t* pp, _cmMidiVoice_t* vp )
832
 void _cmMidFileCalcNoteDurationReleaseNote( _cmMidiVoice_t** listPtrPtr, _cmMidiVoice_t* pp, _cmMidiVoice_t* vp )
832
 {
833
 {
834
+  assert( (pp==NULL && vp==*listPtrPtr) || pp->link==vp);
835
+
833
   // store the duration of the note into the track msg 
836
   // store the duration of the note into the track msg 
834
   // assoc'd with the note-on msg
837
   // assoc'd with the note-on msg
835
   cmMidiChMsg_t* cmp = (cmMidiChMsg_t*)vp->mp->u.chMsgPtr; // cast away const
838
   cmMidiChMsg_t* cmp = (cmMidiChMsg_t*)vp->mp->u.chMsgPtr; // cast away const
836
   cmp->durTicks = vp->durTicks;
839
   cmp->durTicks = vp->durTicks;
837
 
840
 
841
+  _cmMidiVoice_t* np = vp->link;
842
+
843
+  // release the voice msg
844
+  cmMemFree(vp);
845
+
838
   // unlink the active voice msg
846
   // unlink the active voice msg
839
   if( pp == NULL )
847
   if( pp == NULL )
840
-    *listPtrPtr = vp->link;
848
+    *listPtrPtr = np;
841
   else
849
   else
842
-    pp->link = vp->link;
843
-  
844
-  // release the voice msg
845
-  cmMemFree(vp);
850
+    pp->link = np;
846
   
851
   
847
 }
852
 }
848
 
853
 
856
   if( p->msgN == 0 )
861
   if( p->msgN == 0 )
857
     return;
862
     return;
858
 
863
 
859
-  unsigned        mi;
860
-  _cmMidiVoice_t* list          = NULL;
861
-  _cmMidiVoice_t* vp;
864
+  unsigned        mi   = cmInvalidId;
865
+  _cmMidiVoice_t* list = NULL;             // list of active voices
866
+  _cmMidiVoice_t* vp   = NULL;
862
   bool            sustainFlagV[ kMidiChCnt ];
867
   bool            sustainFlagV[ kMidiChCnt ];
863
 
868
 
869
+  // clear the sustain pedal flag
864
   for(mi=0; mi<kMidiChCnt; ++mi)
870
   for(mi=0; mi<kMidiChCnt; ++mi)
865
     sustainFlagV[mi]=false;
871
     sustainFlagV[mi]=false;
866
 
872
 
867
   for(mi=0; mi<p->msgN; ++mi)
873
   for(mi=0; mi<p->msgN; ++mi)
868
   {
874
   {
869
-    cmMidiTrackMsg_t* mp = p->msgV[mi];
875
+    cmMidiTrackMsg_t* mp    = p->msgV[mi];
870
 
876
 
871
     // update the duration of the sounding notes
877
     // update the duration of the sounding notes
872
-    //int ii=0;
873
-    //printf("---- %i ------\n",mi);
874
     for(vp = list; vp!=NULL; vp=vp->link)
878
     for(vp = list; vp!=NULL; vp=vp->link)
875
-    {
876
-      vp->durTicks += mp->dtick;
877
-      //printf("%i %i %p %p\n",ii,vp->sustainFl,vp,vp->link);
878
-      //++ii;
879
-    }
880
-    
879
+      vp->durTicks += mp->dtick;    
881
 
880
 
882
     //
881
     //
883
     // If this is sustain pedal msg
882
     // If this is sustain pedal msg
884
     //
883
     //
885
     if( mp->status==kCtlMdId && mp->u.chMsgPtr->d0 == kSustainCtlMdId )
884
     if( mp->status==kCtlMdId && mp->u.chMsgPtr->d0 == kSustainCtlMdId )
886
     {
885
     {
887
-      assert( mp->u.chMsgPtr->ch < kMidiChCnt );
886
+      unsigned  chIdx = mp->u.chMsgPtr->ch;
887
+      assert(  chIdx < kMidiChCnt );
888
 
888
 
889
       // set the state of the sustain pedal flags
889
       // set the state of the sustain pedal flags
890
-      sustainFlagV[mp->u.chMsgPtr->ch] = mp->u.chMsgPtr->d1 >= 64;
890
+      sustainFlagV[chIdx] = mp->u.chMsgPtr->d1 >= 64;
891
 
891
 
892
       // if the pedal went up ...
892
       // if the pedal went up ...
893
-      if( sustainFlagV[mp->u.chMsgPtr->ch] == false )
893
+      if( sustainFlagV[chIdx] == false )
894
       {
894
       {
895
         // ... then release sustaining notes
895
         // ... then release sustaining notes
896
         _cmMidiVoice_t* pp = NULL;
896
         _cmMidiVoice_t* pp = NULL;
897
         for(vp=list; vp != NULL; )
897
         for(vp=list; vp != NULL; )
898
         {
898
         {
899
           _cmMidiVoice_t* np = vp->link;
899
           _cmMidiVoice_t* np = vp->link;
900
-          if( vp->sustainFl && (vp->mp->u.chMsgPtr->ch == mp->u.chMsgPtr->ch) )
900
+          if( vp->sustainFl && (vp->mp->u.chMsgPtr->ch == chIdx) )
901
             _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
901
             _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
902
           else
902
           else
903
             pp = vp;
903
             pp = vp;
925
       if( (mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1==0) || (mp->status==kNoteOffMdId) )
925
       if( (mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1==0) || (mp->status==kNoteOffMdId) )
926
       {
926
       {
927
         _cmMidiVoice_t* pp = NULL;
927
         _cmMidiVoice_t* pp = NULL;
928
+        unsigned        chIdx = mp->u.chMsgPtr->ch;
929
+        assert(  chIdx < kMidiChCnt );
930
+
928
 
931
 
929
         // for each active voice
932
         // for each active voice
930
         for(vp=list; vp!=NULL; vp=vp->link )
933
         for(vp=list; vp!=NULL; vp=vp->link )
931
         {
934
         {
932
           // if this active voice ch/pitch matches the note-off msg ch pitch 
935
           // if this active voice ch/pitch matches the note-off msg ch pitch 
933
-          if( (vp->mp->u.chMsgPtr->d0==mp->u.chMsgPtr->d0) && (vp->mp->u.chMsgPtr->ch==mp->u.chMsgPtr->ch) )
936
+          if( (vp->mp->u.chMsgPtr->d0==mp->u.chMsgPtr->d0) && (vp->mp->u.chMsgPtr->ch==chIdx) )
934
           {
937
           {
935
-            assert( mp->u.chMsgPtr->ch < kMidiChCnt );
936
-
937
-            if( sustainFlagV[mp->u.chMsgPtr->ch] )
938
+            // if the sustain pedal is down for this channel - then defer turning the note off
939
+            if( sustainFlagV[chIdx] )
938
               vp->sustainFl = true;
940
               vp->sustainFl = true;
939
             else
941
             else
940
               _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
942
               _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
942
           }
944
           }
943
 
945
 
944
           pp = vp;
946
           pp = vp;
945
-        } // end while
947
+        } // end for
946
       } // end if
948
       } // end if
947
 
949
 
948
   } // end-for
950
   } // end-for

Loading…
Cancel
Save