The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

536 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #ifndef DOXYGEN
  18. namespace juce::universal_midi_packets
  19. {
  20. /**
  21. This struct holds functions that can be used to create different kinds
  22. of Universal MIDI Packet.
  23. @tags{Audio}
  24. */
  25. struct Factory
  26. {
  27. /** @internal */
  28. struct Detail
  29. {
  30. static PacketX1 makeSystem() { return PacketX1{}.withMessageType (1); }
  31. static PacketX1 makeV1() { return PacketX1{}.withMessageType (2); }
  32. static PacketX2 makeV2() { return PacketX2{}.withMessageType (4); }
  33. static PacketX2 makeSysEx (uint8_t group,
  34. uint8_t status,
  35. uint8_t numBytes,
  36. const std::byte* data)
  37. {
  38. jassert (numBytes <= 6);
  39. std::array<uint8_t, 8> bytes{{}};
  40. bytes[0] = (0x3 << 0x4) | group;
  41. bytes[1] = (uint8_t) (status << 0x4) | numBytes;
  42. std::memcpy (bytes.data() + 2, data, numBytes);
  43. std::array<uint32_t, 2> words;
  44. size_t index = 0;
  45. for (auto& word : words)
  46. word = ByteOrder::bigEndianInt (bytes.data() + 4 * index++);
  47. return PacketX2 { words };
  48. }
  49. static PacketX4 makeSysEx8 (uint8_t group,
  50. uint8_t status,
  51. uint8_t numBytes,
  52. uint8_t dataStart,
  53. const uint8_t* data)
  54. {
  55. jassert (numBytes <= 16 - dataStart);
  56. std::array<uint8_t, 16> bytes{{}};
  57. bytes[0] = (0x5 << 0x4) | group;
  58. bytes[1] = (uint8_t) (status << 0x4) | numBytes;
  59. std::memcpy (bytes.data() + dataStart, data, numBytes);
  60. std::array<uint32_t, 4> words;
  61. size_t index = 0;
  62. for (auto& word : words)
  63. word = ByteOrder::bigEndianInt (bytes.data() + 4 * index++);
  64. return PacketX4 { words };
  65. }
  66. };
  67. static PacketX1 makeNoop (uint8_t group)
  68. {
  69. return PacketX1{}.withGroup (group);
  70. }
  71. static PacketX1 makeJRClock (uint8_t group, uint16_t time)
  72. {
  73. return PacketX1 { time }.withStatus (1).withGroup (group);
  74. }
  75. static PacketX1 makeJRTimestamp (uint8_t group, uint16_t time)
  76. {
  77. return PacketX1 { time }.withStatus (2).withGroup (group);
  78. }
  79. static PacketX1 makeTimeCode (uint8_t group, uint8_t code)
  80. {
  81. return Detail::makeSystem().withGroup (group)
  82. .withU8<1> (0xf1)
  83. .withU8<2> (code & 0x7f);
  84. }
  85. static PacketX1 makeSongPositionPointer (uint8_t group, uint16_t pos)
  86. {
  87. return Detail::makeSystem().withGroup (group)
  88. .withU8<1> (0xf2)
  89. .withU8<2> (pos & 0x7f)
  90. .withU8<3> ((pos >> 7) & 0x7f);
  91. }
  92. static PacketX1 makeSongSelect (uint8_t group, uint8_t song)
  93. {
  94. return Detail::makeSystem().withGroup (group)
  95. .withU8<1> (0xf3)
  96. .withU8<2> (song & 0x7f);
  97. }
  98. static PacketX1 makeTuneRequest (uint8_t group)
  99. {
  100. return Detail::makeSystem().withGroup (group)
  101. .withU8<1> (0xf6);
  102. }
  103. static PacketX1 makeTimingClock (uint8_t group)
  104. {
  105. return Detail::makeSystem().withGroup (group)
  106. .withU8<1> (0xf8);
  107. }
  108. static PacketX1 makeStart (uint8_t group)
  109. {
  110. return Detail::makeSystem().withGroup (group)
  111. .withU8<1> (0xfa);
  112. }
  113. static PacketX1 makeContinue (uint8_t group)
  114. {
  115. return Detail::makeSystem().withGroup (group)
  116. .withU8<1> (0xfb);
  117. }
  118. static PacketX1 makeStop (uint8_t group)
  119. {
  120. return Detail::makeSystem().withGroup (group)
  121. .withU8<1> (0xfc);
  122. }
  123. static PacketX1 makeActiveSensing (uint8_t group)
  124. {
  125. return Detail::makeSystem().withGroup (group)
  126. .withU8<1> (0xfe);
  127. }
  128. static PacketX1 makeReset (uint8_t group)
  129. {
  130. return Detail::makeSystem().withGroup (group)
  131. .withU8<1> (0xff);
  132. }
  133. static PacketX1 makeNoteOffV1 (uint8_t group,
  134. uint8_t channel,
  135. uint8_t note,
  136. uint8_t velocity)
  137. {
  138. return Detail::makeV1().withGroup (group)
  139. .withStatus (0x8)
  140. .withChannel (channel)
  141. .withU8<2> (note & 0x7f)
  142. .withU8<3> (velocity & 0x7f);
  143. }
  144. static PacketX1 makeNoteOnV1 (uint8_t group,
  145. uint8_t channel,
  146. uint8_t note,
  147. uint8_t velocity)
  148. {
  149. return Detail::makeV1().withGroup (group)
  150. .withStatus (0x9)
  151. .withChannel (channel)
  152. .withU8<2> (note & 0x7f)
  153. .withU8<3> (velocity & 0x7f);
  154. }
  155. static PacketX1 makePolyPressureV1 (uint8_t group,
  156. uint8_t channel,
  157. uint8_t note,
  158. uint8_t pressure)
  159. {
  160. return Detail::makeV1().withGroup (group)
  161. .withStatus (0xa)
  162. .withChannel (channel)
  163. .withU8<2> (note & 0x7f)
  164. .withU8<3> (pressure & 0x7f);
  165. }
  166. static PacketX1 makeControlChangeV1 (uint8_t group,
  167. uint8_t channel,
  168. uint8_t controller,
  169. uint8_t value)
  170. {
  171. return Detail::makeV1().withGroup (group)
  172. .withStatus (0xb)
  173. .withChannel (channel)
  174. .withU8<2> (controller & 0x7f)
  175. .withU8<3> (value & 0x7f);
  176. }
  177. static PacketX1 makeProgramChangeV1 (uint8_t group,
  178. uint8_t channel,
  179. uint8_t program)
  180. {
  181. return Detail::makeV1().withGroup (group)
  182. .withStatus (0xc)
  183. .withChannel (channel)
  184. .withU8<2> (program & 0x7f);
  185. }
  186. static PacketX1 makeChannelPressureV1 (uint8_t group,
  187. uint8_t channel,
  188. uint8_t pressure)
  189. {
  190. return Detail::makeV1().withGroup (group)
  191. .withStatus (0xd)
  192. .withChannel (channel)
  193. .withU8<2> (pressure & 0x7f);
  194. }
  195. static PacketX1 makePitchBend (uint8_t group,
  196. uint8_t channel,
  197. uint16_t pitchbend)
  198. {
  199. return Detail::makeV1().withGroup (group)
  200. .withStatus (0xe)
  201. .withChannel (channel)
  202. .withU8<2> (pitchbend & 0x7f)
  203. .withU8<3> ((pitchbend >> 7) & 0x7f);
  204. }
  205. static PacketX2 makeSysExIn1Packet (uint8_t group,
  206. uint8_t numBytes,
  207. const std::byte* data)
  208. {
  209. return Detail::makeSysEx (group, 0x0, numBytes, data);
  210. }
  211. static PacketX2 makeSysExStart (uint8_t group,
  212. uint8_t numBytes,
  213. const std::byte* data)
  214. {
  215. return Detail::makeSysEx (group, 0x1, numBytes, data);
  216. }
  217. static PacketX2 makeSysExContinue (uint8_t group,
  218. uint8_t numBytes,
  219. const std::byte* data)
  220. {
  221. return Detail::makeSysEx (group, 0x2, numBytes, data);
  222. }
  223. static PacketX2 makeSysExEnd (uint8_t group,
  224. uint8_t numBytes,
  225. const std::byte* data)
  226. {
  227. return Detail::makeSysEx (group, 0x3, numBytes, data);
  228. }
  229. static PacketX2 makeRegisteredPerNoteControllerV2 (uint8_t group,
  230. uint8_t channel,
  231. uint8_t note,
  232. uint8_t controller,
  233. uint32_t data)
  234. {
  235. return Detail::makeV2().withGroup (group)
  236. .withStatus (0x0)
  237. .withChannel (channel)
  238. .withU8<2> (note & 0x7f)
  239. .withU8<3> (controller & 0x7f)
  240. .withU32<1> (data);
  241. }
  242. static PacketX2 makeAssignablePerNoteControllerV2 (uint8_t group,
  243. uint8_t channel,
  244. uint8_t note,
  245. uint8_t controller,
  246. uint32_t data)
  247. {
  248. return Detail::makeV2().withGroup (group)
  249. .withStatus (0x1)
  250. .withChannel (channel)
  251. .withU8<2> (note & 0x7f)
  252. .withU8<3> (controller & 0x7f)
  253. .withU32<1> (data);
  254. }
  255. static PacketX2 makeRegisteredControllerV2 (uint8_t group,
  256. uint8_t channel,
  257. uint8_t bank,
  258. uint8_t index,
  259. uint32_t data)
  260. {
  261. return Detail::makeV2().withGroup (group)
  262. .withStatus (0x2)
  263. .withChannel (channel)
  264. .withU8<2> (bank & 0x7f)
  265. .withU8<3> (index & 0x7f)
  266. .withU32<1> (data);
  267. }
  268. static PacketX2 makeAssignableControllerV2 (uint8_t group,
  269. uint8_t channel,
  270. uint8_t bank,
  271. uint8_t index,
  272. uint32_t data)
  273. {
  274. return Detail::makeV2().withGroup (group)
  275. .withStatus (0x3)
  276. .withChannel (channel)
  277. .withU8<2> (bank & 0x7f)
  278. .withU8<3> (index & 0x7f)
  279. .withU32<1> (data);
  280. }
  281. static PacketX2 makeRelativeRegisteredControllerV2 (uint8_t group,
  282. uint8_t channel,
  283. uint8_t bank,
  284. uint8_t index,
  285. uint32_t data)
  286. {
  287. return Detail::makeV2().withGroup (group)
  288. .withStatus (0x4)
  289. .withChannel (channel)
  290. .withU8<2> (bank & 0x7f)
  291. .withU8<3> (index & 0x7f)
  292. .withU32<1> (data);
  293. }
  294. static PacketX2 makeRelativeAssignableControllerV2 (uint8_t group,
  295. uint8_t channel,
  296. uint8_t bank,
  297. uint8_t index,
  298. uint32_t data)
  299. {
  300. return Detail::makeV2().withGroup (group)
  301. .withStatus (0x5)
  302. .withChannel (channel)
  303. .withU8<2> (bank & 0x7f)
  304. .withU8<3> (index & 0x7f)
  305. .withU32<1> (data);
  306. }
  307. static PacketX2 makePerNotePitchBendV2 (uint8_t group,
  308. uint8_t channel,
  309. uint8_t note,
  310. uint32_t data)
  311. {
  312. return Detail::makeV2().withGroup (group)
  313. .withStatus (0x6)
  314. .withChannel (channel)
  315. .withU8<2> (note & 0x7f)
  316. .withU32<1> (data);
  317. }
  318. enum class NoteAttributeKind : uint8_t
  319. {
  320. none = 0x00,
  321. manufacturer = 0x01,
  322. profile = 0x02,
  323. pitch7_9 = 0x03
  324. };
  325. static PacketX2 makeNoteOffV2 (uint8_t group,
  326. uint8_t channel,
  327. uint8_t note,
  328. NoteAttributeKind attribute,
  329. uint16_t velocity,
  330. uint16_t attributeValue)
  331. {
  332. return Detail::makeV2().withGroup (group)
  333. .withStatus (0x8)
  334. .withChannel (channel)
  335. .withU8<2> (note & 0x7f)
  336. .withU8<3> ((uint8_t) attribute)
  337. .withU16<2> (velocity)
  338. .withU16<3> (attributeValue);
  339. }
  340. static PacketX2 makeNoteOnV2 (uint8_t group,
  341. uint8_t channel,
  342. uint8_t note,
  343. NoteAttributeKind attribute,
  344. uint16_t velocity,
  345. uint16_t attributeValue)
  346. {
  347. return Detail::makeV2().withGroup (group)
  348. .withStatus (0x9)
  349. .withChannel (channel)
  350. .withU8<2> (note & 0x7f)
  351. .withU8<3> ((uint8_t) attribute)
  352. .withU16<2> (velocity)
  353. .withU16<3> (attributeValue);
  354. }
  355. static PacketX2 makePolyPressureV2 (uint8_t group,
  356. uint8_t channel,
  357. uint8_t note,
  358. uint32_t data)
  359. {
  360. return Detail::makeV2().withGroup (group)
  361. .withStatus (0xa)
  362. .withChannel (channel)
  363. .withU8<2> (note & 0x7f)
  364. .withU32<1> (data);
  365. }
  366. static PacketX2 makeControlChangeV2 (uint8_t group,
  367. uint8_t channel,
  368. uint8_t controller,
  369. uint32_t data)
  370. {
  371. return Detail::makeV2().withGroup (group)
  372. .withStatus (0xb)
  373. .withChannel (channel)
  374. .withU8<2> (controller & 0x7f)
  375. .withU32<1> (data);
  376. }
  377. static PacketX2 makeProgramChangeV2 (uint8_t group,
  378. uint8_t channel,
  379. uint8_t optionFlags,
  380. uint8_t program,
  381. uint8_t bankMsb,
  382. uint8_t bankLsb)
  383. {
  384. return Detail::makeV2().withGroup (group)
  385. .withStatus (0xc)
  386. .withChannel (channel)
  387. .withU8<3> (optionFlags)
  388. .withU8<4> (program)
  389. .withU8<6> (bankMsb)
  390. .withU8<7> (bankLsb);
  391. }
  392. static PacketX2 makeChannelPressureV2 (uint8_t group,
  393. uint8_t channel,
  394. uint32_t data)
  395. {
  396. return Detail::makeV2().withGroup (group)
  397. .withStatus (0xd)
  398. .withChannel (channel)
  399. .withU32<1> (data);
  400. }
  401. static PacketX2 makePitchBendV2 (uint8_t group,
  402. uint8_t channel,
  403. uint32_t data)
  404. {
  405. return Detail::makeV2().withGroup (group)
  406. .withStatus (0xe)
  407. .withChannel (channel)
  408. .withU32<1> (data);
  409. }
  410. static PacketX2 makePerNoteManagementV2 (uint8_t group,
  411. uint8_t channel,
  412. uint8_t note,
  413. uint8_t optionFlags)
  414. {
  415. return Detail::makeV2().withGroup (group)
  416. .withStatus (0xf)
  417. .withChannel (channel)
  418. .withU8<2> (note)
  419. .withU8<3> (optionFlags);
  420. }
  421. static PacketX4 makeSysEx8in1Packet (uint8_t group,
  422. uint8_t numBytes,
  423. uint8_t streamId,
  424. const uint8_t* data)
  425. {
  426. return Detail::makeSysEx8 (group, 0x0, numBytes, 3, data).withU8<2> (streamId);
  427. }
  428. static PacketX4 makeSysEx8Start (uint8_t group,
  429. uint8_t numBytes,
  430. uint8_t streamId,
  431. const uint8_t* data)
  432. {
  433. return Detail::makeSysEx8 (group, 0x1, numBytes, 3, data).withU8<2> (streamId);
  434. }
  435. static PacketX4 makeSysEx8Continue (uint8_t group,
  436. uint8_t numBytes,
  437. uint8_t streamId,
  438. const uint8_t* data)
  439. {
  440. return Detail::makeSysEx8 (group, 0x2, numBytes, 3, data).withU8<2> (streamId);
  441. }
  442. static PacketX4 makeSysEx8End (uint8_t group,
  443. uint8_t numBytes,
  444. uint8_t streamId,
  445. const uint8_t* data)
  446. {
  447. return Detail::makeSysEx8 (group, 0x3, numBytes, 3, data).withU8<2> (streamId);
  448. }
  449. static PacketX4 makeMixedDataSetHeader (uint8_t group,
  450. uint8_t dataSetId,
  451. const uint8_t* data)
  452. {
  453. return Detail::makeSysEx8 (group, 0x8, 14, 2, data).withChannel (dataSetId);
  454. }
  455. static PacketX4 makeDataSetPayload (uint8_t group,
  456. uint8_t dataSetId,
  457. const uint8_t* data)
  458. {
  459. return Detail::makeSysEx8 (group, 0x9, 14, 2, data).withChannel (dataSetId);
  460. }
  461. };
  462. } // namespace juce::universal_midi_packets
  463. #endif