Read and write correct sample rate

This commit is contained in:
hodasemi 2023-03-15 23:37:32 +01:00
parent fa4084ae07
commit 58691da7f9

View file

@ -41,7 +41,7 @@ const Indicator = GObject.registerClass(
style_class: 'system-status-icon',
}));
let sampleRate = getCurrentSampleRate();
let sampleRate = getForcedSampleRate();
let currentSampleRate = new PopupMenu.PopupMenuItem(_("Sample Rate: " + sampleRate));
this.menu.addMenuItem(currentSampleRate);
@ -51,8 +51,9 @@ const Indicator = GObject.registerClass(
let allowedRate = new PopupMenu.PopupMenuItem(_("Allowed Rates: " + allowedRates[i]));
allowedRate.connect('activate', () => {
console.log("set sample rate " + allowedRates[i]);
setSampleRate(allowedRates[i]);
currentSampleRate.label.text = "Sample Rate: " + getCurrentSampleRate();
currentSampleRate.label.text = "Sample Rate: " + getForcedSampleRate();
});
this.menu.addMenuItem(allowedRate);
@ -83,13 +84,37 @@ function init(meta) {
}
function setSampleRate(sampleRate) {
spawnCommandLine("pw-metadata -n settings 0 clock.rate " + sampleRate);
spawnCommandLine("pw-metadata -n settings 0 clock.force-rate " + sampleRate);
}
function getCurrentSampleRate() {
function getForcedSampleRate() {
let res = spawnCommandLine("pw-metadata -n settings");
let lines = res.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('clock.force-rate')) {
console.log("found current force rate setting");
let phrases = lines[i].split(" ");
for (let j = 0; j < phrases.length; j++) {
if (phrases[j].includes("value")) {
let value = phrases[j].slice(7, -1);
console.log("with current value " + value);
if (value) {
return value;
}
}
}
}
}
return getCurrentSampleRate(lines);
}
function getCurrentSampleRate(lines) {
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('clock.rate')) {
let phrases = lines[i].split(" ");