Mercurial > hg > svcore
comparison base/Pitch.cpp @ 19:a7ed14263fe4
* Add Chromagram plugin, and make a number of fixes to the dense 3d model
and colour 3d plot class to accommodate it
* Add pitch-conversion methods in base/Pitch
* Commit previously overlooked Command.cpp
| author | Chris Cannam |
|---|---|
| date | Wed, 01 Feb 2006 14:49:49 +0000 |
| parents | |
| children | 090c22aa726a |
comparison
equal
deleted
inserted
replaced
| 18:4563a72c1d8b | 19:a7ed14263fe4 |
|---|---|
| 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ | |
| 2 | |
| 3 /* | |
| 4 A waveform viewer and audio annotation editor. | |
| 5 Chris Cannam, Queen Mary University of London, 2005-2006 | |
| 6 | |
| 7 This is experimental software. Not for distribution. | |
| 8 */ | |
| 9 | |
| 10 #include "Pitch.h" | |
| 11 | |
| 12 #include <cmath> | |
| 13 | |
| 14 float | |
| 15 Pitch::getFrequencyForPitch(int midiPitch, | |
| 16 float centsOffset, | |
| 17 float concertA) | |
| 18 { | |
| 19 float p = float(midiPitch) + (centsOffset / 100); | |
| 20 return concertA * powf(2.0, (p - 69.0) / 12.0); | |
| 21 } | |
| 22 | |
| 23 int | |
| 24 Pitch::getPitchForFrequency(float frequency, | |
| 25 float *centsOffsetReturn, | |
| 26 float concertA) | |
| 27 { | |
| 28 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; | |
| 29 | |
| 30 int midiPitch = int(p + 0.00001); | |
| 31 float centsOffset = (p - midiPitch) * 100.0; | |
| 32 | |
| 33 if (centsOffset >= 50.0) { | |
| 34 midiPitch = midiPitch + 1; | |
| 35 centsOffset = -(100.0 - centsOffset); | |
| 36 } | |
| 37 | |
| 38 if (centsOffsetReturn) *centsOffsetReturn = centsOffset; | |
| 39 return midiPitch; | |
| 40 } | |
| 41 | |
| 42 static QString notes[] = { | |
| 43 "C%1", "C#%1", "D%1", "D#%1", | |
| 44 "E%1", "F%1", "F#%1", "G%1", | |
| 45 "G#%1", "A%1", "A#%1", "B%1" | |
| 46 }; | |
| 47 | |
| 48 QString | |
| 49 Pitch::getPitchLabel(int midiPitch, | |
| 50 float centsOffset) | |
| 51 { | |
| 52 int octave = -2; | |
| 53 | |
| 54 if (midiPitch < 0) { | |
| 55 while (midiPitch < 0) { | |
| 56 midiPitch += 12; | |
| 57 --octave; | |
| 58 } | |
| 59 } else { | |
| 60 octave = midiPitch / 12 - 2; | |
| 61 } | |
| 62 | |
| 63 QString plain = notes[midiPitch % 12].arg(octave); | |
| 64 | |
| 65 int ic = lrintf(centsOffset); | |
| 66 if (ic == 0) return plain; | |
| 67 else if (ic > 0) return QString("%1+%2c").arg(plain).arg(ic); | |
| 68 else return QString("%1%2c").arg(plain).arg(ic); | |
| 69 } | |
| 70 | |
| 71 QString | |
| 72 Pitch::getPitchLabelForFrequency(float frequency, | |
| 73 float concertA) | |
| 74 { | |
| 75 float centsOffset = 0.0; | |
| 76 int midiPitch = getPitchForFrequency(frequency, ¢sOffset, concertA); | |
| 77 return getPitchLabel(midiPitch, centsOffset); | |
| 78 } | |
| 79 |
