Skip to content

Commit d07ca9c

Browse files
committed
Starting audio Amp only when playing sounds, stopping otherwise
1 parent 343f64f commit d07ca9c

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

RetroFE/Source/Execute/Launcher.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,19 @@ bool Launcher::run(std::string collection, Item *collectionItem)
119119
selectedItemsDirectory,
120120
collection);
121121

122+
/* Restart audio amp */
123+
popen(SHELL_CMD_TURN_AMPLI_ON, "r");
124+
125+
/* Execute game */
122126
if(!execute(executablePath, args, currentDirectory))
123127
{
124128
Logger::write(Logger::ZONE_ERROR, "Launcher", "Failed to launch.");
125129
res = false;
126130
}
127131

132+
/* Stop audio amp */
133+
popen(SHELL_CMD_TURN_AMPLI_OFF, "r");
134+
128135
/* Restore stored PID */
129136
char shellCmd[20];
130137
sprintf(shellCmd, "%s %d", SHELL_CMD_RECORD_PID, getpid());

RetroFE/Source/Sound/Sound.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "Sound.h"
1818

1919
#include "../Utility/Log.h"
20+
#include "../Utility/Utils.h"
21+
22+
SDL_TimerID Sound::idTimer = 0;
23+
int Sound::ampliStarted = 0;
2024

2125
Sound::Sound(std::string file, std::string altfile)
2226
: file_(file)
@@ -44,14 +48,40 @@ Sound::~Sound()
4448

4549
void Sound::play()
4650
{
51+
//printf("%s\n", __func__);
52+
SDL_RemoveTimer(idTimer);
53+
if(!ampliStarted){
54+
popen(SHELL_CMD_TURN_AMPLI_ON, "r");
55+
ampliStarted = 1;
56+
}
57+
4758
if(chunk_)
4859
{
4960
channel_ = Mix_PlayChannel(-1, chunk_, 0);
61+
Mix_ChannelFinished(finished);
62+
}
63+
}
64+
65+
uint32_t Sound::turnOffAmpli(uint32_t interval, void *param)
66+
{
67+
//printf("%s\n", __func__);
68+
popen(SHELL_CMD_TURN_AMPLI_OFF, "r");
69+
ampliStarted = 0;
70+
return 0;
71+
}
72+
73+
void Sound::finished(int channel)
74+
{
75+
//printf("%s\n", __func__);
76+
if((channel == -1) || !Mix_Playing(channel)){
77+
SDL_RemoveTimer(idTimer);
78+
idTimer = SDL_AddTimer(500, turnOffAmpli, NULL);
5079
}
5180
}
5281

5382
bool Sound::free()
5483
{
84+
//printf("%s\n", __func__);
5585
if(chunk_)
5686
{
5787
Mix_FreeChunk(chunk_);
@@ -64,6 +94,7 @@ bool Sound::free()
6494

6595
bool Sound::allocate()
6696
{
97+
//printf("%s\n", __func__);
6798
if(!chunk_)
6899
{
69100
chunk_ = Mix_LoadWAV(file_.c_str());
@@ -75,5 +106,6 @@ bool Sound::allocate()
75106

76107
bool Sound::isPlaying()
77108
{
109+
//printf("%s\n", __func__);
78110
return (channel_ != -1) && Mix_Playing(channel_);
79111
}

RetroFE/Source/Sound/Sound.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <string>
1919
#include <SDL/SDL_mixer.h>
20+
#include "SDL.h"
2021
class Sound
2122
{
2223
public:
@@ -27,6 +28,10 @@ class Sound
2728
bool free();
2829
bool isPlaying();
2930
private:
31+
static void finished(int channel);
32+
static uint32_t turnOffAmpli(uint32_t interval, void *param);
33+
static int ampliStarted;
34+
static SDL_TimerID idTimer;
3035
std::string file_;
3136
Mix_Chunk *chunk_;
3237
int channel_;

RetroFE/Source/Utility/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#define SHELL_CMD_ROOTFS_RW "rw"
2424
#define SHELL_CMD_ROOTFS_RO "ro"
2525
#define SHELL_CMD_RECORD_PID "record_pid"
26+
#define SHELL_CMD_TURN_AMPLI_ON "start_audio_amp 1"
27+
#define SHELL_CMD_TURN_AMPLI_OFF "start_audio_amp 0"
2628

2729
class Utils
2830
{

0 commit comments

Comments
 (0)