Skip to content

Commit

Permalink
(LIBNX) Start backporting libnx code; beginning with audio drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Sep 12, 2018
1 parent 9cbd7ec commit 2a7a2e7
Show file tree
Hide file tree
Showing 5 changed files with 648 additions and 4 deletions.
3 changes: 3 additions & 0 deletions audio/audio_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ static const audio_driver_t *audio_drivers[] = {
#endif
#ifdef SWITCH
&audio_switch,
#ifdef HAVE_LIBNX
&audio_switch_thread,
#endif
#endif
&audio_null,
NULL,
Expand Down
1 change: 1 addition & 0 deletions audio/audio_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ extern audio_driver_t audio_psp;
extern audio_driver_t audio_ctr_csnd;
extern audio_driver_t audio_ctr_dsp;
extern audio_driver_t audio_switch;
extern audio_driver_t audio_switch_thread;
extern audio_driver_t audio_rwebaudio;
extern audio_driver_t audio_null;

Expand Down
9 changes: 5 additions & 4 deletions audio/drivers/switch_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ static const size_t sample_buffer_size = ((max_num_samples * num_channels * size

typedef struct
{
audio_output_t output;
handle_t event;
audio_output_buffer_t buffers[3];
audio_output_buffer_t *current_buffer;
bool blocking;
bool is_paused;
uint64_t last_append;
unsigned latency;
audio_output_buffer_t buffers[3];
audio_output_buffer_t *current_buffer;

audio_output_t output;
handle_t event;
} switch_audio_t;

static ssize_t switch_audio_write(void *data, const void *buf, size_t size)
Expand Down
284 changes: 284 additions & 0 deletions audio/drivers/switch_nx_audio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/* RetroArch - A frontend for libretro.
*
* 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 <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdint.h>

#include <switch.h>

#include "../audio_driver.h"
#include "../../verbosity.h"

#include "../../tasks/tasks_internal.h"

typedef struct
{
bool blocking;
bool is_paused;
uint64_t last_append;
unsigned latency;
AudioOutBuffer buffers[5];
AudioOutBuffer *current_buffer;
} switch_audio_t;

static bool switch_tasks_finder(retro_task_t *task, void *userdata)
{
return task;
}
task_finder_data_t switch_tasks_finder_data = {switch_tasks_finder, NULL};

#define SAMPLERATE 48000
#define CHANNELCOUNT 2
#define FRAMERATE (1000 / 30)
#define SAMPLECOUNT SAMPLERATE / FRAMERATE
#define BYTESPERSAMPLE sizeof(uint16_t)

static uint32_t switch_audio_data_size(void)
{
return (SAMPLECOUNT * CHANNELCOUNT * BYTESPERSAMPLE);
}

static size_t switch_audio_buffer_size(void *data)
{
(void)data;
return (switch_audio_data_size() + 0xfff) & ~0xfff;
}

static ssize_t switch_audio_write(void *data, const void *buf, size_t size)
{
size_t to_write = size;
switch_audio_t *swa = (switch_audio_t *)data;

if (!swa)
{
return -1;
}

if (!swa->current_buffer)
{
uint32_t num;
if (R_FAILED(audoutGetReleasedAudioOutBuffer(&swa->current_buffer, &num)))
return -1;

if (num < 1)
{
swa->current_buffer = NULL;

if (swa->blocking)
{
/* No buffer, blocking... */

while (swa->current_buffer == NULL)
{
num = 0;
if (R_FAILED(audoutWaitPlayFinish(&swa->current_buffer, &num, U64_MAX)))
{
#if 0
if (task_queue_find(&switch_tasks_finder_data))
task_queue_check();
#endif
}
}
}
else
return 0;
}

swa->current_buffer->data_size = 0;
}

if (to_write > switch_audio_buffer_size(NULL) - swa->current_buffer->data_size)
to_write = switch_audio_buffer_size(NULL) - swa->current_buffer->data_size;

