Mercurial > sdl-ios-xcode
annotate src/audio/android/SDL_androidaudio.c @ 4995:9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 13 Jan 2011 11:14:20 -0800 |
parents | 58b6bb4a45e9 |
children | 8d7315668e35 |
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 |
4989 | 30 #include "../../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 { | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
106 Android_JNI_WriteAudioBufferAndUnpin(); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
107 this->hidden->mixbuf = NULL; |
4718 | 108 } |
109 | |
110 static Uint8 * | |
111 AndroidAUD_GetDeviceBuf(_THIS) | |
112 { | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
113 if (this->hidden->mixbuf == NULL) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
114 this->hidden->mixbuf = Android_JNI_PinAudioBuffer(); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
115 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
116 return this->hidden->mixbuf; |
4718 | 117 } |
118 | |
119 static void | |
120 AndroidAUD_CloseDevice(_THIS) | |
121 { | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
122 if (this->hidden != NULL) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
123 if (this->hidden->mixbuf != NULL) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
124 Android_JNI_WriteAudioBufferAndUnpin(); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
125 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
126 SDL_free(this->hidden); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
127 this->hidden = NULL; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
128 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
129 Android_JNI_CloseAudioDevice(); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
130 |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
131 if (audioDevice == this) { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
132 audioDevice = NULL; |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
133 } |
4718 | 134 } |
135 | |
136 static int | |
137 AndroidAUD_Init(SDL_AudioDriverImpl * impl) | |
138 { | |
139 /* Set the function pointers */ | |
140 impl->OpenDevice = AndroidAUD_OpenDevice; | |
141 impl->PlayDevice = AndroidAUD_PlayDevice; | |
142 impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf; | |
143 impl->CloseDevice = AndroidAUD_CloseDevice; | |
144 | |
145 /* and the capabilities */ | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
146 impl->ProvidesOwnCallbackThread = 1; |
4718 | 147 impl->HasCaptureSupport = 0; //TODO |
148 impl->OnlyHasDefaultOutputDevice = 1; | |
149 impl->OnlyHasDefaultInputDevice = 1; | |
150 | |
151 return 1; /* this audio target is available. */ | |
152 } | |
153 | |
154 AudioBootStrap ANDROIDAUD_bootstrap = { | |
155 "android", "SDL Android audio driver", AndroidAUD_Init, 0 /*1? */ | |
156 }; | |
157 | |
4995
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
158 /* 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
|
159 void |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
160 Android_RunAudioThread() |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
161 { |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
162 SDL_RunAudio(audioDevice); |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
163 } |
9f9bea41e88f
Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents:
4989
diff
changeset
|
164 |
4718 | 165 /* vi: set ts=4 sw=4 expandtab: */ |