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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. namespace
  20. {
  21. uint16 generateNoteID (int midiChannel, int midiNoteNumber) noexcept
  22. {
  23. jassert (midiChannel > 0 && midiChannel <= 16);
  24. jassert (midiNoteNumber >= 0 && midiNoteNumber < 128);
  25. return uint16 ((midiChannel << 7) + midiNoteNumber);
  26. }
  27. }
  28. //==============================================================================
  29. MPENote::MPENote (int midiChannel_,
  30. int initialNote_,
  31. MPEValue noteOnVelocity_,
  32. MPEValue pitchbend_,
  33. MPEValue pressure_,
  34. MPEValue timbre_,
  35. KeyState keyState_) noexcept
  36. : noteID (generateNoteID (midiChannel_, initialNote_)),
  37. midiChannel (uint8 (midiChannel_)),
  38. initialNote (uint8 (initialNote_)),
  39. noteOnVelocity (noteOnVelocity_),
  40. pitchbend (pitchbend_),
  41. pressure (pressure_),
  42. timbre (timbre_),
  43. noteOffVelocity (MPEValue::minValue()),
  44. keyState (keyState_)
  45. {
  46. jassert (keyState != MPENote::off);
  47. jassert (isValid());
  48. }
  49. MPENote::MPENote() noexcept
  50. : noteID (0),
  51. midiChannel (0),
  52. initialNote (0),
  53. noteOnVelocity (MPEValue::minValue()),
  54. pitchbend (MPEValue::centreValue()),
  55. pressure (MPEValue::centreValue()),
  56. timbre (MPEValue::centreValue()),
  57. noteOffVelocity (MPEValue::minValue()),
  58. keyState (MPENote::off)
  59. {
  60. }
  61. //==============================================================================
  62. bool MPENote::isValid() const noexcept
  63. {
  64. return midiChannel > 0 && midiChannel <= 16 && initialNote < 128;
  65. }
  66. //==============================================================================
  67. double MPENote::getFrequencyInHertz (double frequencyOfA) const noexcept
  68. {
  69. double pitchInSemitones = double (initialNote) + totalPitchbendInSemitones;
  70. return frequencyOfA * std::pow (2.0, (pitchInSemitones - 69.0) / 12.0);
  71. }
  72. //==============================================================================
  73. bool MPENote::operator== (const MPENote& other) const noexcept
  74. {
  75. jassert (isValid() && other.isValid());
  76. return noteID == other.noteID;
  77. }
  78. bool MPENote::operator!= (const MPENote& other) const noexcept
  79. {
  80. jassert (isValid() && other.isValid());
  81. return noteID != other.noteID;
  82. }
  83. //==============================================================================
  84. //==============================================================================
  85. #if JUCE_UNIT_TESTS
  86. class MPENoteTests : public UnitTest
  87. {
  88. public:
  89. MPENoteTests() : UnitTest ("MPENote class", "MIDI/MPE") {}
  90. //==============================================================================
  91. void runTest() override
  92. {
  93. beginTest ("getFrequencyInHertz");
  94. {
  95. MPENote note;
  96. note.initialNote = 60;
  97. note.totalPitchbendInSemitones = -0.5;
  98. expectEqualsWithinOneCent (note.getFrequencyInHertz(), 254.178);
  99. }
  100. }
  101. private:
  102. //==============================================================================
  103. void expectEqualsWithinOneCent (double frequencyInHertzActual,
  104. double frequencyInHertzExpected)
  105. {
  106. double ratio = frequencyInHertzActual / frequencyInHertzExpected;
  107. double oneCent = 1.0005946;
  108. expect (ratio < oneCent);
  109. expect (ratio > 1.0 / oneCent);
  110. }
  111. };
  112. static MPENoteTests MPENoteUnitTests;
  113. #endif // JUCE_UNIT_TESTS
  114. } // namespace juce