Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

xdvshow-audio.c

Go to the documentation of this file.
00001 /* 00002 * Copyright (c) 1999, 2000, 2001, 2002 WIDE Project 00003 * All rights reserved. 00004 * 00005 * Author : Akimichi OGAWA (akimichi@sfc.wide.ad.jp) 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code MUST retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form MUST reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 3. All advertising materials mentioning features or use of this software 00016 * MUST display the following acknowledgement: 00017 * This product includes software developed by Akimichi OGAWA. 00018 * 4. The name of the author MAY NOT be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00022 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00025 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00029 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00030 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 * POSSIBILITY OF SUCH DAMAGE. 00032 * 00033 */ 00034 00035 #include <stdio.h> 00036 #include <fcntl.h> 00037 #include <unistd.h> 00038 00039 #if (FREEBSD_4 || FREEBSD_5) 00040 #include <sys/soundcard.h> 00041 #endif /* FREEBSD_4 */ 00042 00043 #ifdef NETBSD 00044 #include <sys/ioccom.h> 00045 #include <soundcard.h> 00046 #endif /* NETBSD */ 00047 00048 #ifdef LINUX 00049 #include <sys/ioctl.h> 00050 #include <linux/soundcard.h> 00051 #endif /* LINUX */ 00052 00053 #include <libdv/dv.h> 00054 00055 #include "xdvshow-audio.h" 00056 00060 int audio_fd; 00061 00062 static int _init_audio_device __P((dv_audio_t *)); 00063 00075 int 00076 xdvshow_init_audio(char *audio_dev, dv_audio_t *audio) 00077 { 00078 int ret; 00079 00080 if (audio_dev == NULL) { 00081 return(-1); 00082 } 00083 00084 audio_fd = open(audio_dev, O_WRONLY); 00085 if (audio_fd < 0) { 00086 perror("open audio"); 00087 return(-1); 00088 } 00089 00090 ret = _init_audio_device(audio); 00091 if (ret < 0) { 00092 return(-1); 00093 } 00094 00095 return(1); 00096 } 00097 00110 int 00111 xdvshow_play_audio(dv_audio_t *audio, int16_t **audio_buffers) 00112 { 00113 /* 00114 int ret; 00115 */ 00116 int ch, i, j=0; 00117 static int16_t outbuf[DV_AUDIO_MAX_SAMPLES * 4]; 00118 00119 for (i=0; i<audio->samples_this_frame; i++) { 00120 for (ch=0; ch<audio->num_channels; ch++) { 00121 outbuf[j] = audio_buffers[ch][i]; 00122 j++; 00123 } 00124 } 00125 00126 write(audio_fd, 00127 outbuf, 00128 audio->samples_this_frame * audio->num_channels * sizeof(u_int16_t)); 00129 00130 /* 00131 ret = ioctl(audio_fd, SNDCTL_DSP_POST, NULL); 00132 if (ret < 0) { 00133 perror("SNDCTL_DSP_POST"); 00134 return (-1); 00135 } 00136 */ 00137 00138 return(1); 00139 } 00140 00154 static int 00155 _init_audio_device(dv_audio_t *audio) 00156 { 00157 int ret; 00158 int format; 00159 int fragment_request; 00160 int channels; 00161 int rate; 00162 00163 /* 00164 AFMT_S16_LE = little endian, 00165 AFMT_S16_BE = big endian, 00166 AFMT_S16_NE = native endianity of the machine; 00167 */ 00168 format = AFMT_S16_NE; 00169 fragment_request = 0x000300B; 00170 channels = audio->num_channels; 00171 rate = audio->frequency; 00172 00173 #ifdef LINUX 00174 ret = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format); 00175 if (ret < 0) { 00176 perror("SNDCTL_DSP_SETFMT"); 00177 return(-1); 00178 } 00179 00180 ret = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels); 00181 if (ret < 0) { 00182 perror("SNDCTL_DSP_CHANNELS"); 00183 return (-1); 00184 } 00185 00186 ret = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate); 00187 if (ret < 0) { 00188 perror("SNDCTL_DSP_SPEED"); 00189 return (-1); 00190 } 00191 00192 #else /* FreeBSD and perhaps others */ 00193 00194 ret = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &fragment_request); 00195 if (ret < 0) { 00196 perror("SNDCTL_DSP_SETFRAGMENT"); 00197 return(-1); 00198 } 00199 00200 ret = ioctl(audio_fd, SOUND_PCM_WRITE_BITS, &format); 00201 if (ret < 0) { 00202 perror("SOUND_PCM_WRITE_BITS"); 00203 return(-1); 00204 } 00205 00206 ret = ioctl(audio_fd, SOUND_PCM_WRITE_CHANNELS, &channels); 00207 if (ret < 0) { 00208 perror("SOUND_PCM_WRITE_CHANNELS"); 00209 return(-1); 00210 } 00211 00212 ret = ioctl(audio_fd, SOUND_PCM_WRITE_RATE, &rate); 00213 if (ret < 0) { 00214 perror("SOUND_PCM_WRITE_RATE"); 00215 fprintf(stderr, "rate : %d\n", rate); 00216 return(-1); 00217 } 00218 00219 #endif 00220 00221 return(1); 00222 }

Generated on Wed Nov 3 19:19:02 2004 for xdvshow by doxygen 1.3.7