diff --git a/CHANGES b/CHANGES index 166291d..db52637 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,7 @@ Version 0.6.1 (unreleased) * Test against Django 5.1 * Add ability to control color of each character (PR #235, thanks @mheidarian) * Add support for Django REST Framework (PR #236, thanks @michalwrona01) +* Audio CAPTCHA: try to detect the sample rate of the generated WAV file prior to merge (Issue #196) Version 0.6.0 ------------- diff --git a/captcha/views.py b/captcha/views.py index 1d2f364..afb27eb 100644 --- a/captcha/views.py +++ b/captcha/views.py @@ -166,15 +166,27 @@ def captcha_audio(request, key): # Add arbitrary noise if sox is installed if settings.CAPTCHA_SOX_PATH: + try: + sample_rate = ( + subprocess.run( + [settings.CAPTCHA_SOX_PATH, "--i", "-r", path], + capture_output=True, + ) + .stdout.decode() + .strip() + ) + + except Exception: + sample_rate = "8000" + arbnoisepath = str( os.path.join(tempfile.gettempdir(), "%s_arbitrary.wav") % key ) - mergedpath = str(os.path.join(tempfile.gettempdir(), "%s_merged.wav") % key) subprocess.call( [ settings.CAPTCHA_SOX_PATH, "-r", - "8000", + sample_rate, "-n", arbnoisepath, "synth", @@ -184,6 +196,7 @@ def captcha_audio(request, key): "-15", ] ) + mergedpath = str(os.path.join(tempfile.gettempdir(), "%s_merged.wav") % key) subprocess.call( [ settings.CAPTCHA_SOX_PATH,