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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/* RetroArch - A frontend for libretro.
* Copyright (C) 2020 - Justin Weiss
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <3ds.h>
#include <string.h>
#include <malloc.h>
#include <queues/fifo_queue.h>
#include <rthreads/rthreads.h>
#include "../audio_driver.h"
#include "../../ctr/ctr_debug.h"
typedef struct
{
fifo_buffer_t* fifo;
size_t fifo_size;
slock_t* fifo_lock;
scond_t* fifo_avail;
scond_t* fifo_done;
sthread_t* thread;
volatile bool running;
bool nonblocking;
bool playing;
retro_time_t frame_time;
int channel;
ndspWaveBuf dsp_buf;
uint32_t pos;
} ctr_dsp_thread_audio_t;
// PCM16 stereo
#define DSP_BYTES_TO_SAMPLES(bytes) (bytes / (2 * sizeof(uint16_t)))
#define DSP_SAMPLES_TO_BYTES(samples) (samples * 2 * sizeof(uint16_t))
static void ctr_dsp_audio_loop(void* data)
{
uint32_t pos, buf_pos;
ctr_dsp_thread_audio_t *ctr = (ctr_dsp_thread_audio_t*)data;
if (!ctr)
return;
while (1)
{
size_t buf_avail, avail, to_write;
slock_lock(ctr->fifo_lock);
do {
avail = FIFO_READ_AVAIL(ctr->fifo);
if (!avail) {
scond_wait(ctr->fifo_avail, ctr->fifo_lock);
}
} while (!avail && ctr->running);
slock_unlock(ctr->fifo_lock);
if (!ctr->running)
break;
pos = ctr->pos;
buf_pos = DSP_SAMPLES_TO_BYTES(ndspChnGetSamplePos(ctr->channel));
buf_avail = buf_pos >= pos ? buf_pos - pos : ctr->fifo_size - pos;
to_write = MIN(avail, buf_avail);
slock_lock(ctr->fifo_lock);
if (to_write > 0) {
fifo_read(ctr->fifo, ctr->dsp_buf.data_pcm8 + pos, to_write);
DSP_FlushDataCache(ctr->dsp_buf.data_pcm8 + pos, to_write);
scond_signal(ctr->fifo_done);
}
slock_unlock(ctr->fifo_lock);
if (buf_pos == pos) {
svcSleepThread(100000);
}
ctr->pos = (pos + to_write) % ctr->fifo_size;
}
}
static void ctr_dsp_thread_audio_free(void *data);
static void *ctr_dsp_thread_audio_init(const char *device, unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_rate)
{
ctr_dsp_thread_audio_t *ctr = NULL;
(void)device;
(void)rate;
if (ndspInit() < 0)
return NULL;
ctr = (ctr_dsp_thread_audio_t*)calloc(1, sizeof(ctr_dsp_thread_audio_t));
if (!ctr)
return NULL;
*new_rate = 32728;
ctr->running = true;
ctr->channel = 0;
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
ndspSetClippingMode(NDSP_CLIP_SOFT); /* ?? */
ndspSetOutputCount(2);
ndspChnReset(ctr->channel);
ndspChnSetFormat(ctr->channel, NDSP_FORMAT_STEREO_PCM16);
ndspChnSetInterp(ctr->channel, NDSP_INTERP_NONE);
ndspChnSetRate(ctr->channel, 32728.0f);
ndspChnWaveBufClear(ctr->channel);
ctr->fifo_size = DSP_SAMPLES_TO_BYTES((*new_rate * MAX(latency, 8)) / 1000);
ctr->dsp_buf.data_pcm16 = linearAlloc(ctr->fifo_size);
memset(ctr->dsp_buf.data_pcm16, 0, ctr->fifo_size);
DSP_FlushDataCache(ctr->dsp_buf.data_pcm16, ctr->fifo_size);
ctr->dsp_buf.looping = true;
ctr->dsp_buf.nsamples = DSP_BYTES_TO_SAMPLES(ctr->fifo_size);
ndspChnWaveBufAdd(ctr->channel, &ctr->dsp_buf);
ctr->fifo = fifo_new(ctr->fifo_size);
if (!(ctr->fifo_lock = slock_new()) ||
!(ctr->fifo_avail = scond_new()) ||
!(ctr->fifo_done = scond_new()) ||
!(ctr->thread = sthread_create(ctr_dsp_audio_loop, ctr)))
{
RARCH_LOG("[Audio]: thread creation failed.\n");
ctr->running = false;
ctr_dsp_thread_audio_free(ctr);
return NULL;
}
ctr->pos = 0;
ctr->playing = true;
ctr->frame_time = (retro_time_t)roundf(1000000 * 4481134.0 / (*new_rate * 8192.0));
ndspSetMasterVol(1.0);
return ctr;
}
static void ctr_dsp_thread_audio_free(void *data)
{
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
if (!ctr)
return;
if (ctr->running)
{
ctr->running = false;
scond_signal(ctr->fifo_avail);
}
if (ctr->thread)
sthread_join(ctr->thread);
scond_free(ctr->fifo_avail);
scond_free(ctr->fifo_done);
slock_free(ctr->fifo_lock);
if (ctr->fifo)
{
fifo_free(ctr->fifo);
ctr->fifo = NULL;
}
ndspChnWaveBufClear(ctr->channel);
linearFree(ctr->dsp_buf.data_pcm16);
free(ctr);
ndspExit();
ctr = NULL;
}
static ssize_t ctr_dsp_thread_audio_write(void *data, const void *buf, size_t size)
{
size_t avail, written;
ctr_dsp_thread_audio_t * ctr = (ctr_dsp_thread_audio_t*)data;
if (!ctr || !ctr->running)
return 0;
if (ctr->nonblocking)
{
slock_lock(ctr->fifo_lock);
avail = FIFO_WRITE_AVAIL(ctr->fifo);
written = MIN(avail, size);
if (written > 0)
{
fifo_write(ctr->fifo, buf, written);
scond_signal(ctr->fifo_avail);
}
slock_unlock(ctr->fifo_lock);
}
else
{
written = 0;
while (written < size && ctr->running)
{
slock_lock(ctr->fifo_lock);
avail = FIFO_WRITE_AVAIL(ctr->fifo);
if (avail == 0)
{
if (ctr->running)
{
/* Wait a maximum of one frame, skip the write if the thread is still busy */
if (!scond_wait_timeout(ctr->fifo_done, ctr->fifo_lock, ctr->frame_time)) {
slock_unlock(ctr->fifo_lock);
break;
}
}
slock_unlock(ctr->fifo_lock);
}
else
{
size_t write_amt = MIN(size - written, avail);
fifo_write(ctr->fifo, (const char*)buf + written, write_amt);
scond_signal(ctr->fifo_avail);
slock_unlock(ctr->fifo_lock);
written += write_amt;
}
}
}
return written;
}
static bool ctr_dsp_thread_audio_stop(void *data)
{
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
if (!ctr)
return false;
ndspSetMasterVol(0.0);
ctr->playing = false;
return true;
}
static bool ctr_dsp_thread_audio_alive(void *data)
{
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
if (!ctr)
return false;
return ctr->playing;
}
static bool ctr_dsp_thread_audio_start(void *data, bool is_shutdown)
{
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
if (!ctr)
return false;
/* Prevents restarting audio when the menu
* is toggled off on shutdown */
if (is_shutdown)
return true;
ndspSetMasterVol(1.0);
ctr->playing = true;
return true;
}
static void ctr_dsp_thread_audio_set_nonblock_state(void *data, bool state)
{
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
if (ctr)
ctr->nonblocking = state;
}
static bool ctr_dsp_thread_audio_use_float(void *data)
{
(void)data;
return false;
}
static size_t ctr_dsp_thread_audio_write_avail(void *data)
{
size_t val;
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
slock_lock(ctr->fifo_lock);
val = FIFO_WRITE_AVAIL(ctr->fifo);
slock_unlock(ctr->fifo_lock);
return val;
}
static size_t ctr_dsp_thread_audio_buffer_size(void *data)
{
ctr_dsp_thread_audio_t* ctr = (ctr_dsp_thread_audio_t*)data;
return ctr->fifo_size;
}
audio_driver_t audio_ctr_dsp_thread = {
ctr_dsp_thread_audio_init,
ctr_dsp_thread_audio_write,
ctr_dsp_thread_audio_stop,
ctr_dsp_thread_audio_start,
ctr_dsp_thread_audio_alive,
ctr_dsp_thread_audio_set_nonblock_state,
ctr_dsp_thread_audio_free,
ctr_dsp_thread_audio_use_float,
"dsp_thread",
NULL,
NULL,
ctr_dsp_thread_audio_write_avail,
ctr_dsp_thread_audio_buffer_size
};
|