Mercurial > sdl-ios-xcode
annotate src/audio/android/SDL_androidaudio.c @ 5216:25ad4a50d34f
OpenGL ES 2.0 rendering LIVES!
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 06 Feb 2011 19:12:55 -0800 |
parents | 327f181542f1 |
children | b530ef003506 |
rev | line source |
---|---|
4718 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997-2010 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Lesser General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2.1 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Lesser General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Lesser General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 #include "SDL_config.h" | |
23 | |
24 /* Output audio to Android */ | |
25 | |
26 #include "SDL_audio.h" | |
27 #include "../SDL_audio_c.h" | |
28 #include "SDL_androidaudio.h" | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
29 |
5092
327f181542f1
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents:
4996
diff
changeset
|
30 #include "../../core/android/SDL_android.h" |
4724 | 31 |
4718 | 32 #include <android/log.h> |
33 | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
34 static void * audioDevice; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
35 |
4718 | 36 static int |
37 AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture) | |
38 { | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
39 SDL_AudioFormat test_format; |
4724 | 40 int valid_datatype = 0; |
41 | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
42 if (iscapture) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
43 //TODO: implement capture |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
44 SDL_SetError("Capture not supported on Android"); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
45 return 0; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
46 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
47 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
48 if (audioDevice != NULL) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
49 SDL_SetError("Only one audio device at a time please!"); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
50 return 0; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
51 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
52 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
53 audioDevice = this; |
4724 | 54 |
55 this->hidden = SDL_malloc(sizeof(*(this->hidden))); | |
56 if (!this->hidden) { | |
57 SDL_OutOfMemory(); | |
58 return 0; | |
59 } | |
60 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); | |
61 | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
62 test_format = SDL_FirstAudioFormat(this->spec.format); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
63 while (test_format != 0) { // no "UNKNOWN" constant |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
64 if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
65 this->spec.format = test_format; |
4724 | 66 break; |
67 } | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
68 test_format = SDL_NextAudioFormat(); |
4724 | 69 } |
4718 | 70 |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
71 if (test_format == 0) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
72 // Didn't find a compatible format :( |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
73 SDL_SetError("No compatible audio format!"); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
74 return 0; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
75 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
76 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
77 if (this->spec.channels > 1) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
78 this->spec.channels = 2; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
79 } else { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
80 this->spec.channels = 1; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
81 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
82 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
83 if (this->spec.freq < 8000) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
84 this->spec.freq = 8000; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
85 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
86 if (this->spec.freq > 48000) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
87 this->spec.freq = 48000; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
88 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
89 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
90 // TODO: pass in/return a (Java) device ID, also whether we're opening for input or output |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
91 this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
92 SDL_CalculateAudioSpec(&this->spec); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
93 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
94 if (this->spec.samples == 0) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
95 // Init failed? |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
96 SDL_SetError("Java-side initialization failed!"); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
97 return 0; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
98 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
99 |
4718 | 100 return 1; |
101 } | |
102 | |
103 static void | |
104 AndroidAUD_PlayDevice(_THIS) | |
105 { | |
4996
8d7315668e35
Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents:
4995
diff
changeset
|
106 Android_JNI_WriteAudioBuffer(); |
4718 | 107 } |
108 | |
109 static Uint8 * | |
110 AndroidAUD_GetDeviceBuf(_THIS) | |
111 { | |
4996
8d7315668e35
Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents:
4995
diff
changeset
|
112 return Android_JNI_GetAudioBuffer(); |
4718 | 113 } |
114 | |
115 static void | |
116 AndroidAUD_CloseDevice(_THIS) | |
117 { | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
118 if (this->hidden != NULL) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
119 SDL_free(this->hidden); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
120 this->hidden = NULL; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
121 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
122 Android_JNI_CloseAudioDevice(); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
123 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
124 if (audioDevice == this) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
125 audioDevice = NULL; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
126 } |
4718 | 127 } |
128 | |
129 static int | |
130 AndroidAUD_Init(SDL_AudioDriverImpl * impl) | |
131 { | |
132 /* Set the function pointers */ | |
133 impl->OpenDevice = AndroidAUD_OpenDevice; | |
134 impl->PlayDevice = AndroidAUD_PlayDevice; | |
135 impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf; | |
136 impl->CloseDevice = AndroidAUD_CloseDevice; | |
137 | |
138 /* and the capabilities */ | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
139 impl->ProvidesOwnCallbackThread = 1; |
4718 | 140 impl->HasCaptureSupport = 0; //TODO |
141 impl->OnlyHasDefaultOutputDevice = 1; | |
142 impl->OnlyHasDefaultInputDevice = 1; | |
143 | |
144 return 1; /* this audio target is available. */ | |
145 } | |
146 | |
147 AudioBootStrap ANDROIDAUD_bootstrap = { | |
148 "android", "SDL Android audio driver", AndroidAUD_Init, 0 /*1? */ | |
149 }; | |
150 | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
151 /* Called by the Java code to start the audio processing on a thread */ |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
152 void |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
153 Android_RunAudioThread() |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
154 { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
155 SDL_RunAudio(audioDevice); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
156 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
157 |
4718 | 158 /* vi: set ts=4 sw=4 expandtab: */ |