RtttlParser.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  * RtttlParser.h - Support for reading RTTTL files
8  *
9  * RTTTL conversion code based on https://github.com/end2endzone/NonBlockingRTTTL
10  *
11  * @author Sept 2019 mikee47 <mike@sillyhouse.net>
12  *
13  ****/
14 
15 #pragma once
16 
17 #include "StreamBuffer.h"
18 #include <WString.h>
19 #include "RingTone.h"
20 
21 namespace RingTone
22 {
23 struct RtttlHeader {
24  String title;
25  uint8_t defaultNoteDuration = 0; // quarter-note
27  uint16_t bpm = 0;
28 };
29 
30 /*
31  * Define core state variables so state can be easily saved/restored
32  */
33 struct RtttlParserState {
34  RtttlHeader header; // Header for current tune
35  uint16_t wholeNoteMillis = 0; // Calculated from bpm
36  unsigned count = 0; // Cached number of tunes in the file
37  unsigned index = 0;
38  unsigned tuneStartPos = 0; // First character after header
39 
40  enum {
46 
47  // Only used when saving/restoring state
48  unsigned bufferPos = 0;
49 };
50 
57 {
58 public:
62  bool begin(IDataSourceStream* source);
63 
67  void end();
68 
72  bool nextTune();
73 
77  bool seekTune(unsigned index);
78 
82  unsigned getIndex()
83  {
84  return index;
85  }
86 
90  unsigned getCount();
91 
95  const String& getTitle()
96  {
97  return header.title;
98  }
99 
104  {
105  String s('#');
106  s += index;
107  s += ": ";
108  s += header.title;
109  return s;
110  }
111 
115  bool rewind()
116  {
117  if(tuneStartPos == 0) {
118  // No current tune
119  return false;
120  }
121 
122  return buffer.setPos(tuneStartPos);
123  }
124 
130  bool readNextNote(RingTone::NoteDef& note);
131 
132  const RtttlHeader& getHeader()
133  {
134  return header;
135  }
136 
137  RtttlParserState getState()
138  {
139  bufferPos = buffer.getPos();
140  return *this;
141  }
142 
143  void setState(const RtttlParserState& state)
144  {
145  *static_cast<RtttlParserState*>(this) = state;
146  buffer.setPos(bufferPos);
147  }
148 
149 private:
150  bool readHeader();
151  unsigned readNum();
152 
153  // Skip whitespace and comment lines
154  bool skipWhitespace();
155 
156 private:
157  StreamBuffer<64> buffer;
158 };
159 
160 } // namespace RingTone
Base class for read-only stream.
Definition: DataSourceStream.h:45
unsigned count
Definition: RtttlParser.h:48
unsigned tuneStartPos
Definition: RtttlParser.h:50
String title
Definition: RtttlParser.h:48
bool begin(IDataSourceStream *source)
Initialise the parser with the given stream.
Class to parse RTTTL files RTTTL (RingTone Text Transfer Language) format.
Definition: RtttlParser.h:68
RtttlHeader header
Definition: RtttlParser.h:46
The String class.
Definition: WString.h:136
Definition: RingTone.h:18
unsigned getPos()
Definition: StreamBuffer.h:72
bool nextTune()
Locate next tune and read header.
bool seekTune(unsigned index)
Find a tune by index, starting at #0.
void end()
Release the source stream.
unsigned getCount()
Get the number of tunes in this file.
unsigned bufferPos
Definition: RtttlParser.h:60
uint8_t defaultNoteDuration
Definition: RtttlParser.h:49
uint16_t bpm
Definition: RtttlParser.h:51
void setState(const RtttlParserState &state)
Definition: RtttlParser.h:155
String getCaption()
Get a display caption for the current tune.
Definition: RtttlParser.h:115
const RtttlHeader & getHeader()
Definition: RtttlParser.h:144
@ rtps_TuneHeader
Definition: RtttlParser.h:54
unsigned getIndex()
Get the current tune index.
Definition: RtttlParser.h:94
const String & getTitle()
Get the current tune title.
Definition: RtttlParser.h:107
@ rtps_EndOfFile
Definition: RtttlParser.h:56
bool rewind()
Rewind to start of tune.
Definition: RtttlParser.h:127
@ rtps_StartOfFile
Definition: RtttlParser.h:53
uint16_t wholeNoteMillis
Definition: RtttlParser.h:47
enum RingTone::RtttlParserState::@72 state
Definition: RtttlParser.h:35
Definition: RingTone.h:53
uint8_t defaultOctave
Definition: RtttlParser.h:50
bool readNextNote(RingTone::NoteDef &note)
Fetch the next note for this tune.
Definition: RtttlParser.h:45
unsigned index
Definition: RtttlParser.h:49
Base parser class.
Definition: RingTone.h:102
RtttlParserState getState()
Definition: RtttlParser.h:149
@ rtps_TuneContent
Definition: RtttlParser.h:55
bool setPos(unsigned pos)
Definition: StreamBuffer.h:43