Ejemplo n.º 1
0
    def __init__(self, filename, dtype=None):
        """AudioIODataset."""
        with tf.name_scope("AudioIODataset"):
            if not tf.executing_eagerly():
                assert dtype is not None, "dtype must be provided in graph mode"
            resource = core_ops.io_audio_readable_init(filename)
            if tf.executing_eagerly():
                shape, dtype, _ = core_ops.io_audio_readable_spec(resource)
                dtype = tf.as_dtype(dtype.numpy())
            else:
                shape, _, _ = core_ops.io_audio_readable_spec(resource)

            capacity = 1024  # kwargs.get("capacity", 4096)

            self._resource = resource
            dataset = tf.data.Dataset.range(0, shape[0], capacity)
            dataset = dataset.map(
                lambda index: core_ops.io_audio_readable_read(
                    resource, index, index + capacity, dtype=dtype
                )
            )
            dataset = dataset.apply(
                tf.data.experimental.take_while(lambda v: tf.greater(tf.shape(v)[0], 0))
            )
            dataset = dataset.unbatch()
            self._dataset = dataset
            super().__init__(
                self._dataset._variant_tensor
            )  # pylint: disable=protected-access
Ejemplo n.º 2
0
 def __init__(self, filename, dtype=None):
     with tf.name_scope("AudioIOTensor"):
         if not tf.executing_eagerly():
             assert dtype is not None, "dtype must be provided in graph mode"
         resource = core_ops.io_audio_readable_init(filename)
         if tf.executing_eagerly():
             shape, dtype, rate = core_ops.io_audio_readable_spec(resource)
             dtype = tf.as_dtype(dtype.numpy())
         else:
             shape, _, rate = core_ops.io_audio_readable_spec(resource)
         self._resource = resource
         self._shape = shape
         self._dtype = dtype
         self._rate = rate
         super().__init__()