def __init__(self, filename, decode_file=False, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader( filename, decode_file=decode_file, fps=fps, nbytes=nbytes, buffersize=buffersize, ) self.fps = fps self.duration = self.reader.duration self.end = self.reader.duration self.buffersize = self.reader.buffersize self.filename = filename self.make_frame = lambda t: self.reader.get_frame(t) self.nchannels = self.reader.nchannels
def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename, fps=fps, nbytes=nbytes, bufsize=buffersize + 100) self.fps = fps self.duration = self.reader.duration self.end = self.duration self.nframes = self.reader.nframes self.buffersize = buffersize self.buffer = None self._fstart_buffer = 1 self._buffer_around(1) def gf(t): bufsize = self.buffersize if isinstance(t, np.ndarray): # lazy implementation, but should not cause problems in # 99.99 % of the cases result = np.zeros((len(t), 2)) in_time = (t >= 0) & (t < self.duration) inds = (self.fps * t + 1).astype(int)[in_time] f_tmin, f_tmax = inds.min(), inds.max() if not (0 <= (f_tmin - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmin) elif not (0 <= (f_tmax - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmax) try: tup = in_time.nonzero() inds2 = inds - self._fstart_buffer result[in_time] = self.buffer[inds - self._fstart_buffer] return result except IndexError as error: print("Error: wrong indices in video buffer. Maybe" + " buffer too small.") raise error else: ind = int(self.fps * t) if ind < 0 or ind > self.nframes: # out of time: return 0 return np.zeros(self.nchannels) if not (0 <= (ind - self._fstart_buffer) < len(self.buffer)): # out of the buffer: recenter the buffer self._buffer_around(ind) # read the frame in the buffer return self.buffer[ind - self._fstart_buffer] self.get_frame = gf
def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename reader = FFMPEG_AudioReader(filename, fps=fps, nbytes=nbytes, buffersize=buffersize) self.reader = reader self.fps = fps self.duration = reader.duration self.end = reader.duration self.make_frame = lambda t: reader.get_frame(t) self.nchannels = reader.nchannels
def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename reader = FFMPEG_AudioReader(filename,fps=fps,nbytes=nbytes, buffersize=buffersize) self.reader = reader self.fps = fps self.duracion = reader.duracion self.fin = reader.duracion self.make_frame = lambda t: reader.get_frame(t) self.nchannels = reader.nchannels
def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename,fps=fps,nbytes=nbytes, bufsize=buffersize+100) self.fps = fps self.duration = self.reader.duration self.end = self.duration self.nframes = self.reader.nframes self.buffersize= buffersize self.buffer= None self._fstart_buffer = 1 self._buffer_around(1) def gf(t): bufsize = self.buffersize if isinstance(t,np.ndarray): # lazy implementation, but should not cause problems in # 99.99 % of the cases result = np.zeros((len(t),2)) in_time = (t>=0) & (t < self.duration) inds = (self.fps*t+1).astype(int)[in_time] f_tmin, f_tmax = inds.min(), inds.max() if not (0 <= (f_tmin - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmin) elif not (0 <= (f_tmax - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmax) try: tup = in_time.nonzero() inds2 = inds - self._fstart_buffer result[in_time] = self.buffer[inds - self._fstart_buffer] return result except IndexError as error: print ("Error: wrong indices in video buffer. Maybe"+ " buffer too small.") raise error else: ind = int(self.fps*t) if ind<0 or ind> self.nframes: # out of time: return 0 return np.zeros(self.nchannels) if not (0 <= (ind - self._fstart_buffer) <len(self.buffer)): # out of the buffer: recenter the buffer self._buffer_around(ind) # read the frame in the buffer return self.buffer[ind - self._fstart_buffer] self.get_frame = gf
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. Parameters ------------ filename Either a soundfile name (of any extension supported by ffmpeg) as a string or a path-like object, or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. buffersize: Size to load in memory (in number of frames) Attributes ------------ nbytes Number of bits per frame of the original audio file. fps Number of frames per second in the audio file buffersize See Parameters. Lifetime -------- Note that this creates subprocesses and locks files. If you construct one of these instances, you must call close() afterwards, or the subresources will not be cleaned up until the process ends. If copies are made, and close() is called on one, it may cause methods on the other copies to fail. However, coreaders must be closed separately. Examples ---------- >>> snd = AudioFileClip("song.wav") >>> snd.close() >>> snd = AudioFileClip("song.mp3", fps = 44100) >>> second_reader = snd.coreader() >>> second_reader.close() >>> snd.close() >>> with AudioFileClip(mySoundArray, fps=44100) as snd: # from a numeric array >>> pass # Close is implicitly performed by context manager. """ @convert_path_to_string("filename") def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename, fps=fps, nbytes=nbytes, buffersize=buffersize) self.fps = fps self.duration = self.reader.duration self.end = self.reader.duration self.buffersize = self.reader.buffersize self.filename = filename self.make_frame = lambda t: self.reader.get_frame(t) self.nchannels = self.reader.nchannels def coreader(self): """ Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times. """ return AudioFileClip(self.filename, self.buffersize) def close(self): """ Close the internal reader. """ if self.reader: self.reader.close_proc() self.reader = None
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. Parameters ------------ snd Either a soundfile name (of any extension supported by ffmpeg) or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. buffersize: Size to load in memory (in number of frames) temp_wav: Name for the temporary wav file in case conversion is required. If not provided, the default will be filename.wav with some prefix. If the temp_wav already exists it will not be rewritten. Attributes ------------ nbytes Number of bits per frame of the original audio file. fps Number of frames per second in the audio file buffersize See Parameters. Examples ---------- >>> snd = SoundClip("song.wav") >>> snd = SoundClip("song.mp3", fps = 44100, bitrate=3000) >>> snd = SoundClip(mySoundArray,fps=44100) # from a numeric array """ def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename, fps=fps, nbytes=nbytes, buffersize=buffersize) self.fps = fps self.duration = self.reader.duration self.end = self.duration self.get_frame = lambda t: self.reader.get_frame(t) self.nchannels = self.reader.nchannels def coreader(self): """ Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times. """ return AudioFileClip(self.filename, self.buffersize)
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. Parameters ------------ snd Either a soundfile name (of any extension supported by ffmpeg) or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. buffersize: Size to load in memory (in number of frames) temp_wav: Name for the temporary wav file in case conversion is required. If not provided, the default will be filename.wav with some prefix. If the temp_wav already exists it will not be rewritten. Attributes ------------ nbytes Number of bits per frame of the original audio file. fps Number of frames per second in the audio file buffersize See Parameters. Examples ---------- >>> snd = SoundClip("song.wav") >>> snd = SoundClip("song.mp3", fps = 44100, bitrate=3000) >>> snd = SoundClip(mySoundArray,fps=44100) # from a numeric array """ def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename,fps=fps,nbytes=nbytes, buffersize=buffersize) self.fps = fps self.duration = self.reader.duration self.end = self.duration self.make_frame = lambda t: self.reader.get_frame(t) self.nchannels = self.reader.nchannels def coreader(self): """ Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times. """ return AudioFileClip(self.filename,self.buffersize)
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. Parameters ---------- filename Either a soundfile name (of any extension supported by ffmpeg) as a string or a path-like object, or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. buffersize: Size to load in memory (in number of frames) Attributes ---------- nbytes Number of bits per frame of the original audio file. fps Number of frames per second in the audio file buffersize See Parameters. Lifetime -------- Note that this creates subprocesses and locks files. If you construct one of these instances, you must call close() afterwards, or the subresources will not be cleaned up until the process ends. Examples -------- >>> snd = AudioFileClip("song.wav") >>> snd.close() """ @convert_path_to_string("filename") def __init__(self, filename, decode_file=False, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader( filename, decode_file=decode_file, fps=fps, nbytes=nbytes, buffersize=buffersize, ) self.fps = fps self.duration = self.reader.duration self.end = self.reader.duration self.buffersize = self.reader.buffersize self.filename = filename self.make_frame = lambda t: self.reader.get_frame(t) self.nchannels = self.reader.nchannels def close(self): """Close the internal reader.""" if self.reader: self.reader.close() self.reader = None
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. Parameters ------------ snd Either a soundfile name (of any extension supported by ffmpeg) or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. buffersize: Size to load in memory (in number of frames) temp_wav: Name for the temporary wav file in case conversion is required. If not provided, the default will be filename.wav with some prefix. If the temp_wav already exists it will not be rewritten. Attributes ------------ nbytes Number of bits per frame of the original audio file. fps Number of frames per second in the audio file buffersize See Parameters. Examples ---------- >>> snd = SoundClip("song.wav") >>> snd = SoundClip("song.mp3", fps = 44100, bitrate=3000) >>> snd = SoundClip(mySoundArray,fps=44100) # from a numeric array """ def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename, fps=fps, nbytes=nbytes, bufsize=buffersize + 100) self.fps = fps self.duration = self.reader.duration self.end = self.duration self.nframes = self.reader.nframes self.buffersize = buffersize self.buffer = None self._fstart_buffer = 1 self._buffer_around(1) def gf(t): bufsize = self.buffersize if isinstance(t, np.ndarray): # lazy implementation, but should not cause problems in # 99.99 % of the cases result = np.zeros((len(t), 2)) in_time = (t >= 0) & (t < self.duration) inds = (self.fps * t + 1).astype(int)[in_time] f_tmin, f_tmax = inds.min(), inds.max() if not (0 <= (f_tmin - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmin) elif not (0 <= (f_tmax - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmax) try: tup = in_time.nonzero() inds2 = inds - self._fstart_buffer result[in_time] = self.buffer[inds - self._fstart_buffer] return result except IndexError as error: print("Error: wrong indices in video buffer. Maybe" + " buffer too small.") raise error else: ind = int(self.fps * t) if ind < 0 or ind > self.nframes: # out of time: return 0 return np.zeros(self.nchannels) if not (0 <= (ind - self._fstart_buffer) < len(self.buffer)): # out of the buffer: recenter the buffer self._buffer_around(ind) # read the frame in the buffer return self.buffer[ind - self._fstart_buffer] self.get_frame = gf @property def nchannels(self): """ R eturns the number of channels of the reader (1: mono, 2: stereo) """ return self.reader.nchannels def _buffer_around(self, framenumber): """ Fills the buffer with frames, centered on ``framenumber`` if possible """ # start-frame for the buffer fbuffer = framenumber - self.buffersize // 2 fbuffer = max(0, fbuffer) if (self.buffer != None): current_f_end = self._fstart_buffer + self.buffersize - 1 if (fbuffer < current_f_end < fbuffer + self.buffersize): # We already have one bit of what must be read conserved = current_f_end - fbuffer + 1 chunksize = self.buffersize - conserved array = self.reader.read_chunk(chunksize) self.buffer = np.vstack([self.buffer[-conserved:], array]) else: self.reader.seek(fbuffer) self.buffer = self.reader.read_chunk(self.buffersize) else: self.reader.seek(fbuffer) self.buffer = self.reader.read_chunk(self.buffersize) self._fstart_buffer = fbuffer def coreader(self): """ Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times. """ return AudioFileClip(self.filename, self.buffersize)
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. Parameters ------------ snd Either a soundfile name (of any extension supported by ffmpeg) or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. buffersize: Size to load in memory (in number of frames) temp_wav: Name for the temporary wav file in case conversion is required. If not provided, the default will be filename.wav with some prefix. If the temp_wav already exists it will not be rewritten. Attributes ------------ nbytes Number of bits per frame of the original audio file. fps Number of frames per second in the audio file buffersize See Parameters. Lifetime -------- Note that this creates subprocesses and locks files. If you construct one of these instances, you must call close() afterwards, or the subresources will not be cleaned up until the process ends. If copies are made, and close() is called on one, it may cause methods on the other copies to fail. However, coreaders must be closed separately. Examples ---------- >>> snd = AudioFileClip("song.wav") >>> snd.close() >>> snd = AudioFileClip("song.mp3", fps = 44100, bitrate=3000) >>> second_reader = snd.coreader() >>> second_reader.close() >>> snd.close() >>> with AudioFileClip(mySoundArray,fps=44100) as snd: # from a numeric array >>> pass # Close is implicitly performed by context manager. """ def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): AudioClip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename,fps=fps,nbytes=nbytes, buffersize=buffersize) self.fps = fps self.duration = self.reader.duration self.end = self.reader.duration self.make_frame = lambda t: self.reader.get_frame(t) self.nchannels = self.reader.nchannels def coreader(self): """ Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times. """ return AudioFileClip(self.filename, self.buffersize) def close(self): """ Close the internal reader. """ if self.reader: self.reader.close_proc() self.reader = None
class AudioFileClip(AudioClip): """ An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward. :param snd: Either a soundfile or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the ``fps`` and ``bitrate`` arguments. :param buffersize: Size to load in memory (in number of frames) :param temp_wav: name for the temporary wav file in case conversion is required. If not provided, the default will be filename.wav with some prefix. If the temp_wav already exists it will not be rewritten. :ivar nbytes: Number of bits per frame of the original audio file (the more the better). :ivar fps, buffersize: see above. >>> snd = SoundClip("song.wav") >>> snd = SoundClip("song.mp3", fps = 44100, bitrate=3000) >>> snd = SoundClip(mySoundArray,fps=44100) # from a numeric array """ def __init__(self, filename, buffersize=200000, nbytes=2, fps=44100): Clip.__init__(self) self.filename = filename self.reader = FFMPEG_AudioReader(filename,fps=fps,nbytes=nbytes) self.fps = fps self.duration = self.reader.duration self.nframes = self.reader.nframes self.buffersize= buffersize self.buffer=None self._fstart_buffer = 1 self._buffer_around(1) self.nchannels = self.reader.nchannels def gf(t): bufsize = self.buffersize if isinstance(t,np.ndarray): # lazy implementation, but should not cause problems in # 99.99 % of the cases result = np.zeros((len(t),2)) in_time = (t>=0) & (t < self.duration) inds = (self.fps*t+1).astype(int)[in_time] f_tmin, f_tmax = inds.min(), inds.max() if not (0 <= (f_tmin - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmin) elif not (0 <= (f_tmax - self._fstart_buffer) < len(self.buffer)): self._buffer_around(f_tmax) try: result[in_time] = self.buffer[inds - self._fstart_buffer] return result except: print ("Error: wrong indices in video buffer. Maybe"+ " buffer too small.") raise else: ind = int(self.fps*t)+1 if ind<1 or ind> self.nframes: # out of time: return 0 return np.zeros(self.nchannels) if not (0 <= (ind - self._fstart_buffer) <len(self.buffer)): # out of the buffer: recenter the buffer self._buffer_around(ind) # read the frame in the buffer return self.buffer[ind - self._fstart_buffer] self.get_frame = gf def _buffer_around(self,framenumber): """ fill the buffer with frames, centered on ``framenumber`` if possible """ # start frame for the buffer fbuffer = framenumber - self.buffersize/2 fbuffer = max(1, fbuffer) if (self.buffer!=None): current_f_end =self._fstart_buffer + self.buffersize-1 if (fbuffer < current_f_end < fbuffer+ self.buffersize): # We already have one bit of what must be read conserved = current_f_end - fbuffer+1 chunksize = self.buffersize-conserved array = self.reader.read_chunk(chunksize) self.buffer = np.vstack([self.buffer[-conserved:], array]) else: self.reader.seek(fbuffer) self.buffer = self.reader.read_chunk(self.buffersize) else: self.reader.seek(fbuffer) self.buffer = self.reader.read_chunk(self.buffersize) self._fstart_buffer = fbuffer def coreader(self): """ Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times. """ return AudioFileClip(self.filename,self.buffersize)