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

1
src/common/config.h

@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <unistd.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;
for (m = 0; m < alen; m++) {
pcm32 = alaw2linear(abuf[m]) * gain;
pcm32 = round(alaw2linear(abuf[m]) * gain);
if (pcm32 > 32767)
pcm32 = 32767;
if (pcm32 < -32768)

2
src/zkbrc.json

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

Loading…
Cancel
Save