File: devaudio-sun.c

package info (click to toggle)
drawterm-9front 0~git20220608.bee4db6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,256 kB
  • sloc: ansic: 57,635; makefile: 607; java: 492; objc: 436; xml: 174; asm: 19
file content (268 lines) | stat: -rw-r--r-- 4,791 bytes parent folder | download | duplicates (4)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Sun
 */
#include <sys/ioctl.h>
#include <sys/audio.h>
#include	"u.h"
#include	"lib.h"
#include	"dat.h"
#include	"fns.h"
#include	"error.h"
#include	"devaudio.h"

enum
{
	Channels = 2,
	Rate = 44100,
	Bits = 16,
};

static char* afn = 0;
static char* cfn = 0;
static int afd = -1;
static int cfd = -1;
static int speed = Rate;
static int needswap = -1;

static void
audiodevinit(void)
{
	uchar *p;
	ushort leorder;

	if ((afn = getenv("AUDIODEV")) == nil)
		afn = "/dev/audio";
	cfn = (char*)malloc(strlen(afn) + 3 + 1);
	if(cfn == nil)
		panic("out of memory");
	strcpy(cfn, afn);
	strcat(cfn, "ctl");

	/*
	 * Plan 9 /dev/audio is always little endian;
	 * solaris /dev/audio seems to expect native byte order,
	 * so on big endian machine (like sparc) we have to swap.
	 */
	leorder = (ushort) 0x0100;
	p = (uchar*)&leorder;
	if (p[0] == 0 && p[1] == 1) {
		/* little-endian: nothing to do */
		needswap = 0;
	} else {
		/* big-endian: translate Plan 9 little-endian */
		needswap = 1;
	}
}

/* maybe this should return -1 instead of sysfatal */
void
audiodevopen(void)
{
	audio_info_t info;
	struct audio_device ad;

	if (afn == nil || cfn == nil)
		audiodevinit();
	if((afd = open(afn, O_WRONLY)) < 0)
		goto err;
	if(cfd < 0 && (cfd = open(cfn, O_RDWR)) < 0)
		goto err;

	AUDIO_INITINFO(&info);
	info.play.precision = Bits;
	info.play.channels = Channels;
	info.play.sample_rate = speed;
	info.play.encoding = AUDIO_ENCODING_LINEAR;
	if(ioctl(afd, AUDIO_SETINFO, &info) < 0)
		goto err;

	return;

err:
	if(afd >= 0)
		close(afd);
	afd = -1;
	if(cfd >= 0)
		close(cfd);
	cfd = -1;
	oserror();
}

void
audiodevclose(void)
{
	if(afd >= 0)
		close(afd);
	if(cfd >= 0)
		close(cfd);
	afd = -1;
	cfd = -1;
}

static double
fromsun(double val, double min, double max)
{
	return (val-min) / (max-min);
}

static double
tosun(double val, double min, double max)
{
	return val*(max-min) + min;
}

static void
setvolbal(double left, double right)
{
	audio_info_t info;
	double vol, bal;

	if (left < 0 || right < 0) {
		/* should not happen */
		return;
	} else if (left == right) {
		vol = tosun(left/100.0, AUDIO_MIN_GAIN, AUDIO_MAX_GAIN);
		bal = AUDIO_MID_BALANCE;
	} else if (left < right) {
		vol = tosun(right/100.0, AUDIO_MIN_GAIN, AUDIO_MAX_GAIN);
		bal = tosun(1.0 - left/right, AUDIO_MID_BALANCE, AUDIO_RIGHT_BALANCE);
	} else if (right < left) {
		vol = tosun(left/100.0, AUDIO_MIN_GAIN, AUDIO_MAX_GAIN);
		bal = tosun(1.0 - right/left, AUDIO_MID_BALANCE, AUDIO_LEFT_BALANCE);
	}
	AUDIO_INITINFO(&info);
	info.play.gain = (long)(vol+0.5);
	info.play.balance = (long)(bal+0.5);
	if(ioctl(cfd, AUDIO_SETINFO, &info) < 0)
		oserror();
}

static void
getvolbal(int *left, int *right)
{
	audio_info_t info;
	double gain, bal, vol, l, r;

	AUDIO_INITINFO(&info);
	if (ioctl(cfd, AUDIO_GETINFO, &info) < 0)
		oserror();

	gain = info.play.gain;
	bal = info.play.balance;
	vol = fromsun(gain, AUDIO_MIN_GAIN, AUDIO_MAX_GAIN) * 100.0;

	if (bal == AUDIO_MID_BALANCE) {
		l = r = vol;
	} else if (bal < AUDIO_MID_BALANCE) {
		l = vol;
		r = vol * (1.0 - fromsun(bal, AUDIO_MID_BALANCE, AUDIO_LEFT_BALANCE));
	} else {
		r = vol;
		l = vol * (1.0 - fromsun(bal, AUDIO_MID_BALANCE, AUDIO_RIGHT_BALANCE));
	}
	*left = (long)(l+0.5);
	*right = (long)(r+0.5);
	return;
}

void
audiodevsetvol(int what, int left, int right)
{
	audio_info_t info;
	ulong x;
	int l, r;
	
	if (afn == nil || cfn == nil)
		audiodevinit();
	if(cfd < 0 && (cfd = open(cfn, O_RDWR)) < 0) {
		cfd = -1;
		oserror();
	}

	if(what == Vspeed){
		x = left;
		AUDIO_INITINFO(&info);
		info.play.sample_rate = x;
		if(ioctl(cfd, AUDIO_SETINFO, &info) < 0)
			oserror();
		speed = x;
		return;
	}
	if(what == Vaudio){
		getvolbal(&l, &r);
		if (left < 0)
			setvolbal(l, right);
		else if (right < 0)
			setvolbal(left, r);
		else 
			setvolbal(left, right);
		return;
	}
}

void
audiodevgetvol(int what, int *left, int *right)
{
	audio_info_t info;

	if (afn == nil || cfn == nil)
		audiodevinit();
	if(cfd < 0 && (cfd = open(cfn, O_RDWR)) < 0) {
		cfd = -1;
		oserror();
	}
	switch(what) {
	case Vspeed:
		*left = *right = speed;
		break;
	case Vaudio:
		getvolbal(left, right);
		break;
	case Vtreb:
	case Vbass:
		*left = *right = 50;
		break;
	default:
		*left = *right = 0;
	}
}


static uchar *buf = 0;
static int nbuf = 0;

int
audiodevwrite(void *v, int n)
{
	int i, m, tot;
	uchar *p;

	if (needswap) {
		if (nbuf < n) {
			buf = (uchar*)erealloc(buf, n);
			if(buf == nil)
				panic("out of memory");
			nbuf = n;
		}

		p = (uchar*)v;
		for(i=0; i+1<n; i+=2) {
			buf[i] = p[i+1];
			buf[i+1] = p[i];
		}
		p = buf;
	} else
		p = (uchar*)v;
	
	for(tot=0; tot<n; tot+=m)
		if((m = write(afd, p+tot, n-tot)) <= 0)
			oserror();
	return tot;
}

int
audiodevread(void *v, int n)
{
	error("no reading");
	return -1;
}