annotate base/Pitch.h @ 1024:d1ce7a4a920b

Wire up note/octave stuff
author Chris Cannam
date Tue, 02 Dec 2014 17:53:17 +0000
parents 1f62a890da58
children 88b54a185a0a
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@19 33 static float getFrequencyForPitch(int midiPitch,
Chris@19 34 float centsOffset = 0,
Chris@141 35 float 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@19 49 static int getPitchForFrequency(float frequency,
Chris@19 50 float *centsOffsetReturn = 0,
Chris@141 51 float concertA = 0.0);
Chris@19 52
Chris@274 53 /**
Chris@373 54 * Return the nearest MIDI pitch range to the given frequency
Chris@373 55 * range, that is, the difference in MIDI pitch values between the
Chris@373 56 * higher and lower frequencies.
Chris@373 57 *
Chris@373 58 * If centsOffsetReturn is non-NULL, return in *centsOffsetReturn
Chris@373 59 * the number of cents (1/100ths of a semitone) difference between
Chris@373 60 * the given frequency difference and the returned MIDI pitch
Chris@373 61 * range. The cents offset will be in the range [-50,50).
Chris@373 62 *
Chris@373 63 * If concertA is non-zero, use that as the reference frequency
Chris@373 64 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@373 65 * specified in the application preferences (default 440Hz).
Chris@373 66 */
Chris@373 67 static int getPitchForFrequencyDifference(float frequencyA,
Chris@373 68 float frequencyB,
Chris@373 69 float *centsOffsetReturn = 0,
Chris@373 70 float concertA = 0.0);
Chris@373 71
Chris@373 72 /**
Chris@1024 73 * Return the MIDI pitch for the given note number (0-12 where 0
Chris@1024 74 * is C) and octave number. The octave numbering system is based
Chris@1024 75 * on the application preferences (default is C4 = middle C,
Chris@1024 76 * though in previous SV releases that was C3).
Chris@1024 77 */
Chris@1024 78 static int getPitchForNoteAndOctave(int note, int octave);
Chris@1024 79
Chris@1024 80 /**
Chris@1024 81 * Return the note number (0-12 where 0 is C) and octave number
Chris@1024 82 * for the given MIDI pitch. The octave numbering system is based
Chris@1024 83 * on the application preferences (default is C4 = middle C,
Chris@1024 84 * though in previous SV releases that was C3).
Chris@1024 85 */
Chris@1024 86 static void getNoteAndOctaveForPitch(int midiPitch, int &note, int &octave);
Chris@1024 87
Chris@1024 88 /**
Chris@274 89 * Return a string describing the given MIDI pitch, with optional
Chris@892 90 * cents offset. This consists of the note name, octave number,
Chris@892 91 * and optional cents. The octave numbering system is based on the
Chris@892 92 * application preferences (default is C4 = middle C, though in
Chris@892 93 * previous SV releases that was C3).
Chris@274 94 *
Chris@274 95 * For example, "A#3" (A# in octave 3) or "C2-12c" (C in octave 2,
Chris@274 96 * minus 12 cents).
Chris@274 97 *
Chris@274 98 * If useFlats is true, spell notes with flats instead of sharps,
Chris@274 99 * e.g. Bb3 instead of A#3.
Chris@274 100 */
Chris@19 101 static QString getPitchLabel(int midiPitch,
Chris@26 102 float centsOffset = 0,
Chris@26 103 bool useFlats = false);
Chris@1023 104
Chris@274 105 /**
Chris@274 106 * Return a string describing the nearest MIDI pitch to the given
Chris@274 107 * frequency, with cents offset.
Chris@274 108 *
Chris@274 109 * If concertA is non-zero, use that as the reference frequency
Chris@274 110 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@274 111 * specified in the application preferences (default 440Hz).
Chris@274 112 *
Chris@274 113 * If useFlats is true, spell notes with flats instead of sharps,
Chris@274 114 * e.g. Bb3 instead of A#3.
Chris@274 115 */
Chris@19 116 static QString getPitchLabelForFrequency(float frequency,
Chris@141 117 float concertA = 0.0,
Chris@26 118 bool useFlats = false);
Chris@274 119
Chris@274 120 /**
Chris@373 121 * Return a string describing the given pitch range in octaves,
Chris@373 122 * semitones and cents. This is in the form e.g. "1'2+4c".
Chris@373 123 */
Chris@373 124 static QString getLabelForPitchRange(int semis, float cents = 0);
Chris@373 125
Chris@373 126 /**
Chris@274 127 * Return true if the given frequency falls within the range of
Chris@274 128 * MIDI note pitches, plus or minus half a semitone. This is
Chris@274 129 * equivalent to testing whether getPitchForFrequency returns a
Chris@274 130 * pitch in the MIDI range (0 to 127 inclusive) with any cents
Chris@274 131 * offset.
Chris@274 132 *
Chris@274 133 * If concertA is non-zero, use that as the reference frequency
Chris@274 134 * for the A at MIDI pitch 69; otherwise use the tuning frequency
Chris@274 135 * specified in the application preferences (default 440Hz).
Chris@274 136 */
Chris@274 137 static bool isFrequencyInMidiRange(float frequency,
Chris@274 138 float concertA = 0.0);
Chris@19 139 };
Chris@19 140
Chris@19 141
Chris@19 142 #endif