Debian Codegraph @

playdv/oss.c

raw symbols SCIP
1 /*
2 * oss.c
3 *
4 * Copyright (C) Charles 'Buck' Krasic - January 2001
5 *
6 * This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
7 * codec.
8 *
9 * libdv is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser Public License as published by
11 * the Free Software Foundation; either version 2.1, or (at your
12 * option) any later version.
13 *
14 * libdv is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser Public License
20 * along with libdv; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * The libdv homepage is http://libdv.sourceforge.net/.
24 */
25
26 #if HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <sys/soundcard.h>
38
39 #include "oss.h"
40
41 static const char *default_device = "/dev/dsp";
42
43 #ifndef O_LARGEFILE
44 #define O_LARGEFILE 0
45 #endif
46
47 dv_oss_t *
48 dv_oss_new(void)
49 {
50 dv_oss_t *result;
51
52 result = (dv_oss_t *)calloc(1,sizeof(dv_oss_t));
53 if(!result) goto no_mem;
54
55 #if HAVE_LIBPOPT
56 result->option_table[DV_OSS_OPT_DEVICE] = (struct poptOption) {
57 longName: "audio-device",
58 argInfo: POPT_ARG_STRING,
59 arg: &result->arg_audio_device,
60 descrip: "target audio device; e.g. /dev/dsp [default]",
61 argDescrip: "devicename",
62 }; /* device */
63
64 result->option_table[DV_OSS_OPT_FILE] = (struct poptOption) {
65 longName: "audio-file",
66 argInfo: POPT_ARG_STRING,
67 arg: &result->arg_audio_file,
68 descrip: "send raw decoded audio to file, skipping audio ioctls",
69 argDescrip: "filename",
70 }; /* file */
71 #endif // HAVE_LIBPOPT
72
73 no_mem:
74 return(result);
75 } /* dv_oss_new */
76
77 /* Very simplistic for sound output using the OSS API */
78 int
79 dv_oss_init(dv_decoder_t *dv, dv_oss_t *oss)
80 {
81 int format = AFMT_S16_NE, rate_request, channels_request;
82 char *device;
83
84 channels_request = dv_get_num_channels (dv);
85 rate_request = dv_get_frequency (dv);
86 oss->buffer=NULL;
87 oss->fd=-1;
88 if(oss->arg_audio_device && oss->arg_audio_file) goto usage;
89 if(oss->arg_audio_file) {
90 if ((oss->fd = open(oss->arg_audio_file,
91 O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE,
92 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)) == -1) goto no_file;
93 } else {
94 device = (char *)(oss->arg_audio_device ? oss->arg_audio_device : default_device);
95 /* open audio device */
96 if ((oss->fd = open(device, O_RDWR, 0)) == -1) goto no_device;
97 /* set sample format -- try for 16bit */
98 if (ioctl(oss->fd, SNDCTL_DSP_SETFMT, &format) == -1) goto format_ioctl;
99 if (format != AFMT_S16_NE) goto format_unsupported;
100 /* set stereo mode */
101 if (ioctl(oss->fd, SNDCTL_DSP_CHANNELS, &channels_request) == -1) goto channels_ioctl;
102 if (channels_request != dv_get_num_channels (dv)) goto channels_unsupported;
103 /* set sampling rate */
104 if (ioctl(oss->fd, SNDCTL_DSP_SPEED, &rate_request) == -1) goto rate_ioctl;
105 if(rate_request != dv_get_frequency (dv)) goto rate_unsupported;
106 }
107 if(!(oss->buffer = malloc(DV_AUDIO_MAX_SAMPLES * channels_request * sizeof(short)))) goto no_memory;
108 return(TRUE);
109
110 usage:
111 fprintf(stderr,"can't send audio to both device and file\n");
112 exit(-1);
113
114 format_unsupported:
115 fprintf(stderr, "soundcard doesn't support format\n");
116 goto fail;
117
118 rate_unsupported:
119 fprintf(stderr,"audio rate found : %d Hz\n", rate_request);
120 fprintf(stderr, "soundcard doesn't support 48kHz (does %d)\n", rate_request);
121 goto fail;
122
123 channels_unsupported:
124 fprintf(stderr, "soundcard doesn't support %d channels\n", channels_request);
125 goto fail;
126
127 no_device:
128 perror(device);
129 goto fail;
130
131 no_file:
132 perror(oss->arg_audio_file);
133 goto fail;
134
135 format_ioctl:
136 perror("SNDCTL_DSP_SETFMT");
137 goto fail;
138
139 channels_ioctl:
140 perror("SNDCTL_DSP_CHANNELS");
141 goto fail;
142
143 rate_ioctl:
144 perror("SNDCTL_DSP_SPEED");
145 goto fail;
146
147 no_memory:
148 fprintf(stderr, "out of memory\n");
149 goto fail;
150
151 fail:
152 dv_oss_close(oss);
153 return(FALSE);
154 } /* dv_oss_init */
155
156 int
157 dv_oss_play(dv_decoder_t *dv, dv_oss_t *oss, short **out)
158 {
159 int ch, i, j=0, total, written=0, result, samples, channels;
160
161 /* Interleave the audio into a single buffer */
162 for (i = 0, samples = dv_get_num_samples (dv), channels = dv_get_num_channels (dv);
163 i < samples; i++) {
164 for (ch = 0; ch < channels; ch++) {
165 oss -> buffer [j++] = out [ch] [i];
166 } /* for */
167 } /* for */
168
169 /* Send the audio to the device */
170 total = samples * channels * sizeof (short);
171 do {
172 result = write(oss->fd, oss->buffer + written, total - written);
173 if(result <= 0) goto write_error;
174 written += result;
175 } while(total > written);
176 if(!oss->arg_audio_file) {
177 if (ioctl(oss->fd, SNDCTL_DSP_POST, NULL) == -1) goto post_ioctl;
178 } /* if */
179
180 return(TRUE);
181
182 write_error:
183 perror("write");
184 return(FALSE);
185
186 post_ioctl:
187 perror("SNDCTL_DSP_POST");
188 return(FALSE);
189
190 } /* dv_oss_play */
191
192 void
193 dv_oss_close(dv_oss_t *oss)
194 {
195 if(oss->fd != -1) {
196 close(oss->fd);
197 oss->fd = -1;
198 } /* if */
199 if(oss->buffer) {
200 oss->buffer = NULL;
201 free(oss->buffer);
202 } /* if */
203 } /* dv_oss_close */