diff --git a/dejavu/decode.py b/dejavu/decode.py index 0304b08..f578c0e 100644 --- a/dejavu/decode.py +++ b/dejavu/decode.py @@ -15,9 +15,22 @@ def find_files(path, extensions): yield (p, extension) -def read(filename): +def read(filename, limit=None): + """ + Reads any file supported by pydub (ffmpeg) and returns the data contained + within. + + Can be optionally limited to a certain amount of seconds from the start + of the file by specifying the `limit` parameter. This is the amount of + seconds from the start of the file. + + returns: (samplerate, channels) + """ audiofile = AudioSegment.from_file(filename) + if limit: + audiofile = audiofile[:limit * 1000] + data = np.fromstring(audiofile._data, np.int16) channels = []