Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members
SLASH_dev_SLASH_audio.c
00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: SLASH_dev_SLASH_audio.c * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #ifndef WIN32 00045 00046 #include "SLASH_dev_SLASH_audio.h" 00047 00048 //static 00049 int output(int fd, void const *buf, unsigned int len) { 00050 char const *ptr = buf; 00051 int wrote; 00052 00053 while (len) { 00054 wrote = write(fd, ptr, len); 00055 if (wrote == -1) { 00056 if (errno == EINTR) 00057 continue; 00058 else 00059 return -1; 00060 } 00061 00062 ptr += wrote; 00063 len -= wrote; 00064 } 00065 00066 return 0; 00067 } 00068 00069 //static 00070 int audio_buffer(int fd, void const *buf, unsigned int len) { 00071 char const *ptr = buf; 00072 static char hold[AUDIO_FILLSZ]; 00073 static unsigned int held; 00074 unsigned int left, grab; 00075 if (len == 0) { 00076 if (held) { 00077 memset(&hold[held], 0, &hold[AUDIO_FILLSZ] - &hold[held]); 00078 held = 0; 00079 00080 return output(fd, hold, AUDIO_FILLSZ); 00081 } 00082 00083 return 0; 00084 } 00085 00086 if (held == 0 && len == AUDIO_FILLSZ) 00087 return output(fd, ptr, len); 00088 00089 left = AUDIO_FILLSZ - held; 00090 00091 while (len) { 00092 grab = len < left ? len : left; 00093 00094 memcpy(&hold[held], ptr, grab); 00095 held += grab; 00096 left -= grab; 00097 00098 ptr += grab; 00099 len -= grab; 00100 00101 if (left == 0) { 00102 if (output(fd, hold, AUDIO_FILLSZ) == -1) 00103 return -1; 00104 00105 held = 0; 00106 left = AUDIO_FILLSZ; 00107 } 00108 } 00109 00110 return 0; 00111 } 00112 00113 00114 //static 00115 int dsp_audio_init(int fd, unsigned int muiTicksPerSecond, unsigned int muiChannelMod, int enableHeadphones) { 00116 int format, stereo, speed; 00117 00118 /* 00119 if (ioctl(fd,SNDCTL_DSP_RESET,NULL) == -1) { 00120 perror("ioctl(SNDCTL_DSP_RESET)"); 00121 return -1; 00122 } 00123 */ 00124 00125 00126 #ifdef BUILD_ARCH_ARM 00127 int mask; 00128 if (enableHeadphones == 1) 00129 mask = SOUND_MASK_LINE; 00130 else 00131 mask = ~SOUND_MASK_LINE; 00132 00133 if (ioctl(fd, SOUND_MIXER_WRITE_RECSRC, &mask) == -1) { 00134 perror("ioctl: switch to headphones out"); 00135 return -1; 00136 } 00137 #endif 00138 00139 //format = AFMT_S16_BE; // big endian 00140 format = AFMT_S16_LE; // little endian 00141 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1) { 00142 perror("ioctl(SNDCTL_DSP_SETFMT)"); 00143 return -1; 00144 } 00145 if (format != AFMT_S16_LE) { 00146 fprintf(stderr, "AFMT_S16_LE not available\n"); 00147 return -1; 00148 } 00149 00150 stereo = muiChannelMod; 00151 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &muiChannelMod) == -1) { 00152 perror("ioctl(SNDCTL_DSP_CHANNELS)"); 00153 return -1; 00154 } 00155 00156 if (muiChannelMod != stereo) { 00157 fprintf(stderr, "mono/stereo selection failed\n"); 00158 return -1; 00159 } 00160 00161 speed = muiTicksPerSecond; 00162 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) == -1) { 00163 perror("ioctl(SNDCTL_DSP_SPEED)"); 00164 return -1; 00165 } 00166 if (speed != muiTicksPerSecond) { 00167 fprintf(stderr, "sample speed %u not available (closest %u)\n", muiTicksPerSecond, speed); 00168 return -1; 00169 } 00170 00171 return 0; 00172 } 00173 00174 00175 #endif /* WIN32 */ 00176