Beispiel #1
0
    def read_frame_at_index(self, sample_index, framesize=None):
        """Read 'framesize' samples starting at 'sample_index'.
        If framesize is None, defaults to current framesize.

        Parameters
        ----------
        sample_index: int
            Index at which to center the frame.

        framesize: int, default=None
            Number of samples to read from the file.
        """
        if not framesize:
            framesize = self.framesize

        frame_index = 0
        frame = np.zeros([framesize, self.channels])

        # Check boundary conditions
        if sample_index < 0 and sample_index + framesize > 0:
            framesize = framesize - np.abs(sample_index)
            frame_index = np.abs(sample_index)
            sample_index = 0
        elif sample_index > self.num_samples:
            return frame
        elif (sample_index + framesize) <= 0:
            return frame

        logger.debug(util.classy_print(
            FramedAudioReader, "sample_index = %d" % sample_index))
        self._wave_handle.setpos(sample_index)
        newdata = util.byte_string_to_array(
            byte_string=self._wave_handle.readframes(int(framesize)),
            channels=self.channels,
            bytedepth=self.bytedepth)

        # Place new data within the frame
        frame[frame_index:frame_index + newdata.shape[0]] = newdata
        return frame
Beispiel #2
0
 def test_byte_string_to_array_stereo_bytedepthE4(self):
     np.testing.assert_array_equal(util.byte_string_to_array(
         self.stereo_str4, channels=2, bytedepth=4),
                                   self.stereo,
                                   verbose=True)
Beispiel #3
0
 def test_byte_string_to_array_mono_bytedepthE4(self):
     np.testing.assert_array_equal(util.byte_string_to_array(
         self.mono_str4, channels=1, bytedepth=4).flatten(),
                                   self.mono,
                                   verbose=True)