memcpy(((uint8_t *)swa->current_buffer->buffer) + swa->current_buffer->data_size, buf, to_write);
swa->current_buffer->data_size += to_write;
swa->current_buffer->buffer_size = switch_audio_buffer_size(NULL);

if (swa->current_buffer->data_size > (48000 * swa->latency) / 1000)
{
Result r = audoutAppendAudioOutBuffer(swa->current_buffer);
if (R_FAILED(r))
return -1;
swa->current_buffer = NULL;
}

swa->last_append = svcGetSystemTick();

return to_write;
}

static bool switch_audio_stop(void *data)
{
return true;

switch_audio_t *swa = (switch_audio_t *)data;
if (!swa)
return false;

if (!swa->is_paused)
{
Result rc = audoutStopAudioOut();
if (R_FAILED(rc))
return false;
}

swa->is_paused = true;
return true;
}

static bool switch_audio_start(void *data, bool is_shutdown)
{
return true;

switch_audio_t *swa = (switch_audio_t *)data;
if (!swa)
return false;

if (swa->is_paused)
{
Result rc = audoutStartAudioOut();
if (R_FAILED(rc))
return false;
}

swa->is_paused = false;
return true;
}

static bool switch_audio_alive(void *data)
{
switch_audio_t *swa = (switch_audio_t *)data;
if (!swa)
return false;
return !swa->is_paused;
}

static void switch_audio_free(void *data)
{
switch_audio_t *swa = (switch_audio_t *)data;

if (swa)
{
unsigned i;
if (!swa->is_paused)
audoutStopAudioOut();

audoutExit();

for (i = 0; i < 5; i++)
free(swa->buffers[i].buffer);

free(swa);
}
}

static bool switch_audio_use_float(void *data)
{
(void)data;
return false; /* force INT16 */
}

static size_t switch_audio_write_avail(void *data)
{
switch_audio_t *swa = (switch_audio_t *)data;

if (!swa || !swa->current_buffer)
return 0;

return swa->current_buffer->buffer_size;
}

static void switch_audio_set_nonblock_state(void *data, bool state)
{
switch_audio_t *swa = (switch_audio_t *)data;

if (swa)
swa->blocking = !state;
}

static void *switch_audio_init(const char *device,
unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_rate)
{
unsigned i;
switch_audio_t *swa = (switch_audio_t *)calloc(1, sizeof(*swa));
if (!swa)
return NULL;

/* Init Audio Output */
Result rc = audoutInitialize();
if (R_FAILED(rc))
{
goto cleanExit;
}

rc = audoutStartAudioOut();
if (R_FAILED(rc))
goto cleanExit;

/* Create Buffers */
for (i = 0; i < 5; i++)
{
swa->buffers[i].next = NULL; /* Unused */
swa->buffers[i].buffer = memalign(0x1000, switch_audio_buffer_size(NULL));
swa->buffers[i].buffer_size = switch_audio_buffer_size(NULL);
swa->buffers[i].data_size = switch_audio_data_size();
swa->buffers[i].data_offset = 0;

memset(swa->buffers[i].buffer, 0, switch_audio_buffer_size(NULL));

audoutAppendAudioOutBuffer(&swa->buffers[i]);
}

/* Set audio rate */
*new_rate = audoutGetSampleRate();

swa->is_paused = false;
swa->current_buffer = NULL;
swa->latency = latency;
swa->last_append = svcGetSystemTick();
swa->blocking = block_frames;

RARCH_LOG("[Audio]: Audio initialized\n");

return swa;

cleanExit:;

if (swa)
free(swa);

RARCH_LOG("[Audio]: Something failed in Audio Init!\n");

return NULL;
}

audio_driver_t audio_switch = {
switch_audio_init,
switch_audio_write,
switch_audio_stop,
switch_audio_start,
switch_audio_alive,
switch_audio_set_nonblock_state,
switch_audio_free,
switch_audio_use_float,
"switch",
NULL, /* device_list_new */
NULL, /* device_list_free */
switch_audio_write_avail,
switch_audio_buffer_size, /* buffer_size */
};
Loading

0 comments on commit 2a7a2e7

Please sign in to comment.