Browse Source

cmMidi.h/c: Added conversion to/from 14bit values and Pitch Bend.

master
kevin 11 years ago
parent
commit
7859bd0a8c
2 changed files with 147 additions and 118 deletions
  1. 25
    0
      cmMidi.c
  2. 122
    118
      cmMidi.h

+ 25
- 0
cmMidi.c View File

@@ -121,6 +121,31 @@ cmMidiByte_t cmMidiStatusToByteCount( cmMidiByte_t status )
121 121
   return 0; 
122 122
 }
123 123
 
124
+unsigned      cmMidiTo14Bits( cmMidiByte_t d0, cmMidiByte_t d1 )
125
+{
126
+  unsigned val = d0;
127
+  val <<= 7;
128
+  val += d1;
129
+  return val;
130
+}
131
+
132
+void          cmMidiSplit14Bits( unsigned v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref )
133
+{
134
+  *d0Ref = (v & 0x3f80) >> 7;
135
+  *d1Ref = v & 0x7f;
136
+}
137
+
138
+int           cmMidiToPbend(  cmMidiByte_t d0, cmMidiByte_t d1 )
139
+{
140
+  int v = cmMidiTo14Bits(d0,d1);
141
+  return v - 8192;
142
+}
143
+
144
+void          cmMidiSplitPbend( int v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref )
145
+{
146
+  unsigned uv = v + 8192;
147
+  cmMidiSplit14Bits(uv,d0Ref,d1Ref);
148
+}
124 149
 
125 150
 //====================================================================================================
126 151
 const char*     cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt )

+ 122
- 118
cmMidi.h View File

