src/audio/qsa/SDL_qsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 05 Jul 2012 12:16:44 -0400
changeset 6352 a9bcd26e7105
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rwxr-xr-x
Removed unneeded audio buffer memset() for consistent behavior on all platforms.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_config.h"
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
    24
#if SDL_AUDIO_DRIVER_QSA
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
    25
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#include <errno.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include <unistd.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include <fcntl.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#include <signal.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#include <sys/types.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include <sys/time.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
#include <sched.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
#include <sys/select.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#include <sys/neutrino.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#include <sys/asoundlib.h>
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#include "SDL_timer.h"
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
#include "SDL_audio.h"
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
#include "../SDL_audiomem.h"
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
#include "../SDL_audio_c.h"
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
#include "SDL_qsa_audio.h"
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
/* default channel communication parameters */
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
#define DEFAULT_CPARAMS_RATE   44100
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
#define DEFAULT_CPARAMS_VOICES 1
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
#define DEFAULT_CPARAMS_FRAG_SIZE 4096
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
#define DEFAULT_CPARAMS_FRAGS_MIN 1
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
#define DEFAULT_CPARAMS_FRAGS_MAX 1
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
#define QSA_NO_WORKAROUNDS  0x00000000
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
#define QSA_MMAP_WORKAROUND 0x00000001
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
struct BuggyCards
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    56
    char *cardname;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    unsigned long bugtype;
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
};
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
#define QSA_WA_CARDS             3
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
#define QSA_MAX_CARD_NAME_LENGTH 33
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    63
struct BuggyCards buggycards[QSA_WA_CARDS] = {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    64
    {"Sound Blaster Live!", QSA_MMAP_WORKAROUND},
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    65
    {"Vortex 8820", QSA_MMAP_WORKAROUND},
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    66
    {"Vortex 8830", QSA_MMAP_WORKAROUND},
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
};
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
/* List of found devices */
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
#define QSA_MAX_DEVICES       32
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    71
#define QSA_MAX_NAME_LENGTH   81+16     /* Hardcoded in QSA, can't be changed */
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
typedef struct _QSA_Device
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    75
    char name[QSA_MAX_NAME_LENGTH];     /* Long audio device name for SDL  */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    76
    int cardno;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    77
    int deviceno;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
} QSA_Device;
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
QSA_Device qsa_playback_device[QSA_MAX_DEVICES];
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    81
uint32_t qsa_playback_devices;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
QSA_Device qsa_capture_device[QSA_MAX_DEVICES];
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    84
uint32_t qsa_capture_devices;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    86
static inline void
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    87
QSA_SetError(const char *fn, int status)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    89
    SDL_SetError("QSA: %s() failed: %s", fn, snd_strerror(status));
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
/* card names check to apply the workarounds */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    93
static int
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    94
QSA_CheckBuggyCards(_THIS, unsigned long checkfor)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    96
    char scardname[QSA_MAX_CARD_NAME_LENGTH];
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    97
    int it;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
    99
    if (snd_card_get_name
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   100
        (this->hidden->cardno, scardname, QSA_MAX_CARD_NAME_LENGTH - 1) < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   101
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   102
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   104
    for (it = 0; it < QSA_WA_CARDS; it++) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   105
        if (SDL_strcmp(buggycards[it].cardname, scardname) == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   106
            if (buggycards[it].bugtype == checkfor) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   107
                return 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   108
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   109
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   110
    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   111
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   112
    return 0;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
5590
db152c9f31c3 Added some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 5584
diff changeset
   115
/* !!! FIXME: does this need to be here? Does the SDL version not work? */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   116
static void
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   117
QSA_ThreadInit(_THIS)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   119
    struct sched_param param;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   120
    int status;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   122
    /* Increase default 10 priority to 25 to avoid jerky sound */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   123
    status = SchedGet(0, 0, &param);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   124
    param.sched_priority = param.sched_curpriority + 15;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   125
    status = SchedSet(0, 0, SCHED_NOCHANGE, &param);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
/* PCM channel parameters initialize function */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   129
static void
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   130
QSA_InitAudioParams(snd_pcm_channel_params_t * cpars)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   132
    SDL_memset(cpars, 0, sizeof(snd_pcm_channel_params_t));
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   134
    cpars->channel = SND_PCM_CHANNEL_PLAYBACK;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   135
    cpars->mode = SND_PCM_MODE_BLOCK;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   136
    cpars->start_mode = SND_PCM_START_DATA;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   137
    cpars->stop_mode = SND_PCM_STOP_STOP;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   138
    cpars->format.format = SND_PCM_SFMT_S16_LE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   139
    cpars->format.interleave = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   140
    cpars->format.rate = DEFAULT_CPARAMS_RATE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   141
    cpars->format.voices = DEFAULT_CPARAMS_VOICES;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   142
    cpars->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   143
    cpars->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   144
    cpars->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
/* This function waits until it is possible to write a full sound buffer */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   148
static void
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   149
QSA_WaitDevice(_THIS)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   151
    fd_set wfds;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   152
    fd_set rfds;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   153
    int selectret;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   154
    struct timeval timeout;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   156
    if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   157
        FD_ZERO(&wfds);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   158
        FD_SET(this->hidden->audio_fd, &wfds);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   159
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   160
        FD_ZERO(&rfds);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   161
        FD_SET(this->hidden->audio_fd, &rfds);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   162
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   164
    do {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   165
        /* Setup timeout for playing one fragment equal to 2 seconds          */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   166
        /* If timeout occured than something wrong with hardware or driver    */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   167
        /* For example, Vortex 8820 audio driver stucks on second DAC because */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   168
        /* it doesn't exist !                                                 */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   169
        timeout.tv_sec = 2;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   170
        timeout.tv_usec = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   171
        this->hidden->timeout_on_wait = 0;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   173
        if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   174
            selectret =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   175
                select(this->hidden->audio_fd + 1, NULL, &wfds, NULL,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   176
                       &timeout);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   177
        } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   178
            selectret =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   179
                select(this->hidden->audio_fd + 1, &rfds, NULL, NULL,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   180
                       &timeout);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   181
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   182
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   183
        switch (selectret) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   184
        case -1:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   185
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   186
                SDL_SetError("QSA: select() failed: %s\n", strerror(errno));
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   187
                return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   188
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   189
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   190
        case 0:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   191
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   192
                SDL_SetError("QSA: timeout on buffer waiting occured\n");
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   193
                this->hidden->timeout_on_wait = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   194
                return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   195
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   196
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   197
        default:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   198
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   199
                if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   200
                    if (FD_ISSET(this->hidden->audio_fd, &wfds)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   201
                        return;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
                    }
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   203
                } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   204
                    if (FD_ISSET(this->hidden->audio_fd, &rfds)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   205
                        return;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
                    }
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   207
                }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   208
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   209
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   210
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   211
    } while (1);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   214
