arduino-simple-tts

Loading...
Searching...
No Matches
AudioDictionarySD.h
1
2#pragma once
3
4#include <WiFi.h>
5
6#include "SimpleTTSBase.h"
7
8// by default we use the SdFat Library
9#ifdef USE_SD
10#include <SD.h>
11typedef File AudioFile;
12#else
13#include <SdFat.h>
14typedef File32 AudioFile;
15typedef SdFat32 AudioFat;
16#ifndef SDFAT_SPEED
17#define SDFAT_SPEED 2
18#endif
19#endif
20
21namespace simple_tts {
22
28template <class T>
29 class AudioStreamFileWrapper : public AudioStream {
30 public:
31 AudioStreamFileWrapper() = default;
32 virtual bool begin(T &file) {
33 p_file = &file;
34 return true;
35 }
36 virtual bool begin() override {
37 LOGI(LOG_METHOD);
38 p_file->seek(0);
39 return true;
40 }
41 virtual void end() override {
42 LOGI(LOG_METHOD);
43 p_file->close();
44 }
45
46 virtual size_t readBytes(uint8_t *buffer, size_t length) override {
47 return p_file->readBytes((char *)buffer, length);
48 }
49
50 virtual size_t write(const uint8_t *buffer, size_t length) override {
51 return p_file->write(buffer, length);
52 }
53
54 virtual int available() override { return p_file->available(); }
55
56 virtual int availableForWrite() override {
57 return p_file->availableForWrite();
58 }
59
60 operator bool() { return *p_file; }
61
62 protected:
63 T *p_file = nullptr;
64};
65
73 class AudioDictionarySD : public AudioDictionaryBase {
74 public:
75 AudioDictionarySD(const char *path, const char *ext, int cs_pin = PIN_CS) {
76 this->path = path;
77 this->ext = ext;
78 this->cs_pin = cs_pin;
79 }
80
81 ~AudioDictionarySD() { file.close(); }
82
84 AudioStream *get(const char *word) {
85 setup();
86 // provide new file
87 const char *file_name = getFileWithPath(word);
88 if (SD.exists(file_name)) {
89 file = SD.open(file_name, FILE_READ);
90 fileWrapper.begin(file);
91 return &fileWrapper;
92 }
93 LOGE("File does not exist: %s", file_name);
94
95 return nullptr;
96 }
97
98 protected:
99 audio_tools::StrExt url_with_text;
100 AudioFile file;
101 AudioStreamFileWrapper<AudioFile> fileWrapper;
102#ifndef USE_SD
103 AudioFat SD;
104#endif
105 const char *path;
106 const char *ext;
107 StrExt file_path{40}; // allocate 40 bytes as a typical good initial size
108 bool is_setup = false;
109 int cs_pin = -1;
110
111 void setup() {
112 if (!is_setup) {
113#ifdef USE_SD
114 LOGI("Setup SD library");
115 if (!SD.begin(cs_pin)) {
116 LOGE("SD.begin failed for cs_pin: %d", cs_pin);
117 return;
118 }
119#else
120 LOGI("Setup SdFat library");
121 if (!SD.begin(
122 SdSpiConfig(cs_pin, DEDICATED_SPI, SD_SCK_MHZ(SDFAT_SPEED)))) {
123 LOGE("SD.begin failed for cs_pin: %d", cs_pin);
124 return;
125 }
126#endif
127 if (!SD.exists(path)) {
128 LOGI("Creating directory: %s", path)
129 if (!SD.mkdir(path)) {
130 LOGE("Could not create directory: %s", path);
131 }
132 }
133 is_setup = true;
134 }
135 }
136
137 // determines the filename for the word
138 const char *getFileWithPath(const char *name) {
139 file_path = path;
140 file_path.add("/");
141 file_path.add(name);
142 file_path.add(".");
143 file_path.add(ext);
144 file_path.toLowerCase();
145 const char *str = file_path.c_str();
146 LOGI("%s -> %s", name, str);
147 return str;
148 }
149};
150
151} // namespace simple_tts
simple_tts::AudioDictionaryBase
Dictionary which provides a Stream of Audio for the indicated word.
Definition: SimpleTTSBase.h:49
simple_tts::AudioDictionarySD
A dictionary which is based on files stored on an SD card. By default we use the SdFat library....
Definition: AudioDictionarySD.h:73
simple_tts::AudioDictionarySD::get
AudioStream * get(const char *word)
retrieves recorded audio file for the word
Definition: AudioDictionarySD.h:84
simple_tts::AudioStreamFileWrapper
A simple Wrapper that let's a file pretend to be a AudioStream to support the begin and end methods.
Definition: AudioDictionarySD.h:29