129
129
#endif /* DOC_HIDDEN */
132
* \brief Initialize MIDI event parser
133
* \param bufsize buffer size for MIDI message
134
* \param rdev allocated MIDI event parser
135
* \return 0 on success otherwise a negative error code
137
* Allocates and initializes MIDI event parser.
132
* \brief Creates a MIDI event parser.
133
* \param[in] bufsize Size of the buffer used for encoding; this should be
134
* large enough to hold the largest MIDI message to be
136
* \param[out] rdev The new MIDI event parser.
137
* \return Zero on success, otherwise a negative error code.
139
* This function creates and initializes a MIDI parser object that can be used
140
* to convert a MIDI byte stream to sequencer events (encoding) and/or to
141
* convert sequencer events to a MIDI byte stream (decoding).
145
* <dt>-ENOMEM<dd>Out of memory.
147
* \par Conforming to:
139
150
int snd_midi_event_new(size_t bufsize, snd_midi_event_t **rdev)
177
* \brief Enable/disable MIDI command merging
178
* \param dev MIDI event parser
179
* \param on 0 - enable MIDI command merging, 1 - always pass the command
181
* Enable/disable MIDI command merging
190
* \brief Enables/disables MIDI command merging.
191
* \param dev MIDI event parser.
192
* \param on 0 to enable MIDI command merging,
193
* 1 to always write the command byte.
195
* This function enables or disables MIDI command merging (running status).
197
* When MIDI command merging is not disabled, #snd_midi_event_decode is allowed
198
* to omit any status byte that is identical to the previous status byte.
183
200
void snd_midi_event_no_status(snd_midi_event_t *dev, int on)
199
* \brief Reset MIDI encode parser
200
* \param dev MIDI event parser
201
* \return 0 on success otherwise a negative error code
203
* Resets MIDI encode parser
216
* \brief Resets MIDI encode parser.
217
* \param dev MIDI event parser.
219
* This function resets the MIDI encoder of the parser \a dev.
220
* Any partially encoded MIDI message is dropped,
221
* and running status state is cleared.
223
* \par Conforming to:
205
226
void snd_midi_event_reset_encode(snd_midi_event_t *dev)
211
* \brief Reset MIDI decode parser
212
* \param dev MIDI event parser
213
* \return 0 on success otherwise a negative error code
215
* Resets MIDI decode parser
232
* \brief Resets MIDI decode parser.
233
* \param dev MIDI event parser.
235
* This function resets the MIDI decoder of the parser \a dev.
236
* The next decoded message does not use running status from before the call to
237
* \a snd_midi_event_reset_decode.
239
* \par Conforming to:
217
242
void snd_midi_event_reset_decode(snd_midi_event_t *dev)
223
* \brief Initializes MIDI parsers
224
* \param dev MIDI event parser
225
* \return 0 on success otherwise a negative error code
227
* Initializes MIDI parsers (both encode and decode)
248
* \brief Resets MIDI encode/decode parsers.
249
* \param dev MIDI event parser.
251
* This function resets both encoder and decoder of the MIDI event parser.
252
* \sa snd_midi_event_reset_encode, snd_midi_event_reset_decode
254
* \par Conforming to:
229
257
void snd_midi_event_init(snd_midi_event_t *dev)
236
* \brief Resize MIDI message (event) buffer
237
* \param dev MIDI event parser
238
* \param bufsize new requested buffer size
239
* \return 0 on success otherwise a negative error code
241
* Resizes MIDI message (event) buffer.
264
* \brief Resizes the MIDI message encoding buffer.
265
* \param dev MIDI event parser.
266
* \param bufsize The new buffer size.
267
* \return Zero on success, otherwise a negative error code.
269
* This function resizes the buffer that is used to hold partially encoded MIDI
272
* If there is a partially encoded message in the buffer, it is dropped.
276
* <dt>-ENOMEM<dd>Out of memory.
278
* \sa snd_midi_event_encode, snd_midi_event_reset_encode
243
280
int snd_midi_event_resize_buffer(snd_midi_event_t *dev, size_t bufsize)
261
* \brief Read bytes and encode to sequencer event if finished
262
* \param dev MIDI event parser
263
* \param buf MIDI byte stream
264
* \param count count of bytes of MIDI byte stream to encode
265
* \param ev Result - sequencer event
266
* \return count of encoded bytes otherwise a negative error code
268
* Read bytes and encode to sequencer event if finished.
269
* If complete sequencer event is available, ev->type is not
270
* equal to #SND_SEQ_EVENT_NONE.
298
* \brief Encodes bytes to sequencer event.
299
* \param[in] dev MIDI event parser.
300
* \param[in] buf Buffer containing bytes of a raw MIDI stream.
301
* \param[in] count Number of bytes in \a buf.
302
* \param[out] ev Sequencer event.
303
* \return The number of bytes consumed, or a negative error code.
305
* This function tries to use up to \a count bytes from the beginning of the
306
* buffer to encode a sequencer event. If a complete MIDI message has been
307
* encoded, the sequencer event is written to \a ev; otherwise, \a ev->type is
308
* set to #SND_SEQ_EVENT_NONE, and further bytes are required to complete
311
* The buffer in \a dev is used to hold any bytes of a not-yet-complete MIDI
312
* message. If a System Exclusive message is larger than the buffer, the
313
* message is split into multiple parts, and a sequencer event is returned at
314
* the end of each part.
316
* Any bytes that are not part of a valid MIDI message are silently ignored,
317
* i.e., they are consumed without signaling an error.
319
* When this function returns a system exclusive sequencer event (\a ev->type
320
* is #SND_SEQ_EVENT_SYSEX), the data pointer (\a ev->data.ext.ptr) points into
321
* the MIDI event parser's buffer. Therefore, the sequencer event can only be
322
* used as long as that buffer remains valid, i.e., until the next call to
323
* #snd_midi_event_encode, #snd_midi_event_encode_byte,
324
* #snd_midi_event_resize_buffer, #snd_midi_event_init,
325
* #snd_midi_event_reset_encode, or #snd_midi_event_free for that MIDI event
328
* This function can generate any sequencer event that corresponds to a MIDI
330
* - #SND_SEQ_EVENT_NOTEOFF
331
* - #SND_SEQ_EVENT_NOTEON
332
* - #SND_SEQ_EVENT_KEYPRESS
333
* - #SND_SEQ_EVENT_CONTROLLER
334
* - #SND_SEQ_EVENT_PGMCHANGE
335
* - #SND_SEQ_EVENT_CHANPRESS
336
* - #SND_SEQ_EVENT_PITCHBEND
337
* - #SND_SEQ_EVENT_SYSEX
338
* - #SND_SEQ_EVENT_QFRAME
339
* - #SND_SEQ_EVENT_SONGPOS
340
* - #SND_SEQ_EVENT_SONGSEL
341
* - #SND_SEQ_EVENT_TUNE_REQUEST
342
* - #SND_SEQ_EVENT_CLOCK
343
* - #SND_SEQ_EVENT_START
344
* - #SND_SEQ_EVENT_CONTINUE
345
* - #SND_SEQ_EVENT_STOP
346
* - #SND_SEQ_EVENT_SENSING
347
* - #SND_SEQ_EVENT_RESET
349
* Some implementations may also be able to generate the following events
350
* for a sequence of controller change messages:
351
* - #SND_SEQ_EVENT_CONTROL14
352
* - #SND_SEQ_EVENT_NONREGPARAM
353
* - #SND_SEQ_EVENT_REGPARAM
355
* \par Conforming to:
358
* \sa snd_midi_event_new, snd_midi_event_reset_encode, snd_midi_event_encode_byte
272
360
long snd_midi_event_encode(snd_midi_event_t *dev, const unsigned char *buf, long count, snd_seq_event_t *ev)
292
* \brief Read one byte and encode to sequencer event if finished
293
* \param dev MIDI event parser
294
* \param c a byte of MIDI stream
295
* \param ev Result - sequencer event
296
* \return 1 - sequencer event is completed, 0 - next byte is required for completion, otherwise a negative error code
298
* Read byte and encode to sequencer event if finished.
380
* \brief Encodes byte to sequencer event.
381
* \param[in] dev MIDI event parser.
382
* \param[in] c A byte of a raw MIDI stream.
383
* \param[out] ev Sequencer event.
384
* \return 1 if a sequenver event has been completed, 0 if more bytes are
385
* required to complete an event, or a negative error code.
387
* This function tries to use the byte \a c to encode a sequencer event. If
388
* a complete MIDI message has been encoded, the sequencer event is written to
389
* \a ev; otherwise, further bytes are required to complete a message.
391
* See also the description of #snd_midi_event_encode.
393
* \par Conforming to:
396
* \sa snd_midi_event_new, snd_midi_event_reset_encode, snd_midi_event_encode
300
398
int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev)
408
* \brief Decode sequencer event to MIDI byte stream
409
* \param dev MIDI event parser
410
* \param buf Result - MIDI byte stream
411
* \param count Available bytes in MIDI byte stream
412
* \param ev Event to decode
413
* \return count of decoded bytes otherwise a negative error code
415
* Decode sequencer event to MIDI byte stream.
506
* \brief Decodes sequencer event to MIDI byte stream.
507
* \param[in] dev MIDI event parser.
508
* \param[out] buf Buffer for the resulting MIDI byte stream.
509
* \param[in] count Number of bytes in \a buf.
510
* \param[in] ev The sequencer event to decode.
511
* \return The number of bytes written to \a buf, or a negative error code.
513
* This function tries to decode the sequencer event into one or more MIDI
514
* messages, and writes the raw MIDI byte(s) into \a buf.
516
* The generated MIDI messages may use running status, unless disabled with
517
* #snd_midi_event_no_status.
519
* The required buffer size for a sequencer event it as most 12 bytes, except
520
* for System Exclusive events (\a ev->type == #SND_SEQ_EVENT_SYSEX) which can
521
* have any length (as specified by \a ev->data.ext.len).
523
* The following sequencer events correspond to MIDI messages:
524
* - #SND_SEQ_EVENT_NOTEOFF
525
* - #SND_SEQ_EVENT_NOTEON
526
* - #SND_SEQ_EVENT_KEYPRESS
527
* - #SND_SEQ_EVENT_CONTROLLER
528
* - #SND_SEQ_EVENT_PGMCHANGE
529
* - #SND_SEQ_EVENT_CHANPRESS
530
* - #SND_SEQ_EVENT_PITCHBEND
531
* - #SND_SEQ_EVENT_SYSEX
532
* - #SND_SEQ_EVENT_QFRAME
533
* - #SND_SEQ_EVENT_SONGPOS
534
* - #SND_SEQ_EVENT_SONGSEL
535
* - #SND_SEQ_EVENT_TUNE_REQUEST
536
* - #SND_SEQ_EVENT_CLOCK
537
* - #SND_SEQ_EVENT_START
538
* - #SND_SEQ_EVENT_CONTINUE
539
* - #SND_SEQ_EVENT_STOP
540
* - #SND_SEQ_EVENT_SENSING
541
* - #SND_SEQ_EVENT_RESET
542
* - #SND_SEQ_EVENT_CONTROL14
543
* - #SND_SEQ_EVENT_NONREGPARAM
544
* - #SND_SEQ_EVENT_REGPARAM
548
* <dt>-EINVAL<dd>\a ev is not a valid sequencer event.
549
* <dt>-ENOENT<dd>The sequencer event does not correspond to one or more MIDI messages.
550
* <dt>-ENOMEM<dd>The MIDI message(s) would not fit into \a count bytes.
552
* \par Conforming to:
555
* \sa snd_midi_event_reset_decode, snd_midi_event_no_status
417
557
long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count, const snd_seq_event_t *ev)
566
707
if (dev->nostat && count < 12)
568
709
cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
569
bytes[0] = ev->data.control.param & 0x007f;
570
bytes[1] = (ev->data.control.param & 0x3f80) >> 7;
571
bytes[2] = ev->data.control.value & 0x007f;
572
bytes[3] = (ev->data.control.value & 0x3f80) >> 7;
710
bytes[0] = (ev->data.control.param & 0x3f80) >> 7;
711
bytes[1] = ev->data.control.param & 0x007f;
712
bytes[2] = (ev->data.control.value & 0x3f80) >> 7;
713
bytes[3] = ev->data.control.value & 0x007f;
573
714
if (cmd != dev->lastcmd && !dev->nostat) {