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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. initialTimbre (timbre_),
  43. timbre (timbre_),
  44. keyState (keyState_)
  45. {
  46. jassert (keyState != MPENote::off);
  47. jassert (isValid());
  48. }
  49. MPENote::MPENote() noexcept {}
  50. //==============================================================================
  51. bool MPENote::isValid() const noexcept
  52. {
  53. return midiChannel > 0 && midiChannel <= 16 && initialNote < 128;
  54. }
  55. //==============================================================================
  56. double MPENote::getFrequencyInHertz (double frequencyOfA) const noexcept
  57. {
  58. auto pitchInSemitones = double (initialNote) + totalPitchbendInSemitones;
  59. return frequencyOfA * std::pow (2.0, (pitchInSemitones - 69.0) / 12.0);
  60. }
  61. //==============================================================================
  62. bool MPENote::operator== (const MPENote& other) const noexcept
  63. {
  64. jassert (isValid() && other.isValid());
  65. return noteID == other.noteID;
  66. }
  67. bool MPENote::operator!= (const MPENote& other) const noexcept
  68. {
  69. jassert (isValid() && other.isValid());
  70. return noteID != other.noteID;
  71. }
  72. //==============================================================================
  73. //==============================================================================
  74. #if JUCE_UNIT_TESTS
  75. class MPENoteTests : public UnitTest
  76. {
  77. public:
  78. MPENoteTests()
  79. : UnitTest ("MPENote class", UnitTestCategories::midi)
  80. {}
  81. //==============================================================================
  82. void runTest() override
  83. {
  84. beginTest ("getFrequencyInHertz");
  85. {
  86. MPENote note;
  87. note.initialNote = 60;
  88. note.totalPitchbendInSemitones = -0.5;
  89. expectEqualsWithinOneCent (note.getFrequencyInHertz(), 254.178);
  90. }
  91. }
  92. private:
  93. //==============================================================================
  94. void expectEqualsWithinOneCent (double frequencyInHertzActual,
  95. double frequencyInHertzExpected)
  96. {
  97. double ratio = frequencyInHertzActual / frequencyInHertzExpected;
  98. double oneCent = 1.0005946;
  99. expect (ratio < oneCent);
  100. expect (ratio > 1.0 / oneCent);
  101. }
  102. };
  103. static MPENoteTests MPENoteUnitTests;
  104. #endif
  105. } // namespace juce