source: ext/midi/midi_parser.c @ 71d9f52

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 71d9f52 was b60dd4ae, checked in by Paul Brossier <piem@altern.org>, 20 years ago

moved midi functions to ext/

  • Property mode set to 100644
File size: 7.7 KB
Line 
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Library General Public License
4 * as published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 * Library General Public License for more details.
11 * 
12 * You should have received a copy of the GNU Library General Public
13 * License along with this library; if not, write to the Free
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
15 * 02111-1307, USA
16 */
17
18/* This file orginally taken from, Fluidsynth, Peter Hanappe */
19
20#include "aubio_priv.h"
21#include "midi.h"
22#include "midi_event.h"
23#include "midi_parser.h"
24
25/** aubio_midi_parser_t */
26struct _aubio_midi_parser_t {
27  unsigned char status;       /**< Identifies the type of event, that is
28                                 currently received ('Noteon', 'Pitch Bend'
29                                 etc). */
30  unsigned char channel;      /**< The channel of the event that is received
31                                (in case of a channel event) */
32  unsigned int nr_bytes;      /**< How many bytes have been read for the
33                                 current event? */
34  unsigned int nr_bytes_total;/**< How many bytes does the current event
35                                type include? */
36  unsigned short p[AUBIO_MIDI_PARSER_MAX_PAR]; /**< The parameters */
37
38  aubio_midi_event_t event;   /**< The event, that is returned to the
39                                 MIDI driver. */
40};
41
42/** new_aubio_midi_parser */
43aubio_midi_parser_t* new_aubio_midi_parser()
44{
45  aubio_midi_parser_t* parser;
46  parser = AUBIO_NEW(aubio_midi_parser_t);
47  if (parser == NULL) {
48    AUBIO_ERR("Out of memory");
49    return NULL;
50  }
51  /* As long as the status is 0, the parser won't do anything -> no need to
52   * initialize all the fields. */
53  parser->status = 0; 
54  return parser;
55}
56
57/** del_aubio_midi_parser */
58int del_aubio_midi_parser(aubio_midi_parser_t* parser)
59{
60  AUBIO_FREE(parser);
61  return AUBIO_OK;
62}
63
64/** aubio_midi_parser_parse
65 *
66 * The MIDI byte stream is fed into the parser, one byte at a time.
67 * As soon as the parser has recognized an event, it will return it.
68 * Otherwise it returns NULL.
69 */
70aubio_midi_event_t* aubio_midi_parser_parse(aubio_midi_parser_t* parser, 
71    unsigned char c)
72{
73  /*********************************************************************/
74  /* 'Process' system real-time messages                               */
75  /*********************************************************************/
76  /* There are not too many real-time messages that are of interest here.
77   * They can occur anywhere, even in the middle of a noteon message!
78   * Real-time range: 0xF8 .. 0xFF
79   * Note: Real-time does not affect (running) status.
80   */ 
81  if (c >= 0xF8){
82    if (c == MIDI_SYSTEM_RESET){
83      parser->event.type = c;
84      parser->status = 0; /* clear the status */
85      return &parser->event;
86    };
87    return NULL;
88  };
89
90  /*********************************************************************/
91  /* 'Process' system common messages (again, just skip them)          */
92  /*********************************************************************/
93  /* There are no system common messages that are of interest here.
94   * System common range: 0xF0 .. 0xF7
95   */
96
97  if (c > 0xF0){
98    /* MIDI specs say: To ignore a non-real-time message, just discard all data
99     * up to the next status byte.  And our parser will ignore data that is
100     * received without a valid status. 
101     * Note: system common cancels running status. */
102    parser->status = 0;
103    return NULL;
104  };
105
106  /*********************************************************************/
107  /* Process voice category messages:                                  */
108  /*********************************************************************/
109  /* Now that we have handled realtime and system common messages, only
110   * voice messages are left.
111   * Only a status byte has bit # 7 set.
112   * So no matter the status of the parser (in case we have lost sync),
113   * as soon as a byte >= 0x80 comes in, we are dealing with a status byte
114   * and start a new event.
115   */
116
117  if (c & 0x80){
118    parser->channel = c & 0x0F;
119    parser->status = c & 0xF0;
120    /* The event consumes x bytes of data... (subtract 1 for the status
121     * byte) */
122    parser->nr_bytes_total=aubio_midi_event_length(parser->status)-1;
123    /* of which we have read 0 at this time. */
124    parser->nr_bytes = 0;
125    return NULL;
126  };
127
128  /*********************************************************************/
129  /* Process data                                                      */
130  /*********************************************************************/
131  /* If we made it this far, then the received char belongs to the data
132   * of the last event. */
133  if (parser->status == 0){
134    /* We are not interested in the event currently received.
135     * Discard the data. */
136    return NULL;
137  };
138
139  /* Store the first couple of bytes */
140  if (parser->nr_bytes < AUBIO_MIDI_PARSER_MAX_PAR){
141    parser->p[parser->nr_bytes]=c;
142  };
143  parser->nr_bytes++;
144
145  /* Do we still need more data to get this event complete? */
146  if (parser->nr_bytes < parser->nr_bytes_total){
147    return NULL;
148  };
149
150  /*********************************************************************/
151  /* Send the event                                                    */
152  /*********************************************************************/
153  /* The event is ready-to-go.  About 'running status':
154   *
155   * The MIDI protocol has a built-in compression mechanism. If several similar
156   * events are sent in-a-row, for example note-ons, then the event type is
157   * only sent once. For this case, the last event type (status) is remembered.
158   * We simply keep the status as it is, just reset the parameter counter. If
159   * another status byte comes in, it will overwrite the status.
160   */
161  parser->event.type = parser->status;
162  parser->event.channel = parser->channel;
163  parser->nr_bytes = 0; /* Related to running status! */
164  switch (parser->status){
165    case NOTE_OFF:
166    case NOTE_ON:
167    case KEY_PRESSURE:
168    case CONTROL_CHANGE:
169    case PROGRAM_CHANGE:
170    case CHANNEL_PRESSURE:
171      parser->event.param1 = parser->p[0]; /* For example key number */
172      parser->event.param2 = parser->p[1]; /* For example velocity */
173      break;
174    case PITCH_BEND:
175      /* Pitch-bend is transmitted with 14-bit precision. */
176      /* Note: '|' does here the same as '+' (no common bits),
177       * but might be faster */
178      parser->event.param1 = ((parser->p[1] << 7) | parser->p[0]); 
179      break;
180    default: 
181      /* Unlikely */
182      return NULL;
183  };
184  return &parser->event;
185};
186
187
188
189/* Taken from Nagano Daisuke's USB-MIDI driver */
190static int remains_f0f6[] = {
191  0,    /* 0xF0 */
192  2,    /* 0XF1 */
193  3,    /* 0XF2 */
194  2,    /* 0XF3 */
195  2,    /* 0XF4 (Undefined by MIDI Spec, and subject to change) */
196  2,    /* 0XF5 (Undefined by MIDI Spec, and subject to change) */
197  1       /* 0XF6 */
198};
199
200static int remains_80e0[] = {
201  3,    /* 0x8X Note Off */
202  3,    /* 0x9X Note On */
203  3,    /* 0xAX Poly-key pressure */
204  3,    /* 0xBX Control Change */
205  2,    /* 0xCX Program Change */
206  2,    /* 0xDX Channel pressure */
207  3     /* 0xEX PitchBend Change */
208};
209
210/** Returns the length of the MIDI message starting with c.
211 *
212 * Taken from Nagano Daisuke's USB-MIDI driver */
213int aubio_midi_event_length(unsigned char event){
214  if ( event < 0xf0 ) {
215    return remains_80e0[((event-0x80)>>4)&0x0f];
216  } else if ( event < 0xf7 ) {
217    return remains_f0f6[event-0xf0];
218  } else {
219    return 1;
220  }
221}
Note: See TracBrowser for help on using the repository browser.