LV2 Toolkit  1.2.0
write_midi.hpp
1 
21 #ifndef LVTK_WRITE_MIDI_HPP
22 #define LVTK_WRITE_MIDI_HPP
23 
24 #warning "WriteMIDI uses deprecated LV2 extensions. WriteMIDI will \
25  probably not work correctly in a LV2 Rev 3 UI."
26 
27 #include <cstdlib>
28 
29 #include <lvtk/ext/event.hpp>
30 #include <lvtk/ext/urimap.hpp>
31 
32 namespace lvtk {
33 
43  template <bool Required = true>
44  struct WriteMIDI
45  {
46 
47  enum {
48  EVENT_BUFFER_SIZE = 4
49  };
50 
51  template <class Derived>
52  struct I : Behavior<Required>
53  {
54 
55  I() : m_midi_type(0) {
56  m_buffer = lv2_event_buffer_new(sizeof(LV2_Event) + EVENT_BUFFER_SIZE,
57  0);
58  }
60  bool check_ok() {
61 
62  Derived* d = static_cast<Derived*>(this);
63  m_midi_type = d->
64  uri_to_id(LV2_EVENT_URI, "http://lv2plug.in/ns/ext/midi#MidiEvent");
65  m_event_buffer_format = d->
66  uri_to_id(LV2_UI_URI, "http://lv2plug.in/ns/extensions/ui#Events");
67  if (LVTK_DEBUG) {
68  if (m_midi_type != 0) {
69  std::clog<<" [LV2::WriteMIDI] Host does not map (\""
70  <<LV2_EVENT_URI
71  <<"\", \"http://lv2plug.in/ns/ext/midi#MidiEvent\") "
72  <<"to any valid ID.\n";
73  }
74  if (m_event_buffer_format != 0) {
75  std::clog<<" [LV2::WriteMIDI] Host does not map (\""
76  <<LV2_UI_URI
77  <<"\", \"http://lv2plug.in/ns/extensions/ui#Events\") "
78  <<"to any valid ID.\n";
79  }
80  if (!Required || (m_midi_type && m_event_buffer_format))
81  std::clog<<" [LV2::WriteMIDI] Validation succeeded."<<std::endl;
82  else
83  std::clog<<" [LV2::WriteMIDI] Validation failed."<<std::endl;
84  }
85  return !Required || (m_midi_type && m_event_buffer_format);
86 
87  }
88 
89  protected:
90 
101  bool write_midi(uint32_t port, uint32_t size, const uint8_t* data) {
102 
103  if (LVTK_DEBUG) {
104  std::clog<<"[LV2::WriteMIDI] write_midi("<<port<<", "<<size
105  <<", \"";
106  for (uint32_t i = 0; i < size; ++i) {
107  if (i != 0)
108  std::clog<<' ';
109  std::clog<<std::hex<<std::setw(2)<<std::setfill('0')
110  <<static_cast<unsigned>(data[i]);
111  }
112  std::clog<<"\") -> ";
113  }
114 
115  if (m_midi_type == 0) {
116  if (LVTK_DEBUG)
117  std::clog<<"false (Host does not support MIDI writing)"<<std::endl;
118  return false;
119  }
120 
121  LV2_Event_Buffer* buffer;
122  if (size <= 4)
123  buffer = m_buffer;
124  else
125  buffer = lv2_event_buffer_new(sizeof(LV2_Event) + size, 0);
126  lv2_event_buffer_reset(m_buffer, 0, m_buffer->data);
127  LV2_Event_Iterator iter;
128  lv2_event_begin(&iter, m_buffer);
129  lv2_event_write(&iter, 0, 0, m_midi_type, size, data);
130  static_cast<Derived*>(this)->
131  write(port, m_buffer->header_size + m_buffer->capacity,
132  m_event_buffer_format, m_buffer);
133  if (size > 4)
134  std::free(buffer);
135 
136  if (LVTK_DEBUG)
137  std::clog<<"true"<<std::endl;
138  return true;
139  }
140 
141  uint32_t m_midi_type;
142  uint32_t m_event_buffer_format;
143  LV2_Event_Buffer* m_buffer;
144 
145  };
146 
147 };
148 
149 } /* namespace lvtk */
150 
151 
152 #endif /* LVTK_WRITE_MIDI_HPP */
Definition: write_midi.hpp:44
bool write_midi(uint32_t port, uint32_t size, const uint8_t *data)
Definition: write_midi.hpp:101
Definition: write_midi.hpp:52
Definition: feature.hpp:34