-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundclient.cpp
51 lines (41 loc) · 1012 Bytes
/
soundclient.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
#include "sound.hpp"
#include "common.hpp"
#include <vector>
#include <string>
#include <alsa/asoundlib.h>
using namespace std;
using namespace std::chrono;
int main(int argc, char** argv) {
if (argc <= 1 || argc > 3) {
cout << "soundclient <host ip> [<port>=3333]\n";
return 0;
}
const char* host_ip = argv[1];
int port = argc == 3 ? stoi(argv[2]) : 3333;
StreamingSocket ssock(Socket::get_client(host_ip, port));
if (ssock.sock_id == -1) {
cerr << "Failed to connect!\n";
return 0;
}
SoundPlayer player;
auto recv_callback = [&](string&& str) {
vector<uint32_t> a;
for (size_t i=0; i<str.size(); i+=4) {
uint32_t b0 = str[i];
uint32_t b1 = str[i+1];
uint32_t b2 = str[i+2];
uint32_t b3 = str[i+3];
uint32_t val = b0 + (b1 << 8) + (b2 << 16) + (b3 << 24);
a.push_back(val);
}
player.add(move(a));
};
thread t1([&]() {
ssock.receive(recv_callback);
});
t1.detach();
player.run();
cout << "Enjoy! Press enter to quit\n";
string w;
getline(cin, w);
}