Atlas - SDL_vitaaudio.c
Home / ext / SDL / src / audio / vita Lines: 1 | Size: 8036 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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_internal.h" 22 23#ifdef SDL_AUDIO_DRIVER_VITA 24 25#include <stdio.h> 26#include <string.h> 27#include <stdlib.h> 28 29#include "../SDL_audiodev_c.h" 30#include "../SDL_sysaudio.h" 31#include "SDL_vitaaudio.h" 32 33#include <psp2/kernel/threadmgr.h> 34#include <psp2/audioout.h> 35#include <psp2/audioin.h> 36 37#define SCE_AUDIO_SAMPLE_ALIGN(s) (((s) + 63) & ~63) 38#define SCE_AUDIO_MAX_VOLUME 0x8000 39 40static bool VITAAUD_OpenRecordingDevice(SDL_AudioDevice *device) 41{ 42 device->spec.freq = 16000; 43 device->spec.channels = 1; 44 device->sample_frames = 512; 45 46 SDL_UpdatedAudioDeviceFormat(device); 47 48 device->hidden->port = sceAudioInOpenPort(SCE_AUDIO_IN_PORT_TYPE_VOICE, 512, 16000, SCE_AUDIO_IN_PARAM_FORMAT_S16_MONO); 49 50 if (device->hidden->port < 0) { 51 return SDL_SetError("Couldn't open audio in port: %x", device->hidden->port); 52 } 53 54 return true; 55} 56 57static bool VITAAUD_OpenDevice(SDL_AudioDevice *device) 58{ 59 int format, mixlen, i, port = SCE_AUDIO_OUT_PORT_TYPE_MAIN; 60 int vols[2] = { SCE_AUDIO_MAX_VOLUME, SCE_AUDIO_MAX_VOLUME }; 61 SDL_AudioFormat test_format; 62 const SDL_AudioFormat *closefmts; 63 64 device->hidden = (struct SDL_PrivateAudioData *) 65 SDL_calloc(1, sizeof(*device->hidden)); 66 if (!device->hidden) { 67 return false; 68 } 69 70 closefmts = SDL_ClosestAudioFormats(device->spec.format); 71 while ((test_format = *(closefmts++)) != 0) { 72 if (test_format == SDL_AUDIO_S16LE) { 73 device->spec.format = test_format; 74 break; 75 } 76 } 77 78 if (!test_format) { 79 return SDL_SetError("Unsupported audio format"); 80 } 81 82 if (device->recording) { 83 return VITAAUD_OpenRecordingDevice(device); 84 } 85 86 // The sample count must be a multiple of 64. 87 device->sample_frames = SCE_AUDIO_SAMPLE_ALIGN(device->sample_frames); 88 89 // Update the fragment size as size in bytes. 90 SDL_UpdatedAudioDeviceFormat(device); 91 92 /* Allocate the mixing buffer. Its size and starting address must 93 be a multiple of 64 bytes. Our sample count is already a multiple of 94 64, so spec->size should be a multiple of 64 as well. */ 95 mixlen = device->buffer_size * NUM_BUFFERS; 96 device->hidden->rawbuf = (Uint8 *)SDL_aligned_alloc(64, mixlen); 97 if (!device->hidden->rawbuf) { 98 return SDL_SetError("Couldn't allocate mixing buffer"); 99 } 100 101 // Setup the hardware channel. 102 if (device->spec.channels == 1) { 103 format = SCE_AUDIO_OUT_MODE_MONO; 104 } else { 105 format = SCE_AUDIO_OUT_MODE_STEREO; 106 } 107 108 // the main port requires 48000Hz audio, so this drops to the background music port if necessary 109 if (device->spec.freq < 48000) { 110 port = SCE_AUDIO_OUT_PORT_TYPE_BGM; 111 } 112 113 device->hidden->port = sceAudioOutOpenPort(port, device->sample_frames, device->spec.freq, format); 114 if (device->hidden->port < 0) { 115 SDL_aligned_free(device->hidden->rawbuf); 116 device->hidden->rawbuf = NULL; 117 return SDL_SetError("Couldn't open audio out port: %x", device->hidden->port); 118 } 119 120 sceAudioOutSetVolume(device->hidden->port, SCE_AUDIO_VOLUME_FLAG_L_CH | SCE_AUDIO_VOLUME_FLAG_R_CH, vols); 121 122 SDL_memset(device->hidden->rawbuf, 0, mixlen); 123 for (i = 0; i < NUM_BUFFERS; i++) { 124 device->hidden->mixbufs[i] = &device->hidden->rawbuf[i * device->buffer_size]; 125 } 126 127 device->hidden->next_buffer = 0; 128 return true; 129} 130 131static bool VITAAUD_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size) 132{ 133 // sceAudioOutOutput returns amount of samples queued or < 0 on error 134 return (sceAudioOutOutput(device->hidden->port, buffer) >= 0); 135} 136 137// This function waits until it is possible to write a full sound buffer 138static bool VITAAUD_WaitDevice(SDL_AudioDevice *device) 139{ 140 // !!! FIXME: we might just need to sleep roughly as long as playback buffers take to process, based on sample rate, etc. 141 while (!SDL_GetAtomicInt(&device->shutdown) && (sceAudioOutGetRestSample(device->hidden->port) >= device->buffer_size)) { 142 SDL_Delay(1); 143 } 144 return true; 145} 146 147static Uint8 *VITAAUD_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size) 148{ 149 Uint8 *result = device->hidden->mixbufs[device->hidden->next_buffer]; 150 device->hidden->next_buffer = (device->hidden->next_buffer + 1) % NUM_BUFFERS; 151 return result; 152} 153 154static void VITAAUD_CloseDevice(SDL_AudioDevice *device) 155{ 156 if (device->hidden) { 157 if (device->hidden->port >= 0) { 158 if (device->recording) { 159 sceAudioInReleasePort(device->hidden->port); 160 } else { 161 sceAudioOutReleasePort(device->hidden->port); 162 } 163 device->hidden->port = -1; 164 } 165 166 if (!device->recording && device->hidden->rawbuf) { 167 SDL_aligned_free(device->hidden->rawbuf); // this uses SDL_aligned_alloc(), not SDL_malloc() 168 device->hidden->rawbuf = NULL; 169 } 170 SDL_free(device->hidden); 171 device->hidden = NULL; 172 } 173} 174 175static bool VITAAUD_WaitRecordingDevice(SDL_AudioDevice *device) 176{ 177 // there's only a blocking call to obtain more data, so we'll just sleep as 178 // long as a buffer would run. 179 const Uint64 endticks = SDL_GetTicks() + ((device->sample_frames * 1000) / device->spec.freq); 180 while (!SDL_GetAtomicInt(&device->shutdown) && (SDL_GetTicks() < endticks)) { 181 SDL_Delay(1); 182 } 183 return true; 184} 185 186static int VITAAUD_RecordDevice(SDL_AudioDevice *device, void *buffer, int buflen) 187{ 188 int ret; 189 SDL_assert(buflen == device->buffer_size); 190 ret = sceAudioInInput(device->hidden->port, buffer); 191 if (ret < 0) { 192 SDL_SetError("Failed to record from device: %x", ret); 193 return -1; 194 } 195 return device->buffer_size; 196} 197 198static void VITAAUD_FlushRecording(SDL_AudioDevice *device) 199{ 200 // just grab the latest and dump it. 201 sceAudioInInput(device->hidden->port, device->work_buffer); 202} 203 204static void VITAAUD_ThreadInit(SDL_AudioDevice *device) 205{ 206 // Increase the priority of this audio thread by 1 to put it ahead of other SDL threads. 207 SceUID thid; 208 SceKernelThreadInfo info; 209 thid = sceKernelGetThreadId(); 210 info.size = sizeof(SceKernelThreadInfo); 211 if (sceKernelGetThreadInfo(thid, &info) == 0) { 212 sceKernelChangeThreadPriority(thid, info.currentPriority - 1); 213 } 214} 215 216static bool VITAAUD_Init(SDL_AudioDriverImpl *impl) 217{ 218 impl->OpenDevice = VITAAUD_OpenDevice; 219 impl->PlayDevice = VITAAUD_PlayDevice; 220 impl->WaitDevice = VITAAUD_WaitDevice; 221 impl->GetDeviceBuf = VITAAUD_GetDeviceBuf; 222 impl->CloseDevice = VITAAUD_CloseDevice; 223 impl->ThreadInit = VITAAUD_ThreadInit; 224 impl->WaitRecordingDevice = VITAAUD_WaitRecordingDevice; 225 impl->FlushRecording = VITAAUD_FlushRecording; 226 impl->RecordDevice = VITAAUD_RecordDevice; 227 228 impl->HasRecordingSupport = true; 229 impl->OnlyHasDefaultPlaybackDevice = true; 230 impl->OnlyHasDefaultRecordingDevice = true; 231 232 return true; 233} 234 235AudioBootStrap VITAAUD_bootstrap = { 236 "vita", "VITA audio driver", VITAAUD_Init, false, false 237}; 238 239#endif // SDL_AUDIO_DRIVER_VITA 240[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.