From 04dae073f6f2bfed58389bb7d697a38010ffa42c Mon Sep 17 00:00:00 2001 From: Louis Croisez Date: Tue, 16 May 2023 10:46:43 +0200 Subject: [PATCH] Added option in Spectrogram view to activate a frequency shift in the audio livestream --- scripts/advanced.php | 19 ++++++++- scripts/livestream.sh | 6 +++ scripts/spectrogram.php | 62 +++++++++++++++++++++++++++++- scripts/update_birdnet_snippets.sh | 3 ++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/scripts/advanced.php b/scripts/advanced.php index ae46ac3..b91247b 100644 --- a/scripts/advanced.php +++ b/scripts/advanced.php @@ -94,6 +94,23 @@ if(isset($_GET['submit'])) { exec("sudo systemctl restart livestream.service"); } } + + if (isset($_GET["activate_freqshift_in_livestream"])) { + $activate_freqshift_in_livestream = trim($_GET["activate_freqshift_in_livestream"]); + + //Setting exists already, see if the value changed + if (strcmp($activate_freqshift_in_livestream, $config['ACTIVATE_FREQSHIFT_IN_LIVESTREAM']) !== 0) { + $contents = preg_replace("/ACTIVATE_FREQSHIFT_IN_LIVESTREAM=.*/", "ACTIVATE_FREQSHIFT_IN_LIVESTREAM=\"$activate_freqshift_in_livestream\"", $contents); + $contents2 = preg_replace("/ACTIVATE_FREQSHIFT_IN_LIVESTREAM=.*/", "ACTIVATE_FREQSHIFT_IN_LIVESTREAM=\"$activate_freqshift_in_livestream\"", $contents2); + $fh = fopen("/etc/birdnet/birdnet.conf", "w"); + $fh2 = fopen("./scripts/thisrun.txt", "w"); + fwrite($fh, $contents); + fwrite($fh2, $contents2); + sleep(1); + exec("sudo systemctl restart livestream.service"); + } + } + if(isset($_GET["overlap"])) { $overlap = $_GET["overlap"]; @@ -569,7 +586,7 @@ foreach($formats as $format){

Accessibility Settings

Birdsongs Frequency shifting configuration:
- This can be useful for hearing impaired people. Note: audio files will only be pitch shifted when the "FREQ SHIFT" button is manually clicked for a detection on the "Recordings" page.
+ This can be useful for hearing impaired people.
Note: audio files will only be pitch shifted when the "FREQ SHIFT" button is manually clicked for a detection on the "Recordings" page.
The frequency shifting can also be activated for the realtime audio livestream, accessible in the SPECTROGRAM tab of BirdNET-Pi. Once it has been activated, it will be made available for the Live Audio feature as well.
Livestream is using ffmpeg for streaming its audio data, so the pitch shifter in that case will use this tool too. If you choose sox as the tool for freq shifting recorded audio files, then you must configure both sox and ffmpeg parameters: sox for recordings, and ffmpeg for livestream.

diff --git a/scripts/livestream.sh b/scripts/livestream.sh index b402bbc..69fe21a 100755 --- a/scripts/livestream.sh +++ b/scripts/livestream.sh @@ -12,6 +12,10 @@ if [ "$LOGGING_LEVEL" == "info" ] || [ "$LOGGING_LEVEL" == "debug" ];then set -x fi +if [ "$ACTIVATE_FREQSHIFT_IN_LIVESTREAM" == "true" ]; then + FREQSHIFT_OPT='-af rubberband=pitch='${FREQSHIFT_LO}'/'${FREQSHIFT_HI} +fi + if [ -z ${REC_CARD} ];then echo "Stream not supported" elif [[ ! -z ${RTSP_STREAM} ]];then @@ -34,9 +38,11 @@ elif [[ ! -z ${RTSP_STREAM} ]];then ffmpeg -nostdin -loglevel $LOGGING_LEVEL -ac ${CHANNELS} -i ${SELECTED_RSTP_STREAM} -acodec libmp3lame \ -b:a 320k -ac ${CHANNELS} -content_type 'audio/mpeg' \ + ${FREQSHIFT_OPT} \ -f mp3 icecast://source:${ICE_PWD}@localhost:8000/stream -re else ffmpeg -nostdin -loglevel $LOGGING_LEVEL -ac ${CHANNELS} -f alsa -i ${REC_CARD} -acodec libmp3lame \ -b:a 320k -ac ${CHANNELS} -content_type 'audio/mpeg' \ + ${FREQSHIFT_OPT} \ -f mp3 icecast://source:${ICE_PWD}@localhost:8000/stream -re fi diff --git a/scripts/spectrogram.php b/scripts/spectrogram.php index 9bbbbcc..96b118e 100644 --- a/scripts/spectrogram.php +++ b/scripts/spectrogram.php @@ -316,6 +316,46 @@ function toggleCompression(state) { } } +function toggleFreqshift(state) { + if (state == true) { + console.log("freqshift activated") + } else { + console.log("freqshift deactivated") + } + + var livestream_freqshift_spinner = document.getElementById('livestream_freqshift_spinner'); + livestream_freqshift_spinner.style.display = "inline"; + // Create the XMLHttpRequest object. + const xhr = new XMLHttpRequest(); + // Initialize the request + xhr.open("GET", './views.php?activate_freqshift_in_livestream=' + state + '&view=Advanced&submit=advanced'); + // Send the request + xhr.send(); + // Fired once the request completes successfully + xhr.onload = function (e) { + // Check if the request was a success + if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { + // Restart the audio player in case it stopped working while the livestream service was restarted + var audio_player = document.querySelector('audio#player'); + if (audio_player !== 'undefined') { + //central_controls_element.appendChild(h1_loading); + //Wait 2 seconds before restarting the stream + setTimeout(function () { + console.log("Restarting connection with livestream"); + audio_player.pause(); + audio_player.setAttribute('src', '/stream'); + audio_player.load(); + audio_player.play(); + + livestream_freqshift_spinner.style.display = "none"; + }, + 2000 + ) + } + } + } +} + function initialize() { document.body.querySelector('h1').remove(); const CVS = document.body.querySelector('canvas'); @@ -354,6 +394,7 @@ function initialize() { gainNode.connect(ACTX.destination); document.getElementById("compression").removeAttribute("disabled"); + document.getElementById("freqshift").removeAttribute("disabled"); console.log(SOURCE); const DATA = new Uint8Array(ANALYSER.frequencyBinCount); @@ -470,9 +511,21 @@ h1 { —

- +
+
+ + + disabled> + +
@@ -542,4 +595,9 @@ var compression = document.getElementById("compression"); compression.onclick = function() { toggleCompression(this.checked); } - \ No newline at end of file + +var freqshift = document.getElementById("freqshift"); +freqshift.onclick = function() { + toggleFreqshift(this.checked); +} + diff --git a/scripts/update_birdnet_snippets.sh b/scripts/update_birdnet_snippets.sh index 1e88eb9..4a09482 100755 --- a/scripts/update_birdnet_snippets.sh +++ b/scripts/update_birdnet_snippets.sh @@ -148,6 +148,9 @@ fi if ! grep FREQSHIFT_PITCH /etc/birdnet/birdnet.conf &>/dev/null;then sudo -u$USER echo "FREQSHIFT_PITCH=-1500" >> /etc/birdnet/birdnet.conf fi +if ! grep FREQSHIFT_PITCH /etc/birdnet/birdnet.conf &>/dev/null;then + sudo -u$USER echo "ACTIVATE_FREQSHIFT_IN_LIVESTREAM=\"false\"" >> /etc/birdnet/birdnet.conf +fi if ! grep HEARTBEAT_URL /etc/birdnet/birdnet.conf &>/dev/null;then sudo -u$USER echo "HEARTBEAT_URL=" >> /etc/birdnet/birdnet.conf fi