Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playlist improvements #269

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 87 additions & 124 deletions src/AudioPlayer.cpp

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/AudioPlayer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "Playlist.h"

typedef struct { // Bit field
uint8_t playMode : 4; // playMode
char **playlist; // playlist
char title[255]; // current title
bool repeatCurrentTrack : 1; // If current track should be looped
bool repeatPlaylist : 1; // If whole playlist should be looped
Expand Down Expand Up @@ -58,3 +59,5 @@ time_t AudioPlayer_GetPlayTimeSinceStart(void);
time_t AudioPlayer_GetPlayTimeAllTime(void);
uint32_t AudioPlayer_GetCurrentTime(void);
uint32_t AudioPlayer_GetFileDuration(void);

const String AudioPlayer_getCurrentTrackPath(size_t track);
10 changes: 10 additions & 0 deletions src/Common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <FS.h>

// FilePathLength
#define MAX_FILEPATH_LENTGH 256

Expand All @@ -22,6 +24,14 @@ inline bool isNumber(const char *str) {
}
}

inline const char *getPath(File &f) {
if constexpr (ESP_ARDUINO_VERSION_MAJOR >= 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obsolete, we are on Arduino >=2

return f.path();
} else {
return f.name();
}
}

// Checks if string starts with prefix
// Returns true if so
inline bool startsWith(const char *str, const char *pre) {
Expand Down
157 changes: 157 additions & 0 deletions src/Playlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#pragma once

#include "cpp.h"

#include <WString.h>
#include <algorithm>
#include <string.h>

using sortFunc = int (*)(const void *, const void *);

class Playlist {
public:
Playlist() { }
virtual ~Playlist() { }

virtual size_t size() const = 0;

virtual bool isValid() const = 0;

virtual const String getAbsolutePath(size_t idx) const = 0;

virtual const String getFilename(size_t idx) const = 0;

static int alphabeticSort(const void *x, const void *y) {
const char *a = static_cast<const char *>(x);
const char *b = static_cast<const char *>(y);

return strcmp(a, b);
}

virtual void sort(sortFunc func = alphabeticSort) { }

virtual void randomize() { }

// Check if file-type is correct
static bool fileValid(const String _fileItem) {
constexpr size_t maxExtLen = strlen(*std::max_element(audioFileSufix.begin(), audioFileSufix.end(), [](const char *a, const char *b) {
return strlen(a) < strlen(b);
}));

if (!_fileItem) {
return false;
}

// check for http address
if (_fileItem.startsWith("http://") || _fileItem.startsWith("https://")) {
return true;
}

// Ignore hidden files starting with a '.'
// lastIndex is -1 if '/' is not found --> first index will be 0
int fileNameIndex = _fileItem.lastIndexOf('/') + 1;
if (_fileItem[fileNameIndex] == '.') {
return false;
}

String extBuf;
const size_t extStart = _fileItem.lastIndexOf('.');
const size_t extLen = _fileItem.length() - extStart;
if (extLen > maxExtLen) {
// we either did not find a . or extension was too long
return false;
}
extBuf = _fileItem.substring(extStart);
extBuf.toLowerCase();

for (const auto e : audioFileSufix) {
if (extBuf.equals(e)) {
return true;
}
}
return false;
}

protected:
template <typename T>
class PsramAllocator {
public:
typedef T value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

public:
template <typename U>
struct rebind {
typedef PsramAllocator<U> other;
};

public:
inline explicit PsramAllocator() { }
inline ~PsramAllocator() { }
inline PsramAllocator(PsramAllocator const &) { }
template <typename U>
inline explicit PsramAllocator(PsramAllocator<U> const &) { }

// address
inline pointer address(reference r) { return &r; }

inline const_pointer address(const_reference r) { return &r; }

// memory allocation
inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) {
T *ptr = nullptr;
if (psramFound()) {
ptr = (T *) ps_malloc(cnt * sizeof(T));
} else {
ptr = (T *) malloc(cnt * sizeof(T));
}
return ptr;
}

inline void deallocate(pointer p, size_type cnt) {
free(p);
}

// size
inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}

// construction/destruction
inline void construct(pointer p, const T &t) {
new (p) T(t);
}

inline void destroy(pointer p) {
p->~T();
}

inline bool operator==(PsramAllocator const &a) { return this == &a; }
inline bool operator!=(PsramAllocator const &a) { return !operator==(a); }
};

using pstring = std::basic_string<char, std::char_traits<char>, PsramAllocator<char>>;

virtual void destroy() { }

// clang-format off
static constexpr auto audioFileSufix = std::to_array<const char*>({
".mp3",
".aac",
".m4a",
".wav",
".flac",
".aac",
// playlists
".m3u",
".m3u8",
".pls",
".asx"
});
// clang-format on
};
7 changes: 0 additions & 7 deletions src/Queues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Rfid.h"

QueueHandle_t gVolumeQueue;
QueueHandle_t gTrackQueue;
QueueHandle_t gTrackControlQueue;
QueueHandle_t gRfidCardQueue;

Expand All @@ -25,10 +24,4 @@ void Queues_Init(void) {
if (gTrackControlQueue == NULL) {
Log_Println(unableToCreateMgmtQ, LOGLEVEL_ERROR);
}

char **playlistArray;
gTrackQueue = xQueueCreate(1, sizeof(playlistArray));
if (gTrackQueue == NULL) {
Log_Println(unableToCreatePlayQ, LOGLEVEL_ERROR);
}
}
1 change: 0 additions & 1 deletion src/Queues.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

extern QueueHandle_t gVolumeQueue;
extern QueueHandle_t gTrackQueue;
extern QueueHandle_t gTrackControlQueue;
extern QueueHandle_t gRfidCardQueue;

Expand Down
Loading