-
Notifications
You must be signed in to change notification settings - Fork 4
/
audio.c
147 lines (124 loc) · 3.86 KB
/
audio.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
// $Id: audio.c,v 1.90 2018/12/24 05:24:47 karn Exp $
// Audio multicast routines for KA9Q SDR receiver
// Handles linear 16-bit PCM, mono and stereo
// Copyright 2017 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include "misc.h"
#include "multicast.h"
#include "radio.h"
#define PCM_BUFSIZE 480 // 16-bit word count; must fit in Ethernet MTU
#define PACKETSIZE 2048 // Somewhat larger than Ethernet MTU
static short const scaleclip(float const x){
if(x >= 1.0)
return SHRT_MAX;
else if(x <= -1.0)
return SHRT_MIN;
return (short)(SHRT_MAX * x);
}
// Send 'size' stereo samples, each in a pair of floats
int send_stereo_output(struct demod * const demod,float const * buffer,int size){
struct rtp_header rtp;
memset(&rtp,0,sizeof(rtp));
rtp.type = PCM_STEREO_PT; // 16 bit linear, big endian, stereo
rtp.version = RTP_VERS;
rtp.ssrc = demod->output.rtp.ssrc;
int16_t PCM_buf[PCM_BUFSIZE];
while(size > 0){
int not_silent = 0;
int chunk = min(PCM_BUFSIZE,2*size);
for(int i=0; i < chunk; i ++){
float samp = *buffer++;
PCM_buf[i] = htons(scaleclip(samp));
not_silent |= PCM_buf[i];
}
// If packet is all zeroes, don't send it but still increase the timestamp
rtp.timestamp = demod->output.rtp.timestamp;
demod->output.rtp.timestamp += chunk/2; // Increase by sample count
if(not_silent){
demod->output.rtp.bytes += sizeof(signed short) * chunk;
demod->output.rtp.packets++;
if(demod->output.silent){
demod->output.silent = 0;
rtp.marker = 1;
} else
rtp.marker = 0;
rtp.seq = demod->output.rtp.seq++;
unsigned char packet[PACKETSIZE],*dp;
dp = packet;
dp = hton_rtp(dp,&rtp);
memcpy(dp,PCM_buf,2*chunk);
dp += 2*chunk;
int r = send(demod->output.data_fd,&packet,dp - packet,0);
demod->output.samples += chunk/2; // Count stereo samples
if(r < 0){
perror("pcm: send");
break;
}
} else
demod->output.silent = 1;
size -= chunk/2;
}
return 0;
}
// Send 'size' mono samples, each in a float
int send_mono_output(struct demod * const demod,float const * buffer,int size){
struct rtp_header rtp;
memset(&rtp,0,sizeof(rtp));
rtp.version = RTP_VERS;
rtp.type = PCM_MONO_PT; // 16 bit linear, big endian, mono
rtp.ssrc = demod->output.rtp.ssrc;
int16_t PCM_buf[PCM_BUFSIZE];
while(size > 0){
int not_silent = 0;
int chunk = min(PCM_BUFSIZE,size); // # of mono samples (frames)
for(int i=0; i < chunk; i++){
float samp = *buffer++;
PCM_buf[i] = htons(scaleclip(samp));
not_silent |= PCM_buf[i];
}
// If packet is all zeroes, don't send it but still increase the timestamp
rtp.timestamp = demod->output.rtp.timestamp;
demod->output.rtp.timestamp += chunk; // Increase by sample count
if(not_silent){
// Don't send silence, but timestamp is still incremented
demod->output.rtp.packets++;
demod->output.rtp.bytes += sizeof(signed short) * chunk;
if(demod->output.silent){
demod->output.silent = 0;
rtp.marker = 1;
} else
rtp.marker = 0;
rtp.seq = demod->output.rtp.seq++;
unsigned char packet[PACKETSIZE];
unsigned char *dp = packet;
dp = hton_rtp(dp,&rtp);
memcpy(dp,PCM_buf,2*chunk);
dp += 2 * chunk;
int r = send(demod->output.data_fd,&packet,dp - packet,0);
demod->output.samples += chunk;
if(r < 0){
perror("pcm: send");
break;
}
} else
demod->output.silent = 1;
size -= chunk;
}
return 0;
}
void output_cleanup(void *p){
struct demod * const demod = p;
if(demod == NULL)
return;
if(demod->output.data_fd > 0){
close(demod->output.data_fd);
demod->output.data_fd = -1;
}
}