zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_androidaudio.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22 
23 #if SDL_AUDIO_DRIVER_ANDROID
24 
25 /* Output audio to Android */
26 
27 #include "SDL_audio.h"
28 #include "../SDL_audio_c.h"
29 #include "SDL_androidaudio.h"
30 
31 #include "../../core/android/SDL_android.h"
32 
33 #include <android/log.h>
34 
35 static void * audioDevice;
36 
37 static int
38 AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
39 {
40  SDL_AudioFormat test_format;
41 
42  if (iscapture) {
43  /* TODO: implement capture */
44  return SDL_SetError("Capture not supported on Android");
45  }
46 
47  if (audioDevice != NULL) {
48  return SDL_SetError("Only one audio device at a time please!");
49  }
50 
51  audioDevice = this;
52 
53  test_format = SDL_FirstAudioFormat(this->spec.format);
54  while (test_format != 0) { /* no "UNKNOWN" constant */
55  if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
56  this->spec.format = test_format;
57  break;
58  }
59  test_format = SDL_NextAudioFormat();
60  }
61 
62  if (test_format == 0) {
63  /* Didn't find a compatible format :( */
64  return SDL_SetError("No compatible audio format!");
65  }
66 
67  if (this->spec.channels > 1) {
68  this->spec.channels = 2;
69  } else {
70  this->spec.channels = 1;
71  }
72 
73  if (this->spec.freq < 8000) {
74  this->spec.freq = 8000;
75  }
76  if (this->spec.freq > 48000) {
77  this->spec.freq = 48000;
78  }
79 
80  /* TODO: pass in/return a (Java) device ID, also whether we're opening for input or output */
81  this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
82  SDL_CalculateAudioSpec(&this->spec);
83 
84  if (this->spec.samples == 0) {
85  /* Init failed? */
86  return SDL_SetError("Java-side initialization failed!");
87  }
88 
89  return 0;
90 }
91 
92 static void
93 AndroidAUD_PlayDevice(_THIS)
94 {
96 }
97 
98 static Uint8 *
99 AndroidAUD_GetDeviceBuf(_THIS)
100 {
102 }
103 
104 static void
106 {
107  /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread
108  so it's safe to terminate the Java side buffer and AudioTrack
109  */
111 
112  if (audioDevice == this) {
113  audioDevice = NULL;
114  }
115 }
116 
117 static int
118 AndroidAUD_Init(SDL_AudioDriverImpl * impl)
119 {
120  /* Set the function pointers */
121  impl->OpenDevice = AndroidAUD_OpenDevice;
122  impl->PlayDevice = AndroidAUD_PlayDevice;
123  impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
125 
126  /* and the capabilities */
127  impl->HasCaptureSupport = 0; /* TODO */
128  impl->OnlyHasDefaultOutputDevice = 1;
129  impl->OnlyHasDefaultInputDevice = 1;
130 
131  return 1; /* this audio target is available. */
132 }
133 
135  "android", "SDL Android audio driver", AndroidAUD_Init, 0
136 };
137 
138 #endif /* SDL_AUDIO_DRIVER_ANDROID */
139 
140 /* vi: set ts=4 sw=4 expandtab: */
141 
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:45
SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format)
Definition: SDL_audio.c:1226
#define NULL
Definition: ftobjs.h:61
Uint8 *(* GetDeviceBuf)(_THIS)
Definition: SDL_sysaudio.h:43
Uint16 SDL_AudioFormat
Audio format flags.
Definition: SDL_audio.h:64
int(* OpenDevice)(_THIS, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:39
SDL_AudioFormat SDL_NextAudioFormat(void)
Definition: SDL_audio.c:1238
void(* PlayDevice)(_THIS)
Definition: SDL_sysaudio.h:42
#define AUDIO_U8
Definition: SDL_audio.h:89
#define _THIS
void Android_JNI_CloseAudioDevice()
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1247
static void AndroidAUD_CloseDevice(_THIS)
void * Android_JNI_GetAudioBuffer()
void Android_JNI_WriteAudioBuffer()
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:129
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
AudioBootStrap ANDROIDAUD_bootstrap