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.

1821 lines
40KB

  1. //
  2. // Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
  3. // Creation Date: Sat Feb 14 20:49:21 PST 2015
  4. // Last Modified: Sun Apr 15 11:11:05 PDT 2018 Added event removal system.
  5. // Filename: midifile/src-library/MidiMessage.cpp
  6. // Website: http://midifile.sapp.org
  7. // Syntax: C++11
  8. // vim: ts=3 noexpandtab
  9. //
  10. // Description: Storage for bytes of a MIDI message for Standard
  11. // MIDI Files.
  12. //
  13. #include "MidiMessage.h"
  14. #include <vector>
  15. #include <iostream>
  16. #include <iterator>
  17. namespace smf {
  18. //////////////////////////////
  19. //
  20. // MidiMessage::MidiMessage -- Constructor.
  21. //
  22. MidiMessage::MidiMessage(void) : vector<uchar>() {
  23. // do nothing
  24. }
  25. MidiMessage::MidiMessage(int command) : vector<uchar>(1, (uchar)command) {
  26. // do nothing
  27. }
  28. MidiMessage::MidiMessage(int command, int p1) : vector<uchar>(2) {
  29. (*this)[0] = (uchar)command;
  30. (*this)[1] = (uchar)p1;
  31. }
  32. MidiMessage::MidiMessage(int command, int p1, int p2) : vector<uchar>(3) {
  33. (*this)[0] = (uchar)command;
  34. (*this)[1] = (uchar)p1;
  35. (*this)[2] = (uchar)p2;
  36. }
  37. MidiMessage::MidiMessage(const MidiMessage& message) : vector<uchar>() {
  38. (*this) = message;
  39. }
  40. MidiMessage::MidiMessage(const std::vector<uchar>& message) : vector<uchar>() {
  41. setMessage(message);
  42. }
  43. MidiMessage::MidiMessage(const std::vector<char>& message) : vector<uchar>() {
  44. setMessage(message);
  45. }
  46. MidiMessage::MidiMessage(const std::vector<int>& message) : vector<uchar>() {
  47. setMessage(message);
  48. }
  49. //////////////////////////////
  50. //
  51. // MidiMessage::~MidiMessage -- Deconstructor.
  52. //
  53. MidiMessage::~MidiMessage() {
  54. resize(0);
  55. }
  56. //////////////////////////////
  57. //
  58. // MidiMessage::operator= --
  59. //
  60. MidiMessage& MidiMessage::operator=(const MidiMessage& message) {
  61. if (this == &message) {
  62. return *this;
  63. }
  64. (*this) = message;
  65. return *this;
  66. }
  67. MidiMessage& MidiMessage::operator=(const std::vector<uchar>& bytes) {
  68. if (this == &bytes) {
  69. return *this;
  70. }
  71. setMessage(bytes);
  72. return *this;
  73. }
  74. MidiMessage& MidiMessage::operator=(const std::vector<char>& bytes) {
  75. setMessage(bytes);
  76. return *this;
  77. }
  78. MidiMessage& MidiMessage::operator=(const std::vector<int>& bytes) {
  79. setMessage(bytes);
  80. return *this;
  81. }
  82. //////////////////////////////
  83. //
  84. // MidiMessage::setSize -- Change the size of the message byte list.
  85. // If the size is increased, then the new bytes are not initialized
  86. // to any specific values.
  87. //
  88. void MidiMessage::setSize(int asize) {
  89. this->resize(asize);
  90. }
  91. //////////////////////////////
  92. //
  93. // MidiMessage::getSize -- Return the size of the MIDI message bytes.
  94. //
  95. int MidiMessage::getSize(void) const {
  96. return (int)this->size();
  97. }
  98. //////////////////////////////
  99. //
  100. // MidiMessage::setSizeToCommand -- Set the number of parameters if the
  101. // command byte is set in the range from 0x80 to 0xef. Any newly
  102. // added parameter bytes will be set to 0. Commands in the range
  103. // of 0xF) should not use this function, and they will ignore
  104. // modification by this command.
  105. //
  106. int MidiMessage::setSizeToCommand(void) {
  107. int osize = (int)this->size();
  108. if (osize < 1) {
  109. return 0;
  110. }
  111. int command = getCommandNibble();
  112. if (command < 0) {
  113. return 0;
  114. }
  115. int bytecount = 1;
  116. switch (command) {
  117. case 0x80: bytecount = 2; break; // Note Off
  118. case 0x90: bytecount = 2; break; // Note On
  119. case 0xA0: bytecount = 2; break; // Aftertouch
  120. case 0xB0: bytecount = 2; break; // Continuous Controller
  121. case 0xC0: bytecount = 1; break; // Patch Change
  122. case 0xD0: bytecount = 1; break; // Channel Pressure
  123. case 0xE0: bytecount = 2; break; // Pitch Bend
  124. case 0xF0:
  125. default:
  126. return (int)size();
  127. }
  128. if (bytecount + 1 < osize) {
  129. resize(bytecount+1);
  130. for (int i=osize; i<bytecount+1; i++) {
  131. (*this)[i] = 0;
  132. }
  133. }
  134. return (int)size();
  135. }
  136. int MidiMessage::resizeToCommand(void) {
  137. return setSizeToCommand();
  138. }
  139. //////////////////////////////
  140. //
  141. // MidiMessage::getTempoMicro -- Returns the number of microseconds per
  142. // quarter note if the MidiMessage is a tempo meta message.
  143. // Returns -1 if the MIDI message is not a tempo meta message.
  144. //
  145. int MidiMessage::getTempoMicro(void) const {
  146. if (!isTempo()) {
  147. return -1;
  148. } else {
  149. return ((*this)[3] << 16) + ((*this)[4] << 8) + (*this)[5];
  150. }
  151. }
  152. int MidiMessage::getTempoMicroseconds(void) const {
  153. return getTempoMicro();
  154. }
  155. //////////////////////////////
  156. //
  157. // MidiMessage::getTempoSeconds -- Returns the number of seconds per
  158. // quarter note. Returns -1.0 if the MIDI message is not a
  159. // tempo meta message.
  160. //
  161. double MidiMessage::getTempoSeconds(void) const {
  162. int microseconds = getTempoMicroseconds();
  163. if (microseconds < 0) {
  164. return -1.0;
  165. } else {
  166. return (double)microseconds / 1000000.0;
  167. }
  168. }
  169. //////////////////////////////
  170. //
  171. // MidiMessage::getTempoBPM -- Returns the tempo in terms of beats per minute.
  172. // Returns -1 if the MidiMessage is note a tempo meta message.
  173. //
  174. double MidiMessage::getTempoBPM(void) const {
  175. int microseconds = getTempoMicroseconds();
  176. if (microseconds < 0) {
  177. return -1.0;
  178. }
  179. return 60000000.0 / (double)microseconds;
  180. }
  181. //////////////////////////////
  182. //
  183. // MidiMessage::getTempoTPS -- Returns the tempo in terms of ticks per seconds.
  184. //
  185. double MidiMessage::getTempoTPS(int tpq) const {
  186. int microseconds = getTempoMicroseconds();
  187. if (microseconds < 0) {
  188. return -1.0;
  189. } else {
  190. return tpq * 1000000.0 / (double)microseconds;
  191. }
  192. }
  193. //////////////////////////////
  194. //
  195. // MidiMessage::getTempoSPT -- Returns the tempo in terms of seconds per tick.
  196. //
  197. double MidiMessage::getTempoSPT(int tpq) const {
  198. int microseconds = getTempoMicroseconds();
  199. if (microseconds < 0) {
  200. return -1.0;
  201. } else {
  202. return (double)microseconds / 1000000.0 / tpq;
  203. }
  204. }
  205. //////////////////////////////
  206. //
  207. // MidiMessage::isMeta -- Returns true if message is a Meta message
  208. // (when the command byte is 0xff).
  209. //
  210. bool MidiMessage::isMeta(void) const {
  211. if (size() == 0) {
  212. return false;
  213. } else if ((*this)[0] != 0xff) {
  214. return false;
  215. } else if (size() < 3) {
  216. // meta message is ill-formed.
  217. // meta messages must have at least three bytes:
  218. // 0: 0xff == meta message marker
  219. // 1: meta message type
  220. // 2: meta message data bytes to follow
  221. return false;
  222. } else {
  223. return true;
  224. }
  225. }
  226. bool MidiMessage::isMetaMessage(void) const {
  227. return isMeta();
  228. }
  229. //////////////////////////////
  230. //
  231. // MidiMessage::isNoteOff -- Returns true if the command nibble is 0x80
  232. // or if the command nibble is 0x90 with p2=0 velocity.
  233. //
  234. bool MidiMessage::isNoteOff(void) const {
  235. if (size() != 3) {
  236. return false;
  237. } else if (((*this)[0] & 0xf0) == 0x80) {
  238. return true;
  239. } else if ((((*this)[0] & 0xf0) == 0x90) && ((*this)[2] == 0)) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. //////////////////////////////
  246. //
  247. // MidiMessage::isNoteOn -- Returns true if the command byte is in the 0x90
  248. // range and the velocity is non-zero
  249. //
  250. bool MidiMessage::isNoteOn(void) const {
  251. if (size() != 3) {
  252. return false;
  253. } else if (((*this)[0] & 0xf0) != 0x90) {
  254. return false;
  255. } else if ((*this)[2] == 0) {
  256. return false;
  257. } else {
  258. return true;
  259. }
  260. }
  261. //////////////////////////////
  262. //
  263. // MidiMessage::isNote -- Returns true if either a note-on or a note-off
  264. // message.
  265. //
  266. bool MidiMessage::isNote(void) const {
  267. return isNoteOn() || isNoteOff();
  268. }
  269. //////////////////////////////
  270. //
  271. // MidiMessage::isAftertouch -- Returns true if the command byte is in the 0xA0
  272. // range.
  273. //
  274. bool MidiMessage::isAftertouch(void) const {
  275. if (size() != 3) {
  276. return false;
  277. } else if (((*this)[0] & 0xf0) != 0xA0) {
  278. return false;
  279. } else {
  280. return true;
  281. }
  282. }
  283. //////////////////////////////
  284. //
  285. // MidiMessage::isController -- Returns true if the command byte is in the 0xB0
  286. // range and there are two additional data bytes.
  287. //
  288. bool MidiMessage::isController(void) const {
  289. if (size() != 3) {
  290. return false;
  291. } else if (((*this)[0] & 0xf0) != 0xB0) {
  292. return false;
  293. } else {
  294. return true;
  295. }
  296. }
  297. //////////////////////////////
  298. //
  299. // MidiMessage::isTimbre -- Returns true of a patch change message
  300. // (command nibble 0xc0).
  301. //
  302. bool MidiMessage::isTimbre(void) const {
  303. if (((*this)[0] & 0xf0) != 0xc0) {
  304. return false;
  305. } else if (size() != 2) {
  306. return false;
  307. } else {
  308. return true;
  309. }
  310. }
  311. bool MidiMessage::isPatchChange(void) const {
  312. return isTimbre();
  313. }
  314. //////////////////////////////
  315. //
  316. // MidiMessage::isPressure -- Returns true of a channel pressure message
  317. // (command nibble 0xd0).
  318. //
  319. bool MidiMessage::isPressure(void) const {
  320. if (((*this)[0] & 0xf0) != 0xd0) {
  321. return false;
  322. } else if (size() != 2) {
  323. return false;
  324. } else {
  325. return true;
  326. }
  327. }
  328. //////////////////////////////
  329. //
  330. // MidiMessage::isPitchbend -- Returns true of a pitch-bend message
  331. // (command nibble 0xe0).
  332. //
  333. bool MidiMessage::isPitchbend(void) const {
  334. if (((*this)[0] & 0xf0) != 0xe0) {
  335. return false;
  336. } else if (size() != 3) {
  337. return false;
  338. } else {
  339. return true;
  340. }
  341. }
  342. //////////////////////////////
  343. //
  344. // MidiMessage::isEmpty -- Returns true if size of data array is zero.
  345. //
  346. bool MidiMessage::isEmpty(void) const {
  347. return empty();
  348. }
  349. ///////////////////////////////
  350. //
  351. // MidiMessage::getMetaType -- returns the meta-message type for the
  352. // MidiMessage. If the message is not a meta message, then returns
  353. // -1.
  354. //
  355. int MidiMessage::getMetaType(void) const {
  356. if (!isMetaMessage()) {
  357. return -1;
  358. } else {
  359. return (int)(*this)[1];
  360. }
  361. }
  362. //////////////////////////////
  363. //
  364. // MidiMessage::isText -- Returns true if message is a meta
  365. // message describing some text (meta message type 0x01).
  366. //
  367. bool MidiMessage::isText(void) const {
  368. if (!isMetaMessage()) {
  369. return false;
  370. } else if ((*this)[1] != 0x01) {
  371. return false;
  372. } else {
  373. return true;
  374. }
  375. }
  376. //////////////////////////////
  377. //
  378. // MidiMessage::isCopyright -- Returns true if message is a meta
  379. // message describing a copyright notice (meta message type 0x02).
  380. // Copyright messages should be at absolute tick position 0
  381. // (and be the first event in the track chunk as well), but this
  382. // function does not check for those requirements.
  383. //
  384. bool MidiMessage::isCopyright(void) const {
  385. if (!isMetaMessage()) {
  386. return false;
  387. } else if ((*this)[1] != 0x02) {
  388. return false;
  389. } else {
  390. return true;
  391. }
  392. }
  393. //////////////////////////////
  394. //
  395. // MidiMessage::isTrackName -- Returns true if message is a meta
  396. // message describing a track name (meta message type 0x03).
  397. //
  398. bool MidiMessage::isTrackName(void) const {
  399. if (!isMetaMessage()) {
  400. return false;
  401. } else if ((*this)[1] != 0x03) {
  402. return false;
  403. } else {
  404. return true;
  405. }
  406. }
  407. //////////////////////////////
  408. //
  409. // MidiMessage::isInstrumentName -- Returns true if message is a
  410. // meta message describing an instrument name (for the track)
  411. // (meta message type 0x04).
  412. //
  413. bool MidiMessage::isInstrumentName(void) const {
  414. if (!isMetaMessage()) {
  415. return false;
  416. } else if ((*this)[1] != 0x04) {
  417. return false;
  418. } else {
  419. return true;
  420. }
  421. }
  422. //////////////////////////////
  423. //
  424. // MidiMessage::isLyricText -- Returns true if message is a meta message
  425. // describing some lyric text (for karakoke MIDI files)
  426. // (meta message type 0x05).
  427. //
  428. bool MidiMessage::isLyricText(void) const {
  429. if (!isMetaMessage()) {
  430. return false;
  431. } else if ((*this)[1] != 0x05) {
  432. return false;
  433. } else {
  434. return true;
  435. }
  436. }
  437. //////////////////////////////
  438. //
  439. // MidiMessage::isMarkerText -- Returns true if message is a meta message
  440. // describing a marker text (meta message type 0x06).
  441. //
  442. bool MidiMessage::isMarkerText(void) const {
  443. if (!isMetaMessage()) {
  444. return false;
  445. } else if ((*this)[1] != 0x06) {
  446. return false;
  447. } else {
  448. return true;
  449. }
  450. }
  451. //////////////////////////////
  452. //
  453. // MidiMessage::isTempo -- Returns true if message is a meta message
  454. // describing tempo (meta message type 0x51).
  455. //
  456. bool MidiMessage::isTempo(void) const {
  457. if (!isMetaMessage()) {
  458. return false;
  459. } else if ((*this)[1] != 0x51) {
  460. return false;
  461. } else if (size() != 6) {
  462. // Meta tempo message can only be 6 bytes long.
  463. return false;
  464. } else {
  465. return true;
  466. }
  467. }
  468. //////////////////////////////
  469. //
  470. // MidiMessage::isTimeSignature -- Returns true if message is
  471. // a meta message describing a time signature (meta message
  472. // type 0x58).
  473. //
  474. bool MidiMessage::isTimeSignature(void) const {
  475. if (!isMetaMessage()) {
  476. return false;
  477. } else if ((*this)[1] != 0x58) {
  478. return false;
  479. } else if (size() != 7) {
  480. // Meta time signature message can only be 7 bytes long:
  481. // FF 58 <size> <top> <bot-log-2> <clocks-per-beat> <32nds>
  482. return false;
  483. } else {
  484. return true;
  485. }
  486. }
  487. //////////////////////////////
  488. //
  489. // MidiMessage::isKeySignature -- Returns true if message is
  490. // a meta message describing a key signature (meta message
  491. // type 0x59).
  492. //
  493. bool MidiMessage::isKeySignature(void) const {
  494. if (!isMetaMessage()) {
  495. return false;
  496. } else if ((*this)[1] != 0x59) {
  497. return false;
  498. } else if (size() != 5) {
  499. // Meta key signature message can only be 5 bytes long:
  500. // FF 59 <size> <accid> <mode>
  501. return false;
  502. } else {
  503. return true;
  504. }
  505. }
  506. //////////////////////////////
  507. //
  508. // MidiMessage::isEndOfTrack -- Returns true if message is a meta message
  509. // for end-of-track (meta message type 0x2f).
  510. //
  511. bool MidiMessage::isEndOfTrack(void) const {
  512. return getMetaType() == 0x2f ? 1 : 0;
  513. }
  514. //////////////////////////////
  515. //
  516. // MidiMessage::getP0 -- Return index 1 byte, or -1 if it doesn't exist.
  517. //
  518. int MidiMessage::getP0(void) const {
  519. return size() < 1 ? -1 : (*this)[0];
  520. }
  521. //////////////////////////////
  522. //
  523. // MidiMessage::getP1 -- Return index 1 byte, or -1 if it doesn't exist.
  524. //
  525. int MidiMessage::getP1(void) const {
  526. return size() < 2 ? -1 : (*this)[1];
  527. }
  528. //////////////////////////////
  529. //
  530. // MidiMessage::getP2 -- Return index 2 byte, or -1 if it doesn't exist.
  531. //
  532. int MidiMessage::getP2(void) const {
  533. return size() < 3 ? -1 : (*this)[2];
  534. }
  535. //////////////////////////////
  536. //
  537. // MidiMessage::getP3 -- Return index 3 byte, or -1 if it doesn't exist.
  538. //
  539. int MidiMessage::getP3(void) const {
  540. return size() < 4 ? -1 : (*this)[3];
  541. }
  542. //////////////////////////////
  543. //
  544. // MidiMessage::getKeyNumber -- Return the key number (such as 60 for
  545. // middle C). If the message does not have a note parameter, then
  546. // return -1; if the key is invalid (above 127 in value), then
  547. // limit to the range 0 to 127.
  548. //
  549. int MidiMessage::getKeyNumber(void) const {
  550. if (isNote() || isAftertouch()) {
  551. int output = getP1();
  552. if (output < 0) {
  553. return output;
  554. } else {
  555. return 0xff & output;
  556. }
  557. } else {
  558. return -1;
  559. }
  560. }
  561. //////////////////////////////
  562. //
  563. // MidiMessage::getVelocity -- Return the key veolocity. If the message
  564. // is not a note-on or a note-off, then return -1. If the value is
  565. // out of the range 0-127, then chop off the high-bits.
  566. //
  567. int MidiMessage::getVelocity(void) const {
  568. if (isNote()) {
  569. int output = getP2();
  570. if (output < 0) {
  571. return output;
  572. } else {
  573. return 0xff & output;
  574. }
  575. } else {
  576. return -1;
  577. }
  578. }
  579. //////////////////////////////
  580. //
  581. // MidiMessage::getControllerNumber -- Return the controller number (such as 1
  582. // for modulation wheel). If the message does not have a controller number
  583. // parameter, then return -1. If the controller number is invalid (above 127
  584. // in value), then limit the range to to 0-127.
  585. //
  586. int MidiMessage::getControllerNumber(void) const {
  587. if (isController()) {
  588. int output = getP1();
  589. if (output < 0) {
  590. // -1 means no P1, although isController() is false in such a case.
  591. return output;
  592. } else {
  593. return 0x7f & output;
  594. }
  595. } else {
  596. return -1;
  597. }
  598. }
  599. //////////////////////////////
  600. //
  601. // MidiMessage::getControllerValue -- Return the controller value. If the
  602. // message is not a control change message, then return -1. If the value is
  603. // out of the range 0-127, then chop off the high-bits.
  604. //
  605. int MidiMessage::getControllerValue(void) const {
  606. if (isController()) {
  607. int output = getP2();
  608. if (output < 0) {
  609. // -1 means no P2, although isController() is false in such a case.
  610. return output;
  611. } else {
  612. return 0x7f & output;
  613. }
  614. } else {
  615. return -1;
  616. }
  617. }
  618. //////////////////////////////
  619. //
  620. // MidiMessage::setP0 -- Set the command byte.
  621. // If the MidiMessage is too short, add extra spaces to
  622. // allow for P0. The value should be in the range from
  623. // 128 to 255, but this function will not babysit you.
  624. //
  625. void MidiMessage::setP0(int value) {
  626. if (getSize() < 1) {
  627. resize(1);
  628. }
  629. (*this)[0] = value;
  630. }
  631. //////////////////////////////
  632. //
  633. // MidiMessage::setP1 -- Set the first parameter value.
  634. // If the MidiMessage is too short, add extra spaces to
  635. // allow for P1. The command byte will be undefined if
  636. // it was added. The value should be in the range from
  637. // 0 to 127, but this function will not babysit you.
  638. //
  639. void MidiMessage::setP1(int value) {
  640. if (getSize() < 2) {
  641. resize(2);
  642. }
  643. (*this)[1] = value;
  644. }
  645. //////////////////////////////
  646. //
  647. // MidiMessage::setP2 -- Set the second paramter value.
  648. // If the MidiMessage is too short, add extra spaces
  649. // to allow for P2. The command byte and/or the P1 value
  650. // will be undefined if extra space needs to be added and
  651. // those slots are created. The value should be in the range
  652. // from 0 to 127, but this function will not babysit you.
  653. //
  654. void MidiMessage::setP2(int value) {
  655. if (getSize() < 3) {
  656. resize(3);
  657. }
  658. (*this)[2] = value;
  659. }
  660. //////////////////////////////
  661. //
  662. // MidiMessage::setP3 -- Set the third paramter value.
  663. // If the MidiMessage is too short, add extra spaces
  664. // to allow for P3. The command byte and/or the P1/P2 values
  665. // will be undefined if extra space needs to be added and
  666. // those slots are created. The value should be in the range
  667. // from 0 to 127, but this function will not babysit you.
  668. //
  669. void MidiMessage::setP3(int value) {
  670. if (getSize() < 4) {
  671. resize(4);
  672. }
  673. (*this)[3] = value;
  674. }
  675. //////////////////////////////
  676. //
  677. // MidiMessage::setKeyNumber -- Set the note on/off key number (or
  678. // aftertouch key). Ignore if not note or aftertouch message.
  679. // Limits the input value to the range from 0 to 127.
  680. //
  681. void MidiMessage::setKeyNumber(int value) {
  682. if (isNote() || isAftertouch()) {
  683. setP1(value & 0xff);
  684. } else {
  685. // don't do anything since this is not a note-related message.
  686. }
  687. }
  688. //////////////////////////////
  689. //
  690. // MidiMessage::setVelocity -- Set the note on/off velocity; ignore
  691. // if note a note message. Limits the input value to the range
  692. // from 0 to 127.
  693. //
  694. void MidiMessage::setVelocity(int value) {
  695. if (isNote()) {
  696. setP2(value & 0xff);
  697. } else {
  698. // don't do anything since this is not a note-related message.
  699. }
  700. }
  701. //////////////////////////////
  702. //
  703. // MidiMessage::getCommandNibble -- Returns the top 4 bits of the (*this)[0]
  704. // entry, or -1 if there is not (*this)[0].
  705. //
  706. int MidiMessage::getCommandNibble(void) const {
  707. if (size() < 1) {
  708. return -1;
  709. } else {
  710. return (*this)[0] & 0xf0;
  711. }
  712. }
  713. //////////////////////////////
  714. //
  715. // MidiMessage::getCommandByte -- Return the command byte or -1 if not
  716. // allocated.
  717. //
  718. int MidiMessage::getCommandByte(void) const {
  719. if (size() < 1) {
  720. return -1;
  721. } else {
  722. return (*this)[0];
  723. }
  724. }
  725. //////////////////////////////
  726. //
  727. // MidiMessage::getChannelNibble -- Returns the bottom 4 bites of the
  728. // (*this)[0] entry, or -1 if there is not (*this)[0]. Should be refined
  729. // to return -1 if the top nibble is 0xf0, since those commands are
  730. // not channel specific.
  731. //
  732. int MidiMessage::getChannelNibble(void) const {
  733. if (size() < 1) {
  734. return -1;
  735. } else {
  736. return (*this)[0] & 0x0f;
  737. }
  738. }
  739. int MidiMessage::getChannel(void) const {
  740. return getChannelNibble();
  741. }
  742. //////////////////////////////
  743. //
  744. // MidiMessage::setCommandByte --
  745. //
  746. void MidiMessage::setCommandByte(int value) {
  747. if (size() < 1) {
  748. resize(1);
  749. } else {
  750. (*this)[0] = (uchar)(value & 0xff);
  751. }
  752. }
  753. void MidiMessage::setCommand(int value) {
  754. setCommandByte(value);
  755. }
  756. //////////////////////////////
  757. //
  758. // MidiMessage::setCommand -- Set the command byte and parameter bytes
  759. // for a MidiMessage. The size of the message will be adjusted to
  760. // the number of input parameters.
  761. //
  762. void MidiMessage::setCommand(int value, int p1) {
  763. this->resize(2);
  764. (*this)[0] = (uchar)value;
  765. (*this)[1] = (uchar)p1;
  766. }
  767. void MidiMessage::setCommand(int value, int p1, int p2) {
  768. this->resize(3);
  769. (*this)[0] = (uchar)value;
  770. (*this)[1] = (uchar)p1;
  771. (*this)[2] = (uchar)p2;
  772. }
  773. //////////////////////////////
  774. //
  775. // MidiMessage::setCommandNibble --
  776. //
  777. void MidiMessage::setCommandNibble(int value) {
  778. if (this->size() < 1) {
  779. this->resize(1);
  780. }
  781. if (value <= 0x0f) {
  782. (*this)[0] = ((*this)[0] & 0x0f) | ((uchar)((value << 4) & 0xf0));
  783. } else {
  784. (*this)[0] = ((*this)[0] & 0x0f) | ((uchar)(value & 0xf0));
  785. }
  786. }
  787. //////////////////////////////
  788. //
  789. // MidiMessage::setChannelNibble --
  790. //
  791. void MidiMessage::setChannelNibble(int value) {
  792. if (this->size() < 1) {
  793. this->resize(1);
  794. }
  795. (*this)[0] = ((*this)[0] & 0xf0) | ((uchar)(value & 0x0f));
  796. }
  797. void MidiMessage::setChannel(int value) {
  798. setChannelNibble(value);
  799. }
  800. //////////////////////////////
  801. //
  802. // MidiMessage::setParameters -- Set the second and optionally the
  803. // third MIDI byte of a MIDI message. The command byte will not
  804. // be altered, and will be set to 0 if it currently does not exist.
  805. //
  806. void MidiMessage::setParameters(int p1) {
  807. int oldsize = (int)size();
  808. resize(2);
  809. (*this)[1] = (uchar)p1;
  810. if (oldsize < 1) {
  811. (*this)[0] = 0;
  812. }
  813. }
  814. void MidiMessage::setParameters(int p1, int p2) {
  815. int oldsize = (int)size();
  816. resize(3);
  817. (*this)[1] = (uchar)p1;
  818. (*this)[2] = (uchar)p2;
  819. if (oldsize < 1) {
  820. (*this)[0] = 0;
  821. }
  822. }
  823. //////////////////////////////
  824. //
  825. // MidiMessage::setMessage -- Set the contents of MIDI bytes to the
  826. // input list of bytes.
  827. //
  828. void MidiMessage::setMessage(const std::vector<uchar>& message) {
  829. this->resize(message.size());
  830. for (int i=0; i<(int)this->size(); i++) {
  831. (*this)[i] = message[i];
  832. }
  833. }
  834. void MidiMessage::setMessage(const std::vector<char>& message) {
  835. resize(message.size());
  836. for (int i=0; i<(int)size(); i++) {
  837. (*this)[i] = (uchar)message[i];
  838. }
  839. }
  840. void MidiMessage::setMessage(const std::vector<int>& message) {
  841. resize(message.size());
  842. for (int i=0; i<(int)size(); i++) {
  843. (*this)[i] = (uchar)message[i];
  844. }
  845. }
  846. //////////////////////////////
  847. //
  848. // MidiMessage::setSpelling -- Encode a MidiPlus accidental state for a note.
  849. // For example, if a note's key number is 60, the enharmonic pitch name
  850. // could be any of these possibilities:
  851. // C, B-sharp, D-double-flat
  852. // MIDI note 60 is ambiguous as to which of these names are intended,
  853. // so MIDIPlus allows these mappings to be preserved for later recovery.
  854. // See Chapter 5 (pp. 99-104) of Beyond MIDI (1997).
  855. //
  856. // The first parameter is the diatonic pitch number (or pitch class
  857. // if the octave is set to 0):
  858. // octave * 7 + 0 = C pitches
  859. // octave * 7 + 1 = D pitches
  860. // octave * 7 + 2 = E pitches
  861. // octave * 7 + 3 = F pitches
  862. // octave * 7 + 4 = G pitches
  863. // octave * 7 + 5 = A pitches
  864. // octave * 7 + 6 = B pitches
  865. //
  866. // The second parameter is the semitone alteration (accidental).
  867. // 0 = natural state, 1 = sharp, 2 = double sharp, -1 = flat,
  868. // -2 = double flat.
  869. //
  870. // Only note-on messages can be processed (other messages will be
  871. // silently ignored).
  872. //
  873. void MidiMessage::setSpelling(int base7, int accidental) {
  874. if (!isNoteOn()) {
  875. return;
  876. }
  877. // The bottom two bits of the attack velocity are used for the
  878. // spelling, so need to make sure the velocity will not accidentally
  879. // be set to zero (and make the note-on a note-off).
  880. if (getVelocity() < 4) {
  881. setVelocity(4);
  882. }
  883. int dpc = base7 % 7;
  884. uchar spelling = 0;
  885. // Table 5.1, page 101 in Beyond MIDI (1997)
  886. // http://beyondmidi.ccarh.org/beyondmidi-600dpi.pdf
  887. switch (dpc) {
  888. case 0:
  889. switch (accidental) {
  890. case -2: spelling = 1; break; // Cbb
  891. case -1: spelling = 1; break; // Cb
  892. case 0: spelling = 2; break; // C
  893. case +1: spelling = 2; break; // C#
  894. case +2: spelling = 3; break; // C##
  895. }
  896. break;
  897. case 1:
  898. switch (accidental) {
  899. case -2: spelling = 1; break; // Dbb
  900. case -1: spelling = 1; break; // Db
  901. case 0: spelling = 2; break; // D
  902. case +1: spelling = 3; break; // D#
  903. case +2: spelling = 3; break; // D##
  904. }
  905. break;
  906. case 2:
  907. switch (accidental) {
  908. case -2: spelling = 1; break; // Ebb
  909. case -1: spelling = 2; break; // Eb
  910. case 0: spelling = 2; break; // E
  911. case +1: spelling = 3; break; // E#
  912. case +2: spelling = 3; break; // E##
  913. }
  914. break;
  915. case 3:
  916. switch (accidental) {
  917. case -2: spelling = 1; break; // Fbb
  918. case -1: spelling = 1; break; // Fb
  919. case 0: spelling = 2; break; // F
  920. case +1: spelling = 2; break; // F#
  921. case +2: spelling = 3; break; // F##
  922. case +3: spelling = 3; break; // F###
  923. }
  924. break;
  925. case 4:
  926. switch (accidental) {
  927. case -2: spelling = 1; break; // Gbb
  928. case -1: spelling = 1; break; // Gb
  929. case 0: spelling = 2; break; // G
  930. case +1: spelling = 2; break; // G#
  931. case +2: spelling = 3; break; // G##
  932. }
  933. break;
  934. case 5:
  935. switch (accidental) {
  936. case -2: spelling = 1; break; // Abb
  937. case -1: spelling = 1; break; // Ab
  938. case 0: spelling = 2; break; // A
  939. case +1: spelling = 3; break; // A#
  940. case +2: spelling = 3; break; // A##
  941. }
  942. break;
  943. case 6:
  944. switch (accidental) {
  945. case -2: spelling = 1; break; // Bbb
  946. case -1: spelling = 2; break; // Bb
  947. case 0: spelling = 2; break; // B
  948. case +1: spelling = 3; break; // B#
  949. case +2: spelling = 3; break; // B##
  950. }
  951. break;
  952. }
  953. uchar vel = getVelocity();
  954. // suppress any previous content in the first two bits:
  955. vel = vel & 0xFC;
  956. // insert the spelling code:
  957. vel = vel | spelling;
  958. setVelocity(vel);
  959. }
  960. //////////////////////////////
  961. //
  962. // MidiMessage::getSpelling -- Return the diatonic pitch class and accidental
  963. // for a note-on's key number. The MIDI file must be encoded with MIDIPlus
  964. // pitch spelling codes for this function to return valid data; otherwise,
  965. // it will return a neutral fixed spelling for each MIDI key.
  966. //
  967. // The first parameter will be filled in with the base-7 diatonic pitch:
  968. // pc + octave * 7
  969. // where pc is the numbers 0 through 6 representing the pitch classes
  970. // C through B, the octave is MIDI octave (not the scientific pitch
  971. // octave which is one less than the MIDI ocatave, such as C4 = middle C).
  972. // The second number is the accidental for the base-7 pitch.
  973. //
  974. void MidiMessage::getSpelling(int& base7, int& accidental) {
  975. if (!isNoteOn()) {
  976. return;
  977. }
  978. base7 = -123456;
  979. accidental = 123456;
  980. int base12 = getKeyNumber();
  981. int octave = base12 / 12;
  982. int base12pc = base12 - octave * 12;
  983. int base7pc = 0;
  984. int spelling = 0x03 & getVelocity();
  985. // Table 5.1, page 101 in Beyond MIDI (1997)
  986. // http://beyondmidi.ccarh.org/beyondmidi-600dpi.pdf
  987. switch (base12pc) {
  988. case 0:
  989. switch (spelling) {
  990. case 1: base7pc = 1; accidental = -2; break; // Dbb
  991. case 0: case 2: base7pc = 0; accidental = 0; break; // C
  992. case 3: base7pc = 6; accidental = +1; octave--; break; // B#
  993. }
  994. break;
  995. case 1:
  996. switch (spelling) {
  997. case 1: base7pc = 1; accidental = -1; break; // Db
  998. case 0: case 2: base7pc = 0; accidental = +1; break; // C#
  999. case 3: base7pc = 6; accidental = +2; octave--; break; // B##
  1000. }
  1001. break;
  1002. case 2:
  1003. switch (spelling) {
  1004. case 1: base7pc = 2; accidental = -2; break; // Ebb
  1005. case 0: case 2: base7pc = 1; accidental = 0; break; // D
  1006. case 3: base7pc = 0; accidental = +2; break; // C##
  1007. }
  1008. break;
  1009. case 3:
  1010. switch (spelling) {
  1011. case 1: base7pc = 3; accidental = -2; break; // Fbb
  1012. case 0: case 2: base7pc = 2; accidental = -1; break; // Eb
  1013. case 3: base7pc = 1; accidental = +1; break; // D#
  1014. }
  1015. break;
  1016. case 4:
  1017. switch (spelling) {
  1018. case 1: base7pc = 3; accidental = -1; break; // Fb
  1019. case 0: case 2: base7pc = 2; accidental = 0; break; // E
  1020. case 3: base7pc = 1; accidental = +2; break; // D##
  1021. }
  1022. break;
  1023. case 5:
  1024. switch (spelling) {
  1025. case 1: base7pc = 4; accidental = -2; break; // Gbb
  1026. case 0: case 2: base7pc = 3; accidental = 0; break; // F
  1027. case 3: base7pc = 2; accidental = +1; break; // E#
  1028. }
  1029. break;
  1030. case 6:
  1031. switch (spelling) {
  1032. case 1: base7pc = 4; accidental = -1; break; // Gb
  1033. case 0: case 2: base7pc = 3; accidental = +1; break; // F#
  1034. case 3: base7pc = 2; accidental = +2; break; // E##
  1035. }
  1036. break;
  1037. case 7:
  1038. switch (spelling) {
  1039. case 1: base7pc = 5; accidental = -2; break; // Abb
  1040. case 0: case 2: base7pc = 4; accidental = 0; break; // G
  1041. case 3: base7pc = 3; accidental = +2; break; // F##
  1042. }
  1043. break;
  1044. case 8:
  1045. switch (spelling) {
  1046. case 1: base7pc = 5; accidental = -1; break; // Ab
  1047. case 0: case 2: base7pc = 4; accidental = +1; break; // G#
  1048. case 3: base7pc = 3; accidental = +3; break; // F###
  1049. }
  1050. break;
  1051. case 9:
  1052. switch (spelling) {
  1053. case 1: base7pc = 6; accidental = -2; break; // Bbb
  1054. case 0: case 2: base7pc = 5; accidental = 0; break; // A
  1055. case 3: base7pc = 4; accidental = +2; break; // G##
  1056. }
  1057. break;
  1058. case 10:
  1059. switch (spelling) {
  1060. case 1: base7pc = 0; accidental = -2; octave++; break; // Cbb
  1061. case 0: case 2: base7pc = 6; accidental = -1; break; // Bb
  1062. case 3: base7pc = 5; accidental = +1; break; // A#
  1063. }
  1064. break;
  1065. case 11:
  1066. switch (spelling) {
  1067. case 1: base7pc = 0; accidental = -1; octave++; break; // Cb
  1068. case 0: case 2: base7pc = 6; accidental = 0; break; // B
  1069. case 3: base7pc = 5; accidental = +2; break; // A##
  1070. }
  1071. break;
  1072. }
  1073. base7 = base7pc + 7 * octave;
  1074. }
  1075. //////////////////////////////
  1076. //
  1077. // MidiMessage::getMetaContent -- Returns the bytes of the meta
  1078. // message after the length (which is a variable-length-value).
  1079. //
  1080. std::string MidiMessage::getMetaContent(void) {
  1081. std::string output;
  1082. if (!isMetaMessage()) {
  1083. return output;
  1084. }
  1085. int start = 3;
  1086. if (operator[](2) > 0x7f) {
  1087. start++;
  1088. if (operator[](3) > 0x7f) {
  1089. start++;
  1090. if (operator[](4) > 0x7f) {
  1091. start++;
  1092. if (operator[](5) > 0x7f) {
  1093. start++;
  1094. // maximum of 5 bytes in VLV, so last must be < 0x80
  1095. }
  1096. }
  1097. }
  1098. }
  1099. output.reserve(this->size());
  1100. for (int i=start; i<(int)this->size(); i++) {
  1101. output.push_back(operator[](i));
  1102. }
  1103. return output;
  1104. }
  1105. //////////////////////////////
  1106. //
  1107. // MidiMessage::setMetaContent - Set the content of a meta-message. This
  1108. // function handles the size of the message starting at byte 3 in the
  1109. // message, and it does not alter the meta message type. The message
  1110. // must be a meta-message before calling this function and be assigne
  1111. // a meta-message type.
  1112. //
  1113. void MidiMessage::setMetaContent(const std::string& content) {
  1114. if (this->size() < 2) {
  1115. // invalid message, so ignore request
  1116. return;
  1117. }
  1118. if (operator[](0) != 0xFF) {
  1119. // not a meta message, so ignore request
  1120. return;
  1121. }
  1122. this->resize(2);
  1123. // add the size of the meta message data (VLV)
  1124. int dsize = (int)content.size();
  1125. if (dsize < 128) {
  1126. push_back((uchar)dsize);
  1127. } else {
  1128. // calculate VLV bytes and insert into message
  1129. uchar byte1 = dsize & 0x7f;
  1130. uchar byte2 = (dsize >> 7) & 0x7f;
  1131. uchar byte3 = (dsize >> 14) & 0x7f;
  1132. uchar byte4 = (dsize >> 21) & 0x7f;
  1133. uchar byte5 = (dsize >> 28) & 0x7f;
  1134. if (byte5) {
  1135. byte4 |= 0x80;
  1136. }
  1137. if (byte4) {
  1138. byte4 |= 0x80;
  1139. byte3 |= 0x80;
  1140. }
  1141. if (byte3) {
  1142. byte3 |= 0x80;
  1143. byte2 |= 0x80;
  1144. }
  1145. if (byte2) {
  1146. byte2 |= 0x80;
  1147. }
  1148. if (byte5) { push_back(byte5); }
  1149. if (byte4) { push_back(byte4); }
  1150. if (byte3) { push_back(byte3); }
  1151. if (byte2) { push_back(byte2); }
  1152. push_back(byte1);
  1153. }
  1154. std::copy(content.begin(), content.end(), std::back_inserter(*this));
  1155. }
  1156. //////////////////////////////
  1157. //
  1158. // MidiMessage::setMetaTempo -- Input tempo is in quarter notes per minute
  1159. // (meta message #0x51).
  1160. //
  1161. void MidiMessage::setMetaTempo(double tempo) {
  1162. int microseconds = (int)(60.0 / tempo * 1000000.0 + 0.5);
  1163. setTempoMicroseconds(microseconds);
  1164. }
  1165. //////////////////////////////
  1166. //
  1167. // MidiMessage::setTempo -- Alias for MidiMessage::setMetaTempo().
  1168. //
  1169. void MidiMessage::setTempo(double tempo) {
  1170. setMetaTempo(tempo);
  1171. }
  1172. //////////////////////////////
  1173. //
  1174. // MidiMessage::setTempoMicroseconds -- Set the tempo in terms
  1175. // of microseconds per quarter note.
  1176. //
  1177. void MidiMessage::setTempoMicroseconds(int microseconds) {
  1178. resize(6);
  1179. (*this)[0] = 0xff;
  1180. (*this)[1] = 0x51;
  1181. (*this)[2] = 3;
  1182. (*this)[3] = (microseconds >> 16) & 0xff;
  1183. (*this)[4] = (microseconds >> 8) & 0xff;
  1184. (*this)[5] = (microseconds >> 0) & 0xff;
  1185. }
  1186. //////////////////////////////
  1187. //
  1188. // MidiMessage::makeTimeSignature -- create a time signature meta message
  1189. // (meta #0x58). The "bottom" parameter should be a power of two;
  1190. // otherwise, it will be forced to be the next highest power of two,
  1191. // as MIDI time signatures must have a power of two in the denominator.
  1192. //
  1193. // Default values:
  1194. // clocksPerClick == 24 (quarter note)
  1195. // num32ndsPerQuarter == 8 (8 32nds per quarter note)
  1196. //
  1197. // Time signature of 4/4 would be:
  1198. // top = 4
  1199. // bottom = 4 (converted to 2 in the MIDI file for 2nd power of 2).
  1200. // clocksPerClick = 24 (2 eighth notes based on num32ndsPerQuarter)
  1201. // num32ndsPerQuarter = 8
  1202. //
  1203. // Time signature of 6/8 would be:
  1204. // top = 6
  1205. // bottom = 8 (converted to 3 in the MIDI file for 3rd power of 2).
  1206. // clocksPerClick = 36 (3 eighth notes based on num32ndsPerQuarter)
  1207. // num32ndsPerQuarter = 8
  1208. //
  1209. void MidiMessage::makeTimeSignature(int top, int bottom, int clocksPerClick,
  1210. int num32ndsPerQuarter) {
  1211. int base2 = 0;
  1212. while (bottom >>= 1) base2++;
  1213. resize(7);
  1214. (*this)[0] = 0xff;
  1215. (*this)[1] = 0x58;
  1216. (*this)[2] = 4;
  1217. (*this)[3] = 0xff & top;
  1218. (*this)[4] = 0xff & base2;
  1219. (*this)[5] = 0xff & clocksPerClick;
  1220. (*this)[6] = 0xff & num32ndsPerQuarter;
  1221. }
  1222. ///////////////////////////////////////////////////////////////////////////
  1223. //
  1224. // make functions to create various MIDI message --
  1225. //
  1226. //////////////////////////////
  1227. //
  1228. // MidiMessage::makeNoteOn -- create a note-on message.
  1229. //
  1230. // default value: channel = 0
  1231. //
  1232. // Note: The channel parameter used to be last, but makes more sense to
  1233. // have it first...
  1234. //
  1235. void MidiMessage::makeNoteOn(int channel, int key, int velocity) {
  1236. resize(3);
  1237. (*this)[0] = 0x90 | (0x0f & channel);
  1238. (*this)[1] = key & 0x7f;
  1239. (*this)[2] = velocity & 0x7f;
  1240. }
  1241. //////////////////////////////
  1242. //
  1243. // MidiMessage::makeNoteOff -- create a note-off message. If no
  1244. // parameters are given, the current contents is presumed to be a
  1245. // note-on message, which will be converted into a note-off message.
  1246. //
  1247. // default value: channel = 0
  1248. //
  1249. // Note: The channel parameter used to be last, but makes more sense to
  1250. // have it first...
  1251. //
  1252. void MidiMessage::makeNoteOff(int channel, int key, int velocity) {
  1253. resize(3);
  1254. (*this)[0] = 0x80 | (0x0f & channel);
  1255. (*this)[1] = key & 0x7f;
  1256. (*this)[2] = velocity & 0x7f;
  1257. }
  1258. void MidiMessage::makeNoteOff(int channel, int key) {
  1259. resize(3);
  1260. (*this)[0] = 0x90 | (0x0f & channel);
  1261. (*this)[1] = key & 0x7f;
  1262. (*this)[2] = 0x00;
  1263. }
  1264. //
  1265. // MidiMessage::makeNoteOff(void) -- create a 0x90 note message with
  1266. // The key and velocity set to 0.
  1267. //
  1268. void MidiMessage::makeNoteOff(void) {
  1269. if (!isNoteOn()) {
  1270. resize(3);
  1271. (*this)[0] = 0x90;
  1272. (*this)[1] = 0;
  1273. (*this)[2] = 0;
  1274. } else {
  1275. (*this)[2] = 0;
  1276. }
  1277. }
  1278. /////////////////////////////
  1279. //
  1280. // MidiMessage::makePatchChange -- Create a patch-change message.
  1281. //
  1282. void MidiMessage::makePatchChange(int channel, int patchnum) {
  1283. resize(0);
  1284. push_back(0xc0 | (0x0f & channel));
  1285. push_back(0x7f & patchnum);
  1286. }
  1287. //
  1288. // MidiMessage::makeTimbre -- alias for MidiMessage::makePatchChange().
  1289. //
  1290. void MidiMessage::makeTimbre(int channel, int patchnum) {
  1291. makePatchChange(channel, patchnum);
  1292. }
  1293. /////////////////////////////
  1294. //
  1295. // MidiMessage::makeController -- Create a controller message.
  1296. //
  1297. void MidiMessage::makeController(int channel, int num, int value) {
  1298. resize(0);
  1299. push_back(0xb0 | (0x0f & channel));
  1300. push_back(0x7f & num);
  1301. push_back(0x7f & value);
  1302. }
  1303. /////////////////////////////
  1304. //
  1305. // MidiMessage::makeSustain -- Create a sustain pedal message.
  1306. // Value in 0-63 range is a sustain off. Value in the
  1307. // 64-127 value is a sustain on.
  1308. //
  1309. void MidiMessage::makeSustain(int channel, int value) {
  1310. makeController(channel, 64, value);
  1311. }
  1312. //
  1313. // MidiMessage::makeSustain -- Alias for MidiMessage::makeSustain().
  1314. //
  1315. void MidiMessage::makeSustainPedal(int channel, int value) {
  1316. makeSustain(channel, value);
  1317. }
  1318. /////////////////////////////
  1319. //
  1320. // MidiMessage::makeSustainOn -- Create sustain-on controller message.
  1321. //
  1322. void MidiMessage::makeSustainOn(int channel) {
  1323. makeController(channel, 64, 127);
  1324. }
  1325. //
  1326. // MidiMessage::makeSustainPedalOn -- Alias for MidiMessage::makeSustainOn().
  1327. //
  1328. void MidiMessage::makeSustainPedalOn(int channel) {
  1329. makeSustainOn(channel);
  1330. }
  1331. /////////////////////////////
  1332. //
  1333. // MidiMessage::makeSustainOff -- Create a sustain-off controller message.
  1334. //
  1335. void MidiMessage::makeSustainOff(int channel) {
  1336. makeController(channel, 64, 0);
  1337. }
  1338. //
  1339. // MidiMessage::makeSustainPedalOff -- Alias for MidiMessage::makeSustainOff().
  1340. //
  1341. void MidiMessage::makeSustainPedalOff(int channel) {
  1342. makeSustainOff(channel);
  1343. }
  1344. //////////////////////////////
  1345. //
  1346. // MidiMessage::makeMetaMessage -- Create a Meta event with the
  1347. // given text string as the parameter. The length of the string should
  1348. // is a VLV. If the length is larger than 127 byte, then the length
  1349. // will contain more than one byte.
  1350. //
  1351. void MidiMessage::makeMetaMessage(int mnum, const std::string& data) {
  1352. resize(0);
  1353. push_back(0xff);
  1354. push_back(mnum & 0x7f); // max meta-message number is 0x7f.
  1355. setMetaContent(data);
  1356. }
  1357. //////////////////////////////
  1358. //
  1359. // MidiMessage::makeText -- Create a metaevent text message.
  1360. // This is not a real MIDI message, but rather a pretend message for use
  1361. // within Standard MIDI Files.
  1362. //
  1363. void MidiMessage::makeText(const std::string& text) {
  1364. makeMetaMessage(0x01, text);
  1365. }
  1366. //////////////////////////////
  1367. //
  1368. // MidiMessage::makeCopyright -- Create a metaevent copyright message.
  1369. // This is not a real MIDI message, but rather a pretend message for use
  1370. // within Standard MIDI Files.
  1371. //
  1372. void MidiMessage::makeCopyright(const std::string& text) {
  1373. makeMetaMessage(0x02, text);
  1374. }
  1375. //////////////////////////////
  1376. //
  1377. // MidiMessage::makeTrackName -- Create a metaevent track name message.
  1378. // This is not a real MIDI message, but rather a pretend message for use
  1379. // within Standard MIDI Files.
  1380. //
  1381. void MidiMessage::makeTrackName(const std::string& name) {
  1382. makeMetaMessage(0x03, name);
  1383. }
  1384. //////////////////////////////
  1385. //
  1386. // MidiMessage::makeTrackName -- Create a metaevent instrument name message.
  1387. // This is not a real MIDI message, but rather a pretend message for use
  1388. // within Standard MIDI Files.
  1389. //
  1390. void MidiMessage::makeInstrumentName(const std::string& name) {
  1391. makeMetaMessage(0x04, name);
  1392. }
  1393. //////////////////////////////
  1394. //
  1395. // MidiMessage::makeLyric -- Create a metaevent lyrics/text message.
  1396. // This is not a real MIDI message, but rather a pretend message for use
  1397. // within Standard MIDI Files.
  1398. //
  1399. void MidiMessage::makeLyric(const std::string& text) {
  1400. makeMetaMessage(0x05, text);
  1401. }
  1402. //////////////////////////////
  1403. //
  1404. // MidiMessage::makeMarker -- Create a metaevent marker message.
  1405. // This is not a real MIDI message, but rather a pretend message for use
  1406. // within Standard MIDI Files.
  1407. //
  1408. void MidiMessage::makeMarker(const std::string& text) {
  1409. makeMetaMessage(0x06, text);
  1410. }
  1411. //////////////////////////////
  1412. //
  1413. // MidiMessage::makeCue -- Create a metaevent cue-point message.
  1414. // This is not a real MIDI message, but rather a pretend message for use
  1415. // within Standard MIDI Files.
  1416. //
  1417. void MidiMessage::makeCue(const std::string& text) {
  1418. makeMetaMessage(0x07, text);
  1419. }
  1420. } // end namespace smf