annotate base/Pitch.cpp @ 319:3ff8f571da09

* Hoist alignment model set/query up to Model, so any models can be aligned * Add Model::aboutToDelete and aboutToBeDeleted for management of models that are contained by or referred to by other models instead of only the document
author Chris Cannam
date Wed, 24 Oct 2007 15:21:38 +0000
parents e412f65884ee
children 0a44caddd9fe
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@19 55 static QString notes[] = {
Chris@19 56 "C%1", "C#%1", "D%1", "D#%1",
Chris@19 57 "E%1", "F%1", "F#%1", "G%1",
Chris@19 58 "G#%1", "A%1", "A#%1", "B%1"
Chris@19 59 };
Chris@19 60
Chris@26 61 static QString flatNotes[] = {
Chris@26 62 "C%1", "Db%1", "D%1", "Eb%1",
Chris@26 63 "E%1", "F%1", "Gb%1", "G%1",
Chris@26 64 "Ab%1", "A%1", "Bb%1", "B%1"
Chris@26 65 };
Chris@26 66
Chris@19 67 QString
Chris@19 68 Pitch::getPitchLabel(int midiPitch,
Chris@26 69 float centsOffset,
Chris@26 70 bool useFlats)
Chris@19 71 {
Chris@19 72 int octave = -2;
Chris@19 73
Chris@19 74 if (midiPitch < 0) {
Chris@19 75 while (midiPitch < 0) {
Chris@19 76 midiPitch += 12;
Chris@19 77 --octave;
Chris@19 78 }
Chris@19 79 } else {
Chris@19 80 octave = midiPitch / 12 - 2;
Chris@19 81 }
Chris@19 82
Chris@26 83 QString plain = (useFlats ? flatNotes : notes)[midiPitch % 12].arg(octave);
Chris@19 84
Chris@19 85 int ic = lrintf(centsOffset);
Chris@19 86 if (ic == 0) return plain;
Chris@19 87 else if (ic > 0) return QString("%1+%2c").arg(plain).arg(ic);
Chris@19 88 else return QString("%1%2c").arg(plain).arg(ic);
Chris@19 89 }
Chris@19 90
Chris@19 91 QString
Chris@19 92 Pitch::getPitchLabelForFrequency(float frequency,
Chris@26 93 float concertA,
Chris@26 94 bool useFlats)
Chris@19 95 {
Chris@141 96 if (concertA <= 0.0) {
Chris@141 97 concertA = Preferences::getInstance()->getTuningFrequency();
Chris@141 98 }
Chris@19 99 float centsOffset = 0.0;
Chris@19 100 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA);
Chris@26 101 return getPitchLabel(midiPitch, centsOffset, useFlats);
Chris@19 102 }
Chris@19 103
Chris@274 104 bool
Chris@274 105 Pitch::isFrequencyInMidiRange(float frequency,
Chris@274 106 float concertA)
Chris@274 107 {
Chris@274 108 float centsOffset = 0.0;
Chris@274 109 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA);
Chris@274 110 return (midiPitch >= 0 && midiPitch < 128);
Chris@274 111 }
Chris@274 112