summaryrefslogtreecommitdiffstats
path: root/mp3player.cpp
blob: b57c5a183a1520df312c7d145127fb5207b45c6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "mp3player.h"

MP3List::MP3List(File *dir, ListMode mode) {
  Serial.println("MP3List::MP3List");
  this->mode = mode;
  int size = countListElements(dir);

  list = new String[size];

  Serial.println(size);

  listSize = size;
  listCurrentPosition=0;

  insertElementsToList(dir);
  debug();
}

MP3List::~MP3List() {
  delete [] list;
}

void MP3List::debug() {
  Serial.println("============");
  for(int i=0; i<listSize; i++) {
    Serial.print(mode);
    Serial.print(": ");
    Serial.print(list[i]);
    Serial.print("\n\n");
  }
  Serial.print("List size");
  Serial.print(listSize);
  Serial.print("\n");

  Serial.print("List position");
  Serial.print(listCurrentPosition);
  Serial.print("\n");

  Serial.println("============");
}

bool MP3List::canAdd(File *file) {
  String fileName = file->name();

  switch(mode) {
  case ListMode::SECTIONS:
    Serial.println("SECTIONS");
    return file->isDirectory();
    break;

  case ListMode::MUSIC_FILES:
    Serial.println("MUSIC FILES");
    return fileName.endsWith(".mp3");
    break;

  default:
    Serial.println("DEFAULT");
    return false;
    break;
  }
}

int MP3List::countListElements(File *dir) {
  dir->rewindDirectory();

  if (!dir->isDirectory()) {
    Serial.println("Not a directory");
    return 0;
  }

  File file = dir->openNextFile();
  int elementsCount = 0;
  while (file) {
    if(canAdd(&file)) 
      elementsCount++;

    file.close();
    file = dir->openNextFile();
  }
  return elementsCount;
}

void MP3List::insertElementsToList(File *dir) {
  dir->rewindDirectory();

  Serial.println("MP3List::insertElementsToList");
  if (!dir->isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = dir->openNextFile();
  int i=0;
  while (file) {
    if(canAdd(&file)) {
      if (i > listSize) 
	continue;
      list[i] = file.name();
      Serial.print("Dodaje: ");
      Serial.print(file.name());
      Serial.print("\n");

      Serial.print("na index: ");
      Serial.print(i);
      Serial.print("\n");
      i++;
    }
    file.close();
    file = dir->openNextFile();
  }
}

String MP3List::getCurrent() {
  Serial.print("list size: ");
  Serial.print(listSize);
  Serial.print("\n");

  Serial.print("list current position: ");
  Serial.print(listCurrentPosition);
  Serial.print("\n");

  if(listCurrentPosition < listSize)
    return list[listCurrentPosition];
  return "";
}


String MP3List::getNext() {
  if(listCurrentPosition < listSize-1)
    listCurrentPosition++;
  else
    listCurrentPosition = 0;

  return getCurrent();
}

String MP3List::getPrevious() {
  if(listCurrentPosition > 0)
    listCurrentPosition--;
  else
    listCurrentPosition = listSize-1;

  return getCurrent();
}


MP3Player::MP3Player() {
  Serial.println("MP3Player::MP3Player");

  source = new AudioFileSourceSD();
  mp3 = new AudioGeneratorMP3();
  out = new AudioOutputI2SNoDAC();
  updateListSections();
}

MP3Player::~MP3Player() {
  delete source;
  delete mp3;
  delete out;

  delete listSections;
  delete listMusicFiles;
}

void MP3Player::playMP3(String name) {
  Serial.println("MP3Player::playMP3");
  Serial.println(name);

  if (mp3->isRunning())
    mp3->stop();

  source->open(name.c_str());
  mp3->begin(source, out);
}

void MP3Player::loop() {
  if (mp3->isRunning())
    if (!mp3->loop()) mp3->stop();
}

void MP3Player::volumeUp() {
  if(volume + volumeBy < 2.0) {
    volume += volumeBy;
    out->SetGain(volume);
  }
}

void MP3Player::volumeDown() {
  if(volume - volumeBy > 0.0) {
    volume -= volumeBy;
    out->SetGain(volume);
  }
}

String MP3Player::getFilePath(String fileName) {
  Serial.println("MP3Player::getFilePath");
  String currentSection = listSections->getCurrent();

  if(currentSection == "" || fileName == "")
    return "";
  return rootDir+"/"+currentSection+"/"+fileName;
}

void MP3Player::nextFile() {
  Serial.println("MP3Player::next");
  String current = listMusicFiles->getNext();
  playMP3(getFilePath(current));
}

void MP3Player::previousFile() {
  Serial.println("MP3Player::previous");
  String current = listMusicFiles->getPrevious();
  playMP3(getFilePath(current));
}

void MP3Player::nextSection() {
  Serial.println("MP3Player::nextS");
  listSections->getNext();
  updateListMusicFiles();
  nextFile();
}

void MP3Player::previousSection() {
  Serial.println("MP3Player::previousS");
  listSections->getPrevious();
  updateListMusicFiles();
  nextFile();
}


bool MP3Player::updateListSections() {
  Serial.println("MP3Player::updateListSections");
  File root = SD.open(rootDir);
  if (!root) {
    Serial.println("Failed to open directory");
    return false;
  }

  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return false;
  }

  delete listSections;
  listSections = new MP3List(&root, ListMode::SECTIONS);
  //  root.rewindDirectory();
  return updateListMusicFiles();
}

bool MP3Player::updateListMusicFiles() {
  Serial.println("MP3Player::updateMusicFiles");
  String currentSection = listSections->getCurrent();

  Serial.println("CURRENT SECTION: ");
  Serial.println(currentSection);
  if(currentSection == "")
    return false;

  File root = SD.open(rootDir + "/" + currentSection);

  if (!root) {
    Serial.println("Failed to open directory");
    return false;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return false;
  }

  delete listMusicFiles;
  listMusicFiles = new MP3List(&root, ListMode::MUSIC_FILES);
  //  root.rewindDirectory();
  return true;
}