TimePos.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * TimePos.h - declaration of class TimePos 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 TIME_POS_H
  26. #define TIME_POS_H
  27. #include <QtGlobal>
  28. #include "lmms_export.h"
  29. #include "lmms_basics.h"
  30. // note: a bar was erroneously called "tact" in older versions of LMMS
  31. const int DefaultTicksPerBar = 192;
  32. const int DefaultStepsPerBar = 16;
  33. const int DefaultBeatsPerBar = DefaultTicksPerBar / DefaultStepsPerBar;
  34. class MeterModel;
  35. /**
  36. Represents a time signature, in which the numerator is the number of beats
  37. in a bar, while the denominator is the type of note representing a beat.
  38. Example: 6/8 means 6 beats in a bar with each beat having a duration of one 8th-note.
  39. */
  40. class LMMS_EXPORT TimeSig
  41. {
  42. public:
  43. TimeSig( int num, int denom );
  44. TimeSig( const MeterModel &model );
  45. int numerator() const;
  46. int denominator() const;
  47. private:
  48. int m_num;
  49. int m_denom;
  50. };
  51. /**
  52. Represents a position in time or length of a note or event, in ticks, beats, and bars
  53. */
  54. class LMMS_EXPORT TimePos
  55. {
  56. public:
  57. TimePos( const bar_t bar, const tick_t ticks );
  58. TimePos( const tick_t ticks = 0 );
  59. TimePos quantize(float) const;
  60. TimePos toAbsoluteBar() const;
  61. TimePos& operator+=( const TimePos& time );
  62. TimePos& operator-=( const TimePos& time );
  63. // return the bar, rounded down and 0-based
  64. bar_t getBar() const;
  65. // return the bar, rounded up and 0-based
  66. bar_t nextFullBar() const;
  67. void setTicks( tick_t ticks );
  68. tick_t getTicks() const;
  69. operator int() const;
  70. tick_t ticksPerBeat( const TimeSig &sig ) const;
  71. // Remainder ticks after bar is removed
  72. tick_t getTickWithinBar( const TimeSig &sig ) const;
  73. // Returns the beat position inside the bar, 0-based
  74. tick_t getBeatWithinBar( const TimeSig &sig ) const;
  75. // Remainder ticks after bar and beat are removed
  76. tick_t getTickWithinBeat( const TimeSig &sig ) const;
  77. // calculate number of frame that are needed this time
  78. f_cnt_t frames( const float framesPerTick ) const;
  79. double getTimeInMilliseconds( bpm_t beatsPerMinute ) const;
  80. static TimePos fromFrames( const f_cnt_t frames, const float framesPerTick );
  81. static tick_t ticksPerBar();
  82. static tick_t ticksPerBar( const TimeSig &sig );
  83. static int stepsPerBar();
  84. static void setTicksPerBar( tick_t tpt );
  85. static TimePos stepPosition( int step );
  86. static double ticksToMilliseconds( tick_t ticks, bpm_t beatsPerMinute );
  87. static double ticksToMilliseconds( double ticks, bpm_t beatsPerMinute );
  88. private:
  89. tick_t m_ticks;
  90. static tick_t s_ticksPerBar;
  91. } ;
  92. #endif