OpenShot Library | libopenshot-audio  0.2.0
juce_MPENote.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 namespace
27 {
28  uint16 generateNoteID (int midiChannel, int midiNoteNumber) noexcept
29  {
30  jassert (midiChannel > 0 && midiChannel <= 16);
31  jassert (midiNoteNumber >= 0 && midiNoteNumber < 128);
32 
33  return uint16 ((midiChannel << 7) + midiNoteNumber);
34  }
35 }
36 
37 //==============================================================================
38 MPENote::MPENote (int midiChannel_,
39  int initialNote_,
40  MPEValue noteOnVelocity_,
41  MPEValue pitchbend_,
42  MPEValue pressure_,
43  MPEValue timbre_,
44  KeyState keyState_) noexcept
45  : noteID (generateNoteID (midiChannel_, initialNote_)),
46  midiChannel (uint8 (midiChannel_)),
47  initialNote (uint8 (initialNote_)),
48  noteOnVelocity (noteOnVelocity_),
49  pitchbend (pitchbend_),
50  pressure (pressure_),
51  initialTimbre (timbre_),
52  timbre (timbre_),
53  keyState (keyState_)
54 {
55  jassert (keyState != MPENote::off);
56  jassert (isValid());
57 }
58 
59 MPENote::MPENote() noexcept {}
60 
61 //==============================================================================
62 bool MPENote::isValid() const noexcept
63 {
64  return midiChannel > 0 && midiChannel <= 16 && initialNote < 128;
65 }
66 
67 //==============================================================================
68 double MPENote::getFrequencyInHertz (double frequencyOfA) const noexcept
69 {
70  auto pitchInSemitones = double (initialNote) + totalPitchbendInSemitones;
71  return frequencyOfA * std::pow (2.0, (pitchInSemitones - 69.0) / 12.0);
72 }
73 
74 //==============================================================================
75 bool MPENote::operator== (const MPENote& other) const noexcept
76 {
77  jassert (isValid() && other.isValid());
78  return noteID == other.noteID;
79 }
80 
81 bool MPENote::operator!= (const MPENote& other) const noexcept
82 {
83  jassert (isValid() && other.isValid());
84  return noteID != other.noteID;
85 }
86 
87 //==============================================================================
88 //==============================================================================
89 #if JUCE_UNIT_TESTS
90 
91 class MPENoteTests : public UnitTest
92 {
93 public:
94  MPENoteTests() : UnitTest ("MPENote class", "MIDI/MPE") {}
95 
96  //==============================================================================
97  void runTest() override
98  {
99  beginTest ("getFrequencyInHertz");
100  {
101  MPENote note;
102  note.initialNote = 60;
103  note.totalPitchbendInSemitones = -0.5;
104  expectEqualsWithinOneCent (note.getFrequencyInHertz(), 254.178);
105  }
106  }
107 
108 private:
109  //==============================================================================
110  void expectEqualsWithinOneCent (double frequencyInHertzActual,
111  double frequencyInHertzExpected)
112  {
113  double ratio = frequencyInHertzActual / frequencyInHertzExpected;
114  double oneCent = 1.0005946;
115  expect (ratio < oneCent);
116  expect (ratio > 1.0 / oneCent);
117  }
118 };
119 
120 static MPENoteTests MPENoteUnitTests;
121 
122 #endif // JUCE_UNIT_TESTS
123 
124 } // namespace juce
juce::MPENote::isValid
bool isValid() const noexcept
Checks whether the MPE note is valid.
Definition: juce_MPENote.cpp:62
juce::MPEValue
This class represents a single value for any of the MPE dimensions of control.
Definition: juce_MPEValue.h:40
juce::MPENote
This struct represents a playing MPE note.
Definition: juce_MPENote.h:43
juce::MPENote::KeyState
KeyState
Possible values for the note key state.
Definition: juce_MPENote.h:47
juce::MPENote::operator==
bool operator==(const MPENote &other) const noexcept
Returns true if two notes are the same, determined by their unique ID.
Definition: juce_MPENote.cpp:75
juce::MPENote::initialNote
uint8 initialNote
The MIDI note number that was sent when the note was triggered.
Definition: juce_MPENote.h:112
juce::MPENote::midiChannel
uint8 midiChannel
The MIDI channel which this note uses.
Definition: juce_MPENote.h:107
juce::MPENote::off
@ off
The key is up (off).
Definition: juce_MPENote.h:49
juce::UnitTest
This is a base class for classes that perform a unit test.
Definition: juce_UnitTest.h:73
juce::MPENote::getFrequencyInHertz
double getFrequencyInHertz(double frequencyOfA=440.0) const noexcept
Returns the current frequency of the note in Hertz.
Definition: juce_MPENote.cpp:68
juce::MPENote::MPENote
MPENote() noexcept
Default constructor.
Definition: juce_MPENote.cpp:59
juce::MPENote::operator!=
bool operator!=(const MPENote &other) const noexcept
Returns true if two notes are different notes, determined by their unique ID.
Definition: juce_MPENote.cpp:81