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))
                current_frames = 0
                decoder = decoders.SHNDecoder(self.filename)
                frame = decoder.read(4096)
                while (len(frame) > 0):
                    f.write(frame.to_bytes(True, True))
                    current_frames += frame.frames
                    if (progress is not None):
                        progress(current_frames, total_frames)
                    frame = decoder.read(4096)
                f.write(tail)
                f.close()
            except IOError, msg:
                self.__unlink__(aiff_filename)
                raise EncodingError(str(msg))
        else:
            AiffAudio.from_pcm(aiff_filename, to_pcm_progress(self, progress))

    @classmethod
    def from_wave(cls, filename, wave_filename, compression=None,
                  block_size=256, progress=None):
        """encodes a new AudioFile from an existing .wav file

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

        wave = WaveAudio(wave_filename)