@@ -5,133 +5,137 @@
5 5
 extern "C" {
6 6
 #endif
7 7
 
8
-enum
9
-{
10
-  kMidiChCnt           = 16,
11
-  kInvalidMidiByte     = 128,
12
-  kMidiNoteCnt         = kInvalidMidiByte,
13
-  kMidiCtlCnt          = kInvalidMidiByte,
14
-  kMidiPgmCnt          = kInvalidMidiByte,
15
-  kInvalidMidiPitch    = kInvalidMidiByte,
16
-  kInvalidMidiVelocity = kInvalidMidiByte,
17
-  kInvalidMidiCtl      = kInvalidMidiByte,
18
-  kInvalidMidiPgm      = kInvalidMidiByte,
19
-  kMidiSciPitchCharCnt = 5  // A#-1
20
-};
21
-
22
-
23
-// MIDI status bytes
24
-enum
25
-{
26
-  kInvalidStatusMdId = 0x00,
27
-  kNoteOffMdId       = 0x80,
28
-  kNoteOnMdId        = 0x90,
29
-  kPolyPresMdId      = 0xa0,
30
-  kCtlMdId           = 0xb0,
31
-  kPgmMdId           = 0xc0,
32
-  kChPresMdId        = 0xd0,
33
-  kPbendMdId         = 0xe0,
34
-  kSysExMdId         = 0xf0,
35
-
36
-  kSysComMtcMdId     = 0xf1,
37
-  kSysComSppMdId     = 0xf2,
38
-  kSysComSelMdId     = 0xf3,
39
-  kSysComUndef0MdId  = 0xf4,
40
-  kSysComUndef1MdId  = 0xf5,
41
-  kSysComTuneMdId    = 0xf6,
42
-  kSysComEoxMdId     = 0xf7,
43
-
44
-  kSysRtClockMdId  = 0xf8,
45
-  kSysRtUndef0MdId = 0xf9,
46
-  kSysRtStartMdId  = 0xfa,
47
-  kSysRtContMdId   = 0xfb,
48
-  kSysRtStopMdId   = 0xfc,
49
-  kSysRtUndef1MdId = 0xfd,
50
-  kSysRtSenseMdId  = 0xfe,
51
-  kSysRtResetMdId  = 0xff,
52
-  kMetaStId        = 0xff,
53
-
54
-  kSeqNumbMdId     = 0x00,
55
-  kTextMdId        = 0x01,
56
-  kCopyMdId        = 0x02,
57
-  kTrkNameMdId     = 0x03,
58
-  kInstrNameMdId   = 0x04,
59
-  kLyricsMdId      = 0x05,
60
-  kMarkerMdId      = 0x06,
61
-  kCuePointMdId    = 0x07,
62
-  kMidiChMdId      = 0x20,
63
-  kEndOfTrkMdId    = 0x2f,
64
-  kTempoMdId       = 0x51,
65
-  kSmpteMdId       = 0x54,
66
-  kTimeSigMdId     = 0x58,
67
-  kKeySigMdId      = 0x59,
68
-  kSeqSpecMdId     = 0x7f,
69
-  kInvalidMetaMdId = 0x80,
70
-
71
-  kSustainCtlMdId = 64
8
+  enum
9
+  {
10
+    kMidiChCnt           = 16,
11
+    kInvalidMidiByte     = 128,
12
+    kMidiNoteCnt         = kInvalidMidiByte,
13
+    kMidiCtlCnt          = kInvalidMidiByte,
14
+    kMidiPgmCnt          = kInvalidMidiByte,
15
+    kInvalidMidiPitch    = kInvalidMidiByte,
16
+    kInvalidMidiVelocity = kInvalidMidiByte,
17
+    kInvalidMidiCtl      = kInvalidMidiByte,
18
+    kInvalidMidiPgm      = kInvalidMidiByte,
19
+    kMidiSciPitchCharCnt = 5  // A#-1
20
+  };
21
+
22
+
23
+  // MIDI status bytes
24
+  enum
25
+  {
26
+    kInvalidStatusMdId = 0x00,
27
+    kNoteOffMdId       = 0x80,
28
+    kNoteOnMdId        = 0x90,
29
+    kPolyPresMdId      = 0xa0,
30
+    kCtlMdId           = 0xb0,
31
+    kPgmMdId           = 0xc0,
32
+    kChPresMdId        = 0xd0,
33
+    kPbendMdId         = 0xe0,
34
+    kSysExMdId         = 0xf0,
35
+
36
+    kSysComMtcMdId     = 0xf1,
37
+    kSysComSppMdId     = 0xf2,
38
+    kSysComSelMdId     = 0xf3,
39
+    kSysComUndef0MdId  = 0xf4,
40
+    kSysComUndef1MdId  = 0xf5,
41
+    kSysComTuneMdId    = 0xf6,
42
+    kSysComEoxMdId     = 0xf7,
43
+
44
+    kSysRtClockMdId  = 0xf8,
45
+    kSysRtUndef0MdId = 0xf9,
46
+    kSysRtStartMdId  = 0xfa,
47
+    kSysRtContMdId   = 0xfb,
48
+    kSysRtStopMdId   = 0xfc,
49
+    kSysRtUndef1MdId = 0xfd,
50
+    kSysRtSenseMdId  = 0xfe,
51
+    kSysRtResetMdId  = 0xff,
52
+    kMetaStId        = 0xff,
53
+
54
+    kSeqNumbMdId     = 0x00,
55
+    kTextMdId        = 0x01,
56
+    kCopyMdId        = 0x02,
57
+    kTrkNameMdId     = 0x03,
58
+    kInstrNameMdId   = 0x04,
59
+    kLyricsMdId      = 0x05,
60
+    kMarkerMdId      = 0x06,
61
+    kCuePointMdId    = 0x07,
62
+    kMidiChMdId      = 0x20,
63
+    kEndOfTrkMdId    = 0x2f,
64
+    kTempoMdId       = 0x51,
65
+    kSmpteMdId       = 0x54,
66
+    kTimeSigMdId     = 0x58,
67
+    kKeySigMdId      = 0x59,
68
+    kSeqSpecMdId     = 0x7f,
69
+    kInvalidMetaMdId = 0x80,
70
+
71
+    kSustainCtlMdId = 64
72 72
   
73
-};
73
+  };
74 74
 
75 75
 
76
-typedef unsigned char cmMidiByte_t;
76
+  typedef unsigned char cmMidiByte_t;
77 77
 
78
-//===============================================================================================
79
-// Utility Functions
80
-//
78
+  //===============================================================================================
79
+  // Utility Functions
80
+  //
81 81
 
82 82
 #define cmMidiIsStatus( s )   (kNoteOffMdId <= (s) /*&& ((unsigned)(s)) <= kSysRtResetMdId*/ )
83 83
 #define cmMidiIsChStatus( s ) (kNoteOffMdId <= (s) && (s) <  kSysExMdId)
84 84
 
85 85
 
86
-const char*   cmMidiStatusToLabel(     cmMidiByte_t status );
87
-const char*   cmMidiMetaStatusToLabel( cmMidiByte_t metaStatus );
88
-
89
-// Returns kInvalidMidiByte if status is not a valid status byte
90
-cmMidiByte_t    cmMidiStatusToByteCount( cmMidiByte_t status );
91
-
92
-
93
-//===============================================================================================
94
-// MIDI Communication data types
95
-//
96
-
97
-typedef struct 
98
-{
99
-  unsigned     deltaUs; // time since last MIDI msg in microseconds
100
-  cmMidiByte_t status;  // midi status byte
101
-  cmMidiByte_t d0;      // midi data byte 0
102
-  cmMidiByte_t d1;      // midi data byte 1
103
-  cmMidiByte_t pad;
104
-} cmMidiMsg;
105
-
106
-typedef struct
107
-{
108
-  void*         cbDataPtr; // application supplied reference value from mdParserCreate()
109
-  unsigned      devIdx;    // the device the msg originated from
110
-  unsigned      portIdx;   // the port index on the source device
111
-  cmMidiMsg*    msgArray;  // pointer to an array of 'msgCnt' mdMsg records or NULL if sysExMsg is non-NULL
112
-  cmMidiByte_t* sysExMsg;  // pointer to a sys-ex msg or NULL if msgArray is non-NULL (see note below)
113
-  unsigned      msgCnt;    // count of mdMsg records or sys-ex bytes
114
-} cmMidiPacket_t;
115
-
116
-// Notes: If the sys-ex message can be contained in a single msg then
117
-// then the first msg byte is kSysExMdId and the last is kSysComEoxMdId.
118
-// If the sys-ex message is broken into multiple pieces then only the
119
-// first will begin with kSysExMdId and the last will end with kSysComEoxMdId.
120
-
121
-
122
-// If label is NULL or labelCharCnt==0 then a pointer to an internal static
123
-// buffer is returned. If label[] is given the it
124
-// should have at least 5 (kMidiPitchCharCnt) char's (including the terminating zero).
125
-// If 'pitch' is outside of the range 0-127 then a blank string is returned.
126
-const char*     cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt );
127
-
128
-
129
-// Scientific pitch string: [A-Ga-g][#b][#] where  # may be -1 to 9.
130
-// Return kInvalidMidiPitch if sciPtichStr does not contain a valid 
131
-// scientific pitch string. This function will convert C-1 to G9 to 
132
-// valid MIDI pitch values 0 to 127.  Scientific pitch strings outside
133
-// of this range will be returned as kInvalidMidiPitch.   
134
-cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr );
86
+  const char*   cmMidiStatusToLabel(     cmMidiByte_t status );
87
+  const char*   cmMidiMetaStatusToLabel( cmMidiByte_t metaStatus );
88
+
89
+  // Returns kInvalidMidiByte if status is not a valid status byte
90
+  cmMidiByte_t  cmMidiStatusToByteCount( cmMidiByte_t status );
91
+
92
+  unsigned      cmMidiTo14Bits( cmMidiByte_t d0, cmMidiByte_t d1 );
93
+  void          cmMidiSplit14Bits( unsigned v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref );
94
+  int           cmMidiToPbend(  cmMidiByte_t d0, cmMidiByte_t d1 );
95
+  void          cmMidiSplitPbend( int v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref ); 
96
+
97
+  //===============================================================================================
98
+  // MIDI Communication data types
99
+  //
100
+
101
+  typedef struct 
102
+  {
103
+    unsigned     deltaUs; // time since last MIDI msg in microseconds
104
+    cmMidiByte_t status;  // midi status byte
105
+    cmMidiByte_t d0;      // midi data byte 0
106
+    cmMidiByte_t d1;      // midi data byte 1
107
+    cmMidiByte_t pad;
108
+  } cmMidiMsg;
109
+
110
+  typedef struct
111
+  {
112
+    void*         cbDataPtr; // application supplied reference value from mdParserCreate()
113
+    unsigned      devIdx;    // the device the msg originated from
114
+    unsigned      portIdx;   // the port index on the source device
115
+    cmMidiMsg*    msgArray;  // pointer to an array of 'msgCnt' mdMsg records or NULL if sysExMsg is non-NULL
116
+    cmMidiByte_t* sysExMsg;  // pointer to a sys-ex msg or NULL if msgArray is non-NULL (see note below)
117
+    unsigned      msgCnt;    // count of mdMsg records or sys-ex bytes
118
+  } cmMidiPacket_t;
119
+
120
+  // Notes: If the sys-ex message can be contained in a single msg then
121
+  // then the first msg byte is kSysExMdId and the last is kSysComEoxMdId.
122
+  // If the sys-ex message is broken into multiple pieces then only the
123
+  // first will begin with kSysExMdId and the last will end with kSysComEoxMdId.
124
+
125
+
126
+  // If label is NULL or labelCharCnt==0 then a pointer to an internal static
127
+  // buffer is returned. If label[] is given the it
128
+  // should have at least 5 (kMidiPitchCharCnt) char's (including the terminating zero).
129
+  // If 'pitch' is outside of the range 0-127 then a blank string is returned.
130
+  const char*     cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt );
131
+
132
+
133
+  // Scientific pitch string: [A-Ga-g][#b][#] where  # may be -1 to 9.
134
+  // Return kInvalidMidiPitch if sciPtichStr does not contain a valid 
135
+  // scientific pitch string. This function will convert C-1 to G9 to 
136
+  // valid MIDI pitch values 0 to 127.  Scientific pitch strings outside
137
+  // of this range will be returned as kInvalidMidiPitch.   
138
+  cmMidiByte_t    cmSciPitchToMidi( const char* sciPitchStr );
135 139
 
136 140
 #ifdef __cplusplus
137 141
 }

Loading…
Cancel
Save