PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

992 lines
34KB

  1. #include "unit-tests.h"
  2. #include "unit-tests_Settings.h"
  3. #include <src/MIDI.h>
  4. #include <test/mocks/test-mocks_SerialMock.h>
  5. BEGIN_MIDI_NAMESPACE
  6. END_MIDI_NAMESPACE
  7. // -----------------------------------------------------------------------------
  8. BEGIN_UNNAMED_NAMESPACE
  9. using namespace testing;
  10. USING_NAMESPACE_UNIT_TESTS
  11. typedef test_mocks::SerialMock<32> SerialMock;
  12. typedef midi::SerialMIDI<SerialMock> Transport;
  13. typedef midi::MidiInterface<Transport> MidiInterface;
  14. template<unsigned Size>
  15. struct VariableSysExSettings : midi::DefaultSettings
  16. {
  17. static const unsigned SysExMaxSize = Size;
  18. };
  19. TEST(MidiInput, getTypeFromStatusByte)
  20. {
  21. // Channel Messages
  22. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0x81), midi::NoteOff);
  23. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0x92), midi::NoteOn);
  24. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xa3), midi::AfterTouchPoly);
  25. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xb4), midi::ControlChange);
  26. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xc5), midi::ProgramChange);
  27. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xd6), midi::AfterTouchChannel);
  28. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xe7), midi::PitchBend);
  29. // System Messages
  30. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf0), midi::SystemExclusive);
  31. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf1), midi::TimeCodeQuarterFrame);
  32. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf2), midi::SongPosition);
  33. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf3), midi::SongSelect);
  34. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf6), midi::TuneRequest);
  35. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf8), midi::Clock);
  36. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xfa), midi::Start);
  37. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xfb), midi::Continue);
  38. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xfc), midi::Stop);
  39. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xfe), midi::ActiveSensing);
  40. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xff), midi::SystemReset);
  41. // Invalid Messages
  42. for (int i = 0; i < 0x80; ++i)
  43. {
  44. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(i), midi::InvalidType);
  45. }
  46. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf4), midi::InvalidType);
  47. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xf5), midi::InvalidType);
  48. EXPECT_EQ(MidiInterface::getTypeFromStatusByte(0xfd), midi::InvalidType);
  49. }
  50. TEST(MidiInput, getChannelFromStatusByte)
  51. {
  52. EXPECT_EQ(MidiInterface::getChannelFromStatusByte(0x00), 1);
  53. EXPECT_EQ(MidiInterface::getChannelFromStatusByte(0x80), 1);
  54. EXPECT_EQ(MidiInterface::getChannelFromStatusByte(0x94), 5);
  55. EXPECT_EQ(MidiInterface::getChannelFromStatusByte(0xaf), 16);
  56. }
  57. TEST(MidiInput, isChannelMessage)
  58. {
  59. EXPECT_EQ(MidiInterface::isChannelMessage(midi::InvalidType), false);
  60. EXPECT_EQ(MidiInterface::isChannelMessage(midi::NoteOff), true);
  61. EXPECT_EQ(MidiInterface::isChannelMessage(midi::NoteOn), true);
  62. EXPECT_EQ(MidiInterface::isChannelMessage(midi::AfterTouchPoly), true);
  63. EXPECT_EQ(MidiInterface::isChannelMessage(midi::ControlChange), true);
  64. EXPECT_EQ(MidiInterface::isChannelMessage(midi::ProgramChange), true);
  65. EXPECT_EQ(MidiInterface::isChannelMessage(midi::AfterTouchChannel), true);
  66. EXPECT_EQ(MidiInterface::isChannelMessage(midi::PitchBend), true);
  67. EXPECT_EQ(MidiInterface::isChannelMessage(midi::SystemExclusive), false);
  68. EXPECT_EQ(MidiInterface::isChannelMessage(midi::TimeCodeQuarterFrame), false);
  69. EXPECT_EQ(MidiInterface::isChannelMessage(midi::SongPosition), false);
  70. EXPECT_EQ(MidiInterface::isChannelMessage(midi::SongSelect), false);
  71. EXPECT_EQ(MidiInterface::isChannelMessage(midi::TuneRequest), false);
  72. EXPECT_EQ(MidiInterface::isChannelMessage(midi::Clock), false);
  73. EXPECT_EQ(MidiInterface::isChannelMessage(midi::Start), false);
  74. EXPECT_EQ(MidiInterface::isChannelMessage(midi::Continue), false);
  75. EXPECT_EQ(MidiInterface::isChannelMessage(midi::Stop), false);
  76. EXPECT_EQ(MidiInterface::isChannelMessage(midi::ActiveSensing), false);
  77. EXPECT_EQ(MidiInterface::isChannelMessage(midi::SystemReset), false);
  78. }
  79. // --
  80. TEST(MidiInput, begin)
  81. {
  82. SerialMock serial;
  83. Transport transport(serial);
  84. MidiInterface midi((Transport&)transport);
  85. // Default channel
  86. midi.begin();
  87. EXPECT_EQ(serial.mBaudrate, 31250);
  88. EXPECT_EQ(midi.getInputChannel(), 1);
  89. // Specific channel
  90. midi.begin(12);
  91. EXPECT_EQ(serial.mBaudrate, 31250);
  92. EXPECT_EQ(midi.getInputChannel(), 12);
  93. }
  94. TEST(MidiInput, initInputChannel)
  95. {
  96. SerialMock serial;
  97. Transport transport(serial);
  98. MidiInterface midi((Transport&)transport);
  99. EXPECT_EQ(midi.getInputChannel(), 0);
  100. midi.setInputChannel(12);
  101. EXPECT_EQ(midi.getInputChannel(), 12);
  102. }
  103. TEST(MidiInput, initMessage)
  104. {
  105. SerialMock serial;
  106. Transport transport(serial);
  107. MidiInterface midi((Transport&)transport);
  108. EXPECT_EQ(midi.getType(), midi::InvalidType);
  109. EXPECT_EQ(midi.getChannel(), 0);
  110. EXPECT_EQ(midi.getData1(), 0);
  111. EXPECT_EQ(midi.getData2(), 0);
  112. EXPECT_EQ(midi.getSysExArrayLength(), unsigned(0));
  113. EXPECT_EQ(midi.check(), false);
  114. }
  115. TEST(MidiInput, channelFiltering)
  116. {
  117. SerialMock serial;
  118. Transport transport(serial);
  119. MidiInterface midi((Transport&)transport);
  120. static const unsigned rxSize = 3;
  121. static const byte rxData[rxSize] = { 0x9b, 12, 34 };
  122. midi.begin(4); // Mistmatching channel
  123. serial.mRxBuffer.write(rxData, rxSize);
  124. EXPECT_EQ(midi.read(), false);
  125. EXPECT_EQ(midi.read(), false);
  126. EXPECT_EQ(midi.read(), false);
  127. }
  128. TEST(MidiInput, noRxData)
  129. {
  130. SerialMock serial;
  131. Transport transport(serial);
  132. MidiInterface midi((Transport&)transport);
  133. midi.begin();
  134. EXPECT_EQ(midi.read(), false);
  135. }
  136. TEST(MidiInput, inputDisabled)
  137. {
  138. SerialMock serial;
  139. Transport transport(serial);
  140. MidiInterface midi((Transport&)transport);
  141. static const unsigned rxSize = 3;
  142. static const byte rxData[rxSize] = { 0x9b, 12, 34 };
  143. midi.begin(MIDI_CHANNEL_OFF); // Invalid channel
  144. serial.mRxBuffer.write(rxData, rxSize);
  145. EXPECT_EQ(midi.read(), false);
  146. EXPECT_EQ(midi.read(), false);
  147. EXPECT_EQ(midi.read(), false);
  148. }
  149. TEST(MidiInput, multiByteParsing)
  150. {
  151. typedef VariableSettings<false, false> Settings;
  152. typedef midi::MidiInterface<Transport, Settings> MultiByteMidiInterface;
  153. SerialMock serial;
  154. Transport transport(serial);
  155. MultiByteMidiInterface midi(transport);
  156. static const unsigned rxSize = 3;
  157. static const byte rxData[rxSize] = { 0x9b, 12, 34 };
  158. midi.begin(12);
  159. serial.mRxBuffer.write(rxData, rxSize);
  160. EXPECT_EQ(midi.read(), true);
  161. }
  162. TEST(MidiInput, noteOn)
  163. {
  164. SerialMock serial;
  165. Transport transport(serial);
  166. MidiInterface midi((Transport&)transport);
  167. static const unsigned rxSize = 10;
  168. static const byte rxData[rxSize] = {
  169. 0x9b, 12, 34,
  170. 0x9b, 56, 78,
  171. 12, 34, // Running status
  172. 56, 0 // NoteOn with null velocity interpreted as NoteOff
  173. };
  174. midi.begin(12);
  175. serial.mRxBuffer.write(rxData, rxSize);
  176. // 1 byte parsing
  177. EXPECT_EQ(midi.read(), false);
  178. EXPECT_EQ(midi.read(), false);
  179. EXPECT_EQ(midi.read(), true);
  180. // First NoteOn
  181. EXPECT_EQ(midi.getType(), midi::NoteOn);
  182. EXPECT_EQ(midi.getChannel(), 12);
  183. EXPECT_EQ(midi.getData1(), 12);
  184. EXPECT_EQ(midi.getData2(), 34);
  185. EXPECT_EQ(midi.read(), false);
  186. EXPECT_EQ(midi.read(), false);
  187. EXPECT_EQ(midi.read(), true);
  188. EXPECT_EQ(midi.getType(), midi::NoteOn);
  189. EXPECT_EQ(midi.getChannel(), 12);
  190. EXPECT_EQ(midi.getData1(), 56);
  191. EXPECT_EQ(midi.getData2(), 78);
  192. EXPECT_EQ(midi.read(), false);
  193. EXPECT_EQ(midi.read(), true);
  194. EXPECT_EQ(midi.getType(), midi::NoteOn);
  195. EXPECT_EQ(midi.getChannel(), 12);
  196. EXPECT_EQ(midi.getData1(), 12);
  197. EXPECT_EQ(midi.getData2(), 34);
  198. EXPECT_EQ(midi.read(), false);
  199. EXPECT_EQ(midi.read(), true);
  200. EXPECT_EQ(midi.getType(), midi::NoteOff);
  201. EXPECT_EQ(midi.getChannel(), 12);
  202. EXPECT_EQ(midi.getData1(), 56);
  203. EXPECT_EQ(midi.getData2(), 0);
  204. }
  205. TEST(MidiInput, noteOff)
  206. {
  207. SerialMock serial;
  208. Transport transport(serial);
  209. MidiInterface midi((Transport&)transport);
  210. static const unsigned rxSize = 8;
  211. static const byte rxData[rxSize] = {
  212. 0x8b, 12, 34,
  213. 0x8b, 56, 78,
  214. 12, 34, // Running status
  215. };
  216. midi.begin(12);
  217. serial.mRxBuffer.write(rxData, rxSize);
  218. // 1 byte parsing
  219. EXPECT_EQ(midi.read(), false);
  220. EXPECT_EQ(midi.read(), false);
  221. EXPECT_EQ(midi.read(), true);
  222. // First NoteOn
  223. EXPECT_EQ(midi.getType(), midi::NoteOff);
  224. EXPECT_EQ(midi.getChannel(), 12);
  225. EXPECT_EQ(midi.getData1(), 12);
  226. EXPECT_EQ(midi.getData2(), 34);
  227. EXPECT_EQ(midi.read(), false);
  228. EXPECT_EQ(midi.read(), false);
  229. EXPECT_EQ(midi.read(), true);
  230. EXPECT_EQ(midi.getType(), midi::NoteOff);
  231. EXPECT_EQ(midi.getChannel(), 12);
  232. EXPECT_EQ(midi.getData1(), 56);
  233. EXPECT_EQ(midi.getData2(), 78);
  234. EXPECT_EQ(midi.read(), false);
  235. EXPECT_EQ(midi.read(), true);
  236. EXPECT_EQ(midi.getType(), midi::NoteOff);
  237. EXPECT_EQ(midi.getChannel(), 12);
  238. EXPECT_EQ(midi.getData1(), 12);
  239. EXPECT_EQ(midi.getData2(), 34);
  240. }
  241. TEST(MidiInput, programChange)
  242. {
  243. SerialMock serial;
  244. Transport transport(serial);
  245. MidiInterface midi((Transport&)transport);
  246. static const unsigned rxSize = 6;
  247. static const byte rxData[rxSize] = {
  248. 0xc3, 12, 34,
  249. 0xc4, 56, 78
  250. };
  251. midi.begin(MIDI_CHANNEL_OMNI);
  252. serial.mRxBuffer.write(rxData, rxSize);
  253. // 1 byte parsing
  254. EXPECT_EQ(midi.read(), false);
  255. EXPECT_EQ(midi.read(), true);
  256. EXPECT_EQ(midi.getType(), midi::ProgramChange);
  257. EXPECT_EQ(midi.getChannel(), 4);
  258. EXPECT_EQ(midi.getData1(), 12);
  259. EXPECT_EQ(midi.getData2(), 0);
  260. EXPECT_EQ(midi.read(), true);
  261. EXPECT_EQ(midi.getType(), midi::ProgramChange);
  262. EXPECT_EQ(midi.getChannel(), 4);
  263. EXPECT_EQ(midi.getData1(), 34);
  264. EXPECT_EQ(midi.getData2(), 0);
  265. EXPECT_EQ(midi.read(), false);
  266. EXPECT_EQ(midi.read(), true);
  267. EXPECT_EQ(midi.getType(), midi::ProgramChange);
  268. EXPECT_EQ(midi.getChannel(), 5);
  269. EXPECT_EQ(midi.getData1(), 56);
  270. EXPECT_EQ(midi.getData2(), 0);
  271. EXPECT_EQ(midi.read(), true);
  272. EXPECT_EQ(midi.getType(), midi::ProgramChange);
  273. EXPECT_EQ(midi.getChannel(), 5);
  274. EXPECT_EQ(midi.getData1(), 78);
  275. EXPECT_EQ(midi.getData2(), 0);
  276. }
  277. TEST(MidiInput, controlChange)
  278. {
  279. SerialMock serial;
  280. Transport transport(serial);
  281. MidiInterface midi((Transport&)transport);
  282. static const unsigned rxSize = 8;
  283. static const byte rxData[rxSize] = {
  284. 0xbb, 12, 34,
  285. 0xbb, 56, 78,
  286. 12, 34
  287. };
  288. midi.begin(12);
  289. serial.mRxBuffer.write(rxData, rxSize);
  290. // 1 byte parsing
  291. EXPECT_EQ(midi.read(), false);
  292. EXPECT_EQ(midi.read(), false);
  293. EXPECT_EQ(midi.read(), true);
  294. // First NoteOn
  295. EXPECT_EQ(midi.getType(), midi::ControlChange);
  296. EXPECT_EQ(midi.getChannel(), 12);
  297. EXPECT_EQ(midi.getData1(), 12);
  298. EXPECT_EQ(midi.getData2(), 34);
  299. EXPECT_EQ(midi.read(), false);
  300. EXPECT_EQ(midi.read(), false);
  301. EXPECT_EQ(midi.read(), true);
  302. EXPECT_EQ(midi.getType(), midi::ControlChange);
  303. EXPECT_EQ(midi.getChannel(), 12);
  304. EXPECT_EQ(midi.getData1(), 56);
  305. EXPECT_EQ(midi.getData2(), 78);
  306. EXPECT_EQ(midi.read(), false);
  307. EXPECT_EQ(midi.read(), true);
  308. EXPECT_EQ(midi.getType(), midi::ControlChange);
  309. EXPECT_EQ(midi.getChannel(), 12);
  310. EXPECT_EQ(midi.getData1(), 12);
  311. EXPECT_EQ(midi.getData2(), 34);
  312. }
  313. TEST(MidiInput, pitchBend)
  314. {
  315. SerialMock serial;
  316. Transport transport(serial);
  317. MidiInterface midi((Transport&)transport);
  318. static const unsigned rxSize = 8;
  319. static const byte rxData[rxSize] = {
  320. 0xeb, 12, 34,
  321. 0xeb, 56, 78,
  322. 12, 34
  323. };
  324. midi.begin(12);
  325. serial.mRxBuffer.write(rxData, rxSize);
  326. // 1 byte parsing
  327. EXPECT_EQ(midi.read(), false);
  328. EXPECT_EQ(midi.read(), false);
  329. EXPECT_EQ(midi.read(), true);
  330. // First NoteOn
  331. EXPECT_EQ(midi.getType(), midi::PitchBend);
  332. EXPECT_EQ(midi.getChannel(), 12);
  333. EXPECT_EQ(midi.getData1(), 12);
  334. EXPECT_EQ(midi.getData2(), 34);
  335. EXPECT_EQ(midi.read(), false);
  336. EXPECT_EQ(midi.read(), false);
  337. EXPECT_EQ(midi.read(), true);
  338. EXPECT_EQ(midi.getType(), midi::PitchBend);
  339. EXPECT_EQ(midi.getChannel(), 12);
  340. EXPECT_EQ(midi.getData1(), 56);
  341. EXPECT_EQ(midi.getData2(), 78);
  342. EXPECT_EQ(midi.read(), false);
  343. EXPECT_EQ(midi.read(), true);
  344. EXPECT_EQ(midi.getType(), midi::PitchBend);
  345. EXPECT_EQ(midi.getChannel(), 12);
  346. EXPECT_EQ(midi.getData1(), 12);
  347. EXPECT_EQ(midi.getData2(), 34);
  348. }
  349. TEST(MidiInput, afterTouchPoly)
  350. {
  351. SerialMock serial;
  352. Transport transport(serial);
  353. MidiInterface midi((Transport&)transport);
  354. static const unsigned rxSize = 8;
  355. static const byte rxData[rxSize] = {
  356. 0xab, 12, 34,
  357. 0xab, 56, 78,
  358. 12, 34
  359. };
  360. midi.begin(12);
  361. serial.mRxBuffer.write(rxData, rxSize);
  362. // 1 byte parsing
  363. EXPECT_EQ(midi.read(), false);
  364. EXPECT_EQ(midi.read(), false);
  365. EXPECT_EQ(midi.read(), true);
  366. // First NoteOn
  367. EXPECT_EQ(midi.getType(), midi::AfterTouchPoly);
  368. EXPECT_EQ(midi.getChannel(), 12);
  369. EXPECT_EQ(midi.getData1(), 12);
  370. EXPECT_EQ(midi.getData2(), 34);
  371. EXPECT_EQ(midi.read(), false);
  372. EXPECT_EQ(midi.read(), false);
  373. EXPECT_EQ(midi.read(), true);
  374. EXPECT_EQ(midi.getType(), midi::AfterTouchPoly);
  375. EXPECT_EQ(midi.getChannel(), 12);
  376. EXPECT_EQ(midi.getData1(), 56);
  377. EXPECT_EQ(midi.getData2(), 78);
  378. EXPECT_EQ(midi.read(), false);
  379. EXPECT_EQ(midi.read(), true);
  380. EXPECT_EQ(midi.getType(), midi::AfterTouchPoly);
  381. EXPECT_EQ(midi.getChannel(), 12);
  382. EXPECT_EQ(midi.getData1(), 12);
  383. EXPECT_EQ(midi.getData2(), 34);
  384. }
  385. TEST(MidiInput, afterTouchChannel)
  386. {
  387. SerialMock serial;
  388. Transport transport(serial);
  389. MidiInterface midi((Transport&)transport);
  390. static const unsigned rxSize = 6;
  391. static const byte rxData[rxSize] = {
  392. 0xd3, 12, 34,
  393. 0xd4, 56, 78
  394. };
  395. midi.begin(MIDI_CHANNEL_OMNI);
  396. serial.mRxBuffer.write(rxData, rxSize);
  397. // 1 byte parsing
  398. EXPECT_EQ(midi.read(), false);
  399. EXPECT_EQ(midi.read(), true);
  400. EXPECT_EQ(midi.getType(), midi::AfterTouchChannel);
  401. EXPECT_EQ(midi.getChannel(), 4);
  402. EXPECT_EQ(midi.getData1(), 12);
  403. EXPECT_EQ(midi.getData2(), 0);
  404. EXPECT_EQ(midi.read(), true);
  405. EXPECT_EQ(midi.getType(), midi::AfterTouchChannel);
  406. EXPECT_EQ(midi.getChannel(), 4);
  407. EXPECT_EQ(midi.getData1(), 34);
  408. EXPECT_EQ(midi.getData2(), 0);
  409. EXPECT_EQ(midi.read(), false);
  410. EXPECT_EQ(midi.read(), true);
  411. EXPECT_EQ(midi.getType(), midi::AfterTouchChannel);
  412. EXPECT_EQ(midi.getChannel(), 5);
  413. EXPECT_EQ(midi.getData1(), 56);
  414. EXPECT_EQ(midi.getData2(), 0);
  415. EXPECT_EQ(midi.read(), true);
  416. EXPECT_EQ(midi.getType(), midi::AfterTouchChannel);
  417. EXPECT_EQ(midi.getChannel(), 5);
  418. EXPECT_EQ(midi.getData1(), 78);
  419. EXPECT_EQ(midi.getData2(), 0);
  420. }
  421. TEST(MidiInput, sysExWithinBufferSize)
  422. {
  423. typedef VariableSysExSettings<1024> Settings;
  424. typedef test_mocks::SerialMock<2048> LargerSerialMock;
  425. typedef midi::SerialMIDI<LargerSerialMock> LargerTransport;
  426. typedef midi::MidiInterface<LargerTransport, Settings> LargerMidiInterface;
  427. LargerSerialMock serial;
  428. LargerTransport transport(serial);
  429. LargerMidiInterface midi(transport);
  430. // Short Frame < 256
  431. {
  432. static const unsigned frameLength = 15;
  433. static const byte frame[frameLength] = {
  434. 0xf0, 'H','e','l','l','o',',',' ','W','o','r','l','d','!', 0xf7
  435. };
  436. midi.begin();
  437. serial.mRxBuffer.write(frame, frameLength);
  438. for (unsigned i = 0; i < frameLength - 1; ++i)
  439. {
  440. EXPECT_EQ(midi.read(), false);
  441. }
  442. EXPECT_EQ(midi.read(), true); // 0xf7
  443. EXPECT_EQ(midi.getSysExArrayLength(), frameLength);
  444. const std::vector<byte> sysExData(midi.getSysExArray(),
  445. midi.getSysExArray() + frameLength);
  446. EXPECT_THAT(sysExData, ElementsAreArray(frame));
  447. }
  448. // Long Frame
  449. {
  450. static const unsigned frameLength = 957;
  451. static const byte frame[frameLength] = {
  452. 0xf0,
  453. 'L','o','r','e','m',' ','i','p','s','u','m',' ','d','o','l','o','r',' ','s','i','t',' ','a','m','e','t',',',' ',
  454. 'c','o','n','s','e','c','t','e','t','u','r',' ','a','d','i','p','i','s','c','i','n','g',' ','e','l','i','t','.',' ','P','r','o','i','n',' ','m','a','x','i','m','u','s',' ','d','u','i',' ','a',' ','m','a','s','s','a',' ','m','a','x','i','m','u','s',',',' ',
  455. 'a',' ','v','e','s','t','i','b','u','l','u','m',' ','m','i',' ','v','e','n','e','n','a','t','i','s','.',' ','C','r','a','s',' ','s','i','t',' ','a','m','e','t',' ','e','x',' ','i','d',' ','v','e','l','i','t',' ','s','u','s','c','i','p','i','t',' ','p','h','a','r','e','t','r','a',' ','e','g','e','t', ' ','a',' ','t','u','r','p','i','s','.',' ','P','h','a','s','e','l','l','u','s',' ','i','n','t','e','r','d','u','m',' ','m','e','t','u','s',' ','a','c',' ','s','a','g','i','t','t','i','s',' ','c','u','r','s','u','s','.',' ','N','a','m',' ','q','u','i','s',' ','e','s','t',' ','a','t',' ','n','i','s', 'l',' ','u','l','l','a','m','c','o','r','p','e','r',' ','e','g','e','s','t','a','s',' ','p','u','l','v','i','n','a','r',' ','e','u',' ','e','r','a','t','.',' ','D','u','i','s',' ','a',' ','e','l','i','t',' ','d','i','g','n','i','s','s','i','m',',',' ',
  456. 'v','e','s','t','i','b','u','l','u','m',' ','e','r','o','s',' ','v','e','l',',',' ',
  457. 't','e','m','p','u','s',' ','n','i','s','l','.',' ','A','e','n','e','a','n',' ','t','u','r','p','i','s',' ','n','u','n','c',',',' ',
  458. 'c','u','r','s','u','s',' ','v','e','l',' ','l','a','c','i','n','i','a',' ','n','o','n',',',' ',
  459. 'p','h','a','r','e','t','r','a',' ','e','g','e','t',' ','s','a','p','i','e','n','.',' ','D','u','i','s',' ','c','o','n','d','i','m','e','n','t','u','m',',',' ',
  460. 'l','a','c','u','s',' ','a','t',' ','p','u','l','v','i','n','a','r',' ','t','e','m','p','o','r',',',' ',
  461. 'l','e','o',' ','l','i','b','e','r','o',' ','v','o','l','u','t','p','a','t',' ','n','i','s','l',',',' ',
  462. 'e','g','e','t',' ','p','o','r','t','t','i','t','o','r',' ','l','o','r','e','m',' ','m','i',' ','s','e','d',' ','m','a','g','n','a','.',' ','D','u','i','s',' ','d','i','c','t','u','m',',',' ',
  463. 'm','a','s','s','a',' ','v','e','l',' ','e','u','i','s','m','o','d',' ','i','n','t','e','r','d','u','m',',',' ',
  464. 'l','o','r','e','m',' ','m','i',' ','e','g','e','s','t','a','s',' ','e','l','i','t',',',' ',
  465. 'h','e','n','d','r','e','r','i','t',' ','t','i','n','c','i','d','u','n','t',' ','e','s','t',' ','a','r','c','u',' ','a',' ','l','i','b','e','r','o','.',' ','I','n','t','e','r','d','u','m',' ','e','t',' ','m','a','l','e','s','u','a','d','a',' ','f','a','m','e','s',' ','a','c',' ','a','n','t','e',' ', 'i','p','s','u','m',' ','p','r','i','m','i','s',' ','i','n',' ','f','a','u','c','i','b','u','s','.',' ','C','u','r','a','b','i','t','u','r',' ','v','e','h','i','c','u','l','a',' ','m','a','g','n','a',' ','l','i','b','e','r','o',',',' ',
  466. 'a','t',' ','r','h','o','n','c','u','s',' ','s','e','m',' ','o','r','n','a','r','e',' ','a','.',' ','I','n',' ','e','l','e','m','e','n','t','u','m',',',' ',
  467. 'e','l','i','t',' ','e','t',' ','c','o','n','g','u','e',' ','p','u','l','v','i','n','a','r',',',' ',
  468. 'm','a','s','s','a',' ','v','e','l','i','t',' ','c','o','m','m','o','d','o',' ','v','e','l','i','t',',',' ',
  469. 'n','o','n',' ','e','l','e','m','e','n','t','u','m',' ','p','u','r','u','s',' ','l','i','g','u','l','a',' ','e','g','e','t',' ','l','a','c','u','s','.',' ','D','o','n','e','c',' ','e','f','f','i','c','i','t','u','r',' ','n','i','s','i',' ','e','u',' ','u','l','t','r','i','c','e','s',' ','e','f','f', 'i','c','i','t','u','r','.',' ','D','o','n','e','c',' ','n','e','q','u','e',' ','d','u','i',',',' ',
  470. 'u','l','l','a','m','c','o','r','p','e','r',' ','i','d',' ','m','o','l','e','s','t','i','e',' ','q','u','i','s',',',' ',
  471. 'c','o','n','s','e','q','u','a','t',' ','s','i','t',' ','a','m','e','t',' ','l','i','g','u','l','a','.',
  472. 0xf7,
  473. };
  474. midi.begin();
  475. serial.mRxBuffer.write(frame, frameLength);
  476. for (unsigned i = 0; i < frameLength - 1; ++i)
  477. {
  478. EXPECT_EQ(midi.read(), false);
  479. }
  480. EXPECT_EQ(serial.mRxBuffer.getLength(), 1);
  481. EXPECT_EQ(serial.mRxBuffer.peek(), 0xf7);
  482. EXPECT_EQ(midi.read(), true);
  483. }
  484. }
  485. TEST(MidiInput, sysExOverBufferSize)
  486. {
  487. typedef VariableSysExSettings<8> Settings;
  488. typedef midi::MidiInterface<Transport, Settings> SmallMidiInterface;
  489. SerialMock serial;
  490. Transport transport(serial);
  491. SmallMidiInterface midi(transport);
  492. static const unsigned frameLength = 15;
  493. static const byte frame[frameLength] = {
  494. 0xf0, 'H','e','l','l','o',',',' ','W','o','r','l','d','!', 0xf7
  495. };
  496. midi.begin();
  497. serial.mRxBuffer.write(frame, frameLength);
  498. EXPECT_EQ(midi.read(), false); // start sysex f0
  499. EXPECT_EQ(midi.read(), false); // H
  500. EXPECT_EQ(midi.read(), false); // e
  501. EXPECT_EQ(midi.read(), false); // l
  502. EXPECT_EQ(midi.read(), false); // l
  503. EXPECT_EQ(midi.read(), false); // o
  504. EXPECT_EQ(midi.read(), false); // , message send and buffer cleared.
  505. EXPECT_EQ(midi.read(), false); // start sysex
  506. EXPECT_EQ(midi.read(), false); // (space)
  507. EXPECT_EQ(midi.read(), false); // W
  508. EXPECT_EQ(midi.read(), false); // o
  509. EXPECT_EQ(midi.read(), false); // r
  510. EXPECT_EQ(midi.read(), false); // l
  511. EXPECT_EQ(midi.read(), false); // d
  512. EXPECT_EQ(midi.read(), true); // end sysex
  513. }
  514. TEST(MidiInput, mtcQuarterFrame)
  515. {
  516. SerialMock serial;
  517. Transport transport(serial);
  518. MidiInterface midi((Transport&)transport);
  519. static const unsigned rxSize = 4;
  520. static const byte rxData[rxSize] = {
  521. 0xf1, 12,
  522. 0xf1, 42
  523. };
  524. midi.begin(12);
  525. serial.mRxBuffer.write(rxData, rxSize);
  526. // 1 byte parsing
  527. EXPECT_EQ(midi.read(), false);
  528. EXPECT_EQ(midi.read(), true);
  529. EXPECT_EQ(midi.getType(), midi::TimeCodeQuarterFrame);
  530. EXPECT_EQ(midi.getChannel(), 0);
  531. EXPECT_EQ(midi.getData1(), 12);
  532. EXPECT_EQ(midi.getData2(), 0);
  533. EXPECT_EQ(midi.read(), false);
  534. EXPECT_EQ(midi.read(), true);
  535. EXPECT_EQ(midi.getType(), midi::TimeCodeQuarterFrame);
  536. EXPECT_EQ(midi.getChannel(), 0);
  537. EXPECT_EQ(midi.getData1(), 42);
  538. EXPECT_EQ(midi.getData2(), 0);
  539. }
  540. TEST(MidiInput, songPosition)
  541. {
  542. SerialMock serial;
  543. Transport transport(serial);
  544. MidiInterface midi((Transport&)transport);
  545. static const unsigned rxSize = 6;
  546. static const byte rxData[rxSize] = {
  547. 0xf2, 12, 34,
  548. 0xf2, 56, 78
  549. };
  550. midi.begin(12);
  551. serial.mRxBuffer.write(rxData, rxSize);
  552. // 1 byte parsing
  553. EXPECT_EQ(midi.read(), false);
  554. EXPECT_EQ(midi.read(), false);
  555. EXPECT_EQ(midi.read(), true);
  556. EXPECT_EQ(midi.getType(), midi::SongPosition);
  557. EXPECT_EQ(midi.getChannel(), 0);
  558. EXPECT_EQ(midi.getData1(), 12);
  559. EXPECT_EQ(midi.getData2(), 34);
  560. EXPECT_EQ(midi.read(), false);
  561. EXPECT_EQ(midi.read(), false);
  562. EXPECT_EQ(midi.read(), true);
  563. EXPECT_EQ(midi.getType(), midi::SongPosition);
  564. EXPECT_EQ(midi.getChannel(), 0);
  565. EXPECT_EQ(midi.getData1(), 56);
  566. EXPECT_EQ(midi.getData2(), 78);
  567. }
  568. TEST(MidiInput, songSelect)
  569. {
  570. SerialMock serial;
  571. Transport transport(serial);
  572. MidiInterface midi((Transport&)transport);
  573. static const unsigned rxSize = 4;
  574. static const byte rxData[rxSize] = {
  575. 0xf3, 12,
  576. 0xf3, 42
  577. };
  578. midi.begin(12);
  579. serial.mRxBuffer.write(rxData, rxSize);
  580. // 1 byte parsing
  581. EXPECT_EQ(midi.read(), false);
  582. EXPECT_EQ(midi.read(), true);
  583. EXPECT_EQ(midi.getType(), midi::SongSelect);
  584. EXPECT_EQ(midi.getChannel(), 0);
  585. EXPECT_EQ(midi.getData1(), 12);
  586. EXPECT_EQ(midi.getData2(), 0);
  587. EXPECT_EQ(midi.read(), false);
  588. EXPECT_EQ(midi.read(), true);
  589. EXPECT_EQ(midi.getType(), midi::SongSelect);
  590. EXPECT_EQ(midi.getChannel(), 0);
  591. EXPECT_EQ(midi.getData1(), 42);
  592. EXPECT_EQ(midi.getData2(), 0);
  593. }
  594. TEST(MidiInput, tuneRequest)
  595. {
  596. SerialMock serial;
  597. Transport transport(serial);
  598. MidiInterface midi((Transport&)transport);
  599. static const unsigned rxSize = 1;
  600. static const byte rxData[rxSize] = {
  601. 0xf6
  602. };
  603. midi.begin(12);
  604. serial.mRxBuffer.write(rxData, rxSize);
  605. EXPECT_EQ(midi.read(), true);
  606. EXPECT_EQ(midi.getType(), midi::TuneRequest);
  607. EXPECT_EQ(midi.getChannel(), 0);
  608. EXPECT_EQ(midi.getData1(), 0);
  609. EXPECT_EQ(midi.getData2(), 0);
  610. }
  611. TEST(MidiInput, realTime)
  612. {
  613. SerialMock serial;
  614. Transport transport(serial);
  615. MidiInterface midi((Transport&)transport);
  616. static const unsigned rxSize = 8;
  617. static const byte rxData[rxSize] = {
  618. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  619. };
  620. midi.begin(12);
  621. serial.mRxBuffer.write(rxData, rxSize);
  622. EXPECT_EQ(midi.read(), true);
  623. EXPECT_EQ(midi.getType(), midi::Clock);
  624. EXPECT_EQ(midi.getChannel(), 0);
  625. EXPECT_EQ(midi.getData1(), 0);
  626. EXPECT_EQ(midi.getData2(), 0);
  627. EXPECT_EQ(midi.read(), true);
  628. EXPECT_EQ(midi.getType(), midi::Tick);
  629. EXPECT_EQ(midi.getChannel(), 0);
  630. EXPECT_EQ(midi.getData1(), 0);
  631. EXPECT_EQ(midi.getData2(), 0);
  632. EXPECT_EQ(midi.read(), true);
  633. EXPECT_EQ(midi.getType(), midi::Start);
  634. EXPECT_EQ(midi.getChannel(), 0);
  635. EXPECT_EQ(midi.getData1(), 0);
  636. EXPECT_EQ(midi.getData2(), 0);
  637. EXPECT_EQ(midi.read(), true);
  638. EXPECT_EQ(midi.getType(), midi::Continue);
  639. EXPECT_EQ(midi.getChannel(), 0);
  640. EXPECT_EQ(midi.getData1(), 0);
  641. EXPECT_EQ(midi.getData2(), 0);
  642. EXPECT_EQ(midi.read(), true);
  643. EXPECT_EQ(midi.getType(), midi::Stop);
  644. EXPECT_EQ(midi.getChannel(), 0);
  645. EXPECT_EQ(midi.getData1(), 0);
  646. EXPECT_EQ(midi.getData2(), 0);
  647. EXPECT_EQ(midi.read(), false); // 0xfd = undefined
  648. EXPECT_EQ(midi.read(), true);
  649. EXPECT_EQ(midi.getType(), midi::ActiveSensing);
  650. EXPECT_EQ(midi.getChannel(), 0);
  651. EXPECT_EQ(midi.getData1(), 0);
  652. EXPECT_EQ(midi.getData2(), 0);
  653. EXPECT_EQ(midi.read(), true);
  654. EXPECT_EQ(midi.getType(), midi::SystemReset);
  655. EXPECT_EQ(midi.getChannel(), 0);
  656. EXPECT_EQ(midi.getData1(), 0);
  657. EXPECT_EQ(midi.getData2(), 0);
  658. }
  659. // --
  660. TEST(MidiInput, interleavedRealTime)
  661. {
  662. SerialMock serial;
  663. Transport transport(serial);
  664. MidiInterface midi((Transport&)transport);
  665. // Interleaved Clocks between NoteOn / Off messages (with running status)
  666. {
  667. static const unsigned rxSize = 13;
  668. static const byte rxData[rxSize] = {
  669. 0x9b, 12, 0xf8, 34,
  670. 12, 0,
  671. 42, 0xf8, 127,
  672. 0xf8,
  673. 42, 0xf8, 0
  674. };
  675. midi.begin(12);
  676. serial.mRxBuffer.write(rxData, rxSize);
  677. EXPECT_EQ(midi.read(), false);
  678. EXPECT_EQ(midi.read(), false);
  679. EXPECT_EQ(midi.read(), true);
  680. EXPECT_EQ(midi.getType(), midi::Clock);
  681. EXPECT_EQ(midi.getChannel(), 0);
  682. EXPECT_EQ(midi.getData1(), 0);
  683. EXPECT_EQ(midi.getData2(), 0);
  684. EXPECT_EQ(midi.read(), true);
  685. EXPECT_EQ(midi.getType(), midi::NoteOn);
  686. EXPECT_EQ(midi.getChannel(), 12);
  687. EXPECT_EQ(midi.getData1(), 12);
  688. EXPECT_EQ(midi.getData2(), 34);
  689. EXPECT_EQ(midi.read(), false);
  690. EXPECT_EQ(midi.read(), true);
  691. EXPECT_EQ(midi.getType(), midi::NoteOff);
  692. EXPECT_EQ(midi.getChannel(), 12);
  693. EXPECT_EQ(midi.getData1(), 12);
  694. EXPECT_EQ(midi.getData2(), 0);
  695. EXPECT_EQ(midi.read(), false);
  696. EXPECT_EQ(midi.read(), true);
  697. EXPECT_EQ(midi.getType(), midi::Clock);
  698. EXPECT_EQ(midi.getChannel(), 0);
  699. EXPECT_EQ(midi.getData1(), 0);
  700. EXPECT_EQ(midi.getData2(), 0);
  701. EXPECT_EQ(midi.read(), true);
  702. EXPECT_EQ(midi.getType(), midi::NoteOn);
  703. EXPECT_EQ(midi.getChannel(), 12);
  704. EXPECT_EQ(midi.getData1(), 42);
  705. EXPECT_EQ(midi.getData2(), 127);
  706. EXPECT_EQ(midi.read(), true);
  707. EXPECT_EQ(midi.getType(), midi::Clock);
  708. EXPECT_EQ(midi.getChannel(), 0);
  709. EXPECT_EQ(midi.getData1(), 0);
  710. EXPECT_EQ(midi.getData2(), 0);
  711. EXPECT_EQ(midi.read(), false);
  712. EXPECT_EQ(midi.read(), true);
  713. EXPECT_EQ(midi.getType(), midi::Clock);
  714. EXPECT_EQ(midi.getChannel(), 0);
  715. EXPECT_EQ(midi.getData1(), 0);
  716. EXPECT_EQ(midi.getData2(), 0);
  717. EXPECT_EQ(midi.read(), true);
  718. EXPECT_EQ(midi.getType(), midi::NoteOff);
  719. EXPECT_EQ(midi.getChannel(), 12);
  720. EXPECT_EQ(midi.getData1(), 42);
  721. EXPECT_EQ(midi.getData2(), 0);
  722. }
  723. // Interleaved ActiveSensing between SysEx
  724. {
  725. static const unsigned rxSize = 6;
  726. static const byte rxData[rxSize] = {
  727. 0xf0, 12, 34, 0xfe, 56, 0xf7
  728. };
  729. midi.begin(12);
  730. serial.mRxBuffer.write(rxData, rxSize);
  731. EXPECT_EQ(midi.read(), false);
  732. EXPECT_EQ(midi.read(), false);
  733. EXPECT_EQ(midi.read(), false);
  734. EXPECT_EQ(midi.read(), true);
  735. EXPECT_EQ(midi.getType(), midi::ActiveSensing);
  736. EXPECT_EQ(midi.getChannel(), 0);
  737. EXPECT_EQ(midi.getData1(), 0);
  738. EXPECT_EQ(midi.getData2(), 0);
  739. EXPECT_EQ(midi.read(), false);
  740. EXPECT_EQ(midi.read(), true);
  741. EXPECT_EQ(midi.getSysExArrayLength(), rxSize - 1);
  742. const std::vector<byte> sysExData(midi.getSysExArray(),
  743. midi.getSysExArray() + rxSize - 1);
  744. EXPECT_THAT(sysExData, ElementsAreArray({
  745. 0xf0, 12, 34, 56, 0xf7
  746. }));
  747. }
  748. }
  749. TEST(MidiInput, strayEox)
  750. {
  751. // A stray End of Exclusive will reset the parser, but should it ?
  752. SerialMock serial;
  753. Transport transport(serial);
  754. MidiInterface midi((Transport&)transport);
  755. static const unsigned rxSize = 4;
  756. static const byte rxData[rxSize] = {
  757. 0x8b, 42, 0xf7, 12
  758. };
  759. midi.begin(MIDI_CHANNEL_OMNI);
  760. serial.mRxBuffer.write(rxData, rxSize);
  761. EXPECT_EQ(midi.read(), false);
  762. EXPECT_EQ(midi.read(), false);
  763. EXPECT_EQ(midi.read(), false);
  764. EXPECT_EQ(midi.read(), false);
  765. }
  766. TEST(MidiInput, strayUndefinedOneByteParsing)
  767. {
  768. SerialMock serial;
  769. Transport transport(serial);
  770. MidiInterface midi((Transport&)transport);
  771. static const unsigned rxSize = 13;
  772. static const byte rxData[rxSize] = {
  773. 0xbb, 12, 0xfd, 34,
  774. 12, 0,
  775. 42, 0xfd, 127,
  776. 0xfd,
  777. 42, 0xfd, 0
  778. };
  779. midi.begin(12);
  780. serial.mRxBuffer.write(rxData, rxSize);
  781. EXPECT_EQ(midi.read(), false);
  782. EXPECT_EQ(midi.read(), false);
  783. EXPECT_EQ(midi.read(), false); // Invalid, should not reset parser
  784. EXPECT_EQ(midi.read(), true);
  785. EXPECT_EQ(midi.getType(), midi::ControlChange);
  786. EXPECT_EQ(midi.getChannel(), 12);
  787. EXPECT_EQ(midi.getData1(), 12);
  788. EXPECT_EQ(midi.getData2(), 34);
  789. EXPECT_EQ(midi.read(), false);
  790. EXPECT_EQ(midi.read(), true);
  791. EXPECT_EQ(midi.getType(), midi::ControlChange);
  792. EXPECT_EQ(midi.getChannel(), 12);
  793. EXPECT_EQ(midi.getData1(), 12);
  794. EXPECT_EQ(midi.getData2(), 0);
  795. EXPECT_EQ(midi.read(), false);
  796. EXPECT_EQ(midi.read(), false);
  797. EXPECT_EQ(midi.read(), true);
  798. EXPECT_EQ(midi.getType(), midi::ControlChange);
  799. EXPECT_EQ(midi.getChannel(), 12);
  800. EXPECT_EQ(midi.getData1(), 42);
  801. EXPECT_EQ(midi.getData2(), 127);
  802. EXPECT_EQ(midi.read(), false);
  803. EXPECT_EQ(midi.read(), false);
  804. EXPECT_EQ(midi.read(), false);
  805. EXPECT_EQ(midi.read(), true);
  806. EXPECT_EQ(midi.getType(), midi::ControlChange);
  807. EXPECT_EQ(midi.getChannel(), 12);
  808. EXPECT_EQ(midi.getData1(), 42);
  809. EXPECT_EQ(midi.getData2(), 0);
  810. }
  811. TEST(MidiInput, strayUndefinedMultiByteParsing)
  812. {
  813. typedef VariableSettings<false, false> Settings;
  814. typedef midi::MidiInterface<Transport, Settings> MultiByteMidiInterface;
  815. SerialMock serial;
  816. Transport transport(serial);
  817. MultiByteMidiInterface midi(transport);
  818. static const unsigned rxSize = 4;
  819. static const byte rxData[rxSize] = {
  820. 0xbb, 12, 0xfd, 34,
  821. };
  822. midi.begin(12);
  823. serial.mRxBuffer.write(rxData, rxSize);
  824. EXPECT_EQ(midi.read(), true);
  825. EXPECT_EQ(midi.getType(), midi::ControlChange);
  826. EXPECT_EQ(midi.getChannel(), 12);
  827. EXPECT_EQ(midi.getData1(), 12);
  828. EXPECT_EQ(midi.getData2(), 34);
  829. }
  830. END_UNNAMED_NAMESPACE