Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_MPENote.cpp 4.8KB

8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. namespace
  24. {
  25. uint16 generateNoteID (int midiChannel, int midiNoteNumber) noexcept
  26. {
  27. jassert (midiChannel > 0 && midiChannel <= 16);
  28. jassert (midiNoteNumber >= 0 && midiNoteNumber < 128);
  29. return uint16 ((midiChannel << 7) + midiNoteNumber);
  30. }
  31. }
  32. //==============================================================================
  33. MPENote::MPENote (int midiChannel_,
  34. int initialNote_,
  35. MPEValue noteOnVelocity_,
  36. MPEValue pitchbend_,
  37. MPEValue pressure_,
  38. MPEValue timbre_,
  39. KeyState keyState_) noexcept
  40. : noteID (generateNoteID (midiChannel_, initialNote_)),
  41. midiChannel (uint8 (midiChannel_)),
  42. initialNote (uint8 (initialNote_)),
  43. noteOnVelocity (noteOnVelocity_),
  44. pitchbend (pitchbend_),
  45. pressure (pressure_),
  46. timbre (timbre_),
  47. noteOffVelocity (MPEValue::minValue()),
  48. keyState (keyState_)
  49. {
  50. jassert (keyState != MPENote::off);
  51. jassert (isValid());
  52. }
  53. MPENote::MPENote() noexcept
  54. : noteID (0),
  55. midiChannel (0),
  56. initialNote (0),
  57. noteOnVelocity (MPEValue::minValue()),
  58. pitchbend (MPEValue::centreValue()),
  59. pressure (MPEValue::centreValue()),
  60. timbre (MPEValue::centreValue()),
  61. noteOffVelocity (MPEValue::minValue()),
  62. keyState (MPENote::off)
  63. {
  64. }
  65. //==============================================================================
  66. bool MPENote::isValid() const noexcept
  67. {
  68. return midiChannel > 0 && midiChannel <= 16 && initialNote < 128;
  69. }
  70. //==============================================================================
  71. double MPENote::getFrequencyInHertz (double frequencyOfA) const noexcept
  72. {
  73. double pitchInSemitones = double (initialNote) + totalPitchbendInSemitones;
  74. return frequencyOfA * std::pow (2.0, (pitchInSemitones - 69.0) / 12.0);
  75. }
  76. //==============================================================================
  77. bool MPENote::operator== (const MPENote& other) const noexcept
  78. {
  79. jassert (isValid() && other.isValid());
  80. return noteID == other.noteID;
  81. }
  82. bool MPENote::operator!= (const MPENote& other) const noexcept
  83. {
  84. jassert (isValid() && other.isValid());
  85. return noteID != other.noteID;
  86. }
  87. //==============================================================================
  88. //==============================================================================
  89. #if JUCE_UNIT_TESTS
  90. class MPENoteTests : public UnitTest
  91. {
  92. public:
  93. MPENoteTests() : UnitTest ("MPENote class") {}
  94. //==============================================================================
  95. void runTest() override
  96. {
  97. beginTest ("getFrequencyInHertz");
  98. {
  99. MPENote note;
  100. note.initialNote = 60;
  101. note.totalPitchbendInSemitones = -0.5;
  102. expectEqualsWithinOneCent (note.getFrequencyInHertz(), 254.178);
  103. }
  104. }
  105. private:
  106. //==============================================================================
  107. void expectEqualsWithinOneCent (double frequencyInHertzActual,
  108. double frequencyInHertzExpected)
  109. {
  110. double ratio = frequencyInHertzActual / frequencyInHertzExpected;
  111. double oneCent = 1.0005946;
  112. expect (ratio < oneCent);
  113. expect (ratio > 1.0 / oneCent);
  114. }
  115. };
  116. static MPENoteTests MPENoteUnitTests;
  117. #endif // JUCE_UNIT_TESTS