From 197a42b962feaac0f9dd0128eda715506c97131d Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 4 Jun 2024 18:12:28 +0200 Subject: [PATCH] Added audio processing when available --- sbapp/main.py | 22 ++++++++++++++++--- sbapp/plyer/platforms/android/audio.py | 2 +- sbapp/sideband/audioproc.py | 29 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/sbapp/main.py b/sbapp/main.py index 31e3b0b..1f947e8 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -1683,11 +1683,27 @@ class SidebandApp(MDApp): try: if self.audio_msg_mode == LXMF.AM_OPUS_OGG: - self.attach_path = self.msg_audio._file_path - RNS.log("Using unmodified OPUS data in OGG container", RNS.LOG_DEBUG) + from sideband.audioproc import voice_processing + proc_path = voice_processing(self.msg_audio._file_path) + if proc_path: + self.attach_path = proc_path + os.unlink(self.msg_audio._file_path) + RNS.log("Using voice-processed OPUS data in OGG container", RNS.LOG_DEBUG) + else: + self.attach_path = self.msg_audio._file_path + RNS.log("Using unmodified OPUS data in OGG container", RNS.LOG_DEBUG) else: ap_start = time.time() - opus_file = pyogg.OpusFile(self.msg_audio._file_path) + from sideband.audioproc import voice_processing + proc_path = voice_processing(self.msg_audio._file_path) + + if proc_path: + opus_file = pyogg.OpusFile(proc_path) + RNS.log("Using voice-processed audio for codec2 encoding", RNS.LOG_DEBUG) + else: + opus_file = pyogg.OpusFile(self.msg_audio._file_path) + RNS.log("Using unprocessed audio data for codec2 encoding", RNS.LOG_DEBUG) + audio = AudioSegment( bytes(opus_file.as_array()), frame_rate=opus_file.frequency, diff --git a/sbapp/plyer/platforms/android/audio.py b/sbapp/plyer/platforms/android/audio.py index 3d40dc6..a72d1e3 100644 --- a/sbapp/plyer/platforms/android/audio.py +++ b/sbapp/plyer/platforms/android/audio.py @@ -56,7 +56,7 @@ class AndroidAudio(Audio): else: self._recorder.setAudioSource(AudioSource.DEFAULT) self._recorder.setAudioSamplingRate(48000) - self._recorder.setAudioEncodingBitRate(16000) + self._recorder.setAudioEncodingBitRate(12000) self._recorder.setAudioChannels(1) self._recorder.setOutputFormat(OutputFormat.OGG) self._recorder.setAudioEncoder(AudioEncoder.OPUS) diff --git a/sbapp/sideband/audioproc.py b/sbapp/sideband/audioproc.py index a34180b..fcca182 100644 --- a/sbapp/sideband/audioproc.py +++ b/sbapp/sideband/audioproc.py @@ -1,5 +1,6 @@ import os import io +import sh import math import time import struct @@ -99,6 +100,34 @@ def samples_to_wav(samples=None, file_path=None): wf.writeframes(samples) return True +def voice_processing(input_path): + try: + ffmpeg = None + ffmpeg = sh.ffmpeg + if ffmpeg: + filters = "highpass=f=250, lowpass=f=3000,speechnorm=e=12.5:r=0.0001:l=1" + output_bitrate = "12k" + opus_apptype = "audio" + output_path = input_path.replace(".ogg","")+".p.ogg" + args = [ + "-i", input_path, "-filter:a", filters, + "-c:a", "libopus", "-application", opus_apptype, + "-vbr", "on","-b:a", output_bitrate, output_path] + try: + try: + os.unlink(output_path) + except: + pass + ffmpeg(*args) + return output_path + except Exception as e: + RNS.log("Could not process audio with ffmpeg", RNS.LOG_ERROR) + RNS.trace_exception(e) + return None + + except Exception as e: + return None + def detect_codec2(): try: import pycodec2