forked from vladkorotnev/plasma-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
melodies.cpp
160 lines (140 loc) · 4.44 KB
/
melodies.cpp
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
#include <sound/sequencer.h>
#include <sound/melodies.h>
#include <service/localize.h>
#include <bits/stl_algobase.h>
#include <service/disk.h>
#include <vector>
#include <dirent.h>
#include <utils.h>
static char LOG_TAG[] = "MUDB";
static const melody_item_t just_beep_data[] = {
{FREQ_SET, 0, 1000},
{DELAY, 0, 250},
{FREQ_SET, 0, 0},
{DELAY, 0, 500},
};
static const melody_item_t pc98_pipo_data[] = {
{FREQ_SET, 0, 2000},
{DELAY, 0, 125},
{FREQ_SET, 0, 1000},
{DELAY, 0, 125},
};
StaticMelodySequence pc98_pipo(pc98_pipo_data, sizeof(pc98_pipo_data)/sizeof(melody_item_t), "PC-98 PiPo");
static const melody_item_t tulula_fvu_data[] = {
{FREQ_SET, 0, 392},
{DELAY, 0, 120},
{FREQ_SET, 0, 0},
{DELAY, 0, 5},
{FREQ_SET, 0, 392},
{DELAY, 0, 120},
{FREQ_SET, 0, 0},
{DELAY, 0, 5},
{FREQ_SET, 0, 392},
{DELAY, 0, 120},
{FREQ_SET, 0, 0},
{DELAY, 0, 5},
{FREQ_SET, 0, 293},
{DELAY, 0, 250},
};
StaticMelodySequence tulula_fvu(tulula_fvu_data, sizeof(tulula_fvu_data)/sizeof(melody_item_t), "Русь-28");
static const melody_item_t oelutz_fvu_data[] = {
{FREQ_SET, 0, 587},
{DELAY, 0, 250},
{FREQ_SET, 0, 698},
{DELAY, 0, 250},
{FREQ_SET, 0, 880},
{DELAY, 0, 250},
{FREQ_SET, 0, 1046},
{DELAY, 0, 125},
{FREQ_SET, 0, 988},
{DELAY, 0, 500},
};
StaticMelodySequence oelutz_fvu(oelutz_fvu_data, sizeof(oelutz_fvu_data)/sizeof(melody_item_t), "Штрих-М");
std::vector<MelodySequence *> all_chime_list = {
new StaticMelodySequence(just_beep_data, sizeof(just_beep_data)/sizeof(melody_item_t), "Beep"),
&pc98_pipo,
&tulula_fvu,
&oelutz_fvu
};
int all_chime_count = all_chime_list.size();
MelodySequence * melody_from_no(int melody_no) {
if(melody_no >= all_chime_list.size()) melody_no = 0;
return all_chime_list[melody_no];
}
class RandomMelodySequence: public MelodySequence {
public:
RandomMelodySequence(const std::vector<MelodySequence*> & list, size_t offs):
melodies { list },
offset { offs } {}
bool load() {
int no = 0;
static int last_random_no = -1;
do {
no = offset + (esp_random() % (all_chime_count - offset));
} while(no == last_random_no);
last_random_no = no;
choice = melodies[no];
ESP_LOGI(LOG_TAG, "Random choice %i: %s", no, choice->get_title());
if(choice != nullptr)
return choice->load();
return false;
}
void unload() {
if(choice != nullptr) {
choice->unload();
choice = nullptr;
}
}
const char * get_title() { return localized_string("(Randomize)"); }
const char * get_long_title() { return get_title(); }
const melody_item_t * get_array() {
if(choice != nullptr) return choice->get_array();
return nullptr;
}
int get_num_rows() {
if(choice != nullptr) return choice->get_num_rows();
return 0;
}
protected:
const std::vector<MelodySequence*>& melodies;
MelodySequence * choice = nullptr;
size_t offset;
};
#define MUSIC_DIR (FS_MOUNTPOINT "/music")
void load_melodies_from_disk() {
DIR *d;
struct dirent *dir;
d = opendir(MUSIC_DIR);
char pbuf[64] = { 0 };
if (d) {
while ((dir = readdir(d)) != NULL) {
if(dir->d_type == DT_REG && strlen(dir->d_name) > 5 && !strcmp(dir->d_name + strlen(dir->d_name) - 5, ".pomf")) {
ESP_LOGV(LOG_TAG, "Found music file: %s", dir->d_name);
snprintf(pbuf, 63, "%s/%s", MUSIC_DIR, dir->d_name);
auto seq = new PomfMelodySequence(pbuf);
if(seq->valid()) {
ESP_LOGV(LOG_TAG, "Title: %s", seq->get_title());
all_chime_list.push_back(seq);
} else {
ESP_LOGE(LOG_TAG, "File %s not valid", dir->d_name);
delete seq;
}
taskYIELD();
}
}
closedir(d);
}
all_chime_count = all_chime_list.size();
all_chime_list.insert(all_chime_list.begin(), new RandomMelodySequence(all_chime_list, 5));
// TODO: Get rid of those two vectors entirely
all_chime_names.clear();
for(int i = 0; i < all_chime_list.size(); i++) {
all_chime_names.push_back(all_chime_list[i]->get_title());
}
}
std::vector<const char *> all_chime_names = {
"Beep",
"PC-98 Boot",
"Русь 28",
"Штрих-M",
};