source: roaraudio/roard/driver_oss.c @ 1138:cdca5b30dd69

Last change on this file since 1138:cdca5b30dd69 was 1138:cdca5b30dd69, checked in by phi, 15 years ago

use oss specific sync

File size: 5.4 KB
Line 
1//driver_oss.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26#if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS)
27
28#define er() close(fh); return -1
29
30int driver_oss_open(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh) {
31 int tmp;
32 int ctmp;
33 char * es;
34
35#ifdef ROAR_DEFAULT_OSS_DEV
36 if ( device == NULL )
37  device = ROAR_DEFAULT_OSS_DEV;
38#endif
39
40 if ( device == NULL ) {
41  ROAR_ERR("driver_oss_open(*): no default device found, you need to specify one manuelly");
42  return -1;
43 }
44
45 roar_vio_init_calls(inst);
46 inst->sync = driver_oss_sync;
47
48 if (  fh == -1 ) {
49  if ( (fh = open(device, O_WRONLY, 0644)) == -1 ) {
50   ROAR_ERR("driver_oss_open(*): Can not open OSS device: %s: %s", device, strerror(errno));
51   return -1;
52  }
53 }
54
55 roar_vio_set_fh(inst, fh);
56
57
58
59#ifdef SNDCTL_DSP_CHANNELS
60 tmp = info->channels;
61
62 if ( ioctl(fh, SNDCTL_DSP_CHANNELS, &tmp) == -1 ) {
63  ROAR_ERR("driver_oss_open(*): can not set number of channels");
64  er();
65 }
66
67 if ( tmp != info->channels ) {
68   ROAR_ERR("driver_oss_open(*): can not set requested numer of channels, OSS suggested %i channels, to use this restart with -oO channels=%i or set codec manuelly via -oO channels=num", tmp, tmp);
69  er();
70 }
71#else
72 switch (info->channels) {
73  case  1: tmp = 0; break;
74  case  2: tmp = 1; break;
75  default: er();
76 }
77
78 if ( ioctl(fh, SNDCTL_DSP_STEREO, &tmp) == -1 ) {
79  ROAR_ERR("driver_oss_open(*): can not set number of channels");
80  er();
81 }
82#endif
83
84 switch (info->codec) {
85  case ROAR_CODEC_PCM_S_LE:
86    switch (info->bits) {
87     case  8: tmp = AFMT_S8;     break;
88     case 16: tmp = AFMT_S16_LE; break;
89//     case 24: tmp = AFMT_S24_PACKED; break;
90#ifdef AFMT_S32_LE
91     case 32: tmp = AFMT_S32_LE; break;
92#endif
93     default: er();
94    }
95   break;
96  case ROAR_CODEC_PCM_S_BE:
97    switch (info->bits) {
98     case  8: tmp = AFMT_S8;     break;
99     case 16: tmp = AFMT_S16_BE; break;
100//     case 24: tmp = AFMT_S24_PACKED; break;
101#ifdef AFMT_S32_BE
102     case 32: tmp = AFMT_S32_BE; break;
103#endif
104     default: er();
105    }
106   break;
107  case ROAR_CODEC_PCM_U_LE:
108    switch (info->bits) {
109     case  8: tmp = AFMT_U8;     break;
110     case 16: tmp = AFMT_U16_LE; break;
111     default: er();
112    }
113   break;
114  case ROAR_CODEC_PCM_U_BE:
115    switch (info->bits) {
116     case  8: tmp = AFMT_U8;     break;
117     case 16: tmp = AFMT_U16_BE; break;
118     default: er();
119    }
120  case ROAR_CODEC_ALAW:
121    tmp = AFMT_A_LAW;
122   break;
123  case ROAR_CODEC_MULAW:
124    tmp = AFMT_MU_LAW;
125   break;
126#ifdef AFMT_VORBIS
127  case ROAR_CODEC_OGG_VORBIS:
128    tmp = AFMT_VORBIS;
129   break;
130#endif
131  default:
132    er();
133   break;
134 }
135
136 ctmp = tmp;
137#ifdef SNDCTL_DSP_SETFMT
138 if ( ioctl(fh, SNDCTL_DSP_SETFMT, &tmp) == -1 ) {
139#else
140 if ( ioctl(fh, SNDCTL_DSP_SAMPLESIZE, &tmp) == -1 ) {
141#endif
142  ROAR_ERR("driver_oss_open(*): can not set sample format");
143  er();
144 }
145
146 if ( tmp != ctmp ) {
147  es = NULL;
148  switch (tmp) {
149   case AFMT_S8    : es = "bits=8,codec=pcm";       break;
150   case AFMT_U8    : es = "bits=8,codec=pcm_u_le";  break;
151   case AFMT_S16_LE: es = "bits=16,codec=pcm_s_le"; break;
152   case AFMT_S16_BE: es = "bits=16,codec=pcm_s_be"; break;
153   case AFMT_U16_LE: es = "bits=16,codec=pcm_u_le"; break;
154   case AFMT_U16_BE: es = "bits=16,codec=pcm_u_be"; break;
155#ifdef AFMT_S32_LE
156   case AFMT_S32_LE: es = "bits=32,codec=pcm_s_le"; break;
157#endif
158#ifdef AFMT_S32_BE
159   case AFMT_S32_BE: es = "bits=32,codec=pcm_s_be"; break;
160#endif
161   case AFMT_A_LAW : es = "codec=alaw";             break;
162   case AFMT_MU_LAW: es = "codec=mulaw";            break;
163#ifdef AFMT_VORBIS
164   case AFMT_VORBIS: es = "codec=ogg_vorbis";       break;
165#endif
166  }
167
168  if ( es != NULL ) {
169   ROAR_ERR("driver_oss_open(*): can not set requested codec, OSS retruned another codec than requested, to use this restart with -oO %s or set codec manuelly via -oO codec=somecodec", es);
170  } else {
171   ROAR_ERR("driver_oss_open(*): can not set requested codec, set codec manuelly via -oO codec=somecodec");
172  }
173  er();
174 }
175
176 tmp = info->rate;
177
178 if ( ioctl(fh, SNDCTL_DSP_SPEED, &tmp) == -1 ) {
179  ROAR_ERR("driver_oss_open(*): can not set sample rate");
180  er();
181 }
182
183 if ( tmp < info->rate * 0.98 || tmp > info->rate * 1.02 ) {
184  ROAR_ERR("driver_oss_open(*): sample rate out of acceptable accuracy");
185  er();
186 }
187
188 ROAR_DBG("driver_oss_open(*): OSS devices opened :)");
189
190 return 0;
191}
192
193int driver_oss_close(DRIVER_USERDATA_T   inst) {
194 return close(roar_vio_get_fh((struct roar_vio_calls *)inst));
195}
196
197int driver_oss_sync(struct roar_vio_calls * vio) {
198#ifdef SNDCTL_DSP_SYNC
199 return ioctl(roar_vio_get_fh(vio), SNDCTL_DSP_SYNC, NULL);
200#else
201 return 0;
202#endif
203}
204
205#endif
206//ll
Note: See TracBrowser for help on using the repository browser.