Skip to content

Commit

Permalink
Don't access open tempfiles. This fixes audio file downloading in Win…
Browse files Browse the repository at this point in the history
…dows systems.

PiperOrigin-RevId: 661399734
  • Loading branch information
sdenton4 authored and copybara-github committed Aug 9, 2024
1 parent 1143cff commit 239f0ea
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions chirp/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,21 @@ def load_audio_file(
# Handle other audio formats.
# Because librosa passes file handles to soundfile, we need to copy the file
# to a temporary file before passing it to librosa.
with tempfile.NamedTemporaryFile(mode='w+b', suffix=extension) as f:
with tempfile.NamedTemporaryFile(
mode='w+b', suffix=extension, delete=False
) as f:
with filepath.open('rb') as sf:
f.write(sf.read())
# librosa outputs lots of warnings which we can safely ignore when
# processing all Xeno-Canto files and PySoundFile is unavailable.
with warnings.catch_warnings():
warnings.simplefilter('ignore')
audio, _ = librosa.load(
f.name,
sr=target_sample_rate,
res_type=resampling_type,
)
# librosa outputs lots of warnings which we can safely ignore when
# processing all Xeno-Canto files and PySoundFile is unavailable.
with warnings.catch_warnings():
warnings.simplefilter('ignore')
audio, _ = librosa.load(
f.name,
sr=target_sample_rate,
res_type=resampling_type,
)
os.unlink(f.name)
return audio


Expand Down Expand Up @@ -252,20 +255,22 @@ def load_xc_audio(xc_id: str, sample_rate: int) -> jnp.ndarray:
raise requests.exceptions.RequestException(
f'Failed to load audio from Xeno-Canto {xc_id}'
) from e
with tempfile.NamedTemporaryFile(suffix='.mp3', mode='wb') as f:
with tempfile.NamedTemporaryFile(suffix='.mp3', mode='wb', delete=False) as f:
f.write(data)
f.flush()
audio = load_audio_file(f.name, target_sample_rate=sample_rate)
audio = load_audio_file(f.name, target_sample_rate=sample_rate)
os.unlink(f.name)
return audio


def load_url_audio(url: str, sample_rate: int) -> jnp.ndarray:
"""Load audio from a URL."""
data = requests.get(url).content
with tempfile.NamedTemporaryFile(mode='wb') as f:
with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f:
f.write(data)
f.flush()
audio = load_audio_file(f.name, target_sample_rate=sample_rate)
audio = load_audio_file(f.name, target_sample_rate=sample_rate)
os.unlink(f.name)
return audio


Expand Down

0 comments on commit 239f0ea

Please sign in to comment.