2
* Linux audio play and grab interface
3
* Copyright (c) 2000, 2001 Gerard Lantau.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
#include <linux/soundcard.h>
27
#include <sys/ioctl.h>
31
const char *audio_device = "/dev/dsp";
39
#define AUDIO_BLOCK_SIZE 4096
41
/* audio read support */
43
static int audio_read(URLContext *h, UINT8 *buf, int size)
45
AudioData *s = h->priv_data;
48
ret = read(s->fd, buf, size);
55
static int audio_write(URLContext *h, UINT8 *buf, int size)
57
AudioData *s = h->priv_data;
60
ret = write(s->fd, buf, size);
67
static int audio_get_format(URLContext *h, URLFormat *f)
69
AudioData *s = h->priv_data;
71
strcpy(f->format_name, "pcm");
72
f->sample_rate = s->rate;
73
f->channels = s->channels;
77
/* URI syntax: 'audio:[rate[,channels]]'
78
default: rate=44100, channels=2
80
static int audio_open(URLContext *h, const char *uri, int flags)
84
int freq, channels, audio_fd;
88
h->packet_size = AUDIO_BLOCK_SIZE;
90
s = malloc(sizeof(AudioData));
95
/* extract parameters */
97
strstart(p, "audio:", &p);
98
freq = strtol(p, (char **)&p, 0);
103
channels = strtol(p, (char **)&p, 0);
107
s->channels = channels;
109
/* open linux audio device */
110
if (flags & URL_WRONLY)
111
audio_fd = open(audio_device,O_WRONLY);
113
audio_fd = open(audio_device,O_RDONLY);
115
perror(audio_device);
119
/* non blocking mode */
120
fcntl(audio_fd, F_SETFL, O_NONBLOCK);
123
tmp=(NB_FRAGMENTS << 16) | FRAGMENT_BITS;
124
err=ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
126
perror("SNDCTL_DSP_SETFRAGMENT");
131
err=ioctl(audio_fd,SNDCTL_DSP_SETFMT,&tmp);
133
perror("SNDCTL_DSP_SETFMT");
137
tmp= (channels == 2);
138
err=ioctl(audio_fd,SNDCTL_DSP_STEREO,&tmp);
140
perror("SNDCTL_DSP_STEREO");
145
err=ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
147
perror("SNDCTL_DSP_SPEED");
161
static int audio_close(URLContext *h)
163
AudioData *s = h->priv_data;
170
URLProtocol audio_protocol = {