BeatSequence.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "BeatSequence.h"
  2. #include <sstream>
  3. namespace midi {
  4. BeatSequence::BeatSequence()
  5. : BeatSequence(0)
  6. {
  7. }
  8. BeatSequence::BeatSequence(BeatIndex size)
  9. : parent(size)
  10. {
  11. }
  12. void BeatSequence::expand(BeatIndex factor)
  13. {
  14. if(factor < 1) {
  15. throw "factor may not be less than 1.";
  16. }
  17. resize(size() * factor);
  18. for(signed int targetIndex = (size() - factor); targetIndex > 0; targetIndex -= factor)
  19. {
  20. BeatIndex sourceIndex = targetIndex / factor;
  21. at(sourceIndex).swap(at(targetIndex));
  22. }
  23. }
  24. void BeatSequence::print(std::ostream& stream) const
  25. {
  26. stream << "beat sequence:\n";
  27. for(BeatIndex beatIndex = 0; beatIndex < size(); beatIndex++) {
  28. std::stringstream beatLabelStream;
  29. beatLabelStream << "beat #" << beatIndex << ": ";
  30. std::string beatLabel = beatLabelStream.str();
  31. stream << beatLabel;
  32. std::string spacer(beatLabel.size(), ' ');
  33. const MessageList& beat = at(beatIndex);
  34. if(beat.size() == 0) {
  35. stream << "no message" << "\n";
  36. } else {
  37. for(MessageList::const_iterator msg_it = beat.begin(); msg_it != beat.end(); msg_it++) {
  38. if(msg_it != beat.begin()) {
  39. stream << spacer;
  40. }
  41. (*msg_it)->print(stream);
  42. }
  43. }
  44. }
  45. stream << "\n";
  46. stream.flush();
  47. }
  48. } // namepsace