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,
33  F_Sharp,
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 {
44  uint16_t frequency;
45  uint16_t duration;
46 };
47 
51 static unsigned noteFrequencyA4 = 440;
52 
53 static constexpr float frequencyRoot =
54 #ifdef __clang__
55  1.059463094359295;
56 #else
57  pow(2, 1.0 / 12);
58 #endif
59 
60 template <unsigned octave, unsigned note> static constexpr unsigned calculateFrequency()
61 {
62  return round(noteFrequencyA4 * pow(frequencyRoot, (octave - 4) * 12 + (note - 1)));
63 }
64 
71 unsigned charToNoteValue(char c);
72 
79 unsigned getNoteFrequency(unsigned octave, unsigned note);
80 
87 unsigned getClosestNote(unsigned frequency, unsigned& octave);
88 
92 const char* getNoteName(unsigned noteValue);
93 
97 class Parser
98 {
99 public:
100  virtual ~Parser()
101  {
102  }
103 
108  virtual bool readNextNote(NoteDef& note) = 0;
109 };
110 
111 } // namespace RingTone
#define round(x)
Definition: ArduinoCompat.h:23
Base parser class.
Definition: RingTone.h:98
virtual ~Parser()
Definition: RingTone.h:100
virtual bool readNextNote(NoteDef &note)=0
Fetch the next note for this tune.
Definition: RingTone.h:19
unsigned getClosestNote(unsigned frequency, unsigned &octave)
Convert a frequency into a scale/note combination into frequency.
static constexpr unsigned calculateFrequency()
Definition: RingTone.h:60
unsigned charToNoteValue(char c)
Get the corresponding note number for a letter.
const char * getNoteName(unsigned noteValue)
Get text for a given note number.
static constexpr float frequencyRoot
Definition: RingTone.h:53
static unsigned noteFrequencyA4
Reference note frequency.
Definition: RingTone.h:51
Note
Note numbers, defined here for convenience.
Definition: RingTone.h:23
unsigned getNoteFrequency(unsigned octave, unsigned note)
Convert a scale/note combination into frequency.
Definition: RingTone.h:43
uint16_t frequency
Definition: RingTone.h:44
uint16_t duration
Definition: RingTone.h:45