Browse Source

use round() in pcm volume gain

Signed-off-by: qshi <qshi@123456.com>
master
qshi 8 years ago
parent
commit
df3d9d01b5
  1. 167
      demo/recv_voice.c
  2. 1
      src/common/config.h
  3. 2
      src/common/pcm2alaw.c
  4. 2
      src/zkbrc.json

167
demo/recv_voice.c

@ -1,82 +1,85 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
#include<string.h> #include<string.h>
#include <signal.h> #include <signal.h>
#include<unistd.h> #include<unistd.h>
#include<sys/types.h> #include<sys/types.h>
#include<sys/socket.h> #include<sys/socket.h>
#include<netinet/in.h> #include<netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#define HOST_IF_IP "192.168.0.191" #define HOST_IF_IP "192.168.31.89"
#define GROUP_ADDR "225.0.0.5" #define GROUP_ADDR "225.0.0.5"
#define PORTRECV 8310 #define PORTRECV 8310
static int got_sig = 0; static int got_sig = 0;
static void sigterm(int nr) static void sigterm(int nr)
{ {
got_sig = 1; got_sig = 1;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
FILE *myfile; FILE *myfile;
int sock, count; int sock, count;
socklen_t slen; socklen_t slen;
struct sockaddr_in ser_addr; struct sockaddr_in ser_addr;
struct ip_mreq mreq; struct ip_mreq mreq;
int yes=1; int yes=1;
unsigned char buf[64] = {0}; unsigned char buf[64] = {0};
signal(SIGTERM, sigterm); signal(SIGINT, sigterm);
sock = socket(AF_INET,SOCK_DGRAM,0);
sock = socket(AF_INET,SOCK_DGRAM,0); printf("sockfd: %d\n",sock);
printf("sockfd: %d\n",sock); if (sock == -1) {
if (sock == -1) { printf("socket error");
printf("socket error"); exit(1);
exit(1); }
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0){
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0){ perror("Reusing ADDR failed");
perror("Reusing ADDR failed"); exit(1);
exit(1); }
}
memset(&ser_addr,0,sizeof(ser_addr));
memset(&ser_addr,0,sizeof(ser_addr)); ser_addr.sin_family = AF_INET;
ser_addr.sin_family = AF_INET; ser_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr(GROUP_ADDR);
ser_addr.sin_addr.s_addr = inet_addr(HOST_IF_IP); ser_addr.sin_port = htons(PORTRECV);
ser_addr.sin_port = htons(PORTRECV); if (bind(sock,(struct sockaddr *) &ser_addr,sizeof(ser_addr)) < 0){
if (bind(sock,(struct sockaddr *) &ser_addr,sizeof(ser_addr)) < 0){ perror("bind");
perror("bind"); exit(1);
exit(1); }
}
mreq.imr_multiaddr.s_addr = inet_addr(GROUP_ADDR);
mreq.imr_multiaddr.s_addr = inet_addr(GROUP_ADDR); mreq.imr_interface.s_addr = inet_addr(HOST_IF_IP);
mreq.imr_interface.s_addr = inet_addr(HOST_IF_IP); if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) {
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) { perror("setsock");
perror("setsock"); close(sock);
close(sock); exit(1);
exit(1); }
}
myfile = fopen("output.raw", "w"); myfile = fopen("output.raw", "w");
while (!got_sig) {
slen = sizeof ser_addr; while (!got_sig) {
memset(&ser_addr, 0, sizeof ser_addr); slen = sizeof ser_addr;
memset(buf, 0, sizeof buf); memset(&ser_addr, 0, sizeof ser_addr);
if ((count = recvfrom(sock, buf, sizeof buf, 0, (struct sockaddr *) &ser_addr,(socklen_t*)&slen)) < 0) { memset(buf, 0, sizeof buf);
perror("recvfrom"); if ((count = recvfrom(sock, buf, sizeof buf, 0, (struct sockaddr *) &ser_addr,(socklen_t*)&slen)) < 0) {
exit(1); perror("recvfrom");
} exit(1);
}
if (count == 64) { printf("recv data %d\n", count);
if (buf[0] == 0xac && buf[1] == 0x93 && buf[63] == 0x14) { if (count == 64) {
fwrite(buf + 16, 1, 6, myfile); if (buf[0] == 0xac && buf[1] == 0x93 && buf[63] == 0x14) {
} fwrite(buf + 16, 1, 6, myfile);
} }
} }
fclose(myfile); //fflush(myfile);
return 0; }
} printf("---------------\n");
fclose(myfile);
return 0;
}

1
src/common/config.h

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <math.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>

2
src/common/pcm2alaw.c

@ -160,7 +160,7 @@ int alaw2linear_buf_gain(unsigned char *lbuf, int llen, unsigned char *abuf, int
short *pcm16 = (short *)lbuf; short *pcm16 = (short *)lbuf;
for (m = 0; m < alen; m++) { for (m = 0; m < alen; m++) {
pcm32 = alaw2linear(abuf[m]) * gain; pcm32 = round(alaw2linear(abuf[m]) * gain);
if (pcm32 > 32767) if (pcm32 > 32767)
pcm32 = 32767; pcm32 = 32767;
if (pcm32 < -32768) if (pcm32 < -32768)

2
src/zkbrc.json

@ -23,5 +23,5 @@
"audio_local_play": 1, "audio_local_play": 1,
"down_audio_play": [0, 0, 0, 0], "down_audio_play": [0, 0, 0, 0],
"alarm_audio_play": 1, "alarm_audio_play": 1,
"play_volume": 9 "play_volume": 6
} }

Loading…
Cancel
Save