summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/audio/vita/SDL_vitaaudio.c
blob: e194f212dd1c0f49b819d9faccfdbfd66260e06e (plain)
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
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2025 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"

#ifdef SDL_AUDIO_DRIVER_VITA

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../SDL_audiodev_c.h"
#include "../SDL_sysaudio.h"
#include "SDL_vitaaudio.h"

#include <psp2/kernel/threadmgr.h>
#include <psp2/audioout.h>
#include <psp2/audioin.h>

#define SCE_AUDIO_SAMPLE_ALIGN(s) (((s) + 63) & ~63)
#define SCE_AUDIO_MAX_VOLUME      0x8000

static bool VITAAUD_OpenRecordingDevice(SDL_AudioDevice *device)
{
    device->spec.freq = 16000;
    device->spec.channels = 1;
    device->sample_frames = 512;

    SDL_UpdatedAudioDeviceFormat(device);

    device->hidden->port = sceAudioInOpenPort(SCE_AUDIO_IN_PORT_TYPE_VOICE, 512, 16000, SCE_AUDIO_IN_PARAM_FORMAT_S16_MONO);

    if (device->hidden->port < 0) {
        return SDL_SetError("Couldn't open audio in port: %x", device->hidden->port);
    }

    return true;
}

static bool VITAAUD_OpenDevice(SDL_AudioDevice *device)
{
    int format, mixlen, i, port = SCE_AUDIO_OUT_PORT_TYPE_MAIN;
    int vols[2] = { SCE_AUDIO_MAX_VOLUME, SCE_AUDIO_MAX_VOLUME };
    SDL_AudioFormat test_format;
    const SDL_AudioFormat *closefmts;

    device->hidden = (struct SDL_PrivateAudioData *)
        SDL_calloc(1, sizeof(*device->hidden));
    if (!device->hidden) {
        return false;
    }

    closefmts = SDL_ClosestAudioFormats(device->spec.format);
    while ((test_format = *(closefmts++)) != 0) {
        if (test_format == SDL_AUDIO_S16LE) {
            device->spec.format = test_format;
            break;
        }
    }

    if (!test_format) {
        return SDL_SetError("Unsupported audio format");
    }

    if (device->recording) {
        return VITAAUD_OpenRecordingDevice(device);
    }

    // The sample count must be a multiple of 64.
    device->sample_frames = SCE_AUDIO_SAMPLE_ALIGN(device->sample_frames);

    // Update the fragment size as size in bytes.
    SDL_UpdatedAudioDeviceFormat(device);

    /* 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 = device->buffer_size * NUM_BUFFERS;
    device->hidden->rawbuf = (Uint8 *)SDL_aligned_alloc(64, mixlen);
    if (!device->hidden->rawbuf) {
        return SDL_SetError("Couldn't allocate mixing buffer");
    }

    // Setup the hardware channel.
    if (device->spec.channels == 1) {
        format = SCE_AUDIO_OUT_MODE_MONO;
    } else {
        format = SCE_AUDIO_OUT_MODE_STEREO;
    }

    // the main port requires 48000Hz audio, so this drops to the background music port if necessary
    if (device->spec.freq < 48000) {
        port = SCE_AUDIO_OUT_PORT_TYPE_BGM;
    }

    device->hidden->port = sceAudioOutOpenPort(port, device->sample_frames, device->spec.freq, format);
    if (device->hidden->port < 0) {
        SDL_aligned_free(device->hidden->rawbuf);
        device->hidden->rawbuf = NULL;
        return SDL_SetError("Couldn't open audio out port: %x", device->hidden->port);
    }

    sceAudioOutSetVolume(device->hidden->port, SCE_AUDIO_VOLUME_FLAG_L_CH | SCE_AUDIO_VOLUME_FLAG_R_CH, vols);

    SDL_memset(device->hidden->rawbuf, 0, mixlen);
    for (i = 0; i < NUM_BUFFERS; i++) {
        device->hidden->mixbufs[i] = &device->hidden->rawbuf[i * device->buffer_size];
    }

    device->hidden->next_buffer = 0;
    return true;
}

static bool VITAAUD_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size)
{
    return (sceAudioOutOutput(device->hidden->port, buffer) == 0);
}

// This function waits until it is possible to write a full sound buffer
static bool VITAAUD_WaitDevice(SDL_AudioDevice *device)
{
    // !!! FIXME: we might just need to sleep roughly as long as playback buffers take to process, based on sample rate, etc.
    while (!SDL_GetAtomicInt(&device->shutdown) && (sceAudioOutGetRestSample(device->hidden->port) >= device->buffer_size)) {
        SDL_Delay(1);
    }
    return true;
}

static Uint8 *VITAAUD_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
{
    Uint8 *result = device->hidden->mixbufs[device->hidden->next_buffer];
    device->hidden->next_buffer = (device->hidden->next_buffer + 1) % NUM_BUFFERS;
    return result;
}

static void VITAAUD_CloseDevice(SDL_AudioDevice *device)
{
    if (device->hidden) {
        if (device->hidden->port >= 0) {
            if (device->recording) {
                sceAudioInReleasePort(device->hidden->port);
            } else {
                sceAudioOutReleasePort(device->hidden->port);
            }
            device->hidden->port = -1;
        }

        if (!device->recording && device->hidden->rawbuf) {
            SDL_aligned_free(device->hidden->rawbuf); // this uses SDL_aligned_alloc(), not SDL_malloc()
            device->hidden->rawbuf = NULL;
        }
        SDL_free(device->hidden);
        device->hidden = NULL;
    }
}

static bool VITAAUD_WaitRecordingDevice(SDL_AudioDevice *device)
{
    // there's only a blocking call to obtain more data, so we'll just sleep as
    //  long as a buffer would run.
    const Uint64 endticks = SDL_GetTicks() + ((device->sample_frames * 1000) / device->spec.freq);
    while (!SDL_GetAtomicInt(&device->shutdown) && (SDL_GetTicks() < endticks)) {
        SDL_Delay(1);
    }
    return true;
}

static int VITAAUD_RecordDevice(SDL_AudioDevice *device, void *buffer, int buflen)
{
    int ret;
    SDL_assert(buflen == device->buffer_size);
    ret = sceAudioInInput(device->hidden->port, buffer);
    if (ret < 0) {
        SDL_SetError("Failed to record from device: %x", ret);
        return -1;
    }
    return device->buffer_size;
}

static void VITAAUD_FlushRecording(SDL_AudioDevice *device)
{
    // just grab the latest and dump it.
    sceAudioInInput(device->hidden->port, device->work_buffer);
}

static void VITAAUD_ThreadInit(SDL_AudioDevice *device)
{
    // Increase the priority of this audio thread by 1 to put it ahead of other SDL threads.
    SceUID thid;
    SceKernelThreadInfo info;
    thid = sceKernelGetThreadId();
    info.size = sizeof(SceKernelThreadInfo);
    if (sceKernelGetThreadInfo(thid, &info) == 0) {
        sceKernelChangeThreadPriority(thid, info.currentPriority - 1);
    }
}

static bool VITAAUD_Init(SDL_AudioDriverImpl *impl)
{
    impl->OpenDevice = VITAAUD_OpenDevice;
    impl->PlayDevice = VITAAUD_PlayDevice;
    impl->WaitDevice = VITAAUD_WaitDevice;
    impl->GetDeviceBuf = VITAAUD_GetDeviceBuf;
    impl->CloseDevice = VITAAUD_CloseDevice;
    impl->ThreadInit = VITAAUD_ThreadInit;
    impl->WaitRecordingDevice = VITAAUD_WaitRecordingDevice;
    impl->FlushRecording = VITAAUD_FlushRecording;
    impl->RecordDevice = VITAAUD_RecordDevice;

    impl->HasRecordingSupport = true;
    impl->OnlyHasDefaultPlaybackDevice = true;
    impl->OnlyHasDefaultRecordingDevice = true;

    return true;
}

AudioBootStrap VITAAUD_bootstrap = {
    "vita", "VITA audio driver", VITAAUD_Init, false, false
};

#endif // SDL_AUDIO_DRIVER_VITA