BeatSequence.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. const midi::MessageList& BeatSequence::at(BeatIndex beatIndex) const
  43. {
  44. return parent::at(beatIndex);
  45. }
  46. const midi::MessageList& BeatSequence::operator[](BeatIndex beatIndex) const
  47. {
  48. return parent::operator[](beatIndex);
  49. }
  50. void BeatSequence::resize(BeatIndex size)
  51. {
  52. parent::resize(size);
  53. refreshRegistry();
  54. }
  55. void BeatSequence::pushBackMessage(BeatIndex beatIndex, std::shared_ptr<midi::Message>& msg_ptr)
  56. {
  57. parent::at(beatIndex).push_back(msg_ptr);
  58. registerMessage(beatIndex, msg_ptr);
  59. }
  60. void BeatSequence::eraseMessage(BeatIndex beatIndex, midi::MessageList::const_iterator msg_it)
  61. {
  62. unregisterMessage(beatIndex, *msg_it);
  63. parent::at(beatIndex).erase(msg_it);
  64. }
  65. void BeatSequence::expand(BeatIndex factor)
  66. {
  67. parent::expand(factor);
  68. refreshRegistry();
  69. }
  70. void BeatSequence::reduceToNeighbour(BeatIndex factor)
  71. {
  72. parent::reduceToNeighbour(factor);
  73. refreshRegistry();
  74. }
  75. void BeatSequence::reduceErasingConflicts(BeatIndex factor)
  76. {
  77. parent::reduceErasingConflicts(factor);
  78. refreshRegistry();
  79. }
  80. void BeatSequence::printRegistry(std::ostream& stream) const
  81. {
  82. stream << "beat sequence note info registry: \n";
  83. for(NoteInfoMap::const_iterator noteInfo_it = noteInfos.begin(); noteInfo_it != noteInfos.end(); noteInfo_it++) {
  84. stream << std::string(4, ' ');
  85. stream << "channel #" << (int)(noteInfo_it->first.first + 1);
  86. stream << " pitch=" << (int)noteInfo_it->first.second << ": \n";
  87. noteInfo_it->second.print(stream, 8);
  88. }
  89. stream.flush();
  90. }
  91. BeatSequenceNoteInformation& BeatSequence::getNoteInfo(Channel channel, Pitch pitch)
  92. {
  93. ChannelPitchPair key(channel, pitch);
  94. return noteInfos[key];
  95. }
  96. void BeatSequence::refreshRegistry()
  97. {
  98. noteInfos.clear();
  99. for(BeatIndex beatIndex = 0; beatIndex < size(); beatIndex++) {
  100. const midi::MessageList& beat = at(beatIndex);
  101. for(midi::MessageList::const_iterator msg_it = beat.begin(); msg_it != beat.end(); msg_it++) {
  102. registerMessage(beatIndex, *msg_it);
  103. }
  104. }
  105. }
  106. void BeatSequence::registerMessage(BeatIndex beatIndex, const std::shared_ptr<midi::Message>& msg_ptr)
  107. {
  108. std::shared_ptr<midi::NoteMessage> noteMsg_ptr
  109. = std::dynamic_pointer_cast<midi::NoteMessage>(msg_ptr);
  110. if(noteMsg_ptr) {
  111. getNoteInfo(noteMsg_ptr->channel, noteMsg_ptr->pitch)
  112. .registerMessage(beatIndex, noteMsg_ptr);
  113. }
  114. }
  115. void BeatSequence::unregisterMessage(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. getNoteInfo(noteMsg_ptr->channel, noteMsg_ptr->pitch)
  121. .unregisterMessage(beatIndex, noteMsg_ptr);
  122. }
  123. }