LMMS
Loading...
Searching...
No Matches
juce_MPENote.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
23namespace juce
24{
25
26namespace
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//==============================================================================
38MPENote::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{
56 jassert (isValid());
57}
58
60
61//==============================================================================
63{
64 return midiChannel > 0 && midiChannel <= 16 && initialNote < 128;
65}
66
67//==============================================================================
68double 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//==============================================================================
75bool MPENote::operator== (const MPENote& other) const noexcept
76{
77 jassert (isValid() && other.isValid());
78 return noteID == other.noteID;
79}
80
81bool MPENote::operator!= (const MPENote& other) const noexcept
82{
83 jassert (isValid() && other.isValid());
84 return noteID != other.noteID;
85}
86
87
88//==============================================================================
89//==============================================================================
90#if JUCE_UNIT_TESTS
91
92class MPENoteTests : public UnitTest
93{
94public:
95 MPENoteTests()
96 : UnitTest ("MPENote class", UnitTestCategories::midi)
97 {}
98
99 //==============================================================================
100 void runTest() override
101 {
102 beginTest ("getFrequencyInHertz");
103 {
104 MPENote note;
105 note.initialNote = 60;
106 note.totalPitchbendInSemitones = -0.5;
107 expectEqualsWithinOneCent (note.getFrequencyInHertz(), 254.178);
108 }
109 }
110
111private:
112 //==============================================================================
113 void expectEqualsWithinOneCent (double frequencyInHertzActual,
114 double frequencyInHertzExpected)
115 {
116 double ratio = frequencyInHertzActual / frequencyInHertzExpected;
117 double oneCent = 1.0005946;
118 expect (ratio < oneCent);
119 expect (ratio > 1.0 / oneCent);
120 }
121};
122
123static MPENoteTests MPENoteUnitTests;
124
125#endif
126
127} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
uint16_t uint16
Definition basics.h:88
Definition juce_MPEValue.h:37
Definition juce_UnitTest.h:70
#define jassert(expression)
Definition juce_UnitTestCategories.h:27
Definition carla_juce.cpp:31
unsigned char uint8
Definition juce_MathsFunctions.h:37
KeyState
Definition juce_MPENote.h:44
@ off
Definition juce_MPENote.h:45
uint16 noteID
Definition juce_MPENote.h:98
MPEValue noteOnVelocity
Definition juce_MPENote.h:116
MPEValue timbre
Definition juce_MPENote.h:144
MPEValue pitchbend
Definition juce_MPENote.h:128
uint8 initialNote
Definition juce_MPENote.h:108
MPEValue pressure
Definition juce_MPENote.h:133
MPEValue initialTimbre
Definition juce_MPENote.h:138
double getFrequencyInHertz(double frequencyOfA=440.0) const noexcept
Definition juce_MPENote.cpp:68
double totalPitchbendInSemitones
Definition juce_MPENote.h:164
uint8 midiChannel
Definition juce_MPENote.h:103
KeyState keyState
Definition juce_MPENote.h:169
bool isValid() const noexcept
Definition juce_MPENote.cpp:62
MPENote(int midiChannel, int initialNote, MPEValue velocity, MPEValue pitchbend, MPEValue pressure, MPEValue timbre, KeyState keyState=MPENote::keyDown) noexcept
Definition juce_MPENote.cpp:38
#define const
Definition zconf.h:137