StepRecorder.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * This file is part of LMMS - https://lmms.io
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with this program (see COPYING); if not, write to the
  16. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301 USA.
  18. *
  19. */
  20. #ifndef STEP_RECORDER_H
  21. #define STEP_RECORDER_H
  22. #include <QTime>
  23. #include <QTimer>
  24. #include <QObject>
  25. #include <QKeyEvent>
  26. #include "Note.h"
  27. #include "lmms_basics.h"
  28. #include "Pattern.h"
  29. class PianoRoll;
  30. class StepRecorderWidget;
  31. class StepRecorder : public QObject
  32. {
  33. Q_OBJECT
  34. public:
  35. StepRecorder(PianoRoll& pianoRoll, StepRecorderWidget& stepRecorderWidget);
  36. void initialize();
  37. void start(const MidiTime& currentPosition,const MidiTime& stepLength);
  38. void stop();
  39. void notePressed(const Note & n);
  40. void noteReleased(const Note & n);
  41. bool keyPressEvent(QKeyEvent* ke);
  42. bool mousePressEvent(QMouseEvent* ke);
  43. void setCurrentPattern(Pattern* newPattern);
  44. void setStepsLength(const MidiTime& newLength);
  45. QVector<Note*> getCurStepNotes();
  46. bool isRecording() const
  47. {
  48. return m_isRecording;
  49. }
  50. QColor curStepNoteColor() const
  51. {
  52. return QColor(245,3,139); // radiant pink
  53. }
  54. private slots:
  55. void removeNotesReleasedForTooLong();
  56. private:
  57. void stepForwards();
  58. void stepBackwards();
  59. void applyStep();
  60. void dismissStep();
  61. void prepareNewStep();
  62. MidiTime getCurStepEndPos();
  63. void updateCurStepNotes();
  64. void updateWidget();
  65. bool allCurStepNotesReleased();
  66. PianoRoll& m_pianoRoll;
  67. StepRecorderWidget& m_stepRecorderWidget;
  68. bool m_isRecording = false;
  69. MidiTime m_curStepStartPos = 0;
  70. MidiTime m_curStepEndPos = 0;
  71. MidiTime m_stepsLength;
  72. MidiTime m_curStepLength; // current step length refers to the step currently recorded. it may defer from m_stepsLength
  73. // since the user can make current step larger
  74. QTimer m_updateReleasedTimer;
  75. Pattern* m_pattern;
  76. class StepNote
  77. {
  78. public:
  79. StepNote(const Note & note) : m_note(note), m_pressed(true) {};
  80. void setPressed()
  81. {
  82. m_pressed = true;
  83. }
  84. void setReleased()
  85. {
  86. m_pressed = false;
  87. releasedTimer.start();
  88. }
  89. int timeSinceReleased()
  90. {
  91. return releasedTimer.elapsed();
  92. }
  93. bool isPressed() const
  94. {
  95. return m_pressed;
  96. }
  97. bool isReleased() const
  98. {
  99. return !m_pressed;
  100. }
  101. Note m_note;
  102. private:
  103. bool m_pressed;
  104. QTime releasedTimer;
  105. } ;
  106. QVector<StepNote*> m_curStepNotes; // contains the current recorded step notes (i.e. while user still press the notes; before they are applied to the pattern)
  107. StepNote* findCurStepNote(const int key);
  108. bool m_isStepInProgress = false;
  109. };
  110. #endif //STEP_RECORDER_H