src/audio/android/SDL_androidaudio.c
author Sam Lantinga
Sat, 18 May 2013 14:17:52 -0700
changeset 7191 75360622e65f
parent 7130 28df4f1a1712
child 7612 627d571587d3
permissions -rw-r--r--
File style cleanup for the SDL 2.0 release
paul@4718
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
paul@4718
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
paul@4718
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
paul@4718
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
paul@4718
    20
*/
paul@4718
    21
#include "SDL_config.h"
paul@4718
    22
slouken@6044
    23
#if SDL_AUDIO_DRIVER_ANDROID
slouken@6044
    24
paul@4718
    25
/* Output audio to Android */
paul@4718
    26
paul@4718
    27
#include "SDL_audio.h"
paul@4718
    28
#include "../SDL_audio_c.h"
paul@4718
    29
#include "SDL_androidaudio.h"
slouken@4995
    30
slouken@5090
    31
#include "../../core/android/SDL_android.h"
paul@4724
    32
paul@4718
    33
#include <android/log.h>
paul@4718
    34
slouken@4995
    35
static void * audioDevice;
slouken@4995
    36
paul@4718
    37
static int
paul@4718
    38
AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
paul@4718
    39
{
slouken@4995
    40
    SDL_AudioFormat test_format;
philipp@7130
    41
slouken@4995
    42
    if (iscapture) {
slouken@7191
    43
        /* TODO: implement capture */
slouken@7191
    44
        return SDL_SetError("Capture not supported on Android");
slouken@4995
    45
    }
slouken@4995
    46
slouken@4995
    47
    if (audioDevice != NULL) {
slouken@7191
    48
        return SDL_SetError("Only one audio device at a time please!");
slouken@4995
    49
    }
slouken@4995
    50
slouken@4995
    51
    audioDevice = this;
paul@4724
    52
paul@4724
    53
    this->hidden = SDL_malloc(sizeof(*(this->hidden)));
paul@4724
    54
    if (!this->hidden) {
icculus@7038
    55
        return SDL_OutOfMemory();
paul@4724
    56
    }
paul@4724
    57
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
paul@4724
    58
slouken@4995
    59
    test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@7191
    60
    while (test_format != 0) { /* no "UNKNOWN" constant */
slouken@4995
    61
        if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
slouken@4995
    62
            this->spec.format = test_format;
paul@4724
    63
            break;
paul@4724
    64
        }
slouken@4995
    65
        test_format = SDL_NextAudioFormat();
paul@4724
    66
    }
slouken@7191
    67
slouken@4995
    68
    if (test_format == 0) {
slouken@7191
    69
        /* Didn't find a compatible format :( */
slouken@7191
    70
        return SDL_SetError("No compatible audio format!");
slouken@4995
    71
    }
slouken@4995
    72
slouken@4995
    73
    if (this->spec.channels > 1) {
slouken@7191
    74
        this->spec.channels = 2;
slouken@4995
    75
    } else {
slouken@7191
    76
        this->spec.channels = 1;
slouken@4995
    77
    }
slouken@4995
    78
slouken@4995
    79
    if (this->spec.freq < 8000) {
slouken@7191
    80
        this->spec.freq = 8000;
slouken@4995
    81
    }
slouken@4995
    82
    if (this->spec.freq > 48000) {
slouken@7191
    83
        this->spec.freq = 48000;
slouken@4995
    84
    }
slouken@4995
    85
slouken@7191
    86
    /* TODO: pass in/return a (Java) device ID, also whether we're opening for input or output */
slouken@4995
    87
    this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
slouken@4995
    88
    SDL_CalculateAudioSpec(&this->spec);
slouken@4995
    89
slouken@4995
    90
    if (this->spec.samples == 0) {
slouken@7191
    91
        /* Init failed? */
slouken@7191
    92
        return SDL_SetError("Java-side initialization failed!");
slouken@4995
    93
    }
slouken@4995
    94
icculus@7038
    95
    return 0;
paul@4718
    96
}
paul@4718
    97
paul@4718
    98
static void
paul@4718
    99
AndroidAUD_PlayDevice(_THIS)
paul@4718
   100
{
slouken@4996
   101
    Android_JNI_WriteAudioBuffer();
paul@4718
   102
}
paul@4718
   103
paul@4718
   104
static Uint8 *
paul@4718
   105
AndroidAUD_GetDeviceBuf(_THIS)
paul@4718
   106
{
slouken@4996
   107
    return Android_JNI_GetAudioBuffer();
paul@4718
   108
}
paul@4718
   109
paul@4718
   110
static void
paul@4718
   111
AndroidAUD_CloseDevice(_THIS)
paul@4718
   112
{
slouken@4995
   113
    if (this->hidden != NULL) {
slouken@7191
   114
        SDL_free(this->hidden);
slouken@7191
   115
        this->hidden = NULL;
slouken@4995
   116
    }
slouken@7191
   117
    Android_JNI_CloseAudioDevice();
slouken@4995
   118
slouken@4995
   119
    if (audioDevice == this) {
slouken@7191
   120
        audioDevice = NULL;
slouken@4995
   121
    }
paul@4718
   122
}
paul@4718
   123
paul@4718
   124
static int
paul@4718
   125
AndroidAUD_Init(SDL_AudioDriverImpl * impl)
paul@4718
   126
{
paul@4718
   127
    /* Set the function pointers */
paul@4718
   128
    impl->OpenDevice = AndroidAUD_OpenDevice;
paul@4718
   129
    impl->PlayDevice = AndroidAUD_PlayDevice;
paul@4718
   130
    impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
paul@4718
   131
    impl->CloseDevice = AndroidAUD_CloseDevice;
paul@4718
   132
paul@4718
   133
    /* and the capabilities */
slouken@4995
   134
    impl->ProvidesOwnCallbackThread = 1;
slouken@7191
   135
    impl->HasCaptureSupport = 0; /* TODO */
paul@4718
   136
    impl->OnlyHasDefaultOutputDevice = 1;
paul@4718
   137
    impl->OnlyHasDefaultInputDevice = 1;
paul@4718
   138
paul@4718
   139
    return 1;   /* this audio target is available. */
paul@4718
   140
}
paul@4718
   141
paul@4718
   142
AudioBootStrap ANDROIDAUD_bootstrap = {
icculus@5579
   143
    "android", "SDL Android audio driver", AndroidAUD_Init, 0
paul@4718
   144
};
paul@4718
   145
slouken@4995
   146
/* Called by the Java code to start the audio processing on a thread */
slouken@4995
   147
void
slouken@4995
   148
Android_RunAudioThread()
slouken@4995
   149
{
slouken@7191
   150
    SDL_RunAudio(audioDevice);
slouken@4995
   151
}
slouken@4995
   152
slouken@6044
   153
#endif /* SDL_AUDIO_DRIVER_ANDROID */
slouken@6044
   154
paul@4718
   155
/* vi: set ts=4 sw=4 expandtab: */