BeatSequence.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "BeatSequence.h"
  2. void BeatSequenceNoteInformation::print(std::ostream& stream, std::size_t indent) const
  3. {
  4. stream << std::string(indent, ' ') << "note on beat indices:";
  5. for(BeatIndexMultiset::const_iterator it = onBeatIndices.begin(); it != onBeatIndices.end(); it++) {
  6. stream << " " << *it;
  7. }
  8. stream << "\n";
  9. stream << std::string(indent, ' ') << "note off beat indices:";
  10. for(BeatIndexMultiset::const_iterator it = offBeatIndices.begin(); it != offBeatIndices.end(); it++) {
  11. stream << " " << *it;
  12. }
  13. stream << "\n";
  14. if(indent == 0) {
  15. stream << "\n";
  16. }
  17. stream.flush();
  18. }
  19. void BeatSequenceNoteInformation::registerMessage(BeatSequence::BeatIndex beatIndex, const std::shared_ptr<midi::NoteMessage>& noteMsg_ptr)
  20. {
  21. if(std::dynamic_pointer_cast<midi::NoteOnMessage>(noteMsg_ptr)) {
  22. onBeatIndices.insert(beatIndex);
  23. } else {
  24. offBeatIndices.insert(beatIndex);
  25. }
  26. }
  27. void BeatSequenceNoteInformation::unregisterMessage(BeatSequence::BeatIndex beatIndex, const std::shared_ptr<midi::NoteMessage>& noteMsg_ptr)
  28. {
  29. BeatIndexMultiset::const_iterator it;
  30. if(std::dynamic_pointer_cast<midi::NoteOnMessage>(noteMsg_ptr)) {
  31. it = onBeatIndices.find(beatIndex);
  32. if(it != onBeatIndices.end()) {
  33. onBeatIndices.erase(it);
  34. }
  35. } else {
  36. it = offBeatIndices.find(beatIndex);
  37. if(it != offBeatIndices.end()) {
  38. offBeatIndices.erase(it);
  39. }
  40. }
  41. }
  42. void BeatSequence::resize(BeatIndex size)
  43. {
  44. parent::resize(size);
  45. refreshRegistry();
  46. }
  47. void BeatSequence::pushFrontMessage(BeatIndex beatIndex, std::shared_ptr<midi::Message> msg_ptr)
  48. {
  49. parent::at(beatIndex).push_front(msg_ptr);
  50. registerMessage(beatIndex, msg_ptr);
  51. }
  52. void BeatSequence::pushBackMessage(BeatIndex beatIndex, std::shared_ptr<midi::Message> msg_ptr)
  53. {
  54. parent::at(beatIndex).push_back(msg_ptr);
  55. registerMessage(beatIndex, msg_ptr);
  56. }
  57. void BeatSequence::eraseMessage(BeatIndex beatIndex, midi::MessageList::iterator msg_it)
  58. {
  59. unregisterMessage(beatIndex, *msg_it);
  60. parent::at(beatIndex).erase(msg_it);
  61. }
  62. void BeatSequence::expand(BeatIndex factor)
  63. {
  64. parent::expand(factor);
  65. refreshRegistry();
  66. }
  67. void BeatSequence::reduceToNeighbour(BeatIndex factor)
  68. {
  69. parent::reduceToNeighbour(factor);
  70. refreshRegistry();
  71. }
  72. void BeatSequence::reduceErasingConflicts(BeatIndex factor)
  73. {
  74. parent::reduceErasingConflicts(factor);
  75. refreshRegistry();
  76. }
  77. const BeatSequence::BeatIndexMultiset& BeatSequence::getNoteOnBeatIndices(Channel channel, Pitch pitch)
  78. {
  79. return getNoteInfo(channel, pitch).onBeatIndices;
  80. }
  81. const BeatSequence::BeatIndexMultiset& BeatSequence::getNoteOffBeatIndices(Channel channel, Pitch pitch)
  82. {
  83. return getNoteInfo(channel, pitch).offBeatIndices;
  84. }
  85. const BeatSequenceNoteInformation& BeatSequence::getNoteInfo(Channel channel, Pitch pitch)
  86. {
  87. return getMutableNoteInfo(channel, pitch);
  88. }
  89. void BeatSequence::printRegistry(std::ostream& stream) const
  90. {
  91. stream << "beat sequence note info registry: \n";
  92. for(NoteInfoMap::const_iterator noteInfo_it = noteInfos.begin(); noteInfo_it != noteInfos.end(); noteInfo_it++) {
  93. stream << std::string(4, ' ');
  94. stream << "channel #" << (int)(noteInfo_it->first.first + 1);
  95. stream << " pitch=" << (int)noteInfo_it->first.second << ": \n";
  96. noteInfo_it->second.print(stream, 8);
  97. }
  98. stream.flush();
  99. }
  100. BeatSequenceNoteInformation& BeatSequence::getMutableNoteInfo(Channel channel, Pitch pitch)
  101. {
  102. ChannelPitchPair key(channel, pitch);
  103. return noteInfos[key];
  104. }
  105. void BeatSequence::refreshRegistry()
  106. {
  107. noteInfos.clear();
  108. for(BeatIndex beatIndex = 0; beatIndex < size(); beatIndex++) {
  109. const midi::MessageList& beat = at(beatIndex);
  110. for(midi::MessageList::const_iterator msg_it = beat.begin(); msg_it != beat.end(); msg_it++) {
  111. registerMessage(beatIndex, *msg_it);
  112. }
  113. }
  114. }
  115. void BeatSequence::registerMessage(BeatIndex beatIndex, const std::shared_ptr<midi::Message>& msg_ptr)
  116. {
  117. std::shared_ptr<midi::NoteMessage> noteMsg_ptr
  118. = std::dynamic_pointer_cast<midi::NoteMessage>(msg_ptr);
  119. if(noteMsg_ptr) {
  120. getMutableNoteInfo(noteMsg_ptr->channel, noteMsg_ptr->pitch)
  121. .registerMessage(beatIndex, noteMsg_ptr);
  122. }
  123. }
  124. void BeatSequence::unregisterMessage(BeatIndex beatIndex, const std::shared_ptr<midi::Message>& msg_ptr)
  125. {
  126. std::shared_ptr<midi::NoteMessage> noteMsg_ptr
  127. = std::dynamic_pointer_cast<midi::NoteMessage>(msg_ptr);
  128. if(noteMsg_ptr) {
  129. getMutableNoteInfo(noteMsg_ptr->channel, noteMsg_ptr->pitch)
  130. .unregisterMessage(beatIndex, noteMsg_ptr);
  131. }
  132. }