Skip to content

Commit

Permalink
Move audio out of drawing thread
Browse files Browse the repository at this point in the history
  • Loading branch information
igor725 committed Oct 26, 2024
1 parent 7f64040 commit 6c1c200
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
6 changes: 2 additions & 4 deletions OpenOrbis-tc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ STRING(REGEX REPLACE "\\\\" "/" OO_PS4_TOOLCHAIN "$ENV{OO_PS4_TOOLCHAIN}")

set(CMAKE_SYSTEM_NAME FreeBSD)
set(CMAKE_C_COMPILER_TARGET "x86_64-pc-freebsd12-elf")
set(CMAKE_C_FLAGS "-fPIC -funwind-tables -fshort-wchar")
set(CMAKE_CXX_COMPILER_TARGET "${CMAKE_C_COMPILER_TARGET}")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
set(CMAKE_SYSROOT ${OO_PS4_TOOLCHAIN})

Expand All @@ -16,13 +14,13 @@ include_directories(SYSTEM
${OO_PS4_TOOLCHAIN}/include/c++/v1
)

link_directories(
link_directories(BEFORE
${OO_PS4_TOOLCHAIN}/lib
)

add_link_options(-pie -nostartfiles -nodefaultlibs -lc -lc++ -lkernel -fuse-ld=lld -Wl,-m,elf_x86_64 -Wl,--eh-frame-hdr "-Wl,--script,${OO_PS4_TOOLCHAIN}/link.x")

add_compile_options(-nostdinc++ -nostdinc)
add_compile_options(-nostdinc++ -nostdinc -fPIC -funwind-tables -fshort-wchar)

add_compile_definitions(STBI_NO_SIMD=1)

Expand Down
31 changes: 27 additions & 4 deletions input/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "controller.h"
#include "log.h"
#include <cmath>

static OrbisPadColor padColors[8] = {
{0xff, 0xff, 0xff, 0xff}, {0x00, 0xff, 0x00, 0xff},
Expand Down Expand Up @@ -28,11 +29,33 @@ bool Controller::Init(int controllerUserID) {
});

// Open a handle for the controller
this->playAudio = false;
this->userID = controllerUserID;
this->pad = scePadOpen(this->userID, 0, 0, NULL);
this->padSpeaker =
sceAudioOutOpen(controllerUserID, ORBIS_AUDIO_OUT_PORT_TYPE_PADSPK, 0,
1024, 48000, 0 /* S16_MONO */);
this->playThread = std::thread([this]() {
std::mutex aplay;
static int16_t abuf[1024];
static float wpos = 0.0f;

while (true) {
std::unique_lock lock(aplay);
this->playCond.wait(lock, [=]() -> bool { return this->playAudio; });

for (int i = 0; i < 1024; i++) {
abuf[i] = std::sinf(wpos) * (int16_t)32767;
wpos += 0.1f;
}

if (this->padSpeaker == 0)
continue;

if (sceAudioOutOutput(this->padSpeaker, abuf) != 1024)
DEBUGLOG << "[DEBUG] [ERROR] Failed to push audio data!";
}
});

if (this->pad < 1) {
DEBUGLOG << "[DEBUG] [ERROR] Failed to open pad!";
Expand Down Expand Up @@ -144,10 +167,10 @@ bool Controller::TouchpadPressed() {
return CheckButtonsPressed(ORBIS_PAD_BUTTON_TOUCH_PAD);
}

bool Controller::SendAudioData(int16_t abuf[1024]) {
if (abuf == nullptr || this->padSpeaker == 0)
return false;
return sceAudioOutOutput(this->padSpeaker, abuf) == 1024;
bool Controller::SetAudio(bool state) {
this->playAudio = state;
this->playCond.notify_one();
return state;
}

OrbisPadColor Controller::GetColor() { return padColors[this->currPadColor]; }
Expand Down
16 changes: 10 additions & 6 deletions input/controller.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

#include <atomic>
#include <condition_variable>
#include <orbis/AudioOut.h>
#include <orbis/Pad.h>
#include <orbis/UserService.h>

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <thread>

class Controller {
int pad;
Expand All @@ -15,6 +17,10 @@ class Controller {
OrbisPadData padData;
OrbisPadInformation padInfo;

std::atomic<bool> playAudio;
std::condition_variable playCond;
std::thread playThread;

void setButtonState(int state);

public:
Expand Down Expand Up @@ -46,11 +52,9 @@ class Controller {
int ReadFingers(OrbisPadTouch **fingers);
int GetTouchPadResolution(int *w, int *h);
void ReadSticks(float *leftx, float *lefty, float *rightx, float *righty);
bool SendAudioData(int16_t abuf[1024]);
bool SetAudio(bool state);
void ReadGyro(vec_float4 *data);
void ResetOrientation();
OrbisPadColor GetColor();
OrbisPadColor NextColor();
};

#endif
14 changes: 1 addition & 13 deletions input/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <cstdint>
#include <math.h>
#include <sstream>

#include "controller.h"
Expand Down Expand Up @@ -88,24 +87,13 @@ static void drawControllerData(Scene2D *scene, Controller *controller) {
}
}

static int16_t abuf[1024];
static float wpos = 0.0f;

if (controller->CirclePressed()) {
circleBtn->Draw(scene, 1244, 416);
}

if (controller->CrossPressed()) {
if (controller->SetAudio(controller->CrossPressed())) {
xBtn->Draw(scene, 1187, 472);
controller->ResetOrientation();

for (int i = 0; i < 1024; i++) {
abuf[i] = ::sinf(wpos) * (int16_t)32767;
wpos += 0.1f;
}

if (!controller->SendAudioData(abuf))
DEBUGLOG << "Failed to send audio data to the active controller!";
}

if (controller->SquarePressed()) {
Expand Down

0 comments on commit 6c1c200

Please sign in to comment.