Exemple #1
0
 def test_formats(self):
     self.assertEqual(sox.is_valid_file_format("some.wav"),
                      True,
                      "Wav check failed.")
     self.assertEqual(sox.is_valid_file_format("booger"),
                      False,
                      "Extension-less format failed.")
     self.assertEqual(sox.is_valid_file_format("curious.george"),
                      False,
                      "Invalid extension failed.")
Exemple #2
0
    def __init__(self, filepath, samplerate=None, channels=None,
                 bytedepth=None, mode="r"):
        """Base class for interfacing with audio files.

        When writing audio files, samplerate, channels, and bytedepth must be
        specified. Otherwise, these parameters may be None when reading to use
        the default values of the audio file.

        Parameters
        ----------
        filepath : str
            Absolute path to a sound file. Does not need to exist (yet).

        samplerate : float, default=None
            Samplerate for the audio file.

        channels : int, default=None
            Number of channels for the audio file.

        bytedepth : int, default=None
            bytedepth (in bytes) of the returned file. For example, CD-quality
            audio has a bytedepth of 2 (16-bit).

        mode : str, default='r'
            Open the file for [r]eading or [w]riting.
        """
        logging.debug(util.classy_print(AudioFile, "Constructor."))
        if not sox.is_valid_file_format(filepath):
            raise ValueError("Cannot handle this filetype: {}"
                             "".format(filepath))
        if mode == "w":
            # TODO: If/raise
            assert samplerate, "Writing audiofiles requires a samplerate."
            assert channels, "Writing audiofiles requires channels."
            assert bytedepth, "Writing audiofiles requires a bytedepth."

        self._filepath = filepath
        self._wave_handle = None
        self._temp_filepath = util.temp_file(formats.WAVE)

        self._mode = mode
        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!")