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.
22 #include <linux/soundcard.h>
25 #include <sys/ioctl.h>
32 const char *audio_device
= "/dev/dsp";
40 #define AUDIO_BLOCK_SIZE 4096
42 /* audio read support */
44 static int audio_read(URLContext
*h
, UINT8
*buf
, int size
)
46 AudioData
*s
= h
->priv_data
;
49 ret
= read(s
->fd
, buf
, size
);
56 static int audio_write(URLContext
*h
, UINT8
*buf
, int size
)
58 AudioData
*s
= h
->priv_data
;
61 ret
= write(s
->fd
, buf
, size
);
68 static int audio_get_format(URLContext
*h
, URLFormat
*f
)
70 AudioData
*s
= h
->priv_data
;
72 strcpy(f
->format_name
, "pcm");
73 f
->sample_rate
= s
->rate
;
74 f
->channels
= s
->channels
;
78 /* URI syntax: 'audio:[rate[,channels]]'
79 default: rate=44100, channels=2
81 static int audio_open(URLContext
*h
, const char *uri
, int flags
)
85 int freq
, channels
, audio_fd
;
89 h
->packet_size
= AUDIO_BLOCK_SIZE
;
91 s
= malloc(sizeof(AudioData
));
96 /* extract parameters */
98 strstart(p
, "audio:", &p
);
99 freq
= strtol(p
, (char **)&p
, 0);
104 channels
= strtol(p
, (char **)&p
, 0);
108 s
->channels
= channels
;
110 /* open linux audio device */
111 if (flags
& URL_WRONLY
)
112 audio_fd
= open(audio_device
,O_WRONLY
);
114 audio_fd
= open(audio_device
,O_RDONLY
);
116 perror(audio_device
);
120 /* non blocking mode */
121 fcntl(audio_fd
, F_SETFL
, O_NONBLOCK
);
124 tmp
=(NB_FRAGMENTS
<< 16) | FRAGMENT_BITS
;
125 err
=ioctl(audio_fd
, SNDCTL_DSP_SETFRAGMENT
, &tmp
);
127 perror("SNDCTL_DSP_SETFRAGMENT");
132 err
=ioctl(audio_fd
,SNDCTL_DSP_SETFMT
,&tmp
);
134 perror("SNDCTL_DSP_SETFMT");
138 tmp
= (channels
== 2);
139 err
=ioctl(audio_fd
,SNDCTL_DSP_STEREO
,&tmp
);
141 perror("SNDCTL_DSP_STEREO");
146 err
=ioctl(audio_fd
, SNDCTL_DSP_SPEED
, &tmp
);
148 perror("SNDCTL_DSP_SPEED");
162 static int audio_close(URLContext
*h
)
164 AudioData
*s
= h
->priv_data
;
171 URLProtocol audio_protocol
= {