Skip to content

Commit

Permalink
Merge pull request #634 from AI4Bharat/vo_fix
Browse files Browse the repository at this point in the history
Audio overlap Issue
  • Loading branch information
Shruti1229 authored Jan 12, 2024
2 parents aec8c84 + 25c12d7 commit ce617bc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
11 changes: 6 additions & 5 deletions backend/voiceover/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,19 +721,20 @@ def adjust_voiceover(translation_payload):
for index, (translation_text, audio, duration) in enumerate(translation_payload):
if type(audio) == dict and "audioContent" in audio.keys():
if len(audio["audioContent"]) > 400:
audio_file = "temp.wav"
uuid_num = str(uuid.uuid4())
audio_file = "temp_" + uuid_num +".wav"
first_audio_decoded = base64.b64decode(audio["audioContent"])
with open(audio_file, "wb") as output_f:
output_f.write(first_audio_decoded)
try:
AudioSegment.from_file("temp.wav").export("temp.ogg", format="ogg")
AudioSegment.from_file(audio_file).export("temp_" + uuid_num +".ogg", format="ogg")
except:
audio_file = "temp.ogg"
audio_file = "temp_" + uuid_num +".ogg"
first_audio_decoded = base64.b64decode(audio["audioContent"])
with open(audio_file, "wb") as output_f:
output_f.write(first_audio_decoded)
adjust_audio("temp.ogg", translation_payload[index][2], -1)
encoded_audio = base64.b64encode(open("temp.ogg", "rb").read())
encoded_audio = base64.b64encode(open("temp_" + uuid_num +".ogg", "rb").read())
output[index] = (
translation_payload[index][0],
{"audioContent": encoded_audio.decode()},
Expand Down Expand Up @@ -798,7 +799,7 @@ def generate_voiceover_payload(translation_payload, target_language, task):
{"audioContent": encoded_audio.decode()},
)
os.remove(audio_file)
os.remove("temp_" + uuid_num +".ogg")
os.remove("temp_" + uuid_num +".ogg")
else:
output[ind] = (
translation_payload[ind][0],
Expand Down
65 changes: 65 additions & 0 deletions backend/voiceover/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,71 @@ def get_empty_audios(request):
status=status.HTTP_400_BAD_REQUEST,
)

@swagger_auto_schema(
method="get",
manual_parameters=[
openapi.Parameter(
"task_id",
openapi.IN_QUERY,
description=("An integer to pass the task id"),
type=openapi.TYPE_INTEGER,
required=True,
),
openapi.Parameter(
"offset",
openapi.IN_QUERY,
description=("Offset number"),
type=openapi.TYPE_INTEGER,
required=False,
),
],
responses={200: "Returns the Translated Audio."},
)
@api_view(["GET"])
def update_completed_count(request):
try:
task_id = request.query_params["task_id"]
except KeyError:
return Response(
{"message": "Missing required parameters - task_id or offset"},
status=status.HTTP_400_BAD_REQUEST,
)

try:
task = Task.objects.get(pk=task_id)
except Task.DoesNotExist:
return Response(
{"message": "Task doesn't exist."},
status=status.HTTP_404_NOT_FOUND,
)

voice_over = get_voice_over_id(task)
completed_count = 0

if voice_over is not None:
voice_over_id = voice_over.id
try:
voice_over = VoiceOver.objects.get(pk=voice_over_id)
except VoiceOver.DoesNotExist:
return Response(
{"message": "VoiceOver doesn't exist."},
status=status.HTTP_400_BAD_REQUEST,
)

audios_list = len(voice_over.payload["payload"])
empty_audio_list = []
for i in range(len(voice_over.payload["payload"])):
if "audio" in voice_over.payload["payload"][i].keys() and voice_over.payload["payload"][i]["audio"] == "":
empty_audio_list.append(i)
else:
completed_count += 1
voice_over.payload["payload"]["completed_count"] = completed_count
voice_over.save()
count = request.query_params["offset"]
if count != None and int(count) > 0:
voice_over.payload["payload"]["completed_count"] = int(count)
return Reponse({"message": "Count updated."}, status=status.HTTP_200_OK)


@swagger_auto_schema(
method="get",
Expand Down

0 comments on commit ce617bc

Please sign in to comment.