Skip to content

Commit

Permalink
Don't access open tempfiles, which fails on Windows systems.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 661399734
  • Loading branch information
sdenton4 authored and copybara-github committed Aug 12, 2024
1 parent 1143cff commit f16efe5
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 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 Expand Up @@ -631,7 +636,7 @@ def slice_peaked_audio(

# Ensure that the peak locations are such that
# `audio[peak - left_shift: peak + right_shift]` is a non-truncated slice.
peaks = jnp.clip(peaks, left_shift, audio.shape[0] - right_shift)
peaks = jnp.clip(peaks, left_shift, jnp.array(audio).shape[0] - right_shift)
# As a result, it's possible that some (start, stop) pairs become identical;
# eliminate duplicates.
start_stop = jnp.unique(
Expand Down

0 comments on commit f16efe5

Please sign in to comment.