-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.cpp
More file actions
67 lines (58 loc) · 1.68 KB
/
common.cpp
File metadata and controls
67 lines (58 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "common.h"
#include <cmath>
QString Common::RelativePitchToNoteName(float pitch)
{
return MidiNoteToNoteName(RelativePitchToMidiNote(pitch));
}
int Common::RelativePitchToMidiNote(float pitch)
{
// relative pitch = cents deviated from A4 (440Hz, MIDI 69)
float relativeSemitone = pitch / 100.0;
relativeSemitone = roundf(relativeSemitone);
return 69 + relativeSemitone;
}
QString Common::MidiNoteToNoteName(int note)
{
// A0 = MIDI 21, C8 = 108. Return empty when out of range
static const char *NoteNames[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
if (note > 108 || note < 21)
return QString();
note -= 12;
return QString("%1%2").arg(NoteNames[note % 12]).arg(note / 12);
}
QString Common::SanitizeFilename(QString filename)
{
QString ret = filename;
static QString illegalChars = "\\/:%*?\"<>|;=";
for(auto i = ret.begin(); i != ret.end(); i++)
if(illegalChars.indexOf(*i) != -1)
*i = '_';
return ret;
}
QString Common::EscapeStringForCsv(QString str)
{
QString ret;
for(auto i = str.begin(); i != str.end(); i++)
if(*i == ',' || *i == '"' || *i == '\\')
ret += QString('\\') + *i;
else
ret += *i;
return ret;
}
QString Common::devDbDirEncode(QString input)
{
QString ret;
for (QChar c : input) {
if (c < 'a' || c > 'z') {
ret += '%' + QString::number(c.toLatin1()) + '%';
} else {
ret += c;
}
}
return ret;
}
float Common::RelativePitchToFrequency(float pitch)
{
int midiNote = RelativePitchToMidiNote(pitch);
return 440.0 * pow(2.0, (midiNote - 69.0) / 12.0);
}