Scale.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Scale.h - holds information about a scale and its intervals
  3. *
  4. * Copyright (c) 2020 Martin Pavelek <he29.HS/at/gmail.com>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef SCALE_H
  25. #define SCALE_H
  26. #include <cmath>
  27. #include <cstdint>
  28. #include <vector>
  29. #include <QObject>
  30. #include <QString>
  31. #include "SerializingObject.h"
  32. class Interval : public SerializingObject
  33. {
  34. public:
  35. Interval() : m_numerator(1), m_denominator(1), m_cents(0), m_ratio(1) {};
  36. explicit Interval(float cents);
  37. Interval(uint32_t numerator, uint32_t denominator);
  38. float getRatio() const {return m_ratio;}
  39. QString getString() const
  40. {
  41. if (m_denominator) {return QString::number(m_numerator) + "/" + QString::number(m_denominator);}
  42. else {return QString().sprintf("%.4f", m_cents);}
  43. }
  44. void saveSettings(QDomDocument &doc, QDomElement &element) override;
  45. void loadSettings(const QDomElement &element) override;
  46. inline QString nodeName() const override {return "interval";}
  47. private:
  48. // Scala specifies that numerators and denominators should go at least up to 2147483647 → use uint32_t.
  49. uint32_t m_numerator; //!< numerator of the interval fraction
  50. uint32_t m_denominator; //!< denominator of the interval fraction
  51. float m_cents; //!< interval defined in cents (used when denominator is set to zero)
  52. float m_ratio; //!< precomputed output value for better performance
  53. };
  54. class Scale : public QObject, public SerializingObject
  55. {
  56. Q_OBJECT
  57. public:
  58. Scale();
  59. Scale(QString description, std::vector<Interval> intervals);
  60. QString getDescription() const;
  61. void setDescription(QString description);
  62. const std::vector<Interval> &getIntervals() const {return m_intervals;}
  63. void setIntervals(std::vector<Interval> input) {m_intervals = std::move(input);}
  64. void saveSettings(QDomDocument &doc, QDomElement &element) override;
  65. void loadSettings(const QDomElement &element) override;
  66. inline QString nodeName() const override {return "scale";}
  67. private:
  68. QString m_description; //!< name or description of the scale
  69. std::vector<Interval> m_intervals; //!< a series of ratios that define the scale
  70. };
  71. #endif