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
|
/*
** 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/soundcard.h>
#include <sys/ioctl.h>
#define AUDIO "/dev/dsp"
/* Globals */
int audio, abuf_size, fmt_mask;
char *audev = AUDIO;
/* Prototypes */
void sync_audio(void);
void cleanup_audio(void);
/* Extern globals */
extern char *progname;
extern int debug;
/* Extern prototypes */
extern void ErrDie(char *err);
extern void Die(char *err);
void init_sound(int recorder)
{
/* Attempt to open the audio device */
audio = open(audev, (recorder)? O_RDONLY : O_WRONLY);
if (audio == -1)
ErrDie(audev);
#if 1
if (ioctl(audio, SNDCTL_DSP_GETBLKSIZE, &abuf_size) < 0) ErrDie(audev);
if (debug)
fprintf(stderr, "abuf_size = %d\n", abuf_size);
#if 0
if (abuf_size < 1024 || abuf_size > 65536) Die("invalid audio buffer size");
#else
if (abuf_size < 4096) abuf_size = 4096; /* Seems reasonable */
#endif
#else
abuf_size = 65536;
#endif
#if 1
if (ioctl(audio, SNDCTL_DSP_GETFMTS, &fmt_mask) < 0) ErrDie(audev);
#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 */
if (ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &bits) < 0)
ErrDie(audev);
if (ioctl(audio, SNDCTL_DSP_STEREO, &stereo) < 0)
ErrDie(audev);
if (ioctl(audio, SNDCTL_DSP_SPEED, &speed) < 0)
ErrDie(audev);
}
oldspeed = speed; oldbits = bits; oldstereo = stereo;
}
void sync_audio(void)
{
if (ioctl (audio, SNDCTL_DSP_SYNC, NULL) < 0)
ErrDie(audev);
}
void cleanup_audio(void)
{
if (ioctl (audio, SNDCTL_DSP_RESET) < 0)
ErrDie(audev);
close(audio);
}
|