Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more graceful support for empty soundfiles. #3

Merged
merged 1 commit into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions claudio/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import numpy as np
import os
import warnings
import wave

import claudio.formats as formats
Expand Down Expand Up @@ -57,6 +58,8 @@ def __init__(self, filepath, samplerate=None, channels=None,
logging.debug(util.classy_print(AudioFile, "Opening wave file."))
self.__get_handle__(self.filepath, samplerate, channels, bytedepth)
logging.debug(util.classy_print(AudioFile, "Success!"))
if self.duration == 0:
warnings.warn("Caution: You have opened an empty sound file!")

def __get_handle__(self, filepath, samplerate, channels, bytedepth):
"""Get hooks into a wave object for reading or writing.
Expand Down Expand Up @@ -122,7 +125,7 @@ def reset(self):
-------
None
"""
self._EOF = False
pass

def close(self):
"""Explicit destructor."""
Expand Down Expand Up @@ -296,7 +299,7 @@ def __init__(self, filepath, framesize,
self._framesize = framesize
self._alignment = alignment
self._offset = offset
self._time_points = None
self._time_points = [None]
logging.debug(util.classy_print(FramedAudioFile, "Init Striding."))
self._init_striding(time_points, framerate, stride, overlap)
self.reset()
Expand Down Expand Up @@ -505,17 +508,22 @@ def num_frames(self):
else:
return len(self._time_points)

@property
def end_of_file(self):
return self._time_index >= len(self._time_points)

def _next_time_point(self):
"""Compute the next LEFT-ALIGNED time point given the current
state of parameters.

Takes into account the three parameters of absolute index, alignment,
and offset.
"""
if self._time_points is None:
raise ValueError("Audio file has no time grid; is it empty?")

time_point = self._time_points[self._time_index]
self._time_index += 1
if self._time_index == len(self._time_points):
self._EOF = True

if self.alignment == 'center':
time_point -= 0.5 * self.framesize / self.samplerate
Expand Down Expand Up @@ -602,7 +610,7 @@ def read_frame_at_time(self, time_point, framesize=None):

def next(self):
# For python 2.
if not self._EOF:
if not self.end_of_file:
return self.read_frame_at_time(self._next_time_point())
else:
self.reset()
Expand Down
Binary file added claudio/tests/empty.wav
Binary file not shown.
7 changes: 7 additions & 0 deletions claudio/tests/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,12 @@ def test_write_wave(self):
assert fs1 == fs2


def test_read_empty_wav():
sfile = os.path.join(os.path.dirname(__file__), 'empty.wav')
signal, samplerate = fileio.read(sfile)
assert len(signal) == 0
assert samplerate


if __name__ == "__main__":
unittest.main()