Example #1
0
    def from_aiff(cls, filename, aiff_filename, compression=None, block_size=256, progress=None):
        """encodes a new AudioFile from an existing .aiff file

        takes a filename string, aiff_filename string
        of an existing WaveAudio file
        and an optional compression level string
        encodes a new audio file from the aiff's data
        at the given filename with the specified compression level
        and returns a new ShortenAudio object"""

        aiff = AiffAudio(aiff_filename)

        if aiff.bits_per_sample() not in (8, 16):
            raise UnsupportedBitsPerSample(filename, aiff.bits_per_sample())

        (head, tail) = aiff.pcm_split()

        from .encoders import encode_shn

        try:
            if len(tail) == 0:
                encode_shn(
                    filename=filename,
                    pcmreader=to_pcm_progress(aiff, progress),
                    is_big_endian=True,
                    signed_samples=True,
                    header_data=head,
                    block_size=block_size,
                )
            else:
                encode_shn(
                    filename=filename,
                    pcmreader=to_pcm_progress(aiff, progress),
                    is_big_endian=True,
                    signed_samples=True,
                    header_data=head,
                    footer_data=tail,
                    block_size=block_size,
                )

            return cls(filename)
        except IOError, err:
            cls.__unlink__(filename)
            raise EncodingError(str(err))
Example #2
0
    def from_aiff(cls, filename, aiff_filename, compression=None,
                  block_size=256, progress=None):
        """Encodes a new AudioFile from an existing .aiff file.

        Takes a filename string, aiff_filename string
        of an existing WaveAudio file
        and an optional compression level string.
        Encodes a new audio file from the aiff's data
        at the given filename with the specified compression level
        and returns a new ShortenAudio object."""

        aiff = AiffAudio(aiff_filename)

        if (aiff.bits_per_sample() not in (8, 16)):
            raise UnsupportedBitsPerSample(filename, aiff.bits_per_sample())

        (head, tail) = aiff.pcm_split()
        if (len(tail) > 0):
            blocks = [head, None, tail]
        else:
            blocks = [head, None]

        import audiotools.encoders

        try:
            audiotools.encoders.encode_shn(
                filename=filename,
                pcmreader=to_pcm_progress(aiff, progress),
                block_size=block_size,
                file_type={8: 1,  # 8-bit AIFF seems to be signed
                           16: 3}[aiff.bits_per_sample()],
                verbatim_chunks=blocks)

            return cls(filename)
        except IOError, err:
            cls.__unlink__(filename)
            raise EncodingError(str(err))