annotate base/Pitch.h @ 1328:ccc8658914ef 3.0-integration

Merge from branch 3.0-integration
author Chris Cannam
date Wed, 14 Dec 2016 14:28:41 +0000
parents 88b54a185a0a
children 48e9f538e6e9
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@19 2
Chris@19 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@52 7 This file copyright 2006 Chris Cannam.
Chris@19 8
Chris@52 9 This program is free software; you can redistribute it and/or
Chris@52 10 modify it under the terms of the GNU General Public License as
Chris@52 11 published by the Free Software Foundation; either version 2 of the
Chris@52 12 License, or (at your option) any later version. See the file
Chris@52 13 COPYING included with this distribution for more information.
Chris@19 14 */
Chris@19 15
Chris@19 16 #ifndef _PITCH_H_
Chris@19 17 #define _PITCH_H_
Chris@19 18
Chris@19 19 #include <QString>
Chris@19 20
Chris@19 21 class Pitch
Chris@19 22 {
Chris@19 23 public:
Chris@274 24 /**
Chris@274 25 * Return the frequency at the given MIDI pitch plus centsOffset
Chris@274 26 * cents (1/100ths of a semitone). centsOffset does not have to
Chris@274 27 * be in any particular range or sign.
Chris@274 28 *
Chris@274 29 * If concertA is non-zero, use that as the reference frequency
Chris@274 30 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@274 31 * specified in the application preferences (default 440Hz).
Chris@274 32 */
Chris@1025 33 static double getFrequencyForPitch(int midiPitch,
Chris@1025 34 double centsOffset = 0,
Chris@1025 35 double concertA = 0.0);
Chris@19 36
Chris@274 37 /**
Chris@274 38 * Return the nearest MIDI pitch to the given frequency.
Chris@274 39 *
Chris@274 40 * If centsOffsetReturn is non-NULL, return in *centsOffsetReturn
Chris@274 41 * the number of cents (1/100ths of a semitone) difference between
Chris@274 42 * the given frequency and that of the returned MIDI pitch. The
Chris@274 43 * cents offset will be in the range [-50,50).
Chris@274 44 *
Chris@274 45 * If concertA is non-zero, use that as the reference frequency
Chris@274 46 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@274 47 * specified in the application preferences (default 440Hz).
Chris@274 48 */
Chris@1025 49 static int getPitchForFrequency(double frequency,
Chris@1025 50 double *centsOffsetReturn = 0,
Chris@1025 51 double concertA = 0.0);
Chris@1025 52
Chris@1025 53 /**
Chris@1025 54 * Compatibility version of getPitchForFrequency accepting float
Chris@1025 55 * pointer argument.
Chris@1025 56 */
Chris@1025 57 static int getPitchForFrequency(double frequency,
Chris@1025 58 float *centsOffsetReturn,
Chris@1025 59 double concertA = 0.0) {
Chris@1025 60 double c;
Chris@1025 61 int p = getPitchForFrequency(frequency, &c, concertA);
Chris@1025 62 if (centsOffsetReturn) *centsOffsetReturn = float(c);
Chris@1025 63 return p;
Chris@1025 64 }
Chris@19 65
Chris@274 66 /**
Chris@373 67 * Return the nearest MIDI pitch range to the given frequency
Chris@373 68 * range, that is, the difference in MIDI pitch values between the
Chris@373 69 * higher and lower frequencies.
Chris@373 70 *
Chris@373 71 * If centsOffsetReturn is non-NULL, return in *centsOffsetReturn
Chris@373 72 * the number of cents (1/100ths of a semitone) difference between
Chris@373 73 * the given frequency difference and the returned MIDI pitch
Chris@373 74 * range. The cents offset will be in the range [-50,50).
Chris@373 75 *
Chris@373 76 * If concertA is non-zero, use that as the reference frequency
Chris@373 77 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@373 78 * specified in the application preferences (default 440Hz).
Chris@373 79 */
Chris@1025 80 static int getPitchForFrequencyDifference(double frequencyA,
Chris@1025 81 double frequencyB,
Chris@1025 82 double *centsOffsetReturn = 0,
Chris@1025 83 double concertA = 0.0);
Chris@373 84
Chris@373 85 /**
Chris@1025 86 * Compatibility version of getPitchForFrequencyDifference
Chris@1025 87 * accepting float pointer argument.
Chris@1025 88 */
Chris@1025 89 static int getPitchForFrequencyDifference(double frequencyA,
Chris@1025 90 double frequencyB,
Chris@1025 91 float *centsOffsetReturn,
Chris@1025 92 double concertA = 0.0) {
Chris@1025 93 double c;
Chris@1025 94 int p = getPitchForFrequencyDifference(frequencyA, frequencyB,
Chris@1025 95 &c, concertA);
Chris@1025 96 if (centsOffsetReturn) *centsOffsetReturn = float(c);
Chris@1025 97 return p;
Chris@1025 98 }
Chris@1025 99
Chris@1025 100 /**
Chris@1024 101 * Return the MIDI pitch for the given note number (0-12 where 0
Chris@1024 102 * is C) and octave number. The octave numbering system is based
Chris@1024 103 * on the application preferences (default is C4 = middle C,
Chris@1024 104 * though in previous SV releases that was C3).
Chris@1024 105 */
Chris@1024 106 static int getPitchForNoteAndOctave(int note, int octave);
Chris@1024 107
Chris@1024 108 /**
Chris@1024 109 * Return the note number (0-12 where 0 is C) and octave number
Chris@1024 110 * for the given MIDI pitch. The octave numbering system is based
Chris@1024 111 * on the application preferences (default is C4 = middle C,
Chris@1024 112 * though in previous SV releases that was C3).
Chris@1024 113 */
Chris@1024 114 static void getNoteAndOctaveForPitch(int midiPitch, int &note, int &octave);
Chris@1024 115
Chris@1024 116 /**
Chris@274 117 * Return a string describing the given MIDI pitch, with optional
Chris@892 118 * cents offset. This consists of the note name, octave number,
Chris@892 119 * and optional cents. The octave numbering system is based on the
Chris@892 120 * application preferences (default is C4 = middle C, though in
Chris@892 121 * previous SV releases that was C3).
Chris@274 122 *
Chris@274 123 * For example, "A#3" (A# in octave 3) or "C2-12c" (C in octave 2,
Chris@274 124 * minus 12 cents).
Chris@274 125 *
Chris@274 126 * If useFlats is true, spell notes with flats instead of sharps,
Chris@274 127 * e.g. Bb3 instead of A#3.
Chris@274 128 */
Chris@19 129 static QString getPitchLabel(int midiPitch,
Chris@1025 130 double centsOffset = 0,
Chris@26 131 bool useFlats = false);
Chris@1023 132
Chris@274 133 /**
Chris@274 134 * Return a string describing the nearest MIDI pitch to the given
Chris@274 135 * frequency, with cents offset.
Chris@274 136 *
Chris@274 137 * If concertA is non-zero, use that as the reference frequency
Chris@274 138 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@274 139 * specified in the application preferences (default 440Hz).
Chris@274 140 *
Chris@274 141 * If useFlats is true, spell notes with flats instead of sharps,
Chris@274 142 * e.g. Bb3 instead of A#3.
Chris@274 143 */
Chris@1025 144 static QString getPitchLabelForFrequency(double frequency,
Chris@1025 145 double concertA = 0.0,
Chris@26 146 bool useFlats = false);
Chris@274 147
Chris@274 148 /**
Chris@373 149 * Return a string describing the given pitch range in octaves,
Chris@373 150 * semitones and cents. This is in the form e.g. "1'2+4c".
Chris@373 151 */
Chris@1025 152 static QString getLabelForPitchRange(int semis, double cents = 0);
Chris@373 153
Chris@373 154 /**
Chris@274 155 * Return true if the given frequency falls within the range of
Chris@274 156 * MIDI note pitches, plus or minus half a semitone. This is
Chris@274 157 * equivalent to testing whether getPitchForFrequency returns a
Chris@274 158 * pitch in the MIDI range (0 to 127 inclusive) with any cents
Chris@274 159 * offset.
Chris@274 160 *
Chris@274 161 * If concertA is non-zero, use that as the reference frequency
Chris@274 162 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@274 163 * specified in the application preferences (default 440Hz).
Chris@274 164 */
Chris@1025 165 static bool isFrequencyInMidiRange(double frequency,
Chris@1025 166 double concertA = 0.0);
Chris@19 167 };
Chris@19 168
Chris@19 169
Chris@19 170 #endif