File: sun_dac.C

package info (click to toggle)
mixviews 1.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 6,314
  • sloc: cpp: 31,647; ansic: 2,100; makefile: 1,782; sh: 17
file content (429 lines) | stat: -rw-r--r-- 11,874 bytes parent folder | download
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// sun_dac.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <sys/stat.h>
#include <fcntl.h>
#include <stropts.h>
#include "application.h"
#include "typeconvert.h"
#include "sun_audio.h"
#include "sun_dac.h"
#include "statusaction.h"

#ifndef AUDIO_LINE_IN
#define AUDIO_LINE_IN (0x02)
#endif
#ifndef AUDIO_LINE_OUT
#define AUDIO_LINE_OUT (0x04)
#endif

#define PRINT(x, y) fprintf(stderr, x, y)

extern "C" int poll(struct pollfd*, unsigned long, int);

int SunConverter::sparc_version = 1;
struct pollfd SunConverter::pollStruct;

SunConverter::SunConverter() : ConverterDevice("/dev/audio") {
	BUG("SunConverter::SunConverter()");
	initialize();
	failIf(!confirmDeviceType());
}

SunConverter::~SunConverter() {
	BUG("SunConverter::~SunConverter()");
	stop();
}

boolean
SunConverter::isPlayableFormat(DataType type) {
	if(isSparc10())
		return (type == ShortData || type == MuLawData);
	else
		return (type == MuLawData);
}

DataType
SunConverter::bestPlayableType() {
	return isSparc10() ? ShortData : MuLawData;
}

unsigned
SunConverter::sampleEncoding() {
	unsigned encoding;
	DataType type = dataType();
	switch (type) {
	case MuLawData:
		encoding = AUDIO_ENCODING_ULAW; break;
//	case ALawData:
//		encoding = AUDIO_ENCODING_ALAW; break;
	case SignedCharData:
	case ShortData:
		encoding = AUDIO_ENCODING_LINEAR; break;
	case FloatData:
		encoding = AUDIO_ENCODING_FLOAT; break;
	default:
		encoding = 0; break;
	};
	return encoding;
}

int
SunConverter::pause() {
	BUG("SunConverter::pause()");
	int ret = true;
	if(!paused()) {
		ret = willPlay() ? audio_pause_play(fdesc())
			: willRecord() ? audio_pause_record(fdesc()) : -1;
		ret = (ret == AUDIO_SUCCESS && Super::pause());
		if(!ret)
			error("Can't pause audio device.");
	}
	return ret;
}

int
SunConverter::resume() {
	BUG("SunConverter::resume()");
	int ret = true;
	if(paused()) {
		ret = willPlay() ? audio_resume_play(fdesc())
			: willRecord() ? audio_resume_record(fdesc()) : -1;
		ret = (ret == AUDIO_SUCCESS && Super::resume());
		if(!ret) error("Can't unpause audio device.");
	}
	return ret;
}

int
SunConverter::stop() {
	BUG("SunConverter::stop()");
	int status = true;
	if(running()) {
		if(audio_flush(fdesc()) != AUDIO_SUCCESS) {
			error("Can't flush audio device.");
			status = false;
		}
		status = Super::stop() && status;
	}
	return status;
}

int
SunConverter::doConfigure() {
	BUG("SunConverter::doConfigure()");
	Audio_hdr	Dev_hdr;		// audio header for device
	Audio_hdr	Sound_hdr;		// audio header for sound
	
	// fill in values from sound to be played/recorded
	Sound_hdr.sample_rate = sampleRate();
	Sound_hdr.samples_per_unit = 1;
	Sound_hdr.bytes_per_unit = type_to_sampsize(dataType());
	Sound_hdr.channels = channels();
	Sound_hdr.encoding = sampleEncoding();
	Sound_hdr.data_size = dataSize();
	
	boolean status = true;
	// device is opened with correct mode here, not closed again until stop()
	status = reOpen();
	int afd = fdesc();
	// wait for old output to drain, if any
	if(willPlay() && audio_drain(afd, 0) != AUDIO_SUCCESS)
		return false;
	status = pause() && getConfiguration(&Dev_hdr);
	if(status) {
		// reconfigure device if device settings do not match sound
		if(audio_cmp_hdr(&Dev_hdr, &Sound_hdr) != 0)
			status = reconfigure(&Dev_hdr, &Sound_hdr);
	}
	if(status && willRecord()) {
		// flush input queue
		if(audio_flush_record(afd) != AUDIO_SUCCESS) {
			error("Converter::configure:  Unable to flush input queue.");
			status = false;
		}	// Set the device up for non-blocking reads
		else if(fcntl(afd, F_SETFL, fcntl(afd, F_GETFL, 0) | O_NDELAY) < 0) {
			error("Converter::configure: fcntl() failed.");
			status = false;
		}
	}
	pollStruct.fd = afd;	// initialize polling struct
	pollStruct.events = willRecord() ? POLLIN : POLLOUT;
#if 0
	if(status && willRecord()) {
		double gain = 0;
		audio_get_record_gain(fdesc(), &gain);
		PRINT("At end of configuration: device rec level:  %f\n\n", gain);
	}
#endif
	return status && resume();		// device unpaused at last instant
}

