RingTone.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Parser.h - Common definitions for playing ringtones
8  *
9  * @author Sept 2019 mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include <cstdint>
16 #include <cmath>
17 
18 namespace RingTone
19 {
23 enum class Note {
24  MUTE,
25  C,
26  C_Sharp,
27  D_Flat = C_Sharp,
28  D,
29  D_Sharp,
30  E_Flat = D_Sharp,
31  E,
32  F,
34  G,
35  G_Sharp,
36  A_Flat = G_Sharp,
37  A,
38  A_Sharp,
39  B_Flat = A_Sharp,
40  B,
41 };
42 
43 struct NoteDef {
46 };
47 
51 static unsigned noteFrequencyA4 = 440;
52 
53 static constexpr float frequencyRoot = pow(2, 1.0 / 12);
54 
55 template <unsigned octave, unsigned note> static constexpr unsigned calculateFrequency()
56 {
57  return round(noteFrequencyA4 * pow(frequencyRoot, (octave - 4) * 12 + (note - 1)));
58 }
59 
66 unsigned charToNoteValue(char c);
67 
74 unsigned getNoteFrequency(unsigned octave, unsigned note);
75 
82 unsigned getClosestNote(unsigned frequency, unsigned& octave);
83 
87 const char* getNoteName(unsigned noteValue);
88 
92 class Parser
93 {
94 public:
95  virtual ~Parser()
96  {
97  }
98 
103  virtual bool readNextNote(NoteDef& note) = 0;
104 };
105 
106 } // namespace RingTone
const char * getNoteName(unsigned noteValue)
Get text for a given note number.
Definition: RingTone.h:18
unsigned getClosestNote(unsigned frequency, unsigned &octave)
Convert a frequency into a scale/note combination into frequency.
unsigned getNoteFrequency(unsigned octave, unsigned note)
Convert a scale/note combination into frequency.
static constexpr unsigned calculateFrequency()
Definition: RingTone.h:65
unsigned charToNoteValue(char c)
Get the corresponding note number for a letter.
uint16_t frequency
Definition: RingTone.h:54
virtual ~Parser()
Definition: RingTone.h:105
Note
Note numbers, defined here for convenience.
Definition: RingTone.h:33
static constexpr float frequencyRoot
Definition: RingTone.h:63
#define round(x)
Definition: ArduinoCompat.h:23
virtual bool readNextNote(NoteDef &note)=0
Fetch the next note for this tune.
uint16_t duration
Definition: RingTone.h:55
Base parser class.
Definition: RingTone.h:102
static unsigned noteFrequencyA4
Reference note frequency.
Definition: RingTone.h:61