static void
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   215
QSA_PlayDevice(_THIS)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   217
    snd_pcm_channel_status_t cstatus;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   218
    int written;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   219
    int status;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   220
    int towrite;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   221
    void *pcmbuffer;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   223
    if ((!this->enabled) || (!this->hidden)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   224
        return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   225
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   227
    towrite = this->spec.size;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   228
    pcmbuffer = this->hidden->pcm_buf;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   230
    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   231
    do {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   232
        written =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   233
            snd_pcm_plugin_write(this->hidden->audio_handle, pcmbuffer,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   234
                                 towrite);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   235
        if (written != towrite) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   236
            /* Check if samples playback got stuck somewhere in hardware or in */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   237
            /* the audio device driver */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   238
            if ((errno == EAGAIN) && (written == 0)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   239
                if (this->hidden->timeout_on_wait != 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   240
                    SDL_SetError("QSA: buffer playback timeout\n");
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   241
                    return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   242
                }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
            }
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   245
            /* Check for errors or conditions */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   246
            if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   247
                /* Let a little CPU time go by and try to write again */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   248
                SDL_Delay(1);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   250
                /* if we wrote some data */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   251
                towrite -= written;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   252
                pcmbuffer += written * this->spec.channels;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   253
                continue;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   254
            } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   255
                if ((errno == EINVAL) || (errno == EIO)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   256
                    SDL_memset(&cstatus, 0, sizeof(cstatus));
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   257
                    if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   258
                        cstatus.channel = SND_PCM_CHANNEL_PLAYBACK;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   259
                    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   260
                        cstatus.channel = SND_PCM_CHANNEL_CAPTURE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   261
                    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   263
                    status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   264
                        snd_pcm_plugin_status(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   265
                                              &cstatus);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   266
                    if (status < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   267
                        QSA_SetError("snd_pcm_plugin_status", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   268
                        return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   269
                    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   271
                    if ((cstatus.status == SND_PCM_STATUS_UNDERRUN) ||
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   272
                        (cstatus.status == SND_PCM_STATUS_READY)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   273
                        if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   274
                            status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   275
                                snd_pcm_plugin_prepare(this->hidden->
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   276
                                                       audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   277
                                                       SND_PCM_CHANNEL_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   278
                        } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   279
                            status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   280
                                snd_pcm_plugin_prepare(this->hidden->
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   281
                                                       audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   282
                                                       SND_PCM_CHANNEL_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   283
                        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   284
                        if (status < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   285
                            QSA_SetError("snd_pcm_plugin_prepare", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   286
                            return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   287
                        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   288
                    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   289
                    continue;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   290
                } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   291
                    return;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   292
                }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
            }
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   294
        } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   295
            /* we wrote all remaining data */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   296
            towrite -= written;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   297
            pcmbuffer += written * this->spec.channels;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   298
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   299
    } while ((towrite > 0) && (this->enabled));
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   301
    /* If we couldn't write, assume fatal error for now */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   302
    if (towrite != 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   303
        this->enabled = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   304
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   307
static Uint8 *
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   308
QSA_GetDeviceBuf(_THIS)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   310
    return this->hidden->pcm_buf;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   313
static void
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   314
QSA_CloseDevice(_THIS)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   316
    if (this->hidden != NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   317
        if (this->hidden->audio_handle != NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   318
            if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   319
                /* Finish playing available samples */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   320
                snd_pcm_plugin_flush(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   321
                                     SND_PCM_CHANNEL_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   322
            } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   323
                /* Cancel unread samples during capture */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   324
                snd_pcm_plugin_flush(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   325
                                     SND_PCM_CHANNEL_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   326
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   327
            snd_pcm_close(this->hidden->audio_handle);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   328
            this->hidden->audio_handle = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   329
        }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   331
        if (this->hidden->pcm_buf != NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   332
            SDL_FreeAudioMem(this->hidden->pcm_buf);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   333
            this->hidden->pcm_buf = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   334
        }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   336
        SDL_free(this->hidden);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   337
        this->hidden = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   338
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   340
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   341
static int
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   342
QSA_OpenDevice(_THIS, const char *devname, int iscapture)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   343
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   344
    int status = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   345
    int format = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   346
    SDL_AudioFormat test_format = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   347
    int found = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   348
    snd_pcm_channel_setup_t csetup;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   349
    snd_pcm_channel_params_t cparams;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   350
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   351
    /* Initialize all variables that we clean on shutdown */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   352
    this->hidden =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   353
        (struct SDL_PrivateAudioData *) SDL_calloc(1,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   354
                                                   (sizeof
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   355
                                                    (struct
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   356
                                                     SDL_PrivateAudioData)));
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   357
    if (this->hidden == NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   358
        SDL_OutOfMemory();
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   359
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   360
    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   361
    SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData));
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   363
    /* Initialize channel transfer parameters to default */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   364
    QSA_InitAudioParams(&cparams);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   365
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   366
    /* Initialize channel direction: capture or playback */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   367
    this->hidden->iscapture = iscapture;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   369
    /* Find deviceid and cardid by device name for playback */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   370
    if ((!this->hidden->iscapture) && (devname != NULL)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   371
        uint32_t device;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   372
        int32_t status;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   373
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   374
        /* Search in the playback devices */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   375
        device = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   376
        do {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   377
            status = SDL_strcmp(qsa_playback_device[device].name, devname);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   378
            if (status == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   379
                /* Found requested device */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   380
                this->hidden->deviceno = qsa_playback_device[device].deviceno;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   381
                this->hidden->cardno = qsa_playback_device[device].cardno;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   382
                break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   383
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   384
            device++;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   385
            if (device >= qsa_playback_devices) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   386
                QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   387
                SDL_SetError("No such playback device");
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   388
                return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   389
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   390
        } while (1);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   391
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   393
    /* Find deviceid and cardid by device name for capture */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   394
    if ((this->hidden->iscapture) && (devname != NULL)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   395
        /* Search in the capture devices */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   396
        uint32_t device;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   397
        int32_t status;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   398
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   399
        /* Searching in the playback devices */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   400
        device = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   401
        do {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   402
            status = SDL_strcmp(qsa_capture_device[device].name, devname);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   403
            if (status == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   404
                /* Found requested device */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   405
                this->hidden->deviceno = qsa_capture_device[device].deviceno;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   406
                this->hidden->cardno = qsa_capture_device[device].cardno;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   407
                break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   408
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   409
            device++;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   410
            if (device >= qsa_capture_devices) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   411
                QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   412
                SDL_SetError("No such capture device");
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   413
                return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   414
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   415
        } while (1);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   416
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   418
    /* Check if SDL requested default audio device */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   419
    if (devname == NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   420
        /* Open system default audio device */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   421
        if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   422
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   423
                                            &this->hidden->cardno,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   424
                                            &this->hidden->deviceno,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   425
                                            SND_PCM_OPEN_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   426
        } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   427
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   428
                                            &this->hidden->cardno,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   429
                                            &this->hidden->deviceno,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   430
                                            SND_PCM_OPEN_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   431
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   432
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   433
        /* Open requested audio device */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   434
        if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   435
            status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   436
                snd_pcm_open(&this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   437
                             this->hidden->cardno, this->hidden->deviceno,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   438
                             SND_PCM_OPEN_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   439
        } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   440
            status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   441
                snd_pcm_open(&this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   442
                             this->hidden->cardno, this->hidden->deviceno,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   443
                             SND_PCM_OPEN_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   444
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   445
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   447
    /* Check if requested device is opened */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   448
    if (status < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   449
        this->hidden->audio_handle = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   450
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   451
        QSA_SetError("snd_pcm_open", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   452
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   453
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   455
    if (!QSA_CheckBuggyCards(this, QSA_MMAP_WORKAROUND)) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   456
        /* Disable QSA MMAP plugin for buggy audio drivers */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   457
        status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   458
            snd_pcm_plugin_set_disable(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   459
                                       PLUGIN_DISABLE_MMAP);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   460
        if (status < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   461
            QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   462
            QSA_SetError("snd_pcm_plugin_set_disable", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   463
            return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   464
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   465
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   466
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   467
    /* Try for a closest match on audio format */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   468
    format = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   469
    /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   470
    found = 0;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   471
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   472
    for (test_format = SDL_FirstAudioFormat(this->spec.format); !found;) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   473
        /* if match found set format to equivalent QSA format */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   474
        switch (test_format) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   475
        case AUDIO_U8:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   476
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   477
                format = SND_PCM_SFMT_U8;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   478
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   479
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   480
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   481
        case AUDIO_S8:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   482
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   483
                format = SND_PCM_SFMT_S8;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   484
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   485
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   486
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   487
        case AUDIO_S16LSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   488
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   489
                format = SND_PCM_SFMT_S16_LE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   490
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   491
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   492
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   493
        case AUDIO_S16MSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   494
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   495
                format = SND_PCM_SFMT_S16_BE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   496
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   497
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   498
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   499
        case AUDIO_U16LSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   500
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   501
                format = SND_PCM_SFMT_U16_LE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   502
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   503
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   504
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   505
        case AUDIO_U16MSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   506
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   507
                format = SND_PCM_SFMT_U16_BE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   508
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   509
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   510
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   511
        case AUDIO_S32LSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   512
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   513
                format = SND_PCM_SFMT_S32_LE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   514
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   515
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   516
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   517
        case AUDIO_S32MSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   518
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   519
                format = SND_PCM_SFMT_S32_BE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   520
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   521
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   522
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   523
        case AUDIO_F32LSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   524
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   525
                format = SND_PCM_SFMT_FLOAT_LE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   526
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   527
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   528
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   529
        case AUDIO_F32MSB:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   530
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   531
                format = SND_PCM_SFMT_FLOAT_BE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   532
                found = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   533
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   534
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   535
        default:
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   536
            {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   537
                break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   538
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   539
        }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   541
        if (!found) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   542
            test_format = SDL_NextAudioFormat();
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   543
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   544
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   545
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   546
    /* assumes test_format not 0 on success */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   547
    if (test_format == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   548
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   549
        SDL_SetError("QSA: Couldn't find any hardware audio formats");
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   550
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   551
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   552
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   553
    this->spec.format = test_format;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   554
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   555
    /* Set the audio format */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   556
    cparams.format.format = format;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   557
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   558
    /* Set mono/stereo/4ch/6ch/8ch audio */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   559
    cparams.format.voices = this->spec.channels;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   560
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   561
    /* Set rate */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   562
    cparams.format.rate = this->spec.freq;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   563
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   564
    /* Setup the transfer parameters according to cparams */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   565
    status = snd_pcm_plugin_params(this->hidden->audio_handle, &cparams);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   566
    if (status < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   567
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   568
        QSA_SetError("snd_pcm_channel_params", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   569
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   570
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   572
    /* Make sure channel is setup right one last time */
6352
a9bcd26e7105 Removed unneeded audio buffer memset() for consistent behavior on all platforms.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   573
    SDL_memset(&csetup, 0, sizeof(csetup));
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   574
    if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   575
        csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   576
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   577
        csetup.channel = SND_PCM_CHANNEL_CAPTURE;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   578
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   580
    /* Setup an audio channel */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   581
    if (snd_pcm_plugin_setup(this->hidden->audio_handle, &csetup) < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   582
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   583
        SDL_SetError("QSA: Unable to setup channel\n");
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   584
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   585
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   586
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   587
    /* Calculate the final parameters for this audio specification */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   588
    SDL_CalculateAudioSpec(&this->spec);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   590
    this->hidden->pcm_len = this->spec.size;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   592
    if (this->hidden->pcm_len == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   593
        this->hidden->pcm_len =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   594
            csetup.buf.block.frag_size * this->spec.channels *
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   595
            (snd_pcm_format_width(format) / 8);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   596
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   597
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   598
    /*
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   599
     * Allocate memory to the audio buffer and initialize with silence
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   600
     *  (Note that buffer size must be a multiple of fragment size, so find
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   601
     *  closest multiple)
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   602
     */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   603
    this->hidden->pcm_buf =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   604
        (Uint8 *) SDL_AllocAudioMem(this->hidden->pcm_len);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   605
    if (this->hidden->pcm_buf == NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   606
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   607
        SDL_OutOfMemory();
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   608
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   609
    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   610
    SDL_memset(this->hidden->pcm_buf, this->spec.silence,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   611
               this->hidden->pcm_len);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   612
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   613
    /* get the file descriptor */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   614
    if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   615
        this->hidden->audio_fd =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   616
            snd_pcm_file_descriptor(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   617
                                    SND_PCM_CHANNEL_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   618
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   619
        this->hidden->audio_fd =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   620
            snd_pcm_file_descriptor(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   621
                                    SND_PCM_CHANNEL_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   622
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   623
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   624
    if (this->hidden->audio_fd < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   625
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   626
        QSA_SetError("snd_pcm_file_descriptor", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   627
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   628
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   629
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   630
    /* Prepare an audio channel */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   631
    if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   632
        /* Prepare audio playback */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   633
        status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   634
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   635
                                   SND_PCM_CHANNEL_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   636
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   637
        /* Prepare audio capture */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   638
        status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   639
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   640
                                   SND_PCM_CHANNEL_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   641
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   642
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   643
    if (status < 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   644
        QSA_CloseDevice(this);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   645
        QSA_SetError("snd_pcm_plugin_prepare", status);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   646
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   647
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   648
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   649
    /* We're really ready to rock and roll. :-) */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   650
    return 1;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5590
diff changeset
   653
static void
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5590
diff changeset
   654
QSA_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   656
    uint32_t it;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   657
    uint32_t cards;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   658
    uint32_t devices;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   659
    int32_t status;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   660
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   661
    /* Detect amount of available devices       */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   662
    /* this value can be changed in the runtime */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   663
    cards = snd_cards();
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   664
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   665
    /* If io-audio manager is not running we will get 0 as number */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   666
    /* of available audio devices                                 */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   667
    if (cards == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   668
        /* We have no any available audio devices */
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5590
diff changeset
   669
        return;
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   670
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   671
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   672
    /* Find requested devices by type */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   673
    if (!iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   674
        /* Playback devices enumeration requested */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   675
        for (it = 0; it < cards; it++) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   676
            devices = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   677
            do {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   678
                status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   679
                    snd_card_get_longname(it,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   680
                                          qsa_playback_device
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   681
                                          [qsa_playback_devices].name,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   682
                                          QSA_MAX_NAME_LENGTH);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   683
                if (status == EOK) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   684
                    snd_pcm_t *handle;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   685
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   686
                    /* Add device number to device name */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   687
                    sprintf(qsa_playback_device[qsa_playback_devices].name +
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   688
                            SDL_strlen(qsa_playback_device
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   689
                                       [qsa_playback_devices].name), " d%d",
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   690
                            devices);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   691
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   692
                    /* Store associated card number id */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   693
                    qsa_playback_device[qsa_playback_devices].cardno = it;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   694
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   695
                    /* Check if this device id could play anything */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   696
                    status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   697
                        snd_pcm_open(&handle, it, devices,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   698
                                     SND_PCM_OPEN_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   699
                    if (status == EOK) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   700
                        qsa_playback_device[qsa_playback_devices].deviceno =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   701
                            devices;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   702
                        status = snd_pcm_close(handle);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   703
                        if (status == EOK) {
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5590
diff changeset
   704
                            addfn(qsa_playback_device[qsa_playback_devices].name);
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   705
                            qsa_playback_devices++;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   706
                        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   707
                    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   708
                        /* Check if we got end of devices list */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   709
                        if (status == -ENOENT) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   710
                            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   711
                        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   712
                    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   713
                } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   714
                    break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   715
                }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   716
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   717
                /* Check if we reached maximum devices count */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   718
                if (qsa_playback_devices >= QSA_MAX_DEVICES) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   719
                    break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   720
                }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   721
                devices++;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   722
            } while (1);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   723
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   724
            /* Check if we reached maximum devices count */
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   725
            if (qsa_playback_devices >= QSA_MAX_DEVICES) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   726
                break;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   727
            }
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   728
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   729
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   730
        /* Capture devices enumeration requested */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   731
        for (it = 0; it < cards; it++) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   732
            devices = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   733
            do {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   734
                status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   735
                    snd_card_get_longname(it,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   736
                                          qsa_capture_device
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   737
                                          [qsa_capture_devices].name,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   738
                                          QSA_MAX_NAME_LENGTH);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   739
                if (status == EOK) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   740
                    snd_pcm_t *handle;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   741
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   742
                    /* Add device number to device name */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   743
                    sprintf(qsa_capture_device[qsa_capture_devices].name +
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   744
                            SDL_strlen(qsa_capture_device
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   745
                                       [qsa_capture_devices].name), " d%d",
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   746
                            devices);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   747
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   748
                    /* Store associated card number id */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   749
                    qsa_capture_device[qsa_capture_devices].cardno = it;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   750
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   751
                    /* Check if this device id could play anything */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   752
                    status =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   753
                        snd_pcm_open(&handle, it, devices,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   754
                                     SND_PCM_OPEN_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   755
                    if (status == EOK) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   756
                        qsa_capture_device[qsa_capture_devices].deviceno =
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   757
                            devices;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   758
                        status = snd_pcm_close(handle);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   759
                        if (status == EOK) {
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5590
diff changeset
   760
                            addfn(qsa_capture_device[qsa_capture_devices].name);
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   761
                            qsa_capture_devices++;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   762
                        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   763
                    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   764
                        /* Check if we got end of devices list */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   765
                        if (status == -ENOENT) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   766
                            break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   767
                        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   768
                    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   769
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   770
                    /* Check if we reached maximum devices count */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   771
                    if (qsa_capture_devices >= QSA_MAX_DEVICES) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   772
                        break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   773
                    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   774
                } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   775
                    break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   776
                }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   777
                devices++;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   778
            } while (1);
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   779
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   780
            /* Check if we reached maximum devices count */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   781
            if (qsa_capture_devices >= QSA_MAX_DEVICES) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   782
                break;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   783
            }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   784
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   785
    }
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   786
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
5584
9550c786ba3b Mark some QSA audio driver functions as static.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   788
static void
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   789
QSA_WaitDone(_THIS)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   790
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   791
    if (!this->hidden->iscapture) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   792
        if (this->hidden->audio_handle != NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   793
            /* Wait till last fragment is played and stop channel */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   794
            snd_pcm_plugin_flush(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   795
                                 SND_PCM_CHANNEL_PLAYBACK);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   796
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   797
    } else {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   798
        if (this->hidden->audio_handle != NULL) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   799
            /* Discard all unread data and stop channel */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   800
            snd_pcm_plugin_flush(this->hidden->audio_handle,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   801
                                 SND_PCM_CHANNEL_CAPTURE);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   802
        }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   803
    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   804
}
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   805
5584
9550c786ba3b Mark some QSA audio driver functions as static.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   806
static void
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   807
QSA_Deinitialize(void)
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   808
{
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   809
    /* Clear devices array on shutdown */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   810
    SDL_memset(qsa_playback_device, 0x00,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   811
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   812
    SDL_memset(qsa_capture_device, 0x00,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   813
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   814
    qsa_playback_devices = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   815
    qsa_capture_devices = 0;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   816
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   817
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   818
static int
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   819
QSA_Init(SDL_AudioDriverImpl * impl)
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   820
{
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   821
    snd_pcm_t *handle = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   822
    int32_t status = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   823
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   824
    /* Clear devices array */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   825
    SDL_memset(qsa_playback_device, 0x00,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   826
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   827
    SDL_memset(qsa_capture_device, 0x00,
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   828
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   829
    qsa_playback_devices = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   830
    qsa_capture_devices = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   831
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   832
    /* Set function pointers                                     */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   833
    /* DeviceLock and DeviceUnlock functions are used default,   */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   834
    /* provided by SDL, which uses pthread_mutex for lock/unlock */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   835
    impl->DetectDevices = QSA_DetectDevices;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   836
    impl->OpenDevice = QSA_OpenDevice;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   837
    impl->ThreadInit = QSA_ThreadInit;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   838
    impl->WaitDevice = QSA_WaitDevice;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   839
    impl->PlayDevice = QSA_PlayDevice;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   840
    impl->GetDeviceBuf = QSA_GetDeviceBuf;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   841
    impl->CloseDevice = QSA_CloseDevice;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   842
    impl->WaitDone = QSA_WaitDone;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   843
    impl->Deinitialize = QSA_Deinitialize;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   844
    impl->LockDevice = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   845
    impl->UnlockDevice = NULL;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   846
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   847
    impl->OnlyHasDefaultOutputDevice = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   848
    impl->ProvidesOwnCallbackThread = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   849
    impl->SkipMixerLock = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   850
    impl->HasCaptureSupport = 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   851
    impl->OnlyHasDefaultOutputDevice = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   852
    impl->OnlyHasDefaultInputDevice = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   853
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   854
    /* Check if io-audio manager is running or not */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   855
    status = snd_cards();
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   856
    if (status == 0) {
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   857
        /* if no, return immediately */
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   858
        return 1;
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   859
    }
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   860
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   861
    return 1;   /* this audio target is available. */
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   862
}
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   863
3139
Sam Lantinga <slouken@libsdl.org>
parents: 3123
diff changeset
   864
AudioBootStrap QSAAUDIO_bootstrap = {
5594
e741303e08a8 Removed needless macros in various audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5593
diff changeset
   865
    "qsa", "QNX QSA Audio", QSA_Init, 0
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   866
};
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   867
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
   868
#endif /* SDL_AUDIO_DRIVER_QSA */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
   869
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   870
/* vi: set ts=4 sw=4 expandtab: */