MidiTime.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "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. class LMMS_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 LMMS_EXPORT MidiTime
  51. {
  52. public:
  53. MidiTime( const bar_t bar, const tick_t ticks );
  54. MidiTime( const tick_t ticks = 0 );
  55. MidiTime quantize(float) const;
  56. MidiTime toAbsoluteBar() const;
  57. MidiTime& operator+=( const MidiTime& time );
  58. MidiTime& operator-=( const MidiTime& time );
  59. // return the bar, rounded down and 0-based
  60. bar_t getBar() const;
  61. // return the bar, rounded up and 0-based
  62. bar_t nextFullBar() 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. double getTimeInMilliseconds( bpm_t beatsPerMinute ) const;
  76. static MidiTime fromFrames( const f_cnt_t frames, const float framesPerTick );
  77. static tick_t ticksPerBar();
  78. static tick_t ticksPerBar( const TimeSig &sig );
  79. static int stepsPerBar();
  80. static void setTicksPerBar( tick_t tpt );
  81. static MidiTime stepPosition( int step );
  82. static double ticksToMilliseconds( tick_t ticks, bpm_t beatsPerMinute );
  83. static double ticksToMilliseconds( double ticks, bpm_t beatsPerMinute );
  84. private:
  85. tick_t m_ticks;
  86. static tick_t s_ticksPerBar;
  87. } ;
  88. #endif