-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_out.cpp
More file actions
234 lines (212 loc) · 5.3 KB
/
b_out.cpp
File metadata and controls
234 lines (212 loc) · 5.3 KB
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
/*
$Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#ifdef AU_OSS
#if defined(__linux__) || defined(__NetBSD__)
/* new with 1.2.0? Didn't need this under 1.1.64 */
#include <sys/ioctl.h>
#endif
#if !defined(__NetBSD__)
#include <sys/soundcard.h>
#else
#include <soundcard.h>
#endif
#endif
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "output.h"
#include "controls.h"
#define PRESUMED_FULLNESS 20
/* #define BB_SIZE (AUDIO_BUFFER_SIZE*128) */
/* #define BB_SIZE (AUDIO_BUFFER_SIZE*256) */
static unsigned char *bbuf = 0;
static int bboffset = 0, bbcount = 0;
static int outchunk = 0;
static int starting_up = 1, flushing = 0;
static int out_count = 0;
static int total_bytes = 0;
static int fragsize = 0;
static int fragstotal = 0;
#define WRITEDRIVER(fd,buf,cnt) play_mode->driver_output_data(buf,cnt)
int b_out_count()
{
return out_count;
}
void b_out(char id, int fd, int *buf, int ocount)
{
int ret;
uint32 ucount;
if (ocount < 0) {
out_count = bboffset = bbcount = outchunk = 0;
starting_up = 1;
flushing = 0;
output_buffer_full = PRESUMED_FULLNESS;
total_bytes = 0;
return;
}
ucount = (uint32)ocount;
if (!bbuf) {
bbcount = bboffset = 0;
bbuf = (unsigned char *)malloc(BB_SIZE);
if (!bbuf) {
fprintf(stderr, "malloc output error");
#ifdef ADAGIO
X_EXIT
#else
exit(1);
#endif
}
}
if (!total_bytes) {
#ifdef AU_OSS
#ifdef SNDCTL_DSP_GETOSPACE
audio_buf_info info;
if ( (id == 'd' || id == 'D') &&
(ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) != -1)) {
fragstotal = info.fragstotal;
fragsize = info.fragsize;
total_bytes = fragstotal * fragsize;
outchunk = fragsize;
}
else
#endif /* SNDCTL_DSP_GETOSPACE */
#endif
#ifdef AU_ALSA
if (id == 's') {
extern void alsa_tell(int *, int *);
alsa_tell(&fragsize, &fragstotal);
outchunk = fragsize;
total_bytes = fragstotal * fragsize;
}
else
#endif
total_bytes = AUDIO_BUFFER_SIZE*2;
}
if (ucount && !outchunk) outchunk = ucount;
if (starting_up && ucount + bboffset + bbcount >= BB_SIZE) starting_up = 0;
if (!ucount) { outchunk = starting_up = 0; flushing = 1; }
else flushing = 0;
if (starting_up || flushing) output_buffer_full = PRESUMED_FULLNESS;
else {
int samples_queued;
#ifdef AU_OSS
#ifdef SNDCTL_DSP_GETODELAY
if ( (id == 'd' || id == 'D') &&
(ioctl(fd, SNDCTL_DSP_GETODELAY, &samples_queued) == -1))
#endif
#endif
samples_queued = 0;
if (!samples_queued) output_buffer_full = PRESUMED_FULLNESS;
else output_buffer_full = ((bbcount+samples_queued) * 100) / (BB_SIZE + total_bytes);
/* fprintf(stderr," %d",output_buffer_full); */
}
ret = 0;
if (!starting_up)
while (bbcount) {
if (check_for_rc()) return;
if (outchunk && bbcount >= outchunk)
ret = WRITEDRIVER(fd, bbuf + bboffset, outchunk);
else if (flushing) {
if (fragsize) ret = bbcount;
else ret = WRITEDRIVER(fd, bbuf + bboffset, bbcount);
}
if (bboffset) {
memcpy(bbuf, bbuf + bboffset, bbcount);
bboffset = 0;
}
if (ret < 0) {
if (errno == EINTR) continue;
else if (errno == EWOULDBLOCK) {
/* if (bboffset) {
memcpy(bbuf, bbuf + bboffset, bbcount);
bboffset = 0;
} */
/* fprintf(stderr, "BLOCK %d ", bbcount); */
if (ucount && bboffset + bbcount + ucount <= BB_SIZE && !flushing) break;
ctl->refresh();
#ifdef HAVE_USLEEP
usleep(100);
#else
sleep(1);
#endif
}
else {
perror("error writing to dsp device");
#ifdef ADAGIO
X_EXIT
#else
exit(1);
#endif
}
}
else {
if (!ret && bboffset + bbcount + ucount <= BB_SIZE && !flushing) break;
if (!ret) usleep(250);
else {
out_count += ret;
bboffset += ret;
bbcount -= ret;
}
/* fprintf(stderr, "[%d:%d:f=%d/%d]\n", bbcount, ucount, output_buffer_full, BB_SIZE); */
if (!bbcount) bboffset = 0;
else if (bbcount < 0 || bboffset + bbcount > BB_SIZE) {
fprintf(stderr, "b_out.c:Uh oh.\n");
fprintf(stderr, "output error\n");
bbcount = bboffset = 0;
return;
}
if (bbcount < outchunk && !flushing) break;
}
}
if (!ucount) { flushing = 0; starting_up = 1; out_count = bbcount = bboffset = 0; return; }
memcpy(bbuf + bboffset + bbcount, buf, ucount);
bbcount += ucount;
#ifndef KMIDI
if (starting_up) return;
#endif
if (ret >= 0) while (bbcount) {
if (outchunk && bbcount >= outchunk)
ret = WRITEDRIVER(fd, bbuf + bboffset, outchunk);
/*else if (fragsize) ret = bbcount;*/
else if (fragsize) { ret = 0; break; }
else
ret = WRITEDRIVER(fd, bbuf + bboffset, bbcount);
ctl->refresh();
if (ret < 0) {
if (errno == EINTR) continue;
else if (errno == EWOULDBLOCK) {
/* fprintf(stderr, "block %d ", bbcount); */
break;
}
else {
perror("error writing to dsp device");
#ifdef ADAGIO
X_EXIT
#else
exit(1);
#endif
}
}
else {
if (!ret && bboffset + bbcount + ucount <= BB_SIZE && !flushing) break;
out_count += ret;
bboffset += ret;
bbcount -= ret;
/*fprintf(stderr, "{%d:%d:r=%d}\n", bbcount,ucount,ret); */
if (!bbcount) bboffset = 0;
else if (bbcount < 0 || bboffset + bbcount > BB_SIZE) {
fprintf(stderr, "b_out.c:output error\n");
bbcount = bboffset = 0;
return;
}
}
}
}
/* #endif */