Give better name to Inverse_Table_6_9
[mplayer/glamo.git] / stream / ai_oss.c
blobbd7c7c312accf08193be8db4ca7ec7abe8d9dc1b
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
6 #include <string.h> /* strerror */
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <sys/ioctl.h>
11 #ifdef HAVE_SYS_SOUNDCARD_H
12 #include <sys/soundcard.h>
13 #else
14 #ifdef HAVE_SOUNDCARD_H
15 #include <soundcard.h>
16 #else
17 #include <linux/soundcard.h>
18 #endif
19 #endif
21 #include "audio_in.h"
22 #include "mp_msg.h"
23 #include "help_mp.h"
25 int ai_oss_set_samplerate(audio_in_t *ai)
27 int tmp = ai->req_samplerate;
28 if (ioctl(ai->oss.audio_fd, SNDCTL_DSP_SPEED, &tmp) == -1) return -1;
29 ai->samplerate = tmp;
30 return 0;
33 int ai_oss_set_channels(audio_in_t *ai)
35 int err;
36 int ioctl_param;
38 if (ai->req_channels > 2)
40 ioctl_param = ai->req_channels;
41 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp channels: %d\n",
42 err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param));
43 if (err < 0) {
44 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetChanCount,
45 ai->req_channels);
46 return -1;
48 ai->channels = ioctl_param;
50 else
52 ioctl_param = (ai->req_channels == 2);
53 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp stereo: %d (req: %d)\n",
54 err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_STEREO, &ioctl_param),
55 ioctl_param);
56 if (err < 0) {
57 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetStereo,
58 ai->req_channels == 2);
59 return -1;
61 ai->channels = ioctl_param ? 2 : 1;
63 return 0;
66 int ai_oss_init(audio_in_t *ai)
68 int err;
69 int ioctl_param;
71 ai->oss.audio_fd = open(ai->oss.device, O_RDONLY);
72 if (ai->oss.audio_fd < 0)
74 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2Open,
75 ai->oss.device, strerror(errno));
76 return -1;
79 ioctl_param = 0 ;
80 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp getfmt: %d\n",
81 ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETFMTS, &ioctl_param));
83 mp_msg(MSGT_TV, MSGL_V, "Supported formats: %x\n", ioctl_param);
84 if (!(ioctl_param & AFMT_S16_LE))
85 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_UnsupportedFmt);
87 ioctl_param = AFMT_S16_LE;
88 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp setfmt: %d\n",
89 err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param));
90 if (err < 0) {
91 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetAudioFmt);
92 return -1;
95 if (ai_oss_set_channels(ai) < 0) return -1;
97 ioctl_param = ai->req_samplerate;
98 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp speed: %d\n",
99 err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SPEED, &ioctl_param));
100 if (err < 0) {
101 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetSamplerate,
102 ai->req_samplerate);
103 return -1;
105 ai->samplerate = ioctl_param;
107 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp trigger: %d\n",
108 ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETTRIGGER, &ioctl_param));
109 mp_msg(MSGT_TV, MSGL_V, "trigger: %x\n", ioctl_param);
110 ioctl_param = PCM_ENABLE_INPUT;
111 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp trigger: %d\n",
112 err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param));
113 if (err < 0) {
114 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetTrigger,
115 PCM_ENABLE_INPUT);
118 ai->blocksize = 0;
119 mp_msg(MSGT_TV, MSGL_V, "ioctl dsp getblocksize: %d\n",
120 err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETBLKSIZE, &ai->blocksize));
121 if (err < 0) {
122 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2GetBlockSize);
124 mp_msg(MSGT_TV, MSGL_V, "blocksize: %d\n", ai->blocksize);
126 // correct the blocksize to a reasonable value
127 if (ai->blocksize <= 0) {
128 ai->blocksize = 4096*ai->channels*2;
129 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_AudioBlockSizeZero, ai->blocksize);
130 } else if (ai->blocksize < 4096*ai->channels*2) {
131 ai->blocksize *= 4096*ai->channels*2/ai->blocksize;
132 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_AudioBlockSize2Low, ai->blocksize);
135 ai->samplesize = 16;
136 ai->bytes_per_sample = 2;
138 return 0;