mirror of
https://github.com/FAUSheppy/ths-speech
synced 2025-12-06 11:11:36 +01:00
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import base64
|
|
import os.path
|
|
import audiosegment_wrapper as AudioSegment
|
|
from configparse_wrapper.cpwrap import CFG
|
|
|
|
FTP_DIR = CFG("shareDir")
|
|
|
|
def saveAudio(filename, data):
|
|
'''Save a single Audiofile to disk'''
|
|
|
|
if os.path.isfile(filename):
|
|
raise ValueError("File: {} already exists.".format(filename))
|
|
|
|
with open(filename,"wb") as f:
|
|
f.write(data)
|
|
|
|
if False:
|
|
seg = AudioSegment.from_file(orig_filename)
|
|
seg = seg.resample(sample_rate_Hz=32000, sample_width=2, channels=1)
|
|
seg.export("{}.wav".format(filename), format="wav")
|
|
|
|
def saveAudioChain(filename, dataArray):
|
|
'''Combine multiple received audio files into one and save it'''
|
|
|
|
completeAudio = None
|
|
for index, data in enumerate(dataArray):
|
|
tmpFile = "{}.{}.tmp".format(filename, index)
|
|
with open(filename, "wb") as f:
|
|
f.write(data)
|
|
if not completeAudio:
|
|
completeAudio = AudioSegment.from_file(tmpFile)
|
|
else:
|
|
completeAudio += AudioSegment.from_file(tmpFile)
|
|
|
|
completeAudio = completeAudio.resample(sample_rate_Hz=32000, sample_width=2, channels=1)
|
|
completeAudio.export("{}.wav".format(filename), format="wav")
|
|
|
|
def saveTranscript(filename, transcript):
|
|
with open("{}_transcript.txt".format(filename), "w") as f:
|
|
f.write(transcript)
|
|
|
|
def get_transcript(filename):
|
|
with open("{}_transcript.txt".format(filename), "r") as f:
|
|
return f.read()
|