Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lv2-midifunctions.h 3.0KB

12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /****************************************************************************
  2. lv2-midifunctions.h - support file for using MIDI in LV2 plugins
  3. Copyright (C) 2006 Lars Luthman <lars.luthman@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
  15. ****************************************************************************/
  16. #ifndef LV2_MIDIFUNCTIONS
  17. #define LV2_MIDIFUNCTIONS
  18. #include "lv2-miditype.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef struct {
  23. LV2_MIDI* midi;
  24. uint32_t frame_count;
  25. uint32_t position;
  26. } LV2_MIDIState;
  27. inline double lv2midi_get_event(LV2_MIDIState* state,
  28. double* timestamp,
  29. uint32_t* size,
  30. unsigned char** data) {
  31. if (state->position >= state->midi->size) {
  32. state->position = state->midi->size;
  33. *timestamp = state->frame_count;
  34. *size = 0;
  35. *data = NULL;
  36. return *timestamp;
  37. }
  38. *timestamp = *(double*)(state->midi->data + state->position);
  39. *size = *(size_t*)(state->midi->data + state->position + sizeof(double));
  40. *data = state->midi->data + state->position +
  41. sizeof(double) + sizeof(size_t);
  42. return *timestamp;
  43. }
  44. inline double lv2midi_step(LV2_MIDIState* state) {
  45. if (state->position >= state->midi->size) {
  46. state->position = state->midi->size;
  47. return state->frame_count;
  48. }
  49. state->position += sizeof(double);
  50. size_t size = *(size_t*)(state->midi->data + state->position);
  51. state->position += sizeof(size_t);
  52. state->position += size;
  53. return *(double*)(state->midi->data + state->position);
  54. }
  55. inline void lv2midi_put_event(LV2_MIDIState* state,
  56. double timestamp,
  57. uint32_t size,
  58. const unsigned char* data) {
  59. if (state->midi->size + sizeof(double) + sizeof(size_t) + size < state->midi->capacity)
  60. {
  61. *((double*)(state->midi->data + state->midi->size)) = timestamp;
  62. state->midi->size += sizeof(double);
  63. *((size_t*)(state->midi->data + state->midi->size)) = size;
  64. state->midi->size += sizeof(size_t);
  65. memcpy(state->midi->data + state->midi->size, data, size);
  66. state->midi->size += size;
  67. state->midi->event_count++;
  68. }
  69. }
  70. #ifdef __cplusplus
  71. } /* extern "C" */
  72. #endif
  73. #endif