Added audio processing when available

This commit is contained in:
Mark Qvist 2024-06-04 18:12:28 +02:00
parent 0143da127c
commit 197a42b962
3 changed files with 49 additions and 4 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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