Skip to content

Commit

Permalink
Fix for #196 and possibly #238: detect sample rate of generated PCM W…
Browse files Browse the repository at this point in the history
…av files prior to merging with sox
  • Loading branch information
mbi committed Dec 3, 2024
1 parent c3a27ff commit 0613733
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
17 changes: 15 additions & 2 deletions captcha/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit 0613733

Please sign in to comment.