Browse Source

cmMidiFile: Debug Note duration calculation

master
kpl 11 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,21 +828,26 @@ typedef struct _cmMidiVoice_str
828 828
   struct _cmMidiVoice_str*  link;
829 829
 } _cmMidiVoice_t;
830 830
 
831
+
831 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 836
   // store the duration of the note into the track msg 
834 837
   // assoc'd with the note-on msg
835 838
   cmMidiChMsg_t* cmp = (cmMidiChMsg_t*)vp->mp->u.chMsgPtr; // cast away const
836 839
   cmp->durTicks = vp->durTicks;
837 840
 
841
+  _cmMidiVoice_t* np = vp->link;
842
+
843
+  // release the voice msg
844
+  cmMemFree(vp);
845
+
838 846
   // unlink the active voice msg
839 847
   if( pp == NULL )
840
-    *listPtrPtr = vp->link;
848
+    *listPtrPtr = np;
841 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,48 +861,43 @@ void cmMidiFileCalcNoteDurations( cmMidiFileH_t h )
856 861
   if( p->msgN == 0 )
857 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 867
   bool            sustainFlagV[ kMidiChCnt ];
863 868
 
869
+  // clear the sustain pedal flag
864 870
   for(mi=0; mi<kMidiChCnt; ++mi)
865 871
     sustainFlagV[mi]=false;
866 872
 
867 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 877
     // update the duration of the sounding notes
872
-    //int ii=0;
873
-    //printf("---- %i ------\n",mi);
874 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 882
     // If this is sustain pedal msg
884 883
     //
885 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 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 892
       // if the pedal went up ...
893
-      if( sustainFlagV[mp->u.chMsgPtr->ch] == false )
893
+      if( sustainFlagV[chIdx] == false )
894 894
       {
895 895
         // ... then release sustaining notes
896 896
         _cmMidiVoice_t* pp = NULL;
897 897
         for(vp=list; vp != NULL; )
898 898
         {
899 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 901
             _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
902 902
           else
903 903
             pp = vp;
@@ -925,16 +925,18 @@ void cmMidiFileCalcNoteDurations( cmMidiFileH_t h )
925 925
       if( (mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1==0) || (mp->status==kNoteOffMdId) )
926 926
       {
927 927
         _cmMidiVoice_t* pp = NULL;
928
+        unsigned        chIdx = mp->u.chMsgPtr->ch;
929
+        assert(  chIdx < kMidiChCnt );
930
+
928 931
 
929 932
         // for each active voice
930 933
         for(vp=list; vp!=NULL; vp=vp->link )
931 934
         {
932 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 940
               vp->sustainFl = true;
939 941
             else
940 942
               _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
@@ -942,7 +944,7 @@ void cmMidiFileCalcNoteDurations( cmMidiFileH_t h )
942 944
           }
943 945
 
944 946
           pp = vp;
945
-        } // end while
947
+        } // end for
946 948
       } // end if
947 949
 
948 950
   } // end-for

Loading…
Cancel
Save