endpoint: /api/resample
- Define new endpoint /api/resample, which takes param : file_uuid, and sr. - Add Exception Handling for celery tasks - Correct error message for trim_audio_task
This commit is contained in:
@@ -3,6 +3,7 @@ from freqsplit.input.file_reader import read_audio
|
||||
from freqsplit.preprocessing.classify import classify_audio
|
||||
from freqsplit.preprocessing.normalize import normalize_audio
|
||||
from freqsplit.preprocessing.trim import trim_audio
|
||||
from freqsplit.preprocessing.resample import resample
|
||||
from freqsplit.postprocessing.audio_writer import export_audio
|
||||
|
||||
@shared_task
|
||||
@@ -28,6 +29,7 @@ def normalize_audio_task(file_path):
|
||||
export_audio(normalized_audio, file_path, sr) # Save file
|
||||
return True
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"RuntimeError: {e}")
|
||||
return False
|
||||
|
||||
@shared_task
|
||||
@@ -39,4 +41,17 @@ def trim_audio_task(file_path):
|
||||
export_audio(trimmed_audio, file_path, sr)
|
||||
return True
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"RuntimeError: {e}")
|
||||
return False
|
||||
|
||||
@shared_task
|
||||
def resample_audio_task(file_path, sr):
|
||||
"""Celery task to resample the audio asynchronously"""
|
||||
try:
|
||||
audio, org_sr = read_audio(file_path)
|
||||
resampled_audio, sr = resample(audio, org_sr, eval(sr))
|
||||
export_audio(resampled_audio, file_path, sr)
|
||||
return True
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"RuntimeError: {e}")
|
||||
return False
|
||||
+22
-1
@@ -7,6 +7,7 @@ from .utils import get_audio_file_path
|
||||
from .tasks import save_and_classify
|
||||
from .tasks import normalize_audio_task
|
||||
from .tasks import trim_audio_task
|
||||
from .tasks import resample_audio_task
|
||||
from freqsplit.input.format_checker import is_supported_format
|
||||
|
||||
UPLOAD_DIR = "/tmp/freqsplit"
|
||||
@@ -82,4 +83,24 @@ def trim_audio(request):
|
||||
if task.get():
|
||||
return Response({"message": "Audio trimmed successfully"}, status=status.HTTP_200_OK)
|
||||
else:
|
||||
return Response({"error": "Failed to normalize audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
return Response({"error": "Failed to trim audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
# Endpoint to resample audio
|
||||
@api_view(['POST'])
|
||||
def resample_audio(request):
|
||||
"""Handles the resampling of audio"""
|
||||
stat, result, status_code = get_audio_file_path(request, UPLOAD_DIR)
|
||||
if stat == False:
|
||||
return Response({"error": result}, status=status_code)
|
||||
|
||||
sr = request.data.get("sr")
|
||||
if not sr:
|
||||
return Response({"error": "Missing sr"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Call Celery task synchronously
|
||||
task = resample_audio_task.apply(args=(result, sr))
|
||||
|
||||
if task.get():
|
||||
return Response({"message": f"Audio resampled to {sr} successfully"}, status=status.HTTP_200_OK)
|
||||
else:
|
||||
return Response({"error": "Failed to resample audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
+3
-1
@@ -19,10 +19,12 @@ from django.urls import path
|
||||
from api.views import upload_audio
|
||||
from api.views import normalize_audio
|
||||
from api.views import trim_audio
|
||||
from api.views import resample_audio
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/upload', upload_audio, name='upload_audio'),
|
||||
path('api/normalize', normalize_audio, name="normalize_audio"),
|
||||
path('api/trim', trim_audio, name='trim_audio')
|
||||
path('api/trim', trim_audio, name='trim_audio'),
|
||||
path('api/resample', resample_audio, name='resample_audio')
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user