source: roaraudio/roard/driver_oss.c @ 942:7e85e08c6501

Last change on this file since 942:7e85e08c6501 was 942:7e85e08c6501, checked in by phi, 15 years ago

chaned codec handling a lot, return codec name in case OSS suggest another

File size: 4.6 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
47 if (  fh == -1 ) {
48  if ( (fh = open(device, O_WRONLY, 0644)) == -1 ) {
49   ROAR_ERR("driver_oss_open(*): Can not open OSS device: %s: %s", device, strerror(errno));
50   return -1;
51  }
52 }
53
54 roar_vio_set_fh(inst, fh);
55
56 switch (info->channels) {
57  case  1: tmp = 0; break;
58  case  2: tmp = 1; break;
59  default: er();
60 }
61
62 if ( ioctl(fh, SNDCTL_DSP_STEREO, &tmp) == -1 ) {
63  er();
64 }
65
66 switch (info->codec) {
67  case ROAR_CODEC_PCM_S_LE:
68    switch (info->bits) {
69     case  8: tmp = AFMT_S8;     break;
70     case 16: tmp = AFMT_S16_LE; break;
71//     case 24: tmp = AFMT_S24_PACKED; break;
72#ifdef AFMT_S32_LE
73     case 32: tmp = AFMT_S32_LE; break;
74#endif
75     default: er();
76    }
77   break;
78  case ROAR_CODEC_PCM_S_BE:
79    switch (info->bits) {
80     case  8: tmp = AFMT_S8;     break;
81     case 16: tmp = AFMT_S16_BE; break;
82//     case 24: tmp = AFMT_S24_PACKED; break;
83#ifdef AFMT_S32_BE
84     case 32: tmp = AFMT_S32_BE; break;
85#endif
86     default: er();
87    }
88   break;
89  case ROAR_CODEC_PCM_U_LE:
90    switch (info->bits) {
91     case  8: tmp = AFMT_U8;     break;
92     case 16: tmp = AFMT_U16_LE; break;
93     default: er();
94    }
95   break;
96  case ROAR_CODEC_PCM_U_BE:
97    switch (info->bits) {
98     case  8: tmp = AFMT_U8;     break;
99     case 16: tmp = AFMT_U16_BE; break;
100     default: er();
101    }
102  case ROAR_CODEC_ALAW:
103    tmp = AFMT_A_LAW;
104   break;
105  case ROAR_CODEC_MULAW:
106    tmp = AFMT_MU_LAW;
107   break;
108#ifdef AFMT_VORBIS
109  case ROAR_CODEC_OGG_VORBIS:
110    tmp = AFMT_VORBIS;
111   break;
112#endif
113  default:
114    er();
115   break;
116 }
117
118 ctmp = tmp;
119 if ( ioctl(fh, SNDCTL_DSP_SAMPLESIZE, &tmp) == -1 ) {
120  ROAR_ERR("driver_oss_open(*): can not set sample format");
121  er();
122 }
123
124 if ( tmp != ctmp ) {
125  es = NULL;
126  switch (tmp) {
127   case AFMT_S8    : es = "bits=8,codec=pcm";       break;
128   case AFMT_U8    : es = "bits=8,codec=pcm_u_le";  break;
129   case AFMT_S16_LE: es = "bits=16,codec=pcm_s_le"; break;
130   case AFMT_S16_BE: es = "bits=16,codec=pcm_s_be"; break;
131   case AFMT_U16_LE: es = "bits=16,codec=pcm_u_le"; break;
132   case AFMT_U16_BE: es = "bits=16,codec=pcm_u_be"; break;
133#ifdef AFMT_S32_LE
134   case AFMT_S32_LE: es = "bits=32,codec=pcm_s_le"; break;
135#endif
136#ifdef AFMT_S32_BE
137   case AFMT_S32_BE: es = "bits=32,codec=pcm_s_be"; break;
138#endif
139   case AFMT_A_LAW : es = "codec=alaw";             break;
140   case AFMT_MU_LAW: es = "codec=mulaw";            break;
141#ifdef AFMT_VORBIS
142   case AFMT_VORBIS: es = "codec=ogg_vorbis";       break;
143#endif
144  }
145
146  if ( es != NULL ) {
147   ROAR_ERR("driver_oss_open(*): can not set requested codec, OSS retruned another codec then requested, to use this restart with -oO %s or set codec manuelly via -oO codec=somecodec", es);
148  } else {
149   ROAR_ERR("driver_oss_open(*): can not set requested codec, set codec manuelly via -oO codec=somecodec");
150  }
151  er();
152 }
153
154 tmp = info->rate;
155
156 if ( ioctl(fh, SNDCTL_DSP_SPEED, &tmp) == -1 ) {
157  ROAR_ERR("driver_oss_open(*): can not set sample rate");
158  er();
159 }
160
161 if ( tmp < info->rate * 0.98 || tmp > info->rate * 1.02 ) {
162  ROAR_ERR("driver_oss_open(*): sample rate out of acceptable accuracy");
163  er();
164 }
165
166 ROAR_DBG("driver_oss_open(*): OSS devices opened :)");
167
168 return 0;
169}
170
171int driver_oss_close(DRIVER_USERDATA_T   inst) {
172 return close(roar_vio_get_fh((struct roar_vio_calls *)inst));
173}
174
175
176#endif
177//ll
Note: See TracBrowser for help on using the repository browser.