Skip to content

Commit

Permalink
Starting audio Amp only when playing sounds, stopping otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent-FK committed Jan 6, 2021
1 parent 343f64f commit d07ca9c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions RetroFE/Source/Execute/Launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@ bool Launcher::run(std::string collection, Item *collectionItem)
selectedItemsDirectory,
collection);

/* Restart audio amp */
popen(SHELL_CMD_TURN_AMPLI_ON, "r");

/* Execute game */
if(!execute(executablePath, args, currentDirectory))
{
Logger::write(Logger::ZONE_ERROR, "Launcher", "Failed to launch.");
res = false;
}

/* Stop audio amp */
popen(SHELL_CMD_TURN_AMPLI_OFF, "r");

/* Restore stored PID */
char shellCmd[20];
sprintf(shellCmd, "%s %d", SHELL_CMD_RECORD_PID, getpid());
Expand Down
32 changes: 32 additions & 0 deletions RetroFE/Source/Sound/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "Sound.h"

#include "../Utility/Log.h"
#include "../Utility/Utils.h"

SDL_TimerID Sound::idTimer = 0;
int Sound::ampliStarted = 0;

Sound::Sound(std::string file, std::string altfile)
: file_(file)
Expand Down Expand Up @@ -44,14 +48,40 @@ Sound::~Sound()

void Sound::play()
{
//printf("%s\n", __func__);
SDL_RemoveTimer(idTimer);
if(!ampliStarted){
popen(SHELL_CMD_TURN_AMPLI_ON, "r");
ampliStarted = 1;
}

if(chunk_)
{
channel_ = Mix_PlayChannel(-1, chunk_, 0);
Mix_ChannelFinished(finished);
}
}

uint32_t Sound::turnOffAmpli(uint32_t interval, void *param)
{
//printf("%s\n", __func__);
popen(SHELL_CMD_TURN_AMPLI_OFF, "r");
ampliStarted = 0;
return 0;
}

void Sound::finished(int channel)
{
//printf("%s\n", __func__);
if((channel == -1) || !Mix_Playing(channel)){
SDL_RemoveTimer(idTimer);
idTimer = SDL_AddTimer(500, turnOffAmpli, NULL);
}
}

bool Sound::free()
{
//printf("%s\n", __func__);
if(chunk_)
{
Mix_FreeChunk(chunk_);
Expand All @@ -64,6 +94,7 @@ bool Sound::free()

bool Sound::allocate()
{
//printf("%s\n", __func__);
if(!chunk_)
{
chunk_ = Mix_LoadWAV(file_.c_str());
Expand All @@ -75,5 +106,6 @@ bool Sound::allocate()

bool Sound::isPlaying()
{
//printf("%s\n", __func__);
return (channel_ != -1) && Mix_Playing(channel_);
}
5 changes: 5 additions & 0 deletions RetroFE/Source/Sound/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <string>
#include <SDL/SDL_mixer.h>
#include "SDL.h"
class Sound
{
public:
Expand All @@ -27,6 +28,10 @@ class Sound
bool free();
bool isPlaying();
private:
static void finished(int channel);
static uint32_t turnOffAmpli(uint32_t interval, void *param);
static int ampliStarted;
static SDL_TimerID idTimer;
std::string file_;
Mix_Chunk *chunk_;
int channel_;
Expand Down
2 changes: 2 additions & 0 deletions RetroFE/Source/Utility/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#define SHELL_CMD_ROOTFS_RW "rw"
#define SHELL_CMD_ROOTFS_RO "ro"
#define SHELL_CMD_RECORD_PID "record_pid"
#define SHELL_CMD_TURN_AMPLI_ON "start_audio_amp 1"
#define SHELL_CMD_TURN_AMPLI_OFF "start_audio_amp 0"

class Utils
{
Expand Down

0 comments on commit d07ca9c

Please sign in to comment.