Skip to content

Commit

Permalink
Fix currentsong.txt updated too often
Browse files Browse the repository at this point in the history
Basically a redo of worker.php function updExtMetaFile() plus some changes and fixes to alsa.php function getALSAOutputFormat().
  • Loading branch information
moodeaudio committed Feb 21, 2024
1 parent e394714 commit 68ba3f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
82 changes: 40 additions & 42 deletions www/daemon/worker.php
Expand Up @@ -1671,47 +1671,41 @@ function updBoss2DopVolume () {
}

function updExtMetaFile() {
// Output rate
$hwParams = getAlsaHwParams($_SESSION['cardnum']);
if ($hwParams['status'] == 'active') {
if ($hwParams['format'] == 'DSD') {
$hwParamsFormat = 'DSD Bitstream, ' . $hwParams['channels'];
} else {
$hwParamsFormat = 'PCM ' . $hwParams['format'] . ' bit ' . $hwParams['rate'] . ' kHz, ' . $hwParams['channels'];
}
$hwParamsCalcrate = ', ' . $hwParams['calcrate'] . ' Mbps';
} else if ($_SESSION['multiroom_tx'] == 'On') {
$hwParamsFormat = '';
$hwParamsCalcrate = 'Multiroom sender';
$fileData = parseDelimFile(file_get_contents('/var/local/www/currentsong.txt'), '=');

if ($GLOBALS['aplactive'] == '1') {
$renderer = 'AirPlay Active';
} else if ($GLOBALS['spotactive'] == '1') {
$renderer = 'Spotify Active';
} else if ($GLOBALS['slactive'] == '1') {
$renderer = 'Squeezelite Active';
} else if ($GLOBALS['rbactive'] == '1') {
$renderer = 'Roonbridge Active';
} else if ($GLOBALS['inpactive'] == '1') {
$renderer = $_SESSION['audioin'] . ' Input Active';
} else if ($_SESSION['btactive'] == '1' && $_SESSION['audioout'] == 'Local') {
$renderer = 'Bluetooth Active';
} else {
$hwParamsFormat = '';
$hwParamsCalcrate = '0 bps';
$renderer = '';
}
// Currentsong.txt
$fileMeta = parseDelimFile(file_get_contents('/var/local/www/currentsong.txt'), '=');

if ($GLOBALS['aplactive'] == '1' || $GLOBALS['spotactive'] == '1' || $GLOBALS['slactive'] == '1'
|| $GLOBALS['rbactive'] == '1' || $GLOBALS['inpactive'] == '1' || ($_SESSION['btactive'] && $_SESSION['audioout'] == 'Local')) {
if (!empty($renderer)) {
//workerLog('worker: Renderer active');
if ($GLOBALS['aplactive'] == '1') {
$renderer = 'AirPlay Active';
} else if ($GLOBALS['spotactive'] == '1') {
$renderer = 'Spotify Active';
} else if ($GLOBALS['slactive'] == '1') {
$renderer = 'Squeezelite Active';
} else if ($GLOBALS['rbactive'] == '1') {
$renderer = 'Roonbridge Active';
} else if ($GLOBALS['inpactive'] == '1') {
$renderer = $_SESSION['audioin'] . ' Input Active';
$hwParams = getAlsaHwParams($_SESSION['cardnum']);

if ($hwParams['status'] == 'active') {
$hwParamsFormat = 'PCM ' . $hwParams['format'] . '/' . $hwParams['rate'] . ' kHz, 2ch';
} else if ($_SESSION['multiroom_tx'] == 'On') {
$hwParamsFormat = 'PCM 16/48 kHz, 2ch (Multiroom sender)';
} else {
$renderer = 'Bluetooth Active';
$hwParamsFormat = 'Not playing';
}
// Write file only if something has changed
if ($fileMeta['file'] != $renderer && $hwParamsCalcrate != '0 bps') {
//workerLog('worker: Writing currentsong file');

if ($fileData['file'] != $renderer) {
//workerLog('worker: Update currentsong.txt file (Renderer)');
$fh = fopen('/tmp/currentsong.txt', 'w');
$data = 'file=' . $renderer . "\n";
$data .= 'outrate=' . $hwParamsFormat . $hwParamsCalcrate . "\n"; ;
$data .= 'outrate=' . $hwParamsFormat . "\n"; ;
fwrite($fh, $data);
fclose($fh);
rename('/tmp/currentsong.txt', '/var/local/www/currentsong.txt');
Expand All @@ -1720,16 +1714,19 @@ function updExtMetaFile() {
} else {
//workerLog('worker: MPD active');
$sock = openMpdSock('localhost', 6600);
$current = getMpdStatus($sock);
$current = enhanceMetadata($current, $sock, 'worker_php');
$status = getMpdStatus($sock);
$current = enhanceMetadata($status, $sock, 'worker_php');
closeMpdSock($sock);
//workerLog(print_r($current, true));
//workerLog('updExtMetaFile(): currentencoded=' . $_SESSION['currentencoded']);

// Write file only if something has changed
if ($current['title'] != $fileMeta['title'] || $current['album'] != $fileMeta['album'] || $_SESSION['volknob'] != $fileMeta['volume'] ||
$_SESSION['volmute'] != $fileMeta['mute'] || $current['state'] != $fileMeta['state'] || $fileMeta['outrate'] != $hwParamsFormat . $hwParamsCalcrate) {
//workerLog('worker: Writing currentsong file');
if (
$fileData['state'] != $current['state'] ||
$fileData['title'] != $current['title'] ||
$fileData['album'] != $current['album'] ||
$fileData['volume'] != $_SESSION['volknob'] ||
$fileData['mute'] != $_SESSION['volmute'] ||
$fileData['outrate'] != $current['output']
) {
//workerLog('worker: Update currentsong.txt file (MPD)');
$fh = fopen('/tmp/currentsong.txt', 'w');
// Default
$data = 'file=' . $current['file'] . "\n";
Expand All @@ -1744,7 +1741,8 @@ function updExtMetaFile() {
// Other
$data .= 'encoded=' . $current['encoded'] . "\n";
$data .= 'bitrate=' . $current['bitrate'] . "\n";
$data .= 'outrate=' . $current['output'] . $hwParamsCalcrate . "\n"; ;
//$data .= 'outrate=' . $current['output'] . $hwParamsCalcrate . "\n"; ;
$data .= 'outrate=' . $current['output'] . "\n"; ;
$data .= 'volume=' . $_SESSION['volknob'] . "\n";
$data .= 'mute=' . $_SESSION['volmute'] . "\n";
$data .= 'state=' . $current['state'] . "\n";
Expand Down
14 changes: 7 additions & 7 deletions www/inc/alsa.php
Expand Up @@ -171,17 +171,17 @@ function setALSAVolTo0dB($alsaVolMax = '100') {

// Needs the session to be available for getAlsaCardNum()
function getALSAOutputFormat($mpdState = '', $mpdAudioSampleRate = '') {
if ($mpdState == '') { // Called from command/index.php get_output_format
if ($mpdState == '') {
// Called from command/index.php get_output_format
$mpdStatus = getMpdStatus(getMpdSock());
$mpdState = $mpdStatus['state'];
$mpdAudioSampleRate = $mpdStatus['audio_sample_rate'];
$outputStr = alsaOutputStr();
} else if ($mpdState == 'play') { // Called from enhanceMetadata() in inc/mpd.php
$outputStr = alsaOutputStr($mpdStatus['audio_sample_rate']);
} else if ($mpdState == 'play') {
// Called from enhanceMetadata() in inc/mpd.php
$result = sqlQuery("SELECT value FROM cfg_system WHERE param='audioout'", sqlConnect());
if ($result['value'] == 'Bluetooth') {
$outputStr = 'PCM 16/44.1 kHz, 2ch'; // Maybe also 48K ?
} else {
$outputStr = alsaOutputStr();
$outputStr = alsaOutputStr($mpdAudioSampleRate);
}
} else {
$outputStr = 'Not playing';
Expand All @@ -190,7 +190,7 @@ function getALSAOutputFormat($mpdState = '', $mpdAudioSampleRate = '') {
return $outputStr;
}

function alsaOutputStr() {
function alsaOutputStr($mpdAudioSampleRate = '') {
$maxLoops = 3;
$sleepTime = 250000;
// Loop when checking hwparams to allow for any latency in the audio pipeline
Expand Down

0 comments on commit 68ba3f6

Please sign in to comment.