Skip to content

Commit

Permalink
Audio input implementation and fixes (#60)
Browse files Browse the repository at this point in the history
* Implemented audin service and fixed released audio buffer handling
  • Loading branch information
hexkyz authored and yellows8 committed Mar 4, 2018
1 parent ca03c58 commit 208daf6
Show file tree
Hide file tree
Showing 6 changed files with 559 additions and 39 deletions.
1 change: 1 addition & 0 deletions nx/include/switch.h
Expand Up @@ -35,6 +35,7 @@ extern "C" {
#include "switch/services/acc.h"
#include "switch/services/apm.h"
#include "switch/services/applet.h"
#include "switch/services/audin.h"
#include "switch/services/audout.h"
//#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead
#include "switch/services/fatal.h"
Expand Down
19 changes: 19 additions & 0 deletions nx/include/switch/audio/audio.h
@@ -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,

This comment has been minimized.

Copy link
@fincs

fincs Mar 4, 2018

Contributor

This should have been PcmFormat_Adpcm...

} PcmFormat;
60 changes: 60 additions & 0 deletions nx/include/switch/services/audin.h
@@ -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).
27 changes: 9 additions & 18 deletions nx/include/switch/services/audout.h
Expand Up @@ -6,17 +6,7 @@
*/
#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;
#include "../audio/audio.h"

typedef enum {
AudioOutState_Started = 0,
Expand All @@ -30,7 +20,7 @@ struct AudioOutBuffer
{
AudioOutBuffer* next; ///< Next buffer. (Unused)
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_offset; ///< Offset of data inside the buffer. (Unused?)
};
Expand All @@ -44,26 +34,27 @@ Result audoutGetAudioOutState(AudioOutState *State);
Result audoutStartAudioOut(void);
Result audoutStopAudioOut(void);
Result audoutAppendAudioOutBuffer(AudioOutBuffer *Buffer);
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer *Buffer, u32 *ReleasedBuffersCount);
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer **Buffer, u32 *ReleasedBuffersCount);
Result audoutContainsAudioOutBuffer(AudioOutBuffer *Buffer, bool *ContainsBuffer);

/**
* @brief Submits an audio sample data buffer for playing and waits for it to finish playing.
* @brief Uses \ref audoutAppendAudioOutBuffer and \ref audoutWaitPlayFinish internally.
* @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.
* @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.
*/
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.
u32 audoutGetSampleRate(void); ///< Supported sample rate (48000Hz).
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).

0 comments on commit 208daf6

Please sign in to comment.