Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch: clean up audio a bit #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 36 additions & 41 deletions src/platform/switch/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,92 +29,87 @@
#define ALIGN_TO(x, a) (((x) + ((a) - 1)) & ~((a) - 1))

namespace {
const int samplerate = 48000;
const int bytes_per_sample = 4;
const int samples_per_buf = 4096;
const int buf_size = samples_per_buf * bytes_per_sample;
NxAudio* instance = nullptr;
constexpr int samplerate = 48000;
constexpr int bytes_per_sample = 4;
constexpr int samples_per_buf = 4096;
constexpr int buf_size = samples_per_buf * bytes_per_sample;
static Thread audio_thread;
static Mutex audio_mutex;
}

void switch_audio_thread(void*) {
uint8_t *buffer1 = (uint8_t*)memalign(0x1000, ALIGN_TO(buf_size, 0x1000));
uint8_t *buffer2 = (uint8_t*)memalign(0x1000, ALIGN_TO(buf_size, 0x1000));
void switch_audio_thread(void* args) {
NxAudio* instance = (NxAudio *)args;
uint32_t released_count;

AudioOutBuffer source_buffers[2], *released_buffer;

// Init audio buffers
source_buffers[0].buffer = buffer1;
source_buffers[1].buffer = buffer2;

for (int i = 0; i < 2; i++){
source_buffers[i].next = NULL;
source_buffers[i].buffer = memalign(0x1000, ALIGN_TO(buf_size, 0x1000));
if (!source_buffers[i].buffer) {
Output::Error("Could not create audio buffers!");
return;
}
source_buffers[i].next = nullptr;
source_buffers[i].buffer_size = buf_size;
source_buffers[i].data_size = buf_size;
source_buffers[i].data_offset = 0;

// Fill in first portion of audio
instance->LockMutex();
instance->Decode((uint8_t*)source_buffers[i].buffer, buf_size);
instance->UnlockMutex();
audoutAppendAudioOutBuffer(&source_buffers[i]);
}

for(;;) {
// A pretty bad way to close thread
if (instance->termStream) {
instance->termStream = false;
free(buffer1);
free(buffer2);
return;
}

// Render audio until termination requested
while(instance->want_audio) {
audoutWaitPlayFinish(&released_buffer, &released_count, UINT64_MAX);
instance->LockMutex();
instance->Decode((uint8_t*)released_buffer->buffer, buf_size);
instance->UnlockMutex();
audoutAppendAudioOutBuffer(released_buffer);

}

// Free memory
free(source_buffers[0].buffer);
free(source_buffers[1].buffer);
}

NxAudio::NxAudio(const Game_ConfigAudio& cfg) :
GenericAudio(cfg)
{
instance = this;
NxAudio::NxAudio(const Game_ConfigAudio& cfg) : GenericAudio(cfg) {
// Setup GenericAudio
SetFormat(samplerate, AudioDecoder::Format::S16, 2);

// Initialize audio service
audoutInitialize();
audoutStartAudioOut();

mutexInit(&audio_mutex);

threadCreate(&audio_thread, switch_audio_thread, NULL, NULL, 0x10000, 0x2B, -2);

SetFormat(samplerate, AudioDecoder::Format::S16, 2);

// Start streaming thread
want_audio = true;
threadCreate(&audio_thread, switch_audio_thread, this, nullptr, 0x10000, 0x2B, -2);
if (R_FAILED(threadStart(&audio_thread))) {
Output::Error("Failed to init audio thread.");
return;
}
}

NxAudio::~NxAudio() {
// Closing streaming thread
termStream = true;
// Close streaming thread
want_audio = false;
threadWaitForExit(&audio_thread);

// Deleting thread
threadClose(&audio_thread);
// Terminating audio API

// Terminate audio service
audoutStopAudioOut();
audoutExit();
}

void NxAudio::LockMutex() const {
mutexLock((Mutex*)&audio_mutex);
mutexLock(&audio_mutex);
}

void NxAudio::UnlockMutex() const {
mutexUnlock((Mutex*)&audio_mutex);
mutexUnlock(&audio_mutex);
}

#endif
6 changes: 1 addition & 5 deletions src/platform/switch/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ class NxAudio : public GenericAudio {

void LockMutex() const override;
void UnlockMutex() const override;

volatile bool termStream = false;

private:
Thread audio_thread;
Mutex audio_mutex;
volatile bool want_audio;
}; // class NxAudio

#endif
Loading