Skip to content

A live bird collage card for your Home Assistant dashboard.

Bugfixes + added choice between ffmpeg and sox tools

Louis Croisezb49f8f730dd3
diff --git a/birdnet.conf-defaults b/birdnet.conf-defaults
index a3dab576..12d81c70 100644
--- a/birdnet.conf-defaults
+++ b/birdnet.conf-defaults
@@ -116,15 +116,21 @@ CONFIDENCE=0.7
SENSITIVITY=1.25
## Configuration of the frequency shifting feature, useful for earing impaired people.
-## You have to define a freq. shift from HI to LO:
-## FREQSHIFT_HI
-FREQSHIFT_HI=6000
+## FREQSHIFT_TOOL
-## FREQSHIFT_LO
+FREQSHIFT_TOOL=sox
+## If the tool is ffmpeg, you have to define a freq. shift from HI to LO:
+## FREQSHIFT_HI
+FREQSHIFT_HI=6000
+## FREQSHIFT_LO
FREQSHIFT_LO=3000
+## If the tool is sox, you have to define the pitch shift (amount of 100ths of semitone)
+## FREQSHIFT_PITCH
+FREQSHIFT_PITCH=-1500
+
## CHANNELS holds the variable that corresponds to the number of channels the
## sound card supports.
diff --git a/scripts/advanced.php b/scripts/advanced.php
index b7f4c493..650cd782 100644
--- a/scripts/advanced.php
+++ b/scripts/advanced.php
@@ -116,6 +116,22 @@ if(isset($_GET['submit'])) {
}
}
+ if(isset($_GET["freqshift_pitch"])) {
+ $freqshift_pitch = $_GET["freqshift_pitch"];
+ if(strcmp($freqshift_pitch,$config['FREQSHIFT_PITCH']) !== 0) {
+ $contents = preg_replace("/FREQSHIFT_PITCH=.*/", "FREQSHIFT_PITCH=$freqshift_pitch", $contents);
+ $contents2 = preg_replace("/FREQSHIFT_PITCH=.*/", "FREQSHIFT_PITCH=$freqshift_pitch", $contents2);
+ }
+ }
+
+ if(isset($_GET["freqshift_tool"])) {
+ $freqshift_tool = $_GET["freqshift_tool"];
+ if(strcmp($freqshift_tool,$config['FREQSHIFT_TOOL']) !== 0) {
+ $contents = preg_replace("/FREQSHIFT_TOOL=.*/", "FREQSHIFT_TOOL=$freqshift_tool", $contents);
+ $contents2 = preg_replace("/FREQSHIFT_TOOL=.*/", "FREQSHIFT_TOOL=$freqshift_tool", $contents2);
+ }
+ }
+
if(isset($_GET["full_disk"])) {
$full_disk = $_GET["full_disk"];
if(strcmp($full_disk,$config['FULL_DISK']) !== 0) {
@@ -291,12 +307,30 @@ foreach($formats as $format){
<h3>Accessibility Settings</h3>
<p>Birdsongs Frequency shifting configuration:<br>
- &nbsp;&nbsp;&nbsp;&nbsp;this can be useful for earing impaired people.<br>
- &nbsp;&nbsp;&nbsp;&nbsp;e.g. origin=6000, target=4000, performs a shift of 2000 Hz down.<br>
- &nbsp;&nbsp;&nbsp;&nbsp;<label for="hifreqshift">origin [Hz]: </label>
+ this can be useful for earing impaired people.<br>
+
+ <p style="margin-left: 40px">
+ Choose here the shifting tool, must be a value in the list { ffmpeg, sox }.<br>
+ <label for="freqshift_tool">shifting tool: </label>
+ <input name="freqshift_tool" type="text" value="<?php print($newconfig['FREQSHIFT_TOOL']);?>" required/><br>
+ </p>
+
+ <p style="margin-left: 40px">
+ using ffmpeg:
+ e.g. origin=6000, target=4000, performs a shift of 2000 Hz down.<br>
+ <label for="freqshift_hi">origin [Hz]: </label>
<input name="freqshift_hi" type="number" min="0" max="20000" step="1" value="<?php print($newconfig['FREQSHIFT_HI']);?>" required/><br>
- &nbsp;&nbsp;&nbsp;&nbsp;<label for="lofreqshift">target [Hz]: </label>
- <input name="freqshift_lo" type="number" min="0" max="20000" step="1" value="<?php print($newconfig['FREQSHIFT_LO']);?>" required/><br>
+ <label for="freqshift_lo">target [Hz]: </label>
+ <input name="freqshift_lo" type="number" min="0" max="20000" step="1" value="<?php print($newconfig['FREQSHIFT_LO']);?>" required/>
+ </p>
+
+ <p style="margin-left: 40px">
+ using sox:
+ e.g. shiftPitch=-1200 performs a shift of 1 octave down. This value is in 100ths of a semitone.<br>
+ <label for="freqshift_pitch">pitch shift: </label>
+ <input name="freqshift_pitch" type="number" min="-4000" max="4000" step="1" value="<?php print($newconfig['FREQSHIFT_PITCH']);?>" required/><br>
+ </p>
+
</p>
<br><br>
<input type="hidden" name="view" value="Advanced">
diff --git a/scripts/install_config.sh b/scripts/install_config.sh
index da5f6fb1..c71ec489 100755
--- a/scripts/install_config.sh
+++ b/scripts/install_config.sh
@@ -127,15 +127,21 @@ CONFIDENCE=0.7
SENSITIVITY=1.25
## Configuration of the frequency shifting feature, useful for earing impaired people.
-## You have to define a freq. shift from HI to LO:
-## FREQSHIFT_HI
-FREQSHIFT_HI=6000
+## FREQSHIFT_TOOL
-## FREQSHIFT_LO
+FREQSHIFT_TOOL=sox
+## If the tool is ffmpeg, you have to define a freq. shift from HI to LO:
+## FREQSHIFT_HI
+FREQSHIFT_HI=6000
+## FREQSHIFT_LO
FREQSHIFT_LO=3000
+## If the tool is sox, you have to define the pitch shift (amount of 100ths of semintone)
+## FREQSHIFT_PITCH
+FREQSHIFT_PITCH=-1500
+
## CHANNELS holds the variable that corresponds to the number of channels the
## sound card supports.
diff --git a/scripts/play.php b/scripts/play.php
index 020a342a..c211840c 100644
--- a/scripts/play.php
+++ b/scripts/play.php
@@ -100,22 +100,36 @@ if(isset($_GET['excludefile'])) {
$shifted_path = $home."/BirdSongs/Extracted/By_Date/shifted/";
if(isset($_GET['shiftfile'])) {
+
$filename = $_GET['shiftfile'];
- if(isset($_GET['shiftfreq'])) {
- $pp = pathinfo($filename);
- $dir = $pp['dirname'];
- $fn = $pp['filename'];
- $ext = $pp['extension'];
- $pi = $home."/BirdSongs/Extracted/By_Date/";
- $cmd = "/usr/bin/nohup /usr/bin/ffmpeg -y -i \"".$pi.$filename."\" -af \"rubberband=pitch=".$config['FREQSHIFT_LO']."/".$config['FREQSHIFT_HI']."\" \"".$shifted_path.$filename."\"";
- shell_exec("mkdir -p ".$shifted_path.$dir." && echo \"".$cmd."\" > /tmp/shift.sh && chmod +x /tmp/shift.sh");
+ $pp = pathinfo($filename);
+ $dir = $pp['dirname'];
+ $fn = $pp['filename'];
+ $ext = $pp['extension'];
+ $pi = $home."/BirdSongs/Extracted/By_Date/";
+
+ if(isset($_GET['doshift'])) {
+ $freqshift_tool = $config['FREQSHIFT_TOOL'];
+
+ if ($freqshift_tool == "ffmpeg") {
+ $cmd = "/usr/bin/nohup /usr/bin/ffmpeg -y -i \"".$pi.$filename."\" -af \"rubberband=pitch=".$config['FREQSHIFT_LO']."/".$config['FREQSHIFT_HI']."\" \"".$shifted_path.$filename."\"";
+ shell_exec("mkdir -p ".$shifted_path.$dir." && echo \"".$cmd."\" > /tmp/shift.sh && chmod +x /tmp/shift.sh");
+
+ } else if ($freqshift_tool == "sox") {
+ //linux.die.net/man/1/sox
+ $soxopt = "-q";
+ $soxpitch = $config['FREQSHIFT_PITCH'];
+ $cmd = "/usr/bin/nohup /usr/bin/sox \"".$pi.$filename."\" \"".$shifted_path.$filename."\" pitch ".$soxopt." ".$soxpitch;
+ shell_exec("mkdir -p ".$shifted_path.$dir." && echo \"".$cmd."\" > /tmp/shift.sh && chmod +x /tmp/shift.sh");
+ }
+
shell_exec("/tmp/shift.sh");
shell_exec("rm -f /tmp/shift.sh");
} else {
- $cmd = "rm -f " . $shifted_path.$dir.$filename;
- shell_exec("echo \"".$cmd."\" > /tmp/rmshift.sh && chmod +x /tmp/rmshift.sh");
- shell_exec("/tmp/rmshift.sh");
- shell_exec("rm -f /tmp/rmshift.sh");
+ $cmd = "rm -f " . $shifted_path.$filename;
+ shell_exec("echo \"".$cmd."\" > /tmp/unshift.sh && chmod +x /tmp/unshift.sh");
+ shell_exec("/tmp/unshift.sh");
+ shell_exec("rm -f /tmp/unshift.sh");
}
echo "OK";
@@ -265,7 +279,7 @@ function toggleShiftFreq(filename, shiftAction, elem) {
}
if(shiftAction == "shift") {
console.log("shifting freqs of " + filename);
- xhttp.open("GET", "play.php?shiftfile="+filename+"&shiftfreq=true", true);
+ xhttp.open("GET", "play.php?shiftfile="+filename+"&doshift=true", true);
} else {
console.log("unshifting freqs of " + filename);
xhttp.open("GET", "play.php?shiftfile="+filename, true);