Merge commit 'e4a80875519617775184e9f6be5ede51772850fa' into main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1f42cf5..12d6ece 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2544,6 +2544,12 @@ elseif(PS2)
file(GLOB PS2_MAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/ps2/*.c)
set(SDLMAIN_SOURCES ${SDLMAIN_SOURCES} ${PS2_MAIN_SOURCES})
+ if(SDL_AUDIO)
+ set(SDL_AUDIO_DRIVER_PS2 1)
+ file(GLOB PS2_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/ps2/*.c)
+ set(SOURCE_FILES ${SOURCE_FILES} ${PS2_AUDIO_SOURCES})
+ set(HAVE_SDL_AUDIO TRUE)
+ endif()
if(SDL_FILESYSTEM)
set(SDL_FILESYSTEM_PS2 1)
file(GLOB PS2_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/ps2/*.c)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 129e808..043817f 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -325,6 +325,7 @@
#cmakedefine SDL_AUDIO_DRIVER_OS2 @SDL_AUDIO_DRIVER_OS2@
#cmakedefine SDL_AUDIO_DRIVER_VITA @SDL_AUDIO_DRIVER_VITA@
#cmakedefine SDL_AUDIO_DRIVER_PSP @SDL_AUDIO_DRIVER_PSP@
+#cmakedefine SDL_AUDIO_DRIVER_PS2 @SDL_AUDIO_DRIVER_PS2@
/* Enable various input drivers */
#cmakedefine SDL_INPUT_LINUXEV @SDL_INPUT_LINUXEV@
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 0ce2ae6..3ce3cb9 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -95,6 +95,9 @@ static const AudioBootStrap *const bootstrap[] = {
#if SDL_AUDIO_DRIVER_ANDROID
&ANDROIDAUDIO_bootstrap,
#endif
+#if SDL_AUDIO_DRIVER_PS2
+ &PS2AUDIO_bootstrap,
+#endif
#if SDL_AUDIO_DRIVER_PSP
&PSPAUDIO_bootstrap,
#endif
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 8aa7298..d7e1685 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -204,6 +204,7 @@ extern AudioBootStrap FUSIONSOUND_bootstrap;
extern AudioBootStrap aaudio_bootstrap;
extern AudioBootStrap openslES_bootstrap;
extern AudioBootStrap ANDROIDAUDIO_bootstrap;
+extern AudioBootStrap PS2AUDIO_bootstrap;
extern AudioBootStrap PSPAUDIO_bootstrap;
extern AudioBootStrap VITAAUD_bootstrap;
extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
diff --git a/src/audio/ps2/SDL_ps2audio.c b/src/audio/ps2/SDL_ps2audio.c
new file mode 100644
index 0000000..daca266
--- /dev/null
+++ b/src/audio/ps2/SDL_ps2audio.c
@@ -0,0 +1,163 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+/* Output audio to nowhere... */
+
+#include "SDL_timer.h"
+#include "SDL_audio.h"
+#include "../SDL_audio_c.h"
+#include "SDL_ps2audio.h"
+
+#include <kernel.h>
+#include <malloc.h>
+#include <audsrv.h>
+#include <ps2_audio_driver.h>
+
+/* The tag name used by PS2 audio */
+#define PS2AUDIO_DRIVER_NAME "ps2"
+
+static int
+PS2AUDIO_OpenDevice(_THIS, const char *devname)
+{
+ int i, mixlen;
+ struct audsrv_fmt_t format;
+
+ this->hidden = (struct SDL_PrivateAudioData *)
+ SDL_malloc(sizeof(*this->hidden));
+ if (this->hidden == NULL) {
+ return SDL_OutOfMemory();
+ }
+ SDL_zerop(this->hidden);
+
+ /* This is the native supported audio PS2 config */
+ this->spec.freq = 48000;
+ this->spec.samples = 512;
+ this->spec.channels = 2;
+ this->spec.format = AUDIO_S16LSB;
+
+ SDL_CalculateAudioSpec(&this->spec);
+
+ format.bits = 16;
+ format.freq = this->spec.freq;
+ format.channels = this->spec.channels;
+
+ this->hidden->channel = audsrv_set_format(&format);
+ audsrv_set_volume(MAX_VOLUME);
+
+ if (this->hidden->channel < 0) {
+ free(this->hidden->rawbuf);
+ this->hidden->rawbuf = NULL;
+ return SDL_SetError("Couldn't reserve hardware channel");
+ }
+
+ /* Update the fragment size as size in bytes. */
+ SDL_CalculateAudioSpec(&this->spec);
+
+ /* Allocate the mixing buffer. Its size and starting address must
+ be a multiple of 64 bytes. Our sample count is already a multiple of
+ 64, so spec->size should be a multiple of 64 as well. */
+ mixlen = this->spec.size * NUM_BUFFERS;
+ this->hidden->rawbuf = (Uint8 *) memalign(64, mixlen);
+ if (this->hidden->rawbuf == NULL) {
+ return SDL_SetError("Couldn't allocate mixing buffer");
+ }
+
+ SDL_memset(this->hidden->rawbuf, 0, mixlen);
+ for (i = 0; i < NUM_BUFFERS; i++) {
+ this->hidden->mixbufs[i] = &this->hidden->rawbuf[i * this->spec.size];
+ }
+
+ this->hidden->next_buffer = 0;
+ return 0;
+}
+
+static void PS2AUDIO_PlayDevice(_THIS)
+{
+ uint8_t *mixbuf = this->hidden->mixbufs[this->hidden->next_buffer];
+ audsrv_play_audio((char *)mixbuf, this->spec.size);
+
+ this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
+}
+
+/* This function waits until it is possible to write a full sound buffer */
+static void PS2AUDIO_WaitDevice(_THIS)
+{
+ audsrv_wait_audio(this->spec.size);
+}
+
+static Uint8 *PS2AUDIO_GetDeviceBuf(_THIS)
+{
+ return this->hidden->mixbufs[this->hidden->next_buffer];
+}
+
+static void PS2AUDIO_CloseDevice(_THIS)
+{
+ if (this->hidden->channel >= 0) {
+ audsrv_stop_audio();
+ this->hidden->channel = -1;
+ }
+
+ if (this->hidden->rawbuf != NULL) {
+ free(this->hidden->rawbuf);
+ this->hidden->rawbuf = NULL;
+ }
+}
+
+static void PS2AUDIO_ThreadInit(_THIS)
+{
+ /* Increase the priority of this audio thread by 1 to put it
+ ahead of other SDL threads. */
+ int32_t thid;
+ ee_thread_status_t status;
+ thid = GetThreadId();
+ if (ReferThreadStatus(GetThreadId(), &status) == 0) {
+ ChangeThreadPriority(thid, status.current_priority);
+ }
+}
+
+static void PS2AUDIO_Deinitialize(void)
+{
+ deinit_audio_driver();
+}
+
+static SDL_bool PS2AUDIO_Init(SDL_AudioDriverImpl * impl)
+{
+ if(init_audio_driver() < 0)
+ return SDL_FALSE;
+
+ /* Set the function pointers */
+ impl->OpenDevice = PS2AUDIO_OpenDevice;
+ impl->PlayDevice = PS2AUDIO_PlayDevice;
+ impl->WaitDevice = PS2AUDIO_WaitDevice;
+ impl->GetDeviceBuf = PS2AUDIO_GetDeviceBuf;
+ impl->CloseDevice = PS2AUDIO_CloseDevice;
+ impl->ThreadInit = PS2AUDIO_ThreadInit;
+ impl->Deinitialize = PS2AUDIO_Deinitialize;
+ impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
+ return SDL_TRUE; /* this audio target is available. */
+}
+
+AudioBootStrap PS2AUDIO_bootstrap = {
+ "ps2", "PS2 audio driver", PS2AUDIO_Init, SDL_FALSE
+};
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/audio/ps2/SDL_ps2audio.h b/src/audio/ps2/SDL_ps2audio.h
new file mode 100644
index 0000000..0de2506
--- /dev/null
+++ b/src/audio/ps2/SDL_ps2audio.h
@@ -0,0 +1,46 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#ifndef SDL_ps2audio_h_
+#define SDL_ps2audio_h_
+
+#include "../SDL_sysaudio.h"
+
+/* Hidden "this" pointer for the audio functions */
+#define _THIS SDL_AudioDevice *this
+
+#define NUM_BUFFERS 2
+
+struct SDL_PrivateAudioData
+{
+ /* The hardware output channel. */
+ int channel;
+ /* The raw allocated mixing buffer. */
+ Uint8 *rawbuf;
+ /* Individual mixing buffers. */
+ Uint8 *mixbufs[NUM_BUFFERS];
+ /* Index of the next available mixing buffer. */
+ int next_buffer;
+};
+
+#endif /* SDL_ps2audio_h_ */
+/* vi: set ts=4 sw=4 expandtab: */