-
Notifications
You must be signed in to change notification settings - Fork 7
/
audio_pa.c
244 lines (189 loc) · 6.72 KB
/
audio_pa.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*****************************************************************************
* Gnome Wave Cleaner Version 0.19
* Copyright (C) 2003 Jeffrey J. Welty
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*******************************************************************************/
/* pulse audio interface jw Nov 14, 2010 */
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pulse/simple.h>
#include <pulse/stream.h>
#include <pulse/error.h>
#include <pulse/gccmacro.h>
#include "audio_device.h"
#include "gwc.h"
/* The Sample format to use */
static pa_sample_spec ss = {
.format = PA_SAMPLE_S16LE,
.rate = 44100,
.channels = 2
};
#define TOT_BUFSIZE 16384
static pa_simple *pa_device = NULL;
static long written_frames = 0;
static int framesize ;
double frames_per_usec ;
static int last_written_size ;
static int latency_flag ;
static void pa_perr(char *text, int err)
{
fprintf(stderr, "##########################################################\n");
fprintf(stderr, "%s\n", text);
fprintf(stderr, "err=%d, %s\n", err, pa_strerror(err));
warning(text) ;
}
int audio_device_open(char *output_device)
{
int err ;
// -- note, these are defaults, and will be changed
// with a subsequent call to audio_device_set_params
ss.format = PA_SAMPLE_S16LE ;
ss.rate = 44100 ;
ss.channels = 2 ;
//printf("Open the Pulse audio device\n") ;
pa_device = pa_simple_new(NULL,"GWC",PA_STREAM_PLAYBACK,NULL,"GWC Playback", &ss, NULL, NULL, &err) ;
if (pa_device == NULL) {
pa_perr("audio_device_open: pa_simple_new", err);
return -1;
}
written_frames = 0;
last_written_size = -1 ;
latency_flag = 1 ;
return 0;
}
pa_buffer_attr buffer_attributes ;
int audio_device_set_params(AUDIO_FORMAT *format, int *channels, int *rate)
{
int err ;
// Alister: is there any reason we shouldn't combine audio_device_set_params and audio_device_open?
// Why do they need to be two separate steps?
//printf("First close the Pulse audio device in case we are playing a mono file, as we don't seem to be able to switch\n") ;
audio_device_close(0) ;
ss.rate = *rate ;
ss.channels = *channels ;
switch (*format)
{
case GWC_U8: ss.format = PA_SAMPLE_U8; framesize=1 ; break;
case GWC_S8: ss.format = PA_SAMPLE_ALAW; framesize=1 ; break;
case GWC_S16_BE: ss.format = PA_SAMPLE_S16BE; framesize=2 ; break;
default:
case GWC_S16_LE: ss.format = PA_SAMPLE_S16LE; framesize=2 ; break;
}
//printf("Open the Pulse audio device again\n") ;
/* buffer_attributes.fragsize=-1 ; */
/* buffer_attributes.maxlength=-1 ; */
/* buffer_attributes.minreq=-1 ; */
/* buffer_attributes.prebuf=-1 ; */
/* buffer_attributes.tlength=-1 ; */
/* pa_device = pa_simple_new(NULL,"GWC",PA_STREAM_PLAYBACK,NULL,"GWC Playback", &ss, NULL, &buffer_attributes, &err) ; */
/* fprintf(stderr, "PA:maxlength,minreq,prebuf,tlength = %d,%d,%d,%d\n", */
/* buffer_attributes.maxlength, */
/* buffer_attributes.minreq, */
/* buffer_attributes.prebuf, */
/* buffer_attributes.tlength) ; */
pa_device = pa_simple_new(NULL,"GWC",PA_STREAM_PLAYBACK,NULL,"GWC Playback", &ss, NULL, NULL, &err) ;
if (pa_device == NULL) {
pa_perr("audio_device_open: pa_simple_new", err);
return -1;
}
written_frames = 0;
last_written_size = -1 ;
latency_flag = 1 ;
framesize *= *channels ;
frames_per_usec = (double)(*rate) / 1000000.0 ;
return 0;
}
int audio_device_read(unsigned char *buffer, int buffersize)
{
/* not implemented */
return -1;
}
int audio_device_write(unsigned char *data, int count)
{
int err ;
pa_simple_write(pa_device, data, (size_t) count, &err) ;
written_frames += count/framesize ;
return written_frames*framesize ;
}
/* Number of bytes processed since opening the device. */
long query_processed_bytes(void)
{
if(pa_device != NULL) {
int err ;
pa_usec_t latency = pa_simple_get_latency(pa_device, &err) ;
int bytes_unprocessed = (latency*ss.rate)/1000000 * framesize ;
if((written_frames) *framesize == last_written_size) {
latency_flag++ ;
if(latency_flag > 4) {
bytes_unprocessed = 0 ;
} else {
bytes_unprocessed /= latency_flag ;
}
}
last_written_size = (written_frames) *framesize ;
return (written_frames) *framesize - bytes_unprocessed ;
}
return 0 ;
}
long _audio_device_processed_bytes = 0 ;
/* Number of bytes processed since opening the device. */
long audio_device_processed_bytes(void)
{
if(pa_device != NULL)
_audio_device_processed_bytes = query_processed_bytes() ;
return _audio_device_processed_bytes ;
}
void audio_device_close(int drain)
{
if (pa_device != NULL) {
int err;
//printf("Closing the Pulse audio device\n") ;
_audio_device_processed_bytes = query_processed_bytes() ;
if(drain)
err = pa_simple_drain(pa_device, &err);
pa_simple_free(pa_device) ;
pa_device = NULL;
}
}
int audio_device_best_buffer_size(int playback_bytes_per_block)
{
return TOT_BUFSIZE ;
}
/* pa_buffer_attr *pa_stream_get_buffer_attr(pa_stream *) ; */
int audio_device_nonblocking_write_buffer_size(int maxbufsize,
int playback_bytes_remaining)
{
// try making the length of the buffer 10 frames longer than what best buffer size returned
// so the write may block for a few microseconds, but the buffer should
// stay full and the screen redraws be more or less done at the same rate.
int len = TOT_BUFSIZE+framesize*10 ;
if (len > maxbufsize)
len = maxbufsize;
if (len > playback_bytes_remaining)
len = playback_bytes_remaining;
/* printf("audio_device_nonblocking_write_buffer_size:%d\n", len); */
/* pa_buffer_attr *ptr ; */
/* ptr = pa_stream_get_buffer_attr(pa_device) ; */
/* */
/* fprintf(stderr, "PA:maxlength,minreq,prebuf,tlength = %d,%d,%d,%d\n", */
/* ptr->maxlength, */
/* ptr->minreq, */
/* ptr->prebuf, */
/* ptr->tlength) ; */
return len;
}