mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 12:32:40 +02:00
Audio input implementation and fixes (#60)
* Implemented audin service and fixed released audio buffer handling
This commit is contained in:
parent
ca03c58896
commit
208daf6344
@ -35,6 +35,7 @@ extern "C" {
|
|||||||
#include "switch/services/acc.h"
|
#include "switch/services/acc.h"
|
||||||
#include "switch/services/apm.h"
|
#include "switch/services/apm.h"
|
||||||
#include "switch/services/applet.h"
|
#include "switch/services/applet.h"
|
||||||
|
#include "switch/services/audin.h"
|
||||||
#include "switch/services/audout.h"
|
#include "switch/services/audout.h"
|
||||||
//#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead
|
//#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead
|
||||||
#include "switch/services/fatal.h"
|
#include "switch/services/fatal.h"
|
||||||
|
19
nx/include/switch/audio/audio.h
Normal file
19
nx/include/switch/audio/audio.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* @file audio.h
|
||||||
|
* @brief Global audio service.
|
||||||
|
* @author hexkyz
|
||||||
|
* @copyright libnx Authors
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../types.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PcmFormat_Invalid = 0,
|
||||||
|
PcmFormat_Int8 = 1,
|
||||||
|
PcmFormat_Int16 = 2,
|
||||||
|
PcmFormat_Int24 = 3,
|
||||||
|
PcmFormat_Int32 = 4,
|
||||||
|
PcmFormat_Float = 5,
|
||||||
|
PcmFormat_ADPCM = 6,
|
||||||
|
} PcmFormat;
|
60
nx/include/switch/services/audin.h
Normal file
60
nx/include/switch/services/audin.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* @file audin.h
|
||||||
|
* @brief Audio input service.
|
||||||
|
* @author hexkyz
|
||||||
|
* @copyright libnx Authors
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../audio/audio.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
AudioInState_Started = 0,
|
||||||
|
AudioInState_Stopped = 1,
|
||||||
|
} AudioInState;
|
||||||
|
|
||||||
|
/// Audio input buffer format
|
||||||
|
typedef struct AudioInBuffer AudioInBuffer;
|
||||||
|
|
||||||
|
struct AudioInBuffer
|
||||||
|
{
|
||||||
|
AudioInBuffer* next; ///< Next buffer. (Unused)
|
||||||
|
void* buffer; ///< Sample buffer (aligned to 0x1000 bytes).
|
||||||
|
u64 buffer_size; ///< Sample buffer size (aligned to 0x1000 bytes).
|
||||||
|
u64 data_size; ///< Size of data inside the buffer.
|
||||||
|
u64 data_offset; ///< Offset of data inside the buffer. (Unused?)
|
||||||
|
};
|
||||||
|
|
||||||
|
Result audinInitialize(void);
|
||||||
|
void audinExit(void);
|
||||||
|
|
||||||
|
Result audinListAudioIns(char *DeviceNames, u32 *DeviceNamesCount);
|
||||||
|
Result audinOpenAudioIn(const char *DeviceNameIn, char *DeviceNameOut, u32 SampleRateIn, u32 ChannelCountIn, u32 *SampleRateOut, u32 *ChannelCountOut, PcmFormat *Format, AudioInState *State);
|
||||||
|
Result audinGetAudioInState(AudioInState *State);
|
||||||
|
Result audinStartAudioIn(void);
|
||||||
|
Result audinStopAudioIn(void);
|
||||||
|
Result audinAppendAudioInBuffer(AudioInBuffer *Buffer);
|
||||||
|
Result audinGetReleasedAudioInBuffer(AudioInBuffer **Buffer, u32 *ReleasedBuffersCount);
|
||||||
|
Result audinContainsAudioInBuffer(AudioInBuffer *Buffer, bool *ContainsBuffer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Submits an audio sample data buffer for capturing and waits for it to finish capturing.
|
||||||
|
* @brief Uses \ref audinAppendAudioInBuffer and \ref audinWaitCaptureFinish internally.
|
||||||
|
* @param source AudioInBuffer containing the buffer to hold the captured sample data.
|
||||||
|
* @param released AudioInBuffer to receive the captured buffer after being released.
|
||||||
|
*/
|
||||||
|
Result audinCaptureBuffer(AudioInBuffer *source, AudioInBuffer **released);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Waits for audio capture to finish.
|
||||||
|
* @param released AudioInBuffer to receive the first captured buffer after being released.
|
||||||
|
* @param released_count Pointer to receive the number of captured buffers.
|
||||||
|
* @param timeout Timeout value, use U64_MAX to wait until all finished.
|
||||||
|
*/
|
||||||
|
Result audinWaitCaptureFinish(AudioInBuffer **released, u32* released_count, u64 timeout);
|
||||||
|
|
||||||
|
/// These return the state associated with the currently active audio input device.
|
||||||
|
u32 audinGetSampleRate(void); ///< Supported sample rate (48000Hz).
|
||||||
|
u32 audinGetChannelCount(void); ///< Supported channel count (2 channels).
|
||||||
|
PcmFormat audinGetPcmFormat(void); ///< Supported PCM format (Int16).
|
||||||
|
AudioInState audinGetDeviceState(void); ///< Initial device state (stopped).
|
@ -6,17 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../types.h"
|
#include "../audio/audio.h"
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
PcmFormat_Invalid = 0,
|
|
||||||
PcmFormat_INT8 = 1,
|
|
||||||
PcmFormat_INT16 = 2,
|
|
||||||
PcmFormat_INT24 = 3,
|
|
||||||
PcmFormat_INT32 = 4,
|
|
||||||
PcmFormat_FLOAT = 5,
|
|
||||||
PcmFormat_ADPCM = 6,
|
|
||||||
} PcmFormat;
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
AudioOutState_Started = 0,
|
AudioOutState_Started = 0,
|
||||||
@ -30,7 +20,7 @@ struct AudioOutBuffer
|
|||||||
{
|
{
|
||||||
AudioOutBuffer* next; ///< Next buffer. (Unused)
|
AudioOutBuffer* next; ///< Next buffer. (Unused)
|
||||||
void* buffer; ///< Sample buffer (aligned to 0x1000 bytes).
|
void* buffer; ///< Sample buffer (aligned to 0x1000 bytes).
|
||||||
u64 buffer_size; ///< Sample buffer size.
|
u64 buffer_size; ///< Sample buffer size (aligned to 0x1000 bytes).
|
||||||
u64 data_size; ///< Size of data inside the buffer.
|
u64 data_size; ///< Size of data inside the buffer.
|
||||||
u64 data_offset; ///< Offset of data inside the buffer. (Unused?)
|
u64 data_offset; ///< Offset of data inside the buffer. (Unused?)
|
||||||
};
|
};
|
||||||
@ -44,26 +34,27 @@ Result audoutGetAudioOutState(AudioOutState *State);
|
|||||||
Result audoutStartAudioOut(void);
|
Result audoutStartAudioOut(void);
|
||||||
Result audoutStopAudioOut(void);
|
Result audoutStopAudioOut(void);
|
||||||
Result audoutAppendAudioOutBuffer(AudioOutBuffer *Buffer);
|
Result audoutAppendAudioOutBuffer(AudioOutBuffer *Buffer);
|
||||||
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer *Buffer, u32 *ReleasedBuffersCount);
|
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer **Buffer, u32 *ReleasedBuffersCount);
|
||||||
Result audoutContainsAudioOutBuffer(AudioOutBuffer *Buffer, bool *ContainsBuffer);
|
Result audoutContainsAudioOutBuffer(AudioOutBuffer *Buffer, bool *ContainsBuffer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Submits an audio sample data buffer for playing and waits for it to finish playing.
|
* @brief Submits an audio sample data buffer for playing and waits for it to finish playing.
|
||||||
* @brief Uses \ref audoutAppendAudioOutBuffer and \ref audoutWaitPlayFinish internally.
|
* @brief Uses \ref audoutAppendAudioOutBuffer and \ref audoutWaitPlayFinish internally.
|
||||||
* @param source AudioOutBuffer containing the source sample data to be played.
|
* @param source AudioOutBuffer containing the source sample data to be played.
|
||||||
* @param released AudioOutBuffer to receive the last played buffer.
|
* @param released AudioOutBuffer to receive the played buffer after being released.
|
||||||
*/
|
*/
|
||||||
Result audoutPlayBuffer(AudioOutBuffer *source, AudioOutBuffer *released);
|
Result audoutPlayBuffer(AudioOutBuffer *source, AudioOutBuffer **released);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Waits for audio playback to finish.
|
* @brief Waits for audio playback to finish.
|
||||||
* @param released AudioOutBuffer to receive the last played buffer.
|
* @param released AudioOutBuffer to receive the first played buffer after being released.
|
||||||
|
* @param released_count Pointer to receive the number of played buffers.
|
||||||
* @param timeout Timeout value, use U64_MAX to wait until all finished.
|
* @param timeout Timeout value, use U64_MAX to wait until all finished.
|
||||||
*/
|
*/
|
||||||
Result audoutWaitPlayFinish(AudioOutBuffer *released, u64 timeout);
|
Result audoutWaitPlayFinish(AudioOutBuffer **released, u32* released_count, u64 timeout);
|
||||||
|
|
||||||
/// These return the state associated with the currently active audio output device.
|
/// These return the state associated with the currently active audio output device.
|
||||||
u32 audoutGetSampleRate(void); ///< Supported sample rate (48000Hz).
|
u32 audoutGetSampleRate(void); ///< Supported sample rate (48000Hz).
|
||||||
u32 audoutGetChannelCount(void); ///< Supported channel count (2 channels).
|
u32 audoutGetChannelCount(void); ///< Supported channel count (2 channels).
|
||||||
PcmFormat audoutGetPcmFormat(void); ///< Supported PCM format (INT16).
|
PcmFormat audoutGetPcmFormat(void); ///< Supported PCM format (Int16).
|
||||||
AudioOutState audoutGetDeviceState(void); ///< Initial device state (stopped).
|
AudioOutState audoutGetDeviceState(void); ///< Initial device state (stopped).
|
||||||
|
451
nx/source/services/audin.c
Normal file
451
nx/source/services/audin.c
Normal file
@ -0,0 +1,451 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "result.h"
|
||||||
|
#include "ipc.h"
|
||||||
|
#include "services/audin.h"
|
||||||
|
#include "services/sm.h"
|
||||||
|
|
||||||
|
#define DEVICE_NAME_LENGTH 0x100
|
||||||
|
#define DEFAULT_SAMPLE_RATE 0xBB80
|
||||||
|
#define DEFAULT_CHANNEL_COUNT 0x00020000
|
||||||
|
|
||||||
|
static Service g_audinSrv;
|
||||||
|
static Service g_audinIAudioIn;
|
||||||
|
|
||||||
|
static Handle g_audinBufferEventHandle = INVALID_HANDLE;
|
||||||
|
|
||||||
|
static u32 g_sampleRate = 0;
|
||||||
|
static u32 g_channelCount = 0;
|
||||||
|
static PcmFormat g_pcmFormat = PcmFormat_Invalid;
|
||||||
|
static AudioInState g_deviceState = AudioInState_Stopped;
|
||||||
|
|
||||||
|
static Result _audinRegisterBufferEvent(Handle *BufferEvent);
|
||||||
|
|
||||||
|
Result audinInitialize(void)
|
||||||
|
{
|
||||||
|
if (serviceIsActive(&g_audinSrv))
|
||||||
|
return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized);
|
||||||
|
|
||||||
|
Result rc = 0;
|
||||||
|
rc = smGetService(&g_audinSrv, "audin:u");
|
||||||
|
|
||||||
|
// Setup the default device
|
||||||
|
if (R_SUCCEEDED(rc))
|
||||||
|
{
|
||||||
|
// Passing an empty device name will open the default "BuiltInHeadset"
|
||||||
|
char DeviceNameIn[DEVICE_NAME_LENGTH] = {0};
|
||||||
|
char DeviceNameOut[DEVICE_NAME_LENGTH] = {0};
|
||||||
|
|
||||||
|
// Open audio input device
|
||||||
|
rc = audinOpenAudioIn(DeviceNameIn, DeviceNameOut, DEFAULT_SAMPLE_RATE, DEFAULT_CHANNEL_COUNT, &g_sampleRate, &g_channelCount, &g_pcmFormat, &g_deviceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register global handle for buffer events
|
||||||
|
if (R_SUCCEEDED(rc))
|
||||||
|
rc = _audinRegisterBufferEvent(&g_audinBufferEventHandle);
|
||||||
|
|
||||||
|
if (R_FAILED(rc))
|
||||||
|
audinExit();
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void audinExit(void)
|
||||||
|
{
|
||||||
|
if (g_audinBufferEventHandle != INVALID_HANDLE) {
|
||||||
|
svcCloseHandle(g_audinBufferEventHandle);
|
||||||
|
g_audinBufferEventHandle = INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_sampleRate = 0;
|
||||||
|
g_channelCount = 0;
|
||||||
|
g_pcmFormat = PcmFormat_Invalid;
|
||||||
|
g_deviceState = AudioInState_Stopped;
|
||||||
|
|
||||||
|
serviceClose(&g_audinIAudioIn);
|
||||||
|
serviceClose(&g_audinSrv);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 audinGetSampleRate(void) {
|
||||||
|
return g_sampleRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 audinGetChannelCount(void) {
|
||||||
|
return g_channelCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
PcmFormat audinGetPcmFormat(void) {
|
||||||
|
return g_pcmFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
AudioInState audinGetDeviceState(void) {
|
||||||
|
return g_deviceState;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinWaitCaptureFinish(AudioInBuffer **released, u32* released_count, u64 timeout) {
|
||||||
|
// Wait on the buffer event handle
|
||||||
|
Result rc = svcWaitSynchronizationSingle(g_audinBufferEventHandle, timeout);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc))
|
||||||
|
{
|
||||||
|
// Signal the buffer event handle right away
|
||||||
|
svcResetSignal(g_audinBufferEventHandle);
|
||||||
|
|
||||||
|
// Grab the released buffer
|
||||||
|
rc = audinGetReleasedAudioInBuffer(released, released_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinCaptureBuffer(AudioInBuffer *source, AudioInBuffer **released) {
|
||||||
|
Result rc = 0;
|
||||||
|
u32 released_count = 0;
|
||||||
|
|
||||||
|
// Try to push the supplied buffer to the audio input device
|
||||||
|
rc = audinAppendAudioInBuffer(source);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc))
|
||||||
|
rc = audinWaitCaptureFinish(released, &released_count, U64_MAX);
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinListAudioIns(char *DeviceNames, u32 *DeviceNamesCount) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
ipcAddRecvBuffer(&c, DeviceNames, DEVICE_NAME_LENGTH, 0);
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 0;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinSrv);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
u32 DeviceNamesCount;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc) && DeviceNamesCount)
|
||||||
|
*DeviceNamesCount = resp->DeviceNamesCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinOpenAudioIn(const char *DeviceNameIn, char *DeviceNameOut, u32 SampleRateIn, u32 ChannelCountIn, u32 *SampleRateOut, u32 *ChannelCountOut, PcmFormat *Format, AudioInState *State) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
u32 sample_rate;
|
||||||
|
u32 channel_count;
|
||||||
|
u64 client_pid;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
ipcSendPid(&c);
|
||||||
|
ipcSendHandleCopy(&c, CUR_PROCESS_HANDLE);
|
||||||
|
ipcAddSendBuffer(&c, DeviceNameIn, DEVICE_NAME_LENGTH, 0);
|
||||||
|
ipcAddRecvBuffer(&c, DeviceNameOut, DEVICE_NAME_LENGTH, 0);
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 1;
|
||||||
|
raw->sample_rate = SampleRateIn;
|
||||||
|
raw->channel_count = ChannelCountIn;
|
||||||
|
raw->client_pid = 0;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinSrv);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
u32 sample_rate;
|
||||||
|
u32 channel_count;
|
||||||
|
u32 pcm_format;
|
||||||
|
u32 state;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
serviceCreate(&g_audinIAudioIn, r.Handles[0]);
|
||||||
|
|
||||||
|
if (SampleRateOut)
|
||||||
|
*SampleRateOut = resp->sample_rate;
|
||||||
|
|
||||||
|
if (ChannelCountOut)
|
||||||
|
*ChannelCountOut = resp->channel_count;
|
||||||
|
|
||||||
|
if (Format)
|
||||||
|
*Format = resp->pcm_format;
|
||||||
|
|
||||||
|
if (State)
|
||||||
|
*State = resp->state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinGetAudioInState(AudioInState *State) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 0;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
u32 state;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc) && State)
|
||||||
|
*State = resp->state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinStartAudioIn(void) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 1;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinStopAudioIn(void) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 2;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinAppendAudioInBuffer(AudioInBuffer *Buffer) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
u64 tag;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
ipcAddSendBuffer(&c, Buffer, sizeof(*Buffer), 0);
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 3;
|
||||||
|
raw->tag = (u64)Buffer;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Result _audinRegisterBufferEvent(Handle *BufferEvent) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 4;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc) && BufferEvent)
|
||||||
|
*BufferEvent = r.Handles[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinGetReleasedAudioInBuffer(AudioInBuffer **Buffer, u32 *ReleasedBuffersCount) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
ipcAddRecvBuffer(&c, Buffer, sizeof(*Buffer), 0);
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 5;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
u32 released_buffers_count;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc) && ReleasedBuffersCount)
|
||||||
|
*ReleasedBuffersCount = resp->released_buffers_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audinContainsAudioInBuffer(AudioInBuffer *Buffer, bool *ContainsBuffer) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
u64 tag;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 6;
|
||||||
|
raw->tag = (u64)Buffer;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_audinIAudioIn);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
u32 contains_buffer;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc) && ContainsBuffer)
|
||||||
|
*ContainsBuffer = (resp->contains_buffer & 0x01);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
@ -82,35 +82,33 @@ AudioOutState audoutGetDeviceState(void) {
|
|||||||
return g_deviceState;
|
return g_deviceState;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result audoutWaitPlayFinish(AudioOutBuffer *released, u64 timeout) {
|
Result audoutWaitPlayFinish(AudioOutBuffer **released, u32* released_count, u64 timeout) {
|
||||||
// Wait on the buffer event handle
|
// Wait on the buffer event handle
|
||||||
Result do_wait = svcWaitSynchronizationSingle(g_audoutBufferEventHandle, timeout);
|
Result rc = svcWaitSynchronizationSingle(g_audoutBufferEventHandle, timeout);
|
||||||
|
|
||||||
if (R_SUCCEEDED(do_wait))
|
if (R_SUCCEEDED(rc))
|
||||||
{
|
{
|
||||||
|
// Signal the buffer event handle right away
|
||||||
svcResetSignal(g_audoutBufferEventHandle);
|
svcResetSignal(g_audoutBufferEventHandle);
|
||||||
|
|
||||||
|
// Grab the released buffer
|
||||||
|
rc = audoutGetReleasedAudioOutBuffer(released, released_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result audoutPlayBuffer(AudioOutBuffer *source, AudioOutBuffer **released) {
|
||||||
|
Result rc = 0;
|
||||||
u32 released_count = 0;
|
u32 released_count = 0;
|
||||||
Result do_release = audoutGetReleasedAudioOutBuffer(released, &released_count);
|
|
||||||
|
|
||||||
// Ensure that all buffers are released and return the last one only
|
|
||||||
while (R_SUCCEEDED(do_release) && (released_count > 0))
|
|
||||||
do_release = audoutGetReleasedAudioOutBuffer(released, &released_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return do_wait;
|
|
||||||
}
|
|
||||||
|
|
||||||
Result audoutPlayBuffer(AudioOutBuffer *source, AudioOutBuffer *released) {
|
|
||||||
// Try to push the supplied buffer to the audio output device
|
// Try to push the supplied buffer to the audio output device
|
||||||
Result do_append = audoutAppendAudioOutBuffer(source);
|
rc = audoutAppendAudioOutBuffer(source);
|
||||||
|
|
||||||
if (R_SUCCEEDED(do_append))
|
if (R_SUCCEEDED(rc))
|
||||||
{
|
rc = audoutWaitPlayFinish(released, &released_count, U64_MAX);
|
||||||
audoutWaitPlayFinish(released, U64_MAX);
|
|
||||||
}
|
|
||||||
|
|
||||||
return do_append;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result audoutListAudioOuts(char *DeviceNames, u32 *DeviceNamesCount) {
|
Result audoutListAudioOuts(char *DeviceNames, u32 *DeviceNamesCount) {
|
||||||
@ -319,7 +317,7 @@ Result audoutAppendAudioOutBuffer(AudioOutBuffer *Buffer) {
|
|||||||
u64 tag;
|
u64 tag;
|
||||||
} *raw;
|
} *raw;
|
||||||
|
|
||||||
ipcAddSendBuffer(&c, Buffer, sizeof(AudioOutBuffer), 0);
|
ipcAddSendBuffer(&c, Buffer, sizeof(*Buffer), 0);
|
||||||
|
|
||||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
@ -378,7 +376,7 @@ static Result _audoutRegisterBufferEvent(Handle *BufferEvent) {
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer *Buffer, u32 *ReleasedBuffersCount) {
|
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer **Buffer, u32 *ReleasedBuffersCount) {
|
||||||
IpcCommand c;
|
IpcCommand c;
|
||||||
ipcInitialize(&c);
|
ipcInitialize(&c);
|
||||||
|
|
||||||
@ -387,7 +385,7 @@ Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer *Buffer, u32 *ReleasedBuff
|
|||||||
u64 cmd_id;
|
u64 cmd_id;
|
||||||
} *raw;
|
} *raw;
|
||||||
|
|
||||||
ipcAddRecvBuffer(&c, Buffer, sizeof(AudioOutBuffer), 0);
|
ipcAddRecvBuffer(&c, Buffer, sizeof(*Buffer), 0);
|
||||||
|
|
||||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
@ -429,7 +427,7 @@ Result audoutContainsAudioOutBuffer(AudioOutBuffer *Buffer, bool *ContainsBuffer
|
|||||||
|
|
||||||
raw->magic = SFCI_MAGIC;
|
raw->magic = SFCI_MAGIC;
|
||||||
raw->cmd_id = 6;
|
raw->cmd_id = 6;
|
||||||
raw->tag = (u64)&Buffer;
|
raw->tag = (u64)Buffer;
|
||||||
|
|
||||||
Result rc = serviceIpcDispatch(&g_audoutIAudioOut);
|
Result rc = serviceIpcDispatch(&g_audoutIAudioOut);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user