boolean
SunConverter::getConfiguration(Audio_hdr *devhdr) {
	BUG("SunConverter::getConfiguration()");
	// Get the device output encoding configuration
	boolean status = true;
	if(willPlay()) {
		if(audio_get_play_config(fdesc(), devhdr) != AUDIO_SUCCESS) {
			error("The device is not an audio device.");
			status = false;
		}
	}
	else if(willRecord()) {
		if(audio_get_record_config(fdesc(), devhdr) != AUDIO_SUCCESS) {
			error("The device is not an audio device.");
			status = false;
		}
	}
	return status;
}

boolean
SunConverter::reconfigure(Audio_hdr *devhdr, Audio_hdr *soundhdr) {
	BUG("SunConverter::reconfigure()");
	char msg[AUDIO_MAX_ENCODE_INFO+64];
	char encoding[AUDIO_MAX_ENCODE_INFO];
	char* action = willRecord() ? "record" : "play";
	*devhdr = *soundhdr;
	int retval = willPlay() ?
		audio_set_play_config(fdesc(), devhdr)
		: audio_set_record_config(fdesc(), devhdr);
	static const double SampRateThreshold = 0.05;
	switch (retval) {
	case AUDIO_SUCCESS:
		break;
	case AUDIO_ERR_NOEFFECT:
		// Couldn't change the device.
		// Check to see if we're nearly compatible.
		// audio_cmp_hdr() returns >0 if only sample rate difference.
		if(audio_cmp_hdr(devhdr, soundhdr) == 1) {
			double ratio = double(abs(
				int(devhdr->sample_rate - soundhdr->sample_rate))) /
				double(soundhdr->sample_rate);
			if (ratio <= SampRateThreshold) {
				sprintf(msg, "Samp rate was %d, %sing at %d",
					soundhdr->sample_rate, action, devhdr->sample_rate);
				Application::inform(msg, true);
			}
			else {
				sprintf(msg, "Cannot %s at sample rate %d.", action,
					soundhdr->sample_rate);
				Application::alert(msg);
				return false;
			}
		}
		else {
			(void) audio_enc_to_str(soundhdr, encoding);
			sprintf(msg, "Cannot %s sound in format:", action);
			Application::alert(msg, encoding);
			return false;
		}
		break;	/*NOTREACHED*/
	case AUDIO_ERR_ENCODING:
		(void) audio_enc_to_str(soundhdr, encoding);
		sprintf(msg, "Cannot %s sound in format:", action);
		Application::alert(msg, encoding);
		return false;
		break;	/*NOTREACHED*/
	case AUDIO_UNIXERROR:
		if(isSparc10()) {	// Sparc 10 generates this instead of previous
			(void) audio_enc_to_str(soundhdr, encoding);
			sprintf(msg, "Cannot %s sound in format:", action);
			error(msg, encoding);
		}
		else
			error("audio configuration failed");
		return false;
		break;	/*NOTREACHED*/
	default:
		(void) audio_enc_to_str(soundhdr, encoding);
		Application::alert("audio configuration failed for format:", encoding);
		return false;
		break;	/*NOTREACHED*/
	}
	return true;
}

// Write EOF to device, then loop to check for play completion

int
SunConverter::waitForStop(StatusAction* askedToStop) {
	BUG("SunConverter::waitForStop()");
	if(audio_play_eof(fdesc()) != AUDIO_SUCCESS)
		return error("Unable to write EOF to device");
	unsigned EOF_count = 0;
	do {
		usleep(50000);	// sleep .05 secs
		if((*askedToStop)())
			break;
		if(audio_get_play_eof(fdesc(), &EOF_count) != AUDIO_SUCCESS)
			return error("waitForStop: audio_get_play_eof failed");
	} while (EOF_count == 0);
	return true;
}

