음악, 삶, 개발

lee::midi::Pitch 본문

개발 공부/namespace lee

lee::midi::Pitch

Lee_____ 2020. 9. 19. 05:45
#pragma once

#include <JuceHeader.h>

namespace lee {

    namespace midi {

        class Pitch {

            public:

                Pitch() {}
                Pitch(int n) {

                    pitch = fix(n);

                }
                Pitch(const Pitch& other) {

                    pitch = other.pitch;

                }
                Pitch(const juce::MidiMessage& midiMessage) {

                    if (midiMessage.isNoteOnOrOff()) pitch = midiMessage.getNoteNumber();

                }
                Pitch& operator= (const Pitch& other) {

                    pitch = other.pitch;

                    return *this;

                }
                Pitch& operator+= (const Pitch& other) {

                    pitch = fix(pitch + other.pitch);

                    return *this;

                }
                Pitch& operator-= (const Pitch& other) {

                    pitch = fix(pitch - other.pitch);

                    return *this;

                }
                Pitch& operator++ () {

                    pitch = fix(++pitch);

                    return *this;

                }
                Pitch& operator-- () {

                    pitch = fix(++pitch);

                    return *this;

                }
                Pitch operator+ (const Pitch& other) const {

                    return Pitch(fix(pitch + other.pitch));

                }
                Pitch operator- (const Pitch& other) const {

                    return Pitch(fix(pitch - other.pitch));

                }
                bool operator> (const Pitch& other) const {

                    if (pitch > other.pitch) return true;

                    return false;

                }
                bool operator< (const Pitch& other) const {

                    if (pitch < other.pitch) return true;

                    return false;

                }
                bool operator== (const Pitch& other) const {

                    if (pitch == other.pitch) return true;

                    return false;

                }
                bool operator>= (const Pitch& other) const {

                    if (pitch >= other.pitch) return true;

                    return false;

                }
                bool operator<= (const Pitch& other) const {

                    if (pitch <= other.pitch) return true;

                    return false;

                }
                int operator() () const {

                    return pitch;

                }
                friend Pitch operator+ (int n, const Pitch& other);
                friend Pitch operator- (int n, const Pitch& other);

                void set(const Pitch& other) {

                    pitch = other.pitch;

                }
                int get() const {

                    return pitch;

                }

            protected:

                int fix(int n) const {

                    if (n < min) n = min;
                    else if (n > max) n = max;

                    return n;

                }

            private:

                int pitch {64};
                const int min {0};
                const int max {127};

        };

        Pitch operator+ (int n, const Pitch& other) {

            return other + n;

        }
        Pitch operator- (int n, const Pitch& other) {

            return Pitch(n - other.pitch);

        }

    }

}