MidiTime.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * MidiTime.h - declaration of class MidiTime which provides data type for
  3. * position- and length-variables
  4. *
  5. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef MIDI_TIME_H
  26. #define MIDI_TIME_H
  27. #include <QtGlobal>
  28. #include "export.h"
  29. #include "lmms_basics.h"
  30. // note: 1 "Tact" = 1 Measure
  31. const int DefaultTicksPerTact = 192;
  32. const int DefaultStepsPerTact = 16;
  33. const int DefaultBeatsPerTact = DefaultTicksPerTact / DefaultStepsPerTact;
  34. class MeterModel;
  35. class EXPORT TimeSig
  36. {
  37. public:
  38. // in a time signature,
  39. // the numerator represents the number of beats in a measure.
  40. // the denominator indicates which type of note represents a beat.
  41. // example: 6/8 means 6 beats in a measure, where each beat has duration equal to one 8th-note.
  42. TimeSig( int num, int denom );
  43. TimeSig( const MeterModel &model );
  44. int numerator() const;
  45. int denominator() const;
  46. private:
  47. int m_num;
  48. int m_denom;
  49. };
  50. class EXPORT MidiTime
  51. {
  52. public:
  53. MidiTime( const tact_t tact, const tick_t ticks );
  54. MidiTime( const tick_t ticks = 0 );
  55. MidiTime toNearestTact() const;
  56. MidiTime toAbsoluteTact() const;
  57. MidiTime& operator+=( const MidiTime& time );
  58. MidiTime& operator-=( const MidiTime& time );
  59. // return the tact, rounded down and 0-based
  60. tact_t getTact() const;
  61. // return the tact, rounded up and 0-based
  62. tact_t nextFullTact() const;
  63. void setTicks( tick_t ticks );
  64. tick_t getTicks() const;
  65. operator int() const;
  66. tick_t ticksPerBeat( const TimeSig &sig ) const;
  67. // Remainder ticks after bar is removed
  68. tick_t getTickWithinBar( const TimeSig &sig ) const;
  69. // Returns the beat position inside the bar, 0-based
  70. tick_t getBeatWithinBar( const TimeSig &sig ) const;
  71. // Remainder ticks after bar and beat are removed
  72. tick_t getTickWithinBeat( const TimeSig &sig ) const;
  73. // calculate number of frame that are needed this time
  74. f_cnt_t frames( const float framesPerTick ) const;
  75. static MidiTime fromFrames( const f_cnt_t frames, const float framesPerTick );
  76. static tick_t ticksPerTact();
  77. static tick_t ticksPerTact( const TimeSig &sig );
  78. static int stepsPerTact();
  79. static void setTicksPerTact( tick_t tpt );
  80. static MidiTime stepPosition( int step );
  81. private:
  82. tick_t m_ticks;
  83. static tick_t s_ticksPerTact;
  84. } ;
  85. #endif