SunConverter::InputPort
SunConverter::currentInputPort() {
	unsigned audio_port = 0;
	if(audio_get_record_port(fdesc(), &audio_port) != AUDIO_SUCCESS)
		error("Unable to retrieve input port setting.");
	return (audio_port & AUDIO_MICROPHONE) ? Mike : Line;
}

boolean
SunConverter::setInputPort(InputPort port) {
	unsigned audio_port = (port == Mike) ? AUDIO_MICROPHONE : AUDIO_LINE_IN;
	if(audio_set_record_port(fdesc(), &audio_port) != AUDIO_SUCCESS)
		return error("Can't set audio input port!");
	return true;
}

SunConverter::OutputPort
SunConverter::currentOutputPort() {
	unsigned audio_port = 0;
	if(audio_get_play_port(fdesc(), &audio_port) != AUDIO_SUCCESS)
		error("Unable to retrieve output port setting.");
	return (audio_port & AUDIO_SPEAKER) ? Speaker : Headphone;
}


boolean
SunConverter::setOutputPort(OutputPort port) {
	unsigned audio_port = (port == Speaker) ? AUDIO_SPEAKER : AUDIO_HEADPHONE;
	// enable line output at all times when hardware permits
	if(isSparc20()) audio_port |= AUDIO_LINE_OUT;
	if(audio_set_play_port(fdesc(), &audio_port) != AUDIO_SUCCESS)
		return error("Can't set audio output port!");
	return true;
}

int
SunConverter::currentRecordLevel() const {
	double gain = 0;
	if(audio_get_record_gain(fdesc(), &gain) != AUDIO_SUCCESS)
		error("Unable to retrieve record gain level.");
#ifdef debug
	PRINT("Retrieved rec. level from device:  %f\n\n", gain);
#endif
	return int(gain * 100.0);
}

boolean
SunConverter::setRecordLevel(int volume) {
	double gain = volume / 100.0;
#ifdef debug
	PRINT("Setting device record level to %f\n", gain);
#endif
	if(audio_set_record_gain(fdesc(), &gain) != AUDIO_SUCCESS)
		return error("Unable to set converter record level.");
#ifdef debug
	audio_get_record_gain(fdesc(), &gain);
	PRINT("Retrieving just-set level for check:  %f\n\n", gain);
#endif
	return true;
}

int
SunConverter::currentPlayLevel() const {
	double gain = 0;
	if(audio_get_play_gain(fdesc(), &gain) != AUDIO_SUCCESS)
		error("Unable to retrieve playback gain level.");
#ifdef debug
	PRINT("Retrieved play level from device:  %f\n\n", gain);
#endif
	return round(gain * 100.0);
}

boolean
SunConverter::setPlayLevel(int volume) {
	double gain = volume / 100.0;
#ifdef debug
	PRINT("Setting device play level to %f\n", gain);
#endif
	if(audio_set_play_gain(fdesc(), &gain) != AUDIO_SUCCESS)
		return error("Unable to set converter play level.");
#ifdef debug
	audio_get_play_gain(fdesc(), &gain);
	PRINT("Retrieving just-set level for check:  %f\n\n", gain);
#endif
	return true;
}

int
SunConverter::confirmDeviceType() {
	struct stat st;
	if(stat(deviceName(), &st) < 0) {
		error("Cannot stat converter device.");
		return false;
	}
	if (!S_ISCHR(st.st_mode)) {
		char msg[128];
		sprintf(msg, "%s %s", deviceName(), "is not an audio device.");
		Application::alert(msg);
		return false;
	}
	return true;
}

#ifndef __GNUG__
extern "C" char* getenv(const char*);
#endif

void
SunConverter::initialize() {
	BUG("SunConverter::initialize()");
	// to avoid multiple binaries, we use environmental vars to distinguish
	const char* enVar = getenv("MXV_SPARC_10");
	sparc_version = 1;	// default
	if(enVar && (!strcmp(enVar, "YES") || !strcmp(enVar, "yes")))
		sparc_version = 10;
	enVar = getenv("MXV_SPARC_20");
	if(enVar && (!strcmp(enVar, "YES") || !strcmp(enVar, "yes")))
		sparc_version = 20;
}

void
SunConverter::waitForDevice() {
	(void) poll(&pollStruct, 1L, -1);
}

// always write 0.1 seconds of sound per buffer

int
SunConverter::writeSize() {
	return int(0.1 * sampleRate() * channels() * type_to_sampsize(dataType()));
}

// later this may be changed

int
SunConverter::readSize() {
	return writeSize();
}