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
|
/* sqUnixSoundSun.c -- sound support for SunOS and Solaris
*
* Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Authors: Ian.Piumarta@inria.fr, Lex Spoon <lex@cc.gatech.edu>, and
* Andrew Gaylard <ag@computer.org>
*
* This driver is playback-only; recording sound is not supported at this time.
*
*/
#include "sq.h"
#define DEBUG 0
#include "sqaio.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_AUDIOIO_H
# include <sys/audioio.h>
#else
# include <sun/audioio.h>
#endif
#include <errno.h>
#include <stropts.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#if DEBUG
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __FUNCTION__
# else
# define __func__ "<unknown>"
# endif
#endif
# define PRINTF(ARGS) printf("%s:%d %s ", strrchr(__FILE__, '/') ? \
strrchr(__FILE__, '/') + 1: \
__FILE__, \
__LINE__, __func__); \
printf ARGS ; \
printf("\n")
#else
# define PRINTF(ARGS)
#endif
static sqInt sound_Stop(void);
static int sound_AvailableSpace(void);
static int auFd= -1; /* open on /dev/audio */
static int auCtlFd= -1; /* open on /dev/audioctl */
static sqInt fmtStereo= 0; /* whether we are playing in stereo or not */
static sqInt auPlaySemaIndex= 0; /* an index to signal when new data may be played */
static sqInt auBufBytes= 0; /* buffer size to use for playback.
unfortunately, this bears no relationship to
whatever the kernel and soundcard are using */
static int auBuffersPlayed= 0;
static struct sigaction action, oldAction;
/* The Solaris STREAMS audio driver sends a SIGIO
* each time it reads the EOF sent by sound_PlaySamplesFromAtLength */
static void auHandle(int sig)
{
PRINTF(("(sig=%d)", sig));
if (auFd < 0) return;
/* Not all SIGIOs are for us */
if (sound_AvailableSpace() > 0)
{
PRINTF(("Signalling semaphore %d", auPlaySemaIndex));
signalSemaphoreWithIndex(auPlaySemaIndex);
}
}
/*** exported sound output functions ***/
static sqInt sound_Stop(void)
{
PRINTF();
if (auFd == -1) return 0;
ioctl(auCtlFd, I_SETSIG, 0);
/* clear any queued sound for immediate silence */
ioctl(auFd, I_FLUSH, FLUSHW);
sigaction(SIGIO, &oldAction, NULL);
close(auFd); auFd= -1;
close(auCtlFd); auCtlFd= -1;
return 0;
}
static sqInt sound_Start(sqInt frameCount, sqInt samplesPerSec, sqInt stereo, sqInt semaIndex)
{
PRINTF(("(frameCount=%d, samplesPerSec=%d, stereo=%d, semaIndex=%d)",
frameCount, samplesPerSec, stereo, semaIndex));
int bytesPerFrame= (stereo ? 4 : 2);
struct audio_info info;
int err;
if (auFd != -1) sound_Stop();
auPlaySemaIndex= semaIndex;
fmtStereo= stereo;
auBufBytes= bytesPerFrame * frameCount;
if ((auFd= open("/dev/audio", O_WRONLY|O_NONBLOCK)) == -1)
{
perror("/dev/audio");
return false;
}
PRINTF(("auFd=%d", auFd));
if ((auCtlFd= open("/dev/audioctl", O_WRONLY|O_NONBLOCK)) == -1)
{
perror("/dev/audioctl");
return false;
}
PRINTF(("auCtlFd=%d", auCtlFd));
/* set up device */
if (ioctl(auFd, AUDIO_GETINFO, &info))
{
perror("AUDIO_GETINFO");
goto closeAndFail;
}
info.play.gain= 255;
info.play.precision= 16;
info.play.encoding= AUDIO_ENCODING_LINEAR;
info.play.channels= fmtStereo ? 2 : 1;
info.play.sample_rate= samplesPerSec;
auBuffersPlayed= info.play.eof;
while ((err= ioctl(auFd, AUDIO_SETINFO, &info)) && errno == EINTR)
;
if (err)
{
perror("AUDIO_SETINFO");
goto closeAndFail;
}
action.sa_handler= auHandle;
sigemptyset(&action.sa_mask);
action.sa_flags= 0;
action.sa_flags|= SA_RESTART;
if (sigaction(SIGIO, &action, &oldAction) < 0) /* On Solaris, SIGIO == SIGPOLL */
{
perror("sigaction(SIGIO, auHandle)");
}
if (ioctl(auCtlFd, I_SETSIG, S_MSG)) /* send SIGIO whenever EOF arrives */
{
perror("ioctl(auFd, I_SETSIG, S_MSG)");
}
return true;
closeAndFail:
close(auFd); auFd= -1;
close(auCtlFd); auCtlFd= -1;
return false;
}
static sqInt sound_AvailableSpace(void)
{
PRINTF();
struct audio_info info;
int avail;
if (auFd < 0) return 0;
if (ioctl(auFd, AUDIO_GETINFO, &info))
{
perror("AUDIO_GETINFO");
sound_Stop();
}
avail= auBufBytes * (info.play.eof - auBuffersPlayed + 2);
PRINTF(("auBufBytes=%d, info.play.eof=%d, auBuffersPlayed=%d, avail=%d",
auBufBytes, info.play.eof, auBuffersPlayed, avail));
return avail;
}
static sqInt sound_PlaySamplesFromAtLength(sqInt frameCount, void *srcBufPtr, sqInt startIndex)
{
PRINTF(("(frameCount=%d, arrayIndex=%d, startIndex=%d)",
frameCount, arrayIndex, startIndex));
short *src= (short *) (srcBufPtr + 4*startIndex);
short buf[2*frameCount];
int i;
int bytes;
PRINTF(("src=%p", src));
if (auFd < 0) return -1;
if (fmtStereo)
{
bytes= 4 * frameCount;
for (i= 0; i < 2 * frameCount; i++)
buf[i]= src[i];
}
else
{
bytes= 2 * frameCount;
for (i= 0; i < frameCount; i++)
buf[i]= src[2*i];
}
/* write data to device from auBuf to dst */
while (bytes > 0)
{
int len;
char *pos= (char *) buf;
len= write(auFd, pos, bytes);
if (len < 0)
{
perror("/dev/audio");
return 0;
}
bytes -= len;
pos += len;
}
/* add an EOF marker */
write(auFd, buf, 0);
auBuffersPlayed += 1;
return frameCount;
}
static sqInt sound_InsertSamplesFromLeadTime(sqInt frameCount, void *srcBufPtr, sqInt samplesOfLeadTime)
{
PRINTF(("(frameCount=%d, srcBufPtr=%d, samplesOfLeadTime=%d)",
frameCount, srcBufPtr, samplesOfLeadTime));
return 0;
}
static sqInt sound_PlaySilence(void)
{
PRINTF();
success(false);
return 0;
}
/** recording not supported **/
static sqInt sound_SetRecordLevel(sqInt level)
{
PRINTF();
success(false);
return 0;
}
static sqInt sound_StartRecording(sqInt desiredSamplesPerSec, sqInt stereo, sqInt semaIndex)
{
PRINTF();
success(false);
return 0;
}
static sqInt sound_StopRecording(void)
{
PRINTF();
return 0;
}
static double sound_GetRecordingSampleRate(void)
{
PRINTF();
success(false);
return 0.0;
}
static sqInt sound_RecordSamplesIntoAtLength(void *buf, sqInt startSliceIndex, sqInt bufferSizeInBytes)
{
PRINTF();
success(false);
return 0;
}
static void sound_Volume(double *left, double *right)
{
PRINTF();
success(false);
return;
}
static void sound_SetVolume(double left, double right)
{
PRINTF();
success(false);
return;
}
static sqInt sound_SetSwitch(sqInt id, sqInt captureFlag, sqInt parameter)
{
return -1;
}
static sqInt sound_GetSwitch(sqInt id, sqInt captureFlag, sqInt channel)
{
return -1;
}
static sqInt sound_SetDevice(sqInt id, char *arg)
{
return -1;
}
#include "SqSound.h"
SqSoundDefine(Sun);
#include "SqModule.h"
static void sound_parseEnvironment(void) { PRINTF(); }
static int sound_parseArgument(int argc, char **argv) { PRINTF(("(argc=%d)", argc)); return 0; }
static void sound_printUsage(void) { PRINTF(); }
static void sound_printUsageNotes(void) { PRINTF(); }
static void *sound_makeInterface(void) { PRINTF(); return &sound_Sun_itf; }
SqModuleDefine(sound, Sun);
|