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 {
25  uint8_t defaultNoteDuration = 0; // quarter-note
26  uint8_t defaultOctave = 0;
27  uint16_t bpm = 0;
28 };
29 
30 /*
31  * Define core state variables so state can be easily saved/restored
32  */
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 
131 
133  {
134  return header;
135  }
136 
138  {
139  bufferPos = buffer.getPos();
140  return *this;
141  }
142 
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:46
Base parser class.
Definition: RingTone.h:93
Class to parse RTTTL files RTTTL (RingTone Text Transfer Language) format.
Definition: RtttlParser.h:57
const RtttlHeader & getHeader()
Definition: RtttlParser.h:132
bool nextTune()
Locate next tune and read header.
bool rewind()
Rewind to start of tune.
Definition: RtttlParser.h:115
const String & getTitle()
Get the current tune title.
Definition: RtttlParser.h:95
RtttlParserState getState()
Definition: RtttlParser.h:137
unsigned getIndex()
Get the current tune index.
Definition: RtttlParser.h:82
bool begin(IDataSourceStream *source)
Initialise the parser with the given stream.
unsigned getCount()
Get the number of tunes in this file.
bool readNextNote(RingTone::NoteDef &note)
Fetch the next note for this tune.
bool seekTune(unsigned index)
Find a tune by index, starting at #0.
String getCaption()
Get a display caption for the current tune.
Definition: RtttlParser.h:103
void end()
Release the source stream.
void setState(const RtttlParserState &state)
Definition: RtttlParser.h:143
unsigned getPos()
Definition: StreamBuffer.h:62
bool setPos(unsigned pos)
Definition: StreamBuffer.h:33
The String class.
Definition: WString.h:137
Definition: RingTone.h:19
Definition: RingTone.h:43
Definition: RtttlParser.h:23
uint8_t defaultOctave
Definition: RtttlParser.h:26
uint16_t bpm
Definition: RtttlParser.h:27
uint8_t defaultNoteDuration
Definition: RtttlParser.h:25
String title
Definition: RtttlParser.h:24
Definition: RtttlParser.h:33
unsigned index
Definition: RtttlParser.h:37
enum RingTone::RtttlParserState::@83 state
@ rtps_EndOfFile
Definition: RtttlParser.h:44
@ rtps_TuneHeader
Definition: RtttlParser.h:42
@ rtps_TuneContent
Definition: RtttlParser.h:43
@ rtps_StartOfFile
Definition: RtttlParser.h:41
unsigned bufferPos
Definition: RtttlParser.h:48
unsigned count
Definition: RtttlParser.h:36
RtttlHeader header
Definition: RtttlParser.h:34
uint16_t wholeNoteMillis
Definition: RtttlParser.h:35
unsigned tuneStartPos
Definition: RtttlParser.h:38