src/audio/android/SDL_androidaudio.c
author Sam Lantinga
Sat, 06 Oct 2012 12:16:32 -0700
changeset 6566 dd7e57847ea9
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rw-r--r--
Add flags to the vidmode debug output
paul@4718
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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;
paul@4724
    41
    int valid_datatype = 0;
paul@4724
    42
    
slouken@4995
    43
    if (iscapture) {
slouken@4995
    44
    	//TODO: implement capture
slouken@4995
    45
    	SDL_SetError("Capture not supported on Android");
slouken@4995
    46
    	return 0;
slouken@4995
    47
    }
slouken@4995
    48
slouken@4995
    49
    if (audioDevice != NULL) {
slouken@4995
    50
    	SDL_SetError("Only one audio device at a time please!");
slouken@4995
    51
    	return 0;
slouken@4995
    52
    }
slouken@4995
    53
slouken@4995
    54
    audioDevice = this;
paul@4724
    55
paul@4724
    56
    this->hidden = SDL_malloc(sizeof(*(this->hidden)));
paul@4724
    57
    if (!this->hidden) {
paul@4724
    58
        SDL_OutOfMemory();
paul@4724
    59
        return 0;
paul@4724
    60
    }
paul@4724
    61
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
paul@4724
    62
slouken@4995
    63
    test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@4995
    64
    while (test_format != 0) { // no "UNKNOWN" constant
slouken@4995
    65
        if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
slouken@4995
    66
            this->spec.format = test_format;
paul@4724
    67
            break;
paul@4724
    68
        }
slouken@4995
    69
        test_format = SDL_NextAudioFormat();
paul@4724
    70
    }
paul@4718
    71
    
slouken@4995
    72
    if (test_format == 0) {
slouken@4995
    73
    	// Didn't find a compatible format :(
slouken@4995
    74
    	SDL_SetError("No compatible audio format!");
slouken@4995
    75
    	return 0;
slouken@4995
    76
    }
slouken@4995
    77
slouken@4995
    78
    if (this->spec.channels > 1) {
slouken@4995
    79
    	this->spec.channels = 2;
slouken@4995
    80
    } else {
slouken@4995
    81
    	this->spec.channels = 1;
slouken@4995
    82
    }
slouken@4995
    83
slouken@4995
    84
    if (this->spec.freq < 8000) {
slouken@4995
    85
    	this->spec.freq = 8000;
slouken@4995
    86
    }
slouken@4995
    87
    if (this->spec.freq > 48000) {
slouken@4995
    88
    	this->spec.freq = 48000;
slouken@4995
    89
    }
slouken@4995
    90
slouken@4995
    91
    // TODO: pass in/return a (Java) device ID, also whether we're opening for input or output
slouken@4995
    92
    this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
slouken@4995
    93
    SDL_CalculateAudioSpec(&this->spec);
slouken@4995
    94
slouken@4995
    95
    if (this->spec.samples == 0) {
slouken@4995
    96
    	// Init failed?
slouken@4995
    97
    	SDL_SetError("Java-side initialization failed!");
slouken@4995
    98
    	return 0;
slouken@4995
    99
    }
slouken@4995
   100
paul@4718
   101
    return 1;
paul@4718
   102
}
paul@4718
   103
paul@4718
   104
static void
paul@4718
   105
AndroidAUD_PlayDevice(_THIS)
paul@4718
   106
{
slouken@4996
   107
    Android_JNI_WriteAudioBuffer();
paul@4718
   108
}
paul@4718
   109
paul@4718
   110
static Uint8 *
paul@4718
   111
AndroidAUD_GetDeviceBuf(_THIS)
paul@4718
   112
{
slouken@4996
   113
    return Android_JNI_GetAudioBuffer();
paul@4718
   114
}
paul@4718
   115
paul@4718
   116
static void
paul@4718
   117
AndroidAUD_CloseDevice(_THIS)
paul@4718
   118
{
slouken@4995
   119
    if (this->hidden != NULL) {
slouken@4995
   120
    	SDL_free(this->hidden);
slouken@4995
   121
    	this->hidden = NULL;
slouken@4995
   122
    }
slouken@4995
   123
	Android_JNI_CloseAudioDevice();
slouken@4995
   124
slouken@4995
   125
    if (audioDevice == this) {
slouken@4995
   126
    	audioDevice = NULL;
slouken@4995
   127
    }
paul@4718
   128
}
paul@4718
   129
paul@4718
   130
static int
paul@4718
   131
AndroidAUD_Init(SDL_AudioDriverImpl * impl)
paul@4718
   132
{
paul@4718
   133
    /* Set the function pointers */
paul@4718
   134
    impl->OpenDevice = AndroidAUD_OpenDevice;
paul@4718
   135
    impl->PlayDevice = AndroidAUD_PlayDevice;
paul@4718
   136
    impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
paul@4718
   137
    impl->CloseDevice = AndroidAUD_CloseDevice;
paul@4718
   138
paul@4718
   139
    /* and the capabilities */
slouken@4995
   140
    impl->ProvidesOwnCallbackThread = 1;
paul@4718
   141
    impl->HasCaptureSupport = 0; //TODO
paul@4718
   142
    impl->OnlyHasDefaultOutputDevice = 1;
paul@4718
   143
    impl->OnlyHasDefaultInputDevice = 1;
paul@4718
   144
paul@4718
   145
    return 1;   /* this audio target is available. */
paul@4718
   146
}
paul@4718
   147
paul@4718
   148
AudioBootStrap ANDROIDAUD_bootstrap = {
icculus@5579
   149
    "android", "SDL Android audio driver", AndroidAUD_Init, 0
paul@4718
   150
};
paul@4718
   151
slouken@4995
   152
/* Called by the Java code to start the audio processing on a thread */
slouken@4995
   153
void
slouken@4995
   154
Android_RunAudioThread()
slouken@4995
   155
{
slouken@4995
   156
	SDL_RunAudio(audioDevice);
slouken@4995
   157
}
slouken@4995
   158
slouken@6044
   159
#endif /* SDL_AUDIO_DRIVER_ANDROID */
slouken@6044
   160
paul@4718
   161
/* vi: set ts=4 sw=4 expandtab: */