annotate base/Pitch.cpp @ 537:3cc4b7cd2aa5

* Merge from one-fftdataserver-per-fftmodel branch. This bit of reworking (which is not described very accurately by the title of the branch) turns the MatrixFile object into something that either reads or writes, but not both, and separates the FFT file cache reader and writer implementations separately. This allows the FFT data server to have a single thread owning writers and one reader per "customer" thread, and for all locking to be vastly simplified and concentrated in the data server alone (because none of the classes it makes use of is used in more than one thread at a time). The result is faster and more trustworthy code.
author Chris Cannam
date Tue, 27 Jan 2009 13:25:10 +0000
parents ad4911230a66
children bdc9bb371a9f
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 #include "Pitch.h"
Chris@141 17 #include "Preferences.h"
Chris@19 18
Chris@19 19 #include <cmath>
Chris@19 20
Chris@19 21 float
Chris@19 22 Pitch::getFrequencyForPitch(int midiPitch,
Chris@19 23 float centsOffset,
Chris@19 24 float concertA)
Chris@19 25 {
Chris@141 26 if (concertA <= 0.0) {
Chris@141 27 concertA = Preferences::getInstance()->getTuningFrequency();
Chris@141 28 }
Chris@19 29 float p = float(midiPitch) + (centsOffset / 100);
Chris@19 30 return concertA * powf(2.0, (p - 69.0) / 12.0);
Chris@19 31 }
Chris@19 32
Chris@19 33 int
Chris@19 34 Pitch::getPitchForFrequency(float frequency,
Chris@19 35 float *centsOffsetReturn,
Chris@19 36 float concertA)
Chris@19 37 {
Chris@141 38 if (concertA <= 0.0) {
Chris@141 39 concertA = Preferences::getInstance()->getTuningFrequency();
Chris@141 40 }
Chris@19 41 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;
Chris@19 42
Chris@19 43 int midiPitch = int(p + 0.00001);
Chris@19 44 float centsOffset = (p - midiPitch) * 100.0;
Chris@19 45
Chris@19 46 if (centsOffset >= 50.0) {
Chris@19 47 midiPitch = midiPitch + 1;
Chris@19 48 centsOffset = -(100.0 - centsOffset);
Chris@19 49 }
Chris@19 50
Chris@19 51 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
Chris@19 52 return midiPitch;
Chris@19 53 }
Chris@19 54
Chris@373 55 int
Chris@373 56 Pitch::getPitchForFrequencyDifference(float frequencyA,
Chris@373 57 float frequencyB,
Chris@373 58 float *centsOffsetReturn,
Chris@373 59 float concertA)
Chris@373 60 {
Chris@373 61 if (concertA <= 0.0) {
Chris@373 62 concertA = Preferences::getInstance()->getTuningFrequency();
Chris@373 63 }
Chris@373 64
Chris@373 65 if (frequencyA > frequencyB) {
Chris@373 66 std::swap(frequencyA, frequencyB);
Chris@373 67 }
Chris@373 68
Chris@373 69 float pA = 12.0 * (log(frequencyA / (concertA / 2.0)) / log(2.0)) + 57.0;
Chris@373 70 float pB = 12.0 * (log(frequencyB / (concertA / 2.0)) / log(2.0)) + 57.0;
Chris@373 71
Chris@373 72 float p = pB - pA;
Chris@373 73
Chris@373 74 int midiPitch = int(p + 0.00001);
Chris@373 75 float centsOffset = (p - midiPitch) * 100.0;
Chris@373 76
Chris@373 77 if (centsOffset >= 50.0) {
Chris@373 78 midiPitch = midiPitch + 1;
Chris@373 79 centsOffset = -(100.0 - centsOffset);
Chris@373 80 }
Chris@373 81
Chris@373 82 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
Chris@373 83 return midiPitch;
Chris@373 84 }
Chris@373 85
Chris@19 86 static QString notes[] = {
Chris@19 87 "C%1", "C#%1", "D%1", "D#%1",
Chris@19 88 "E%1", "F%1", "F#%1", "G%1",
Chris@19 89 "G#%1", "A%1", "A#%1", "B%1"
Chris@19 90 };
Chris@19 91
Chris@26 92 static QString flatNotes[] = {
Chris@26 93 "C%1", "Db%1", "D%1", "Eb%1",
Chris@26 94 "E%1", "F%1", "Gb%1", "G%1",
Chris@26 95 "Ab%1", "A%1", "Bb%1", "B%1"
Chris@26 96 };
Chris@26 97
Chris@19 98 QString
Chris@19 99 Pitch::getPitchLabel(int midiPitch,
Chris@26 100 float centsOffset,
Chris@26 101 bool useFlats)
Chris@19 102 {
Chris@19 103 int octave = -2;
Chris@19 104
Chris@19 105 if (midiPitch < 0) {
Chris@19 106 while (midiPitch < 0) {
Chris@19 107 midiPitch += 12;
Chris@19 108 --octave;
Chris@19 109 }
Chris@19 110 } else {
Chris@19 111 octave = midiPitch / 12 - 2;
Chris@19 112 }
Chris@19 113
Chris@26 114 QString plain = (useFlats ? flatNotes : notes)[midiPitch % 12].arg(octave);
Chris@19 115
Chris@19 116 int ic = lrintf(centsOffset);
Chris@19 117 if (ic == 0) return plain;
Chris@19 118 else if (ic > 0) return QString("%1+%2c").arg(plain).arg(ic);
Chris@19 119 else return QString("%1%2c").arg(plain).arg(ic);
Chris@19 120 }
Chris@19 121
Chris@19 122 QString
Chris@19 123 Pitch::getPitchLabelForFrequency(float frequency,
Chris@26 124 float concertA,
Chris@26 125 bool useFlats)
Chris@19 126 {
Chris@141 127 if (concertA <= 0.0) {
Chris@141 128 concertA = Preferences::getInstance()->getTuningFrequency();
Chris@141 129 }
Chris@19 130 float centsOffset = 0.0;
Chris@19 131 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA);
Chris@26 132 return getPitchLabel(midiPitch, centsOffset, useFlats);
Chris@19 133 }
Chris@19 134
Chris@373 135 QString
Chris@373 136 Pitch::getLabelForPitchRange(int semis, float cents)
Chris@373 137 {
Chris@433 138 if (semis > 0) {
Chris@433 139 while (cents < 0.f) {
Chris@433 140 --semis;
Chris@433 141 cents += 100.f;
Chris@433 142 }
Chris@433 143 }
Chris@433 144 if (semis < 0) {
Chris@433 145 while (cents > 0.f) {
Chris@433 146 ++semis;
Chris@433 147 cents -= 100.f;
Chris@433 148 }
Chris@433 149 }
Chris@433 150
Chris@373 151 int ic = lrintf(cents);
Chris@373 152
Chris@373 153 if (ic == 0) {
Chris@373 154 if (semis >= 12) {
Chris@373 155 return QString("%1'%2").arg(semis/12).arg(semis - 12*(semis/12));
Chris@373 156 } else {
Chris@373 157 return QString("%1").arg(semis);
Chris@373 158 }
Chris@373 159 } else {
Chris@373 160 if (ic > 0) {
Chris@373 161 if (semis >= 12) {
Chris@373 162 return QString("%1'%2+%3c").arg(semis/12).arg(semis - 12*(semis/12)).arg(ic);
Chris@373 163 } else {
Chris@373 164 return QString("%1+%3c").arg(semis).arg(ic);
Chris@373 165 }
Chris@373 166 } else {
Chris@373 167 if (semis >= 12) {
Chris@373 168 return QString("%1'%2%3c").arg(semis/12).arg(semis - 12*(semis/12)).arg(ic);
Chris@373 169 } else {
Chris@373 170 return QString("%1%3c").arg(semis).arg(ic);
Chris@373 171 }
Chris@373 172 }
Chris@373 173 }
Chris@373 174 }
Chris@373 175
Chris@274 176 bool
Chris@274 177 Pitch::isFrequencyInMidiRange(float frequency,
Chris@274 178 float concertA)
Chris@274 179 {
Chris@274 180 float centsOffset = 0.0;
Chris@274 181 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA);
Chris@274 182 return (midiPitch >= 0 && midiPitch < 128);
Chris@274 183 }
Chris@274 184