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
|
/*
** brec/sndfunc.c (C) David Monro 1996
**
** Copyright under the GPL - see the file COPYING in this directory
**
**
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifndef __FreeBSD__
#include <sys/soundcard.h>
#else
#include <machine/soundcard.h>
#endif
#define AUDIO "/dev/dsp"
/* Globals */
int audio, abuf_size, fmt_mask;
int audio_recorder;
/* Prototypes */
void sync_audio(void);
/* Extern globals */
extern char *progname;
/* Extern prototypes */
extern void ErrDie(char *err);
extern void Die(char *err);
void init_sound(int recorder)
{
/* Attempt to open the audio device */
audio_recorder = recorder;
audio = open(AUDIO, (recorder)? O_RDONLY : O_WRONLY);
if (audio == -1)
ErrDie(AUDIO);
#if 0
if (ioctl(audio, SNDCTL_DSP_GETBLKSIZE, &abuf_size) < 0) ErrDie(AUDIO);
if (abuf_size < 4096 || abuf_size > 65536) Die("invalid audio buffer size");
fprintf(stderr, "abuf_size = %d\n", abuf_size);
#else
abuf_size = 65536;
#endif
#if 1
if (ioctl(audio, SNDCTL_DSP_GETFMTS, &fmt_mask) < 0) ErrDie(AUDIO);
#endif
}
void snd_parm(int speed, int bits, int stereo)
{
static int oldspeed = -1, oldbits = -1, oldstereo = -1;
if ((speed != oldspeed) || (bits != oldbits) || (stereo != oldstereo))
{
/* Sync the dsp - otherwise strange things may happen */
#ifdef DEBUG
fprintf(stderr, " - syncing - ");
#endif
sync_audio();
/* Set the sample speed, size and stereoness */
/* We only use values of 8 and 16 for bits. This implies
* unsigned data for 8 bits, and little-endian signed for 16 bits.
*/
if (ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &bits) < 0)
ErrDie(AUDIO);
if (ioctl(audio, SNDCTL_DSP_STEREO, &stereo) < 0)
ErrDie(AUDIO);
if (ioctl(audio, SNDCTL_DSP_SPEED, &speed) < 0)
ErrDie(AUDIO);
}
oldspeed = speed; oldbits = bits; oldstereo = stereo;
}
void sync_audio(void)
{
/* at least Linux' via82cxxx_audio-driver reports error */
/* when trying to SNDCTL_DSP_SYNC in O_RDONLY mode */
if (ioctl (audio, SNDCTL_DSP_SYNC, NULL) < 0)
if (!audio_recorder)
ErrDie(AUDIO);
}
|