src/audio/SDL_audiodev.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 31 Aug 2006 21:00:10 +0000
changeset 1994 6abc7e6f9817
parent 1895 c121d94672cb
child 2049 5f6550e5184f
child 3809 7852b5b78af5
permissions -rw-r--r--
Added int32 adn float32 support to SDL_LoadWAV_RW().
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Get the name of the audio device we use for output */
    25 
    26 #if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
    27 
    28 #include <fcntl.h>
    29 #include <sys/types.h>
    30 #include <sys/stat.h>
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_audiodev_c.h"
    34 
    35 #ifndef _PATH_DEV_DSP
    36 #if defined(__NETBSD__) || defined(__OPENBSD__)
    37 #define _PATH_DEV_DSP  "/dev/audio"
    38 #else
    39 #define _PATH_DEV_DSP  "/dev/dsp"
    40 #endif
    41 #endif
    42 #ifndef _PATH_DEV_DSP24
    43 #define _PATH_DEV_DSP24	"/dev/sound/dsp"
    44 #endif
    45 #ifndef _PATH_DEV_AUDIO
    46 #define _PATH_DEV_AUDIO	"/dev/audio"
    47 #endif
    48 
    49 
    50 int
    51 SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
    52 {
    53     const char *audiodev;
    54     int audio_fd;
    55     char audiopath[1024];
    56 
    57     /* Figure out what our audio device is */
    58     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
    59         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
    60         if (classic) {
    61             audiodev = _PATH_DEV_AUDIO;
    62         } else {
    63             struct stat sb;
    64 
    65             /* Added support for /dev/sound/\* in Linux 2.4 */
    66             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
    67                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
    68                     && S_ISCHR(sb.st_mode))) {
    69                 audiodev = _PATH_DEV_DSP24;
    70             } else {
    71                 audiodev = _PATH_DEV_DSP;
    72             }
    73         }
    74     }
    75     audio_fd = open(audiodev, flags, 0);
    76 
    77     /* If the first open fails, look for other devices */
    78     if ((audio_fd < 0) && (SDL_strlen(audiodev) < (sizeof(audiopath) - 3))) {
    79         int exists, instance;
    80         struct stat sb;
    81 
    82         instance = 1;
    83         do {                    /* Don't use errno ENOENT - it may not be thread-safe */
    84             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
    85                          "%s%d", audiodev, instance++);
    86             exists = 0;
    87             if (stat(audiopath, &sb) == 0) {
    88                 exists = 1;
    89                 audio_fd = open(audiopath, flags, 0);
    90             }
    91         }
    92         while (exists && (audio_fd < 0));
    93         audiodev = audiopath;
    94     }
    95     if (path != NULL) {
    96         SDL_strlcpy(path, audiodev, maxlen);
    97         path[maxlen - 1] = '\0';
    98     }
    99     return (audio_fd);
   100 }
   101 
   102 #elif SDL_AUDIO_DRIVER_PAUD
   103 
   104 /* Get the name of the audio device we use for output */
   105 
   106 #include <sys/types.h>
   107 #include <sys/stat.h>
   108 
   109 #include "SDL_stdinc.h"
   110 #include "SDL_audiodev_c.h"
   111 
   112 #ifndef _PATH_DEV_DSP
   113 #define _PATH_DEV_DSP	"/dev/%caud%c/%c"
   114 #endif
   115 
   116 char devsettings[][3] = {
   117     {'p', '0', '1'}, {'p', '0', '2'}, {'p', '0', '3'}, {'p', '0', '4'},
   118     {'p', '1', '1'}, {'p', '1', '2'}, {'p', '1', '3'}, {'p', '1', '4'},
   119     {'p', '2', '1'}, {'p', '2', '2'}, {'p', '2', '3'}, {'p', '2', '4'},
   120     {'p', '3', '1'}, {'p', '3', '2'}, {'p', '3', '3'}, {'p', '3', '4'},
   121     {'b', '0', '1'}, {'b', '0', '2'}, {'b', '0', '3'}, {'b', '0', '4'},
   122     {'b', '1', '1'}, {'b', '1', '2'}, {'b', '1', '3'}, {'b', '1', '4'},
   123     {'b', '2', '1'}, {'b', '2', '2'}, {'b', '2', '3'}, {'b', '2', '4'},
   124     {'b', '3', '1'}, {'b', '3', '2'}, {'b', '3', '3'}, {'b', '3', '4'},
   125     {'\0', '\0', '\0'}
   126 };
   127 
   128 static int
   129 OpenUserDefinedDevice(char *path, int maxlen, int flags)
   130 {
   131     const char *audiodev;
   132     int audio_fd;
   133 
   134     /* Figure out what our audio device is */
   135     if ((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) {
   136         audiodev = SDL_getenv("AUDIODEV");
   137     }
   138     if (audiodev == NULL) {
   139         return -1;
   140     }
   141     audio_fd = open(audiodev, flags, 0);
   142     if (path != NULL) {
   143         SDL_strlcpy(path, audiodev, maxlen);
   144         path[maxlen - 1] = '\0';
   145     }
   146     return audio_fd;
   147 }
   148 
   149 int
   150 SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
   151 {
   152     struct stat sb;
   153     int audio_fd;
   154     char audiopath[1024];
   155     int cycle;
   156 
   157     audio_fd = OpenUserDefinedDevice(path, maxlen, flags);
   158     if (audio_fd != -1) {
   159         return audio_fd;
   160     }
   161 
   162     cycle = 0;
   163     while (devsettings[cycle][0] != '\0') {
   164         SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   165                      _PATH_DEV_DSP,
   166                      devsettings[cycle][0],
   167                      devsettings[cycle][1], devsettings[cycle][2]);
   168 
   169         if (stat(audiopath, &sb) == 0) {
   170             audio_fd = open(audiopath, flags, 0);
   171             if (audio_fd > 0) {
   172                 if (path != NULL) {
   173                     SDL_strlcpy(path, audiopath, maxlen);
   174                 }
   175                 return audio_fd;
   176             }
   177         }
   178     }
   179     return -1;
   180 }
   181 
   182 #endif /* Audio driver selection */
   183 /* vi: set ts=4 sw=4 expandtab: */