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.

ysfx_midi.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #pragma once
  18. #include "ysfx.h"
  19. #include <vector>
  20. #include <memory>
  21. struct ysfx_midi_header_t {
  22. uint32_t bus;
  23. uint32_t offset;
  24. uint32_t size;
  25. };
  26. struct ysfx_midi_buffer_t {
  27. std::vector<uint8_t> data;
  28. size_t read_pos = 0;
  29. size_t read_pos_for_bus[ysfx_max_midi_buses] = {};
  30. bool extensible = false;
  31. };
  32. using ysfx_midi_buffer_u = std::unique_ptr<ysfx_midi_buffer_t>;
  33. enum {
  34. ysfx_midi_message_max_size = 1 << 24,
  35. };
  36. // NOTE: regarding buses,
  37. // The buffer keeps 2 kinds of read positions: global, and per-bus.
  38. //
  39. // These are tracked separately, so use either global/per-bus reading API,
  40. // but not both mixed in the same piece of code.
  41. //
  42. // The JSFX API `midi*` implementations should always use per-bus access:
  43. // if `ext_midi_bus` is true, use the bus defined by `midi_bus`, otherwise 0.
  44. void ysfx_midi_reserve(ysfx_midi_buffer_t *midi, uint32_t capacity, bool extensible);
  45. void ysfx_midi_clear(ysfx_midi_buffer_t *midi);
  46. bool ysfx_midi_push(ysfx_midi_buffer_t *midi, const ysfx_midi_event_t *event);
  47. void ysfx_midi_rewind(ysfx_midi_buffer_t *midi);
  48. bool ysfx_midi_get_next(ysfx_midi_buffer_t *midi, ysfx_midi_event_t *event);
  49. bool ysfx_midi_get_next_from_bus(ysfx_midi_buffer_t *midi, uint32_t bus, ysfx_midi_event_t *event);
  50. // incremental writer into a midi buffer
  51. struct ysfx_midi_push_t {
  52. ysfx_midi_buffer_t *midi = nullptr;
  53. size_t start = 0;
  54. uint32_t count = 0;
  55. bool eob = false;
  56. };
  57. bool ysfx_midi_push_begin(ysfx_midi_buffer_t *midi, uint32_t bus, uint32_t offset, ysfx_midi_push_t *mp);
  58. bool ysfx_midi_push_data(ysfx_midi_push_t *mp, const uint8_t *data, uint32_t size);
  59. bool ysfx_midi_push_end(ysfx_midi_push_t *mp);
  60. //------------------------------------------------------------------------------
  61. // determine the length of a midi message according to its status byte
  62. // if length is dynamic, returns 0
  63. uint32_t ysfx_midi_sizeof(uint8_t id);