File: proport_dac.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (208 lines) | stat: -rw-r--r-- 5,305 bytes parent folder | download | duplicates (3)
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
// proport_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

extern "C" {
#include "s56dspUser.h"
#include "qckMon.h"
#include "drp.h"
}

#undef debug

#include "application.h"
#include "proport_dac.h"
#include "statusaction.h"

ProportConverter::ProportConverter() : drp(nil) {
	failIf(!initialize());
}

ProportConverter::~ProportConverter() {
	close();
}

boolean
ProportConverter::isPlayableFormat(DataType type) {
	return type == ShortData;
}

void
ProportConverter::close() {
	BUG("ProportConverter::close()");
	if(drp && drp->dsp)
		(void) ::close(drp->dsp->fd);
}

void
ProportConverter::fail() {
	BUG("ProportConverter::fail()");
	if(running()) stop();
	Super::fail();
}

boolean
ProportConverter::initialize() {
	// DSP application program
	static char	dspcode[256] = "/usr/local/s56dsp/libdrp/ssirp.lod";
	const int	nbits = 16;				// DMA mode (number of bits)
	const int	pagesize = 128*1024;	// DMA page size
	const int	writesize = 16384;
	boolean status = true;
	if((drp = drp_bootdsp("qckMon.lod", dspcode)) == NULL) {
		error("drp_bootdsp failed");
		status = false;
	}
	else if(drp->error[0]) {
		Application::alert("drp_bootdsp failed:", drp->error);
		status = false;
	}
	else if(drp_confio(drp, nbits, pagesize, writesize) == -1) {
		Application::alert("drp_confio failed:", drp->error);
		status = false;
	}
	return status;
}

int
ProportConverter::checkSampleRate(int rate) {
	static char periph[256] = "proport";	// Peripheral
	if(drp_confperiph(drp, periph, rate,
			willRecord() ? DRP_DIR_TODISK : DRP_DIR_TODSP) == -1) {
		char msg[256];
		sprintf(msg,
			"Cannot set sample rate to %d.  drp_confperiph: %s",
			rate, drp->error);
		Application::alert(msg);
		return false;
	}
	return true;
}

int
ProportConverter::checkChannels(int chans) {
	if(chans != 2) {
		Application::alert(
			"This converter can only play and record stereo sound."
		);
		return false;
	}
	return true;
}

int
ProportConverter::doConfigure() {
	BUG("ProportConverter::configure()");
	boolean status = true;
	if(drp_start(drp, willRecord()? DRP_DIR_TODISK:DRP_DIR_TODSP) == -1) {
		Application::alert(
			"ProportConverter::configure: drp_start failed:",
			drp->error);
		status = false;
	}
	return status;
}

int
ProportConverter::doConversion(StatusAction* askedToStop) {
	int bytesToGo = dataSize();
	int bufsize = min(dataSize(), bufferSize());
	addr databuffer = (addr) pointerToData();

	Application::inform("Playing...");
#ifdef DEBUG_PROPORT
	fprintf(stderr, "starting conversion.  total bytes = %d\n\n",bytesToGo);
#endif
	while(bytesToGo > 0) {
		if((*askedToStop)())
			return stop();
		int bytesWritten = ::write(drp->dsp->fd, databuffer, bufsize);
		if(bytesWritten < 0) {
			error("ProportConverter::doConversion:  write error");
			return false;
		}
		else if(bytesWritten != bufsize) {
			char msg[256];
			sprintf(msg, "write error: %d != %d", bytesWritten, bufsize);
			error(msg);
			return false;
		}
		bytesToGo -= bufsize;
#ifdef DEBUG_PROPORT
		fprintf(stderr, "%d byte block written.  %d bytes left.\n", bytesWritten, bytesToGo);
#endif
		databuffer += bufsize;
		bufsize = min(bufsize, bytesToGo);
	}
	return stop();
}

int
ProportConverter::doRecording(StatusAction* askedToStop) {
	int bytesToGo = dataSize();
	int bufsize = min(dataSize(), bufferSize());
	addr databuffer = (addr) pointerToData();

	Application::inform("Recording...");
	while(bytesToGo > 0) {
		if((*askedToStop)())
			return stop();
		int bytesRead = ::read(drp->dsp->fd, databuffer, bufsize);
		if(bytesRead < 0) {
			error("ProportConverter::doRecording: read error");
			return false;
		}
		else if(bytesRead != bufsize) {
			char msg[256];
			sprintf(msg, "read error: %d != %d", bytesRead, bufsize);
			error(msg);
			return false;
		}
		else if(bytesRead == 0) {
			Application::alert("read: got EOF from DSP.");
			stop();
			return false;
		}
		bytesToGo -= bufsize;
		databuffer += bufsize;
		bufsize = min(bufsize, bytesToGo);
	}
	return stop();
}

int
ProportConverter::stop() {
	BUG("ProportConverter::stop()");
	if (drp_end(drp) == -1) {
		Application::alert(
			"ProportConverter::stop: drp_end failed:", drp->error);
		return false;
	}
	// else
	return Super::stop();
}