50 #include <sys/types.h> 62 #include <sys/filio.h> 63 #if defined(__svr4__) || defined(SYSV) || defined(SVR4) 65 #include <sys/audioio.h> 68 #include <sun/audioio.h> 71 static int sun16_check_device(
int audio);
72 static int sun16_set_info(
int audio,
int sample_rate);
77 static int dev_sr[] = {8000, 9600, 11025, 16000, 18900, 22050, 32000,
78 37800, 44100, 48000, -1};
80 #define AUDIOBUFFSIZE 256 93 int samp_rate_ok =
FALSE;
94 for (i=0; dev_sr[i] != -1; i++)
95 if (sample_rate == dev_sr[i])
97 if (samp_rate_ok ==
FALSE)
99 if (sample_rate == 10000)
105 if (al.
present(
"-audiodevice"))
106 audiodevice = al.
val(
"-audiodevice");
107 else if ((audiodevice =
getenv(
"AUDIODEV")) ==
NULL)
108 audiodevice =
"/dev/audio";
110 if ((fdaudio = fopen(audiodevice,
"wb")) ==
NULL)
112 cerr <<
"SUN16: can't open " << audiodevice << endl;
116 audio = fileno(fdaudio);
122 if (sun16_check_device(audio) ==
FALSE)
124 cerr <<
"SUN16: device doesn't support 16bit linear." << endl;
129 if (sun16_set_info(audio,sample_rate) ==
FALSE)
131 cerr <<
"SUN16: unable to set sample rate " <<
137 for (i=0; i < num_samples; i += r/2)
139 if (num_samples > i+AUDIOBUFFSIZE)
143 r = write(audio,&waveform[i], n*2);
146 cerr <<
"SUN16: failed to write to buffer" <<
159 static int sun16_check_device(
int audio)
165 ioctl(audio, AUDIO_DRAIN, 0);
167 if ((ioctl(audio, AUDIO_GETDEV, &type) != -1) &&
168 ((
streq(
"SUNW,CS4231",type.name)) ||
169 (
streq(
"SUNW,dbri",type.name)) ||
170 (
streq(
"SUNW,audiots",type.name)) ||
171 (
streq(
"SUNW,sb16",type.name))))
179 ioctl(audio, AUDIO_DRAIN, 0);
181 if ((ioctl(audio, AUDIO_GETDEV, &type) != -1) &&
182 ((type == AUDIO_DEV_SPEAKERBOX) || (type == AUDIO_DEV_CODEC)))
190 static int sun16_set_info(
int audio,
int sample_rate)
194 ioctl(audio, AUDIO_GETINFO, &info);
196 info.play.sample_rate = sample_rate;
197 info.play.encoding = AUDIO_ENCODING_LINEAR;
198 info.play.precision = 16;
199 info.play.channels = 1;
201 if (ioctl(audio, AUDIO_SETINFO, &info) == -1)
207 static int sun16_setrecord_info(
int audio,
int sample_rate)
214 unsigned char buff[64];
216 ioctl(audio, AUDIO_GETINFO, &info);
218 info.record.pause = 1;
220 ioctl(audio, AUDIO_SETINFO, &info);
223 ioctl(audio, FIONREAD, &read_size);
224 for (r=i=0; (i < read_size); i += r)
225 r = read(audio,buff,64);
228 ioctl(audio, AUDIO_GETINFO, &info);
229 info.record.sample_rate = sample_rate;
230 info.record.encoding = AUDIO_ENCODING_LINEAR;
231 info.record.precision = 16;
232 info.record.channels = 1;
233 info.record.pause = 0;
234 info.record.samples = 0;
235 info.record.error = 0;
237 if (ioctl(audio, AUDIO_SETINFO, &info) == -1)
245 int desired_sample_rate = 16000;
246 int actual_sample_rate;
252 desired_sample_rate = al.
ival(
"-sample_rate");
253 actual_sample_rate = -1;
254 for (i=0; dev_sr[i] != -1; i++)
255 if (desired_sample_rate == dev_sr[i])
256 actual_sample_rate = desired_sample_rate;
257 if (actual_sample_rate == -1)
258 actual_sample_rate = 16000;
260 if ((audio =
open(
"/dev/audio",O_RDONLY)) == -1)
262 cerr <<
"SUN16: can't open /dev/audio for reading" << endl;
266 if (sun16_check_device(audio) ==
FALSE)
268 cerr <<
"SUN16: device doesn't support 16bit linear." << endl;
273 if (sun16_setrecord_info(audio,actual_sample_rate) ==
FALSE)
275 cerr <<
"SUN16: unable to set sample rate " <<
276 actual_sample_rate << endl;
281 wave.
resize((
int)(actual_sample_rate*al.
fval(
"-time")));
288 for (r=i=0; i < num_samples; i+= r)
290 if (num_samples > i+AUDIOBUFFSIZE)
294 ioctl(audio, FIONREAD, &read_size);
302 r = read(audio,&waveform[i], n*2);
306 cerr <<
"SUN16: failed to read from audio device" << endl;
314 if (actual_sample_rate != desired_sample_rate)
326 cerr <<
"Sun 16bit linear not supported" << endl;
334 cerr <<
"Sun 16bit linear not supported" << endl;
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
void set_sample_rate(const int n)
Set sampling rate to n
Utility IO Function header file.
int ival(const EST_String &rkey, int m=1) const
float fval(const EST_String &rkey, int m=1) const
ssize_t num_samples() const
return the number of samples in the waveform
int record_sun16_wave(EST_Wave &inwave, EST_Option &al)
const EST_SMatrix & values() const
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
void resize(int num_samples, int num_channels=EST_ALL, int set=1)
resize the waveform
int sample_rate() const
return the sampling rate (frequency)
int present(const K &rkey) const
Returns true if key is present.
FILE16 *(* open)(const char *, const char *, int, const char *, const char *)
int play_sun16_wave(EST_Wave &inwave, EST_Option &al)
void resample(int rate)
Resample waveform to rate