2015-04-01 21:14:13 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2016-01-10 03:06:50 +00:00
|
|
|
* Copyright (C) 2014-2016 - Ali Bouhlel
|
2015-04-01 21:14:13 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-09-28 14:39:30 +00:00
|
|
|
#include <3ds.h>
|
2015-11-24 11:02:02 +00:00
|
|
|
#include <string.h>
|
2016-09-28 14:01:46 +00:00
|
|
|
#include <malloc.h>
|
2015-11-23 18:40:09 +00:00
|
|
|
|
|
|
|
#include "../audio_driver.h"
|
|
|
|
#include "../../configuration.h"
|
2016-05-10 06:53:14 +00:00
|
|
|
#include "../../performance_counters.h"
|
2015-12-24 20:17:44 +00:00
|
|
|
#include "../../runloop.h"
|
2015-04-01 21:14:13 +00:00
|
|
|
|
2015-04-10 18:10:34 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool nonblocking;
|
2015-04-17 18:50:36 +00:00
|
|
|
bool playing;
|
2015-04-10 18:10:34 +00:00
|
|
|
int16_t* l;
|
|
|
|
int16_t* r;
|
|
|
|
|
|
|
|
uint32_t l_paddr;
|
|
|
|
uint32_t r_paddr;
|
|
|
|
|
|
|
|
uint32_t pos;
|
2015-04-17 18:50:36 +00:00
|
|
|
|
|
|
|
uint32_t playpos;
|
|
|
|
uint64_t cpu_ticks_last;
|
2015-10-27 00:06:34 +00:00
|
|
|
} ctr_csnd_audio_t;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
#define CTR_CSND_AUDIO_COUNT (1u << 11u)
|
|
|
|
#define CTR_CSND_AUDIO_COUNT_MASK (CTR_CSND_AUDIO_COUNT - 1u)
|
|
|
|
#define CTR_CSND_AUDIO_SIZE (CTR_CSND_AUDIO_COUNT * sizeof(int16_t))
|
|
|
|
#define CTR_CSND_AUDIO_SIZE_MASK (CTR_CSND_AUDIO_SIZE - 1u)
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
#define CTR_CSND_AUDIO_RATE 32730
|
2015-09-28 19:09:07 +00:00
|
|
|
#define CTR_CSND_TICKS_PER_SAMPLE 2048
|
2015-10-27 00:06:34 +00:00
|
|
|
#define CTR_CSND_CPU_TICKS_PER_SAMPLE (CTR_CSND_TICKS_PER_SAMPLE * 4)
|
2015-09-28 19:09:07 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static void ctr_csnd_audio_update_playpos(ctr_csnd_audio_t* ctr)
|
2015-09-28 14:39:30 +00:00
|
|
|
{
|
2016-09-08 09:41:58 +00:00
|
|
|
uint64_t current_tick = svcGetSystemTick();
|
|
|
|
uint32_t samples_played = (current_tick - ctr->cpu_ticks_last)
|
|
|
|
/ CTR_CSND_CPU_TICKS_PER_SAMPLE;
|
2015-09-28 14:39:30 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr->playpos = (ctr->playpos + samples_played) & CTR_CSND_AUDIO_COUNT_MASK;
|
|
|
|
ctr->cpu_ticks_last += samples_played * CTR_CSND_CPU_TICKS_PER_SAMPLE;
|
2015-09-28 14:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-08 09:41:58 +00:00
|
|
|
Result csndPlaySound_custom(int chn, u32 flags, float vol, float pan,
|
|
|
|
void* data0, void* data1, u32 size)
|
2015-09-27 12:48:23 +00:00
|
|
|
{
|
2016-09-08 09:41:58 +00:00
|
|
|
u32 paddr0 = 0;
|
|
|
|
u32 paddr1 = 0;
|
2015-09-27 12:48:23 +00:00
|
|
|
int encoding = (flags >> 12) & 3;
|
|
|
|
int loopMode = (flags >> 10) & 3;
|
|
|
|
|
2016-09-08 09:41:58 +00:00
|
|
|
if (!(csndChannels & BIT(chn)))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!loopMode)
|
|
|
|
flags |= SOUND_ONE_SHOT;
|
2015-09-27 12:48:23 +00:00
|
|
|
|
|
|
|
if (encoding != CSND_ENCODING_PSG)
|
|
|
|
{
|
2016-09-08 09:41:58 +00:00
|
|
|
if (data0)
|
|
|
|
paddr0 = osConvertVirtToPhys(data0);
|
|
|
|
if (data1)
|
|
|
|
paddr1 = osConvertVirtToPhys(data1);
|
2015-09-27 12:48:23 +00:00
|
|
|
|
|
|
|
if (data0 && encoding == CSND_ENCODING_ADPCM)
|
|
|
|
{
|
|
|
|
int adpcmSample = ((s16*)data0)[-2];
|
2016-09-08 09:41:58 +00:00
|
|
|
int adpcmIndex = ((u8*)data0)[-2];
|
2015-09-27 12:48:23 +00:00
|
|
|
CSND_SetAdpcmState(chn, 0, adpcmSample, adpcmIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flags &= ~0xFFFF001F;
|
2015-09-28 19:09:07 +00:00
|
|
|
flags |= SOUND_ENABLE | SOUND_CHANNEL(chn) | (CTR_CSND_TICKS_PER_SAMPLE << 16);
|
2015-09-27 12:48:23 +00:00
|
|
|
|
|
|
|
u32 volumes = CSND_VOL(vol, pan);
|
|
|
|
CSND_SetChnRegs(flags, paddr0, paddr1, size, volumes, volumes);
|
|
|
|
|
|
|
|
if (loopMode == CSND_LOOPMODE_NORMAL && paddr1 > paddr0)
|
|
|
|
{
|
|
|
|
// Now that the first block is playing, configure the size of the subsequent blocks
|
|
|
|
size -= paddr1 - paddr0;
|
|
|
|
CSND_SetBlock(chn, 1, paddr1, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-09 12:09:14 +00:00
|
|
|
static void *ctr_csnd_audio_init(const char *device, unsigned rate, unsigned latency,
|
2017-01-11 06:25:42 +00:00
|
|
|
unsigned block_frames,
|
2017-01-09 12:09:14 +00:00
|
|
|
unsigned *new_rate)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t *ctr = (ctr_csnd_audio_t*)calloc(1, sizeof(ctr_csnd_audio_t));
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-09-20 08:02:47 +00:00
|
|
|
if (!ctr)
|
|
|
|
return NULL;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-04-01 21:14:13 +00:00
|
|
|
(void)device;
|
|
|
|
(void)rate;
|
|
|
|
(void)latency;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2017-01-09 12:09:14 +00:00
|
|
|
*new_rate = CTR_CSND_AUDIO_RATE;
|
2015-09-28 19:09:07 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr->l = linearAlloc(CTR_CSND_AUDIO_SIZE);
|
|
|
|
ctr->r = linearAlloc(CTR_CSND_AUDIO_SIZE);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
memset(ctr->l, 0, CTR_CSND_AUDIO_SIZE);
|
|
|
|
memset(ctr->r, 0, CTR_CSND_AUDIO_SIZE);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-11-24 11:02:02 +00:00
|
|
|
ctr->l_paddr = osConvertVirtToPhys(ctr->l);
|
|
|
|
ctr->r_paddr = osConvertVirtToPhys(ctr->r);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-09-20 08:02:47 +00:00
|
|
|
ctr->pos = 0;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-11-19 22:15:48 +00:00
|
|
|
GSPGPU_FlushDataCache((void*)ctr->l_paddr, CTR_CSND_AUDIO_SIZE);
|
|
|
|
GSPGPU_FlushDataCache((void*)ctr->r_paddr, CTR_CSND_AUDIO_SIZE);
|
2015-09-27 12:48:23 +00:00
|
|
|
csndPlaySound_custom(0x8, SOUND_LOOPMODE(CSND_LOOPMODE_NORMAL)| SOUND_FORMAT(CSND_ENCODING_PCM16),
|
2015-10-27 00:06:34 +00:00
|
|
|
1.0, -1.0, ctr->l, ctr->l, CTR_CSND_AUDIO_SIZE);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-09-27 12:48:23 +00:00
|
|
|
csndPlaySound_custom(0x9, SOUND_LOOPMODE(CSND_LOOPMODE_NORMAL)| SOUND_FORMAT(CSND_ENCODING_PCM16),
|
2015-10-27 00:06:34 +00:00
|
|
|
1.0, 1.0, ctr->r, ctr->r, CTR_CSND_AUDIO_SIZE);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-09-27 12:59:11 +00:00
|
|
|
csndExecCmds(true);
|
2015-09-20 08:02:47 +00:00
|
|
|
ctr->playpos = 0;
|
|
|
|
ctr->cpu_ticks_last = svcGetSystemTick();
|
|
|
|
ctr->playing = true;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
|
|
|
return ctr;
|
2015-04-01 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static void ctr_csnd_audio_free(void *data)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t* ctr = (ctr_csnd_audio_t*)data;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
|
|
|
// csndExit();
|
|
|
|
CSND_SetPlayState(0x8, 0);
|
|
|
|
CSND_SetPlayState(0x9, 0);
|
2015-04-10 20:46:42 +00:00
|
|
|
csndExecCmds(false);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
|
|
|
linearFree(ctr->l);
|
|
|
|
linearFree(ctr->r);
|
|
|
|
|
|
|
|
free(ctr);
|
2015-04-01 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static ssize_t ctr_csnd_audio_write(void *data, const void *buf, size_t size)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
2015-09-20 08:02:47 +00:00
|
|
|
int i;
|
2016-09-08 09:41:58 +00:00
|
|
|
uint32_t samples_played = 0;
|
|
|
|
uint64_t current_tick = 0;
|
2015-09-20 08:02:47 +00:00
|
|
|
static struct retro_perf_counter ctraudio_f = {0};
|
2016-09-08 09:41:58 +00:00
|
|
|
const uint16_t *src = buf;
|
|
|
|
ctr_csnd_audio_t *ctr = (ctr_csnd_audio_t*)data;
|
2015-09-20 08:02:47 +00:00
|
|
|
|
2015-04-01 21:14:13 +00:00
|
|
|
(void)data;
|
|
|
|
(void)buf;
|
2015-09-28 16:12:02 +00:00
|
|
|
(void)samples_played;
|
|
|
|
(void)current_tick;
|
2015-04-01 21:14:13 +00:00
|
|
|
|
2016-05-16 07:58:45 +00:00
|
|
|
performance_counter_init(&ctraudio_f, "ctraudio_f");
|
2016-05-16 07:53:54 +00:00
|
|
|
performance_counter_start(&ctraudio_f);
|
2015-04-10 20:46:42 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_update_playpos(ctr);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
if((((ctr->playpos - ctr->pos) & CTR_CSND_AUDIO_COUNT_MASK) < (CTR_CSND_AUDIO_COUNT >> 2)) ||
|
|
|
|
(((ctr->pos - ctr->playpos ) & CTR_CSND_AUDIO_COUNT_MASK) < (CTR_CSND_AUDIO_COUNT >> 4)) ||
|
|
|
|
(((ctr->playpos - ctr->pos) & CTR_CSND_AUDIO_COUNT_MASK) < (size >> 2)))
|
2015-04-10 18:10:34 +00:00
|
|
|
{
|
|
|
|
if (ctr->nonblocking)
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr->pos = (ctr->playpos + (CTR_CSND_AUDIO_COUNT >> 1)) & CTR_CSND_AUDIO_COUNT_MASK;
|
2015-04-10 18:10:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
do{
|
2015-04-17 18:50:36 +00:00
|
|
|
/* todo: compute the correct sleep period */
|
2015-09-22 16:55:14 +00:00
|
|
|
retro_sleep(1);
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_update_playpos(ctr);
|
|
|
|
}while (((ctr->playpos - ctr->pos) & CTR_CSND_AUDIO_COUNT_MASK) < (CTR_CSND_AUDIO_COUNT >> 1)
|
|
|
|
|| (((ctr->pos - ctr->playpos) & CTR_CSND_AUDIO_COUNT_MASK) < (CTR_CSND_AUDIO_COUNT >> 4)));
|
2015-04-10 18:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (size >> 1); i += 2)
|
|
|
|
{
|
|
|
|
ctr->l[ctr->pos] = src[i];
|
|
|
|
ctr->r[ctr->pos] = src[i + 1];
|
|
|
|
ctr->pos++;
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr->pos &= CTR_CSND_AUDIO_COUNT_MASK;
|
2015-04-10 18:10:34 +00:00
|
|
|
}
|
2015-09-20 08:02:47 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
GSPGPU_FlushDataCache(ctr->l, CTR_CSND_AUDIO_SIZE);
|
|
|
|
GSPGPU_FlushDataCache(ctr->r, CTR_CSND_AUDIO_SIZE);
|
2015-04-17 18:50:36 +00:00
|
|
|
|
2016-05-16 07:48:14 +00:00
|
|
|
performance_counter_stop(&ctraudio_f);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-04-01 21:14:13 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static bool ctr_csnd_audio_stop(void *data)
|
2015-04-10 18:10:34 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t* ctr = (ctr_csnd_audio_t*)data;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-04-17 18:50:36 +00:00
|
|
|
/* using SetPlayState would make tracking the playback
|
|
|
|
* position more difficult */
|
|
|
|
|
2015-09-20 08:02:47 +00:00
|
|
|
#if 0
|
|
|
|
CSND_SetPlayState(0x8, 0);
|
|
|
|
CSND_SetPlayState(0x9, 0);
|
|
|
|
#endif
|
2015-04-17 18:50:36 +00:00
|
|
|
|
|
|
|
/* setting the channel volume to 0 seems to make it
|
|
|
|
* impossible to set it back to full volume later */
|
|
|
|
|
|
|
|
CSND_SetVol(0x8, 0x00000001, 0);
|
|
|
|
CSND_SetVol(0x9, 0x00010000, 0);
|
2015-04-10 20:46:42 +00:00
|
|
|
csndExecCmds(false);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-04-17 18:50:36 +00:00
|
|
|
ctr->playing = false;
|
|
|
|
|
2015-04-01 21:14:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static bool ctr_csnd_audio_alive(void *data)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t* ctr = (ctr_csnd_audio_t*)data;
|
2015-04-17 18:50:36 +00:00
|
|
|
return ctr->playing;
|
2015-04-01 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static bool ctr_csnd_audio_start(void *data)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t* ctr = (ctr_csnd_audio_t*)data;
|
2015-04-17 18:50:36 +00:00
|
|
|
|
2015-09-20 08:02:47 +00:00
|
|
|
/* Prevents restarting audio when the menu
|
2015-04-17 18:50:36 +00:00
|
|
|
* is toggled off on shutdown */
|
|
|
|
|
2015-12-04 11:44:12 +00:00
|
|
|
if (runloop_ctl(RUNLOOP_CTL_IS_SHUTDOWN, NULL))
|
2015-04-17 18:50:36 +00:00
|
|
|
return true;
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-09-20 08:02:47 +00:00
|
|
|
#if 0
|
|
|
|
CSND_SetPlayState(0x8, 1);
|
|
|
|
CSND_SetPlayState(0x9, 1);
|
|
|
|
#endif
|
2015-04-17 18:50:36 +00:00
|
|
|
|
|
|
|
CSND_SetVol(0x8, 0x00008000, 0);
|
|
|
|
CSND_SetVol(0x9, 0x80000000, 0);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-04-10 20:46:42 +00:00
|
|
|
csndExecCmds(false);
|
2015-04-10 18:10:34 +00:00
|
|
|
|
2015-04-17 18:50:36 +00:00
|
|
|
ctr->playing = true;
|
|
|
|
|
2015-04-01 21:14:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static void ctr_csnd_audio_set_nonblock_state(void *data, bool state)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t* ctr = (ctr_csnd_audio_t*)data;
|
2015-04-10 18:10:34 +00:00
|
|
|
if (ctr)
|
|
|
|
ctr->nonblocking = state;
|
2015-04-01 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static bool ctr_csnd_audio_use_float(void *data)
|
2015-04-01 21:14:13 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
2015-04-10 18:10:34 +00:00
|
|
|
return false;
|
2015-04-01 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static size_t ctr_csnd_audio_write_avail(void *data)
|
2015-04-11 04:56:02 +00:00
|
|
|
{
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_t* ctr = (ctr_csnd_audio_t*)data;
|
2015-09-28 14:39:30 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_update_playpos(ctr);
|
|
|
|
return (ctr->playpos - ctr->pos) & CTR_CSND_AUDIO_COUNT_MASK;
|
2015-09-28 14:39:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
static size_t ctr_csnd_audio_buffer_size(void *data)
|
2015-09-28 14:39:30 +00:00
|
|
|
{
|
2015-04-11 04:56:02 +00:00
|
|
|
(void)data;
|
2015-10-27 00:06:34 +00:00
|
|
|
return CTR_CSND_AUDIO_COUNT;
|
2015-04-11 04:56:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 14:39:30 +00:00
|
|
|
|
2015-10-27 00:06:34 +00:00
|
|
|
audio_driver_t audio_ctr_csnd = {
|
|
|
|
ctr_csnd_audio_init,
|
|
|
|
ctr_csnd_audio_write,
|
|
|
|
ctr_csnd_audio_stop,
|
|
|
|
ctr_csnd_audio_start,
|
|
|
|
ctr_csnd_audio_alive,
|
|
|
|
ctr_csnd_audio_set_nonblock_state,
|
|
|
|
ctr_csnd_audio_free,
|
|
|
|
ctr_csnd_audio_use_float,
|
|
|
|
"csnd",
|
2016-04-26 15:55:20 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2015-10-27 00:06:34 +00:00
|
|
|
ctr_csnd_audio_write_avail,
|
|
|
|
ctr_csnd_audio_buffer_size
|
2015-04-01 21:14:13 +00:00
|
|
|
};
|