浏览代码

cmScore.c : Completed implementation of cmScoreFileFromMidi().

master
kevin 10 年前
父节点
当前提交
9181a5d804
共有 1 个文件被更改,包括 90 次插入9 次删除
  1. 90
    9
      app/cmScore.c

+ 90
- 9
app/cmScore.c 查看文件

@@ -1961,6 +1961,7 @@ cmScRC_t      cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const c
1961 1961
   cmMidiFileH_t mfH = cmMidiFileNullHandle;
1962 1962
   cmCsvH_t      csvH = cmCsvNullHandle;
1963 1963
   cmErr_t       err;
1964
+  cmChar_t*     titles[] = {"id","trk","evt","opcode","dticks","micros","status","meta","ch","d0","d1","arg0","arg1","bar","skip","even","grace","tempo","t frac","dyn","section","remark", NULL };
1964 1965
   
1965 1966
   cmErrSetup(&err,&ctx->rpt,"MIDI to Score");
1966 1967
 
@@ -1976,25 +1977,56 @@ cmScRC_t      cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const c
1976 1977
   // Convert the track message 'dtick' field to delta-microseconds.
1977 1978
   cmMidiFileTickToMicros(mfH);
1978 1979
 
1980
+
1979 1981
   unsigned                 msgCnt = cmMidiFileMsgCount(mfH);
1980 1982
   unsigned                 i;
1981 1983
   const cmMidiTrackMsg_t** tmpp   = cmMidiFileMsgArray(mfH);
1982 1984
   double                   acc_secs = 0;
1985
+  unsigned                lexTId = 0;
1986
+  cmCsvCell_t*            cp     = NULL;
1987
+    
1988
+
1989
+  if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymText(csvH,titles[0]), kStrCsvTFl, lexTId ) != kOkCsvRC )
1990
+  {
1991
+    cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
1992
+    goto errLabel;
1993
+  }
1994
+
1995
+  for(i=1; titles[i]!=NULL; ++i)
1996
+  {
1997
+    if( cmCsvInsertTextColAfter(csvH, cp, &cp, titles[i], lexTId ) != kOkCsvRC )
1998
+    {
1999
+      cmErrMsg(&err,kCsvFailScRC,"Error inserting column index '%i' label in '%s'.",i,cmStringNullGuard(scoreFn));
2000
+      goto errLabel;
2001
+    }
2002
+
2003
+  }    
2004
+
2005
+
2006
+
1983 2007
   for(i=0; i<msgCnt; ++i)
1984 2008
   {
1985 2009
     const cmMidiTrackMsg_t* tmp    = tmpp[i];
1986
-    cmCsvCell_t*            cp     = NULL;
1987
-    unsigned                lexTId = 0;
1988 2010
     const cmChar_t*         opStr  = NULL;
1989
-    unsigned                midiCh  = 0;
2011
+    unsigned                midiCh = 0;
1990 2012
     unsigned                d0     = 0;
1991 2013
     unsigned                d1     = 0;
1992
-    double dsecs = (double)tmp->dtick / 1000000.0;
1993
-    
2014
+    unsigned metaId = 0;
2015
+    double   dsecs  = (double)tmp->dtick / 1000000.0;
2016
+
1994 2017
     acc_secs += dsecs;
1995 2018
 
1996
-    if( tmp->status == 0xff )
1997
-      opStr = cmMidiMetaStatusToLabel(tmp->metaId);
2019
+    if( tmp->status == kMetaStId )
2020
+    {
2021
+      opStr  = cmMidiMetaStatusToLabel(tmp->metaId);
2022
+      metaId = tmp->metaId;
2023
+
2024
+      switch( tmp->metaId )
2025
+      {
2026
+        case kTempoMdId:
2027
+          d0 = tmp->u.iVal;
2028
+      }
2029
+    }
1998 2030
     else
1999 2031
     {
2000 2032
       opStr = cmMidiStatusToLabel(tmp->status);
@@ -2006,6 +2038,12 @@ cmScRC_t      cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const c
2006 2038
       }
2007 2039
     }
2008 2040
 
2041
+    cp = NULL;
2042
+
2043
+    // skip note-off messages
2044
+    if( tmp->status == kNoteOffMdId )
2045
+      continue;
2046
+
2009 2047
     if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymUInt(csvH,i), kIntCsvTFl, lexTId ) != kOkCsvRC )
2010 2048
     {
2011 2049
       cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
@@ -2042,13 +2080,13 @@ cmScRC_t      cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const c
2042 2080
       goto errLabel;
2043 2081
     }
2044 2082
     
2045
-    if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
2083
+    if( cmCsvInsertHexColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
2046 2084
     {
2047 2085
       cmErrMsg(&err,kCsvFailScRC,"Error inserting 'status' column in '%s'.",cmStringNullGuard(scoreFn));
2048 2086
       goto errLabel;
2049 2087
     }
2050 2088
 
2051
-    if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->metaId, lexTId ) != kOkCsvRC )
2089
+    if( cmCsvInsertUIntColAfter(csvH, cp, &cp, metaId, lexTId ) != kOkCsvRC )
2052 2090
     {
2053 2091
       cmErrMsg(&err,kCsvFailScRC,"Error inserting 'meta' column in '%s'.",cmStringNullGuard(scoreFn));
2054 2092
       goto errLabel;
@@ -2072,9 +2110,52 @@ cmScRC_t      cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const c
2072 2110
       goto errLabel;
2073 2111
     }
2074 2112
 
2113
+      switch( tmp->status )
2114
+      {
2115
+        case kNoteOnMdId:
2116
+          if( cmCsvInsertTextColAfter(csvH, cp, &cp, cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0), lexTId ) != kOkCsvRC )
2117
+          {
2118
+            cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
2119
+            goto errLabel;
2120
+          }
2121
+
2122
+        case kMetaStId:
2123
+          switch( tmp->metaId )
2124
+          {
2125
+            case kTimeSigMdId:
2126
+              if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->num, lexTId ) != kOkCsvRC )
2127
+              {
2128
+                cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. numerator column in '%s'.",cmStringNullGuard(scoreFn));
2129
+                goto errLabel;
2130
+              }
2131
+
2132
+              if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->den, lexTId ) != kOkCsvRC )
2133
+              {
2134
+                cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. denominator column in '%s'.",cmStringNullGuard(scoreFn));
2135
+                goto errLabel;
2136
+              }
2137
+              break;
2138
+
2139
+            case kTempoMdId:
2140
+              if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 60000000/tmp->u.iVal, lexTId ) != kOkCsvRC )
2141
+              {
2142
+                cmErrMsg(&err,kCsvFailScRC,"Error inserting 'tempo' in '%s'.",cmStringNullGuard(scoreFn));
2143
+                goto errLabel;
2144
+              }
2145
+              break;
2146
+          }
2147
+
2148
+      }
2149
+
2075 2150
 
2076 2151
   }
2077 2152
   
2153
+  if(  cmCsvWrite(csvH,scoreFn) != kOkCsvRC )
2154
+  {
2155
+    cmErrMsg(&err,kCsvFailScRC,"The score output file '%s' could not be written.",cmStringNullGuard(scoreFn));
2156
+    goto errLabel;
2157
+  }
2158
+
2078 2159
  errLabel:
2079 2160
    cmMidiFileClose(&mfH);
2080 2161
    cmCsvFinalize(&csvH);

正在加载...
取消
保存