-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioServer.c
156 lines (127 loc) · 6.24 KB
/
AudioServer.c
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
// `.:/` `:+sysmmddhyso+/++oossssss-
// ./oooo//.` `.:/+osyyyyyssyyysssoo+/:/oydMh+.````
// `/ooo/.. -/osys+/:--.`` `/sdh/`
// `:ss+`` -+syo:`` .ody:
// `:ss/. -+yds:` `:yd/
// `/yy/` `+dds-` .sd:
// `+ho- :yho-` -hs`
// `+h+` /ds-` `om/
// `+my. /dh- -/o: -ds
// .dd: :hd: -++hho--.- ` `hh`
// /mo` .ym+` . `-:smNMm++ymNNNdNh` `yd`
// /d- :do` `:odhhmdmMMMMNNMMMMMMMMMs `yd`
// +m- `sd- -/ymMMMMMMMMMMMMMMdy//dMMMMM/ `ds
// :N/ -dd. ./smNMMMMMMMMMMMMMMNo-` .MMMMMN` -N.
// .mo :Nh` ./ymNMMMMMMMMMMMMMMNMMMMm: dMMMMMs ss
// hy /Ny` `:odNMMMMMMMMMMMMMMMmy:-NMMMMNo` -NMMMMd :N`
// /N` :Nh` ./ymMMMMMMMMMMMMMMMMMy:` +MMMMMMd -mMMMd .Mo
// ds -NN. `-/yhmMMMMMNMMMMMMMMMMMMMMm- oMMMMMM- -NMMs `Mo
// :M. `dM+ `.-/sdNMMMMMMmddNMMMMMMMMMMMMMMMN+ +NMMNd` -/-` M/
// yd sMd `-/sdmmNMMMMMMMMMMmMMMMMMs:dMMMMMMMMMMMo ./:-` `M:
// N/ `NM- -/ydNMMMMMMMMMMMMMMMMNNMMMMM` :MMMMMMMMMMMs /M`
// -M` oMM .+hNMMMMMMMMMMMMMMMMMmNN--MMMMMo dMMMMMMMNmy` yh
// oM hMN .+hNMMMMMMMMMMMMMmNMMMMM++m hMMMMN. .dMMMMMM/. N/
// ym dMM:yNMMMMMMNNMMMMMMMy..dMMMMM/M` -NMMMMy `dMMMMMd` +d`
// yh dMMMMMMMMNh/.-NMMMMMMy .NMMMMyN: :dNMm+ :MMMMMMo -m.
// yy :MMMMMMmmNy /MMMMMMM` :MMMMNN+ /N` NMMMMMM: :d:
// oy oNNMMM+. oh sMMMMMM/ omMy-oy h+ `dMMMMMMm .yh.
// +y .:sNMd- `m- `NmdNNNy `d+ :N /N.:+ssmMMMMMMM/ `oms`
// -N :dMNo`/d /d ../N` +d `M.`.:+yMm/-./dMMMMMMMm `+do.
// N. /dMm+ho `N- m+ -M/oMds+/--ymoys/-mMMMMMMM/ ./sho.
// y+ `/mMNN. ys sd`./oyMd/md/+osssMo` oMMMMMMMy `.+yh+-
// :d `oNMd- /m .yMhs/--hNhmM/-` dy :MMMMMMMm `.+yy/.
// d. .yMNs. +MoyyyM/`/yhoN+-M. /N` MMMMMMMM` `-ohy/`
// /y :yMNhMN+` +Mdh+. yy N/ N/ dMMMMMMMs` ``/oys:`
// y- /sNMs:oyNMs -M`sy oh yMMMMMMMM/:ohdo-
// `/ `yNNMM+`No d+.d .M. /MMMMMMMMds/`
// ` `/hMdNh /m hs .MMMMMMM/
// .+dMms:. /` .o ./dMMM+mMm`
// `/shNmhysoo++++osyhdhy+::+./my.
// `.-://++//:-.`
// Audio TCP Server For the Dafang running modded firmware
// For NurdSpace
// By Melan - 2020
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <imp/imp_audio.h>
#include <imp/imp_log.h>
// For socket server
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#define AEC_SAMPLE_RATE 8000
#define AEC_SAMPLE_TIME 10
#define IMP_AUDIO_BUF_SIZE (5 * (AEC_SAMPLE_RATE * sizeof(short) * AEC_SAMPLE_TIME / 1000))
int main(int argc, char *argv[])
{
int chnVol = 50;
int hSocket;
int ret;
if (argc > 1 && argv[1] != NULL) chnVol = atoi(argv[1]);
// Setup socket
hSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server = {0};
server.sin_family = AF_INET;
server.sin_port = htons(9000);
server.sin_addr.s_addr = INADDR_ANY;
// Bind
bind(hSocket, (struct sockaddr*) &server, sizeof(server));
listen(hSocket, 5);
/* Step 1: set public attribute of AO device. */
int devID = 0;
IMPAudioIOAttr attr;
attr.samplerate = AUDIO_SAMPLE_RATE_8000;
attr.bitwidth = AUDIO_BIT_WIDTH_16;
attr.soundmode = AUDIO_SOUND_MODE_MONO;
attr.frmNum = 20;
attr.numPerFrm = 400;
attr.chnCnt = 1;
ret = IMP_AO_SetPubAttr(devID, &attr);
if(ret != 0) fprintf(stderr, "set ao %d attr err: %d\n", devID, ret); return NULL;
memset(&attr, 0x0, sizeof(attr));
ret = IMP_AO_GetPubAttr(devID, &attr);
if(ret != 0) fprintf(stderr, "get ao %d attr err: %d\n", devID, ret); return NULL;
/* Step 2: enable AO device. */
ret = IMP_AO_Enable(devID);
if(ret != 0) fprintf(stderr, "enable ao %d err\n", devID); return NULL;
/* Step 3: enable AI channel. */
int chnID = 0;
ret = IMP_AO_EnableChn(devID, chnID);
if(ret != 0) fprintf(stderr, "Audio play enable channel failed\n"); return NULL;
/* Step 4: Set audio channel volume. */
ret = IMP_AO_SetVol(devID, chnID, chnVol);
if(ret != 0) fprintf(stderr, "Audio Play set volume failed\n"); return NULL;
ret = IMP_AO_GetVol(devID, chnID, &chnVol);
if(ret != 0) fprintf(stderr, "Audio Play get volume failed\n"); return NULL;
fprintf(stderr, "Audio Out GetVol vol:%d\n", chnVol);
ret = IMP_AO_EnableHpf(&attr);
if(ret != 0) fprintf(stderr, "enable audio hpf error.\n"); return NULL;
// Listen for new clients
char buf[IMP_AUDIO_BUF_SIZE];
while (1) {
int client;
client = accept(hSocket, NULL, NULL);
fprintf(stderr, "Client connected\n");
//read data into buffer
while(recv(client, buf, sizeof(buf), MSG_PEEK) != 0) {
int rc = read(client, buf, sizeof(buf));
// Play audio?
if (rc != 0) {
/* Step 5: send frame data. */
IMPAudioFrame frm;
frm.virAddr = (uint32_t *)buf;
frm.len = rc;
IMP_AO_SendFrame(devID, chnID, &frm, BLOCK);
}
}
fprintf(stderr, "Disconnecting client.\n");
close(client);
}
close(hSocket);
return 0;
}