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