-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.cpp
57 lines (52 loc) · 1.44 KB
/
music.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
#include "music.h"
unsigned long long getMusicStrData(char* str)
{
unsigned long long data = 0;
for (int i = 0; i < MUSICSTRLEN; i++) {
data |= (unsigned long long)(str[i] - 'a') << (i * 5);
}
return data;
}
char* getMusicStr(std::set<unsigned long long> musicSet, std::random_device& randSeed)
{
unsigned long long data = 0;
char* p = (char*)malloc(sizeof(char) * (MUSICSTRLEN + 1));
while (p == NULL) {
p = (char*)malloc(sizeof(char) * (MUSICSTRLEN + 1));
}
while (data == (unsigned long long)0) {
for (int i = 0; i < MUSICSTRLEN; i++) {
p[i] = randSeed() % 26 + 'a';
}
data = getMusicStrData(p);
if (musicSet.find(data) == musicSet.end()) {
musicSet.insert(data);
break;
}
data = (unsigned long long)0;
}
p[MUSICSTRLEN] = 0;
return p;
}
void playOnce(const char* musicPos, std::set<unsigned long long> musicSet)
{
std::random_device randSeed;
WCHAR text[MAXLEN] = { 0 };
char* musicStr = getMusicStr(musicSet, randSeed);
swprintf(text, MAXLEN, L"%hs%hs%hs%hs", "open ", musicPos, " alias ", musicStr);
std::cout << musicStr << std::endl;
mciSendString(text, NULL, 0, NULL);
swprintf(text, MAXLEN, L"%hs%hs", "play ", musicStr);
mciSendString(text, NULL, 0, NULL);
musicSet.erase(getMusicStrData(musicStr));
free(musicStr);
return;
}
void playLoop(const char* musicPos)
{
WCHAR text[MAXLEN] = { 0 };
swprintf(text, MAXLEN, L"%hs%hs", "play ", musicPos);
while (true) {
mciSendString(text, NULL, 0, NULL);
}
}