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 2.9KB

12 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. typedef struct {
  20. LV2_MIDI* midi;
  21. uint32_t frame_count;
  22. uint32_t position;
  23. } LV2_MIDIState;
  24. inline double lv2midi_get_event(LV2_MIDIState* state,
  25. double* timestamp,
  26. uint32_t* size,
  27. unsigned char** data) {
  28. if (state->position >= state->midi->size) {
  29. state->position = state->midi->size;
  30. *timestamp = state->frame_count;
  31. *size = 0;
  32. *data = NULL;
  33. return *timestamp;
  34. }
  35. *timestamp = *(double*)(state->midi->data + state->position);
  36. *size = *(size_t*)(state->midi->data + state->position + sizeof(double));
  37. *data = state->midi->data + state->position +
  38. sizeof(double) + sizeof(size_t);
  39. return *timestamp;
  40. }
  41. inline double lv2midi_step(LV2_MIDIState* state) {
  42. if (state->position >= state->midi->size) {
  43. state->position = state->midi->size;
  44. return state->frame_count;
  45. }
  46. state->position += sizeof(double);
  47. size_t size = *(size_t*)(state->midi->data + state->position);
  48. state->position += sizeof(size_t);
  49. state->position += size;
  50. return *(double*)(state->midi->data + state->position);
  51. }
  52. inline void lv2midi_put_event(LV2_MIDIState* state,
  53. double timestamp,
  54. uint32_t size,
  55. const unsigned char* data) {
  56. if (state->midi->size + sizeof(double) + sizeof(size_t) + size < state->midi->capacity)
  57. {
  58. *((double*)(state->midi->data + state->midi->size)) = timestamp;
  59. state->midi->size += sizeof(double);
  60. *((size_t*)(state->midi->data + state->midi->size)) = size;
  61. state->midi->size += sizeof(size_t);
  62. memcpy(state->midi->data + state->midi->size, data, size);
  63. state->midi->size += size;
  64. state->midi->event_count++;
  65. }
  66. }
  67. #endif