Ejemplo n.º 1
0
    def __init__(self, aiff_file,
                 sample_rate, channels, channel_mask, bits_per_sample,
                 chunk_length, process=None):
        """aiff_file should be rewound to the start of the SSND chunk."""

        alignment = AiffAudio.SSND_ALIGN.parse_stream(aiff_file)
        PCMReader.__init__(self,
                           file=__capped_stream_reader__(
                aiff_file,
                chunk_length - AiffAudio.SSND_ALIGN.sizeof()),
                           sample_rate=sample_rate,
                           channels=channels,
                           channel_mask=channel_mask,
                           bits_per_sample=bits_per_sample,
                           process=process,
                           signed=True,
                           big_endian=True)
        self.ssnd_chunk_length = chunk_length - 8
        standard_channel_mask = ChannelMask(self.channel_mask)
        aiff_channel_mask = AIFFChannelMask(standard_channel_mask)
        if (channels in (3, 4, 6)):
            self.channel_order = [aiff_channel_mask.channels().index(channel)
                                  for channel in
                                  standard_channel_mask.channels()]
        else:
            self.channel_order = None
Ejemplo n.º 2
0
    def __init__(self,
                 aiff_file,
                 sample_rate,
                 channels,
                 channel_mask,
                 bits_per_sample,
                 chunk_length,
                 process=None):
        """aiff_file should be rewound to the start of the SSND chunk."""

        alignment = AiffAudio.SSND_ALIGN.parse_stream(aiff_file)
        PCMReader.__init__(self,
                           file=__capped_stream_reader__(
                               aiff_file,
                               chunk_length - AiffAudio.SSND_ALIGN.sizeof()),
                           sample_rate=sample_rate,
                           channels=channels,
                           channel_mask=channel_mask,
                           bits_per_sample=bits_per_sample,
                           process=process,
                           signed=True,
                           big_endian=True)
        self.ssnd_chunk_length = chunk_length - 8
        standard_channel_mask = ChannelMask(self.channel_mask)
        aiff_channel_mask = AIFFChannelMask(standard_channel_mask)
        if (channels in (3, 4, 6)):
            self.channel_order = [
                aiff_channel_mask.channels().index(channel)
                for channel in standard_channel_mask.channels()
            ]
        else:
            self.channel_order = None
Ejemplo n.º 3
0
    def from_pcm(cls, filename, pcmreader, compression=None):
        if (compression not in cls.COMPRESSION_MODES):
            compression = cls.DEFAULT_COMPRESSION

        devnull = file(os.devnull, 'ab')

        sub = subprocess.Popen([
            BIN['oggenc'], '-Q', '-r', '-B',
            str(pcmreader.bits_per_sample), '-C',
            str(pcmreader.channels), '-R',
            str(pcmreader.sample_rate), '--raw-endianness',
            str(0), '-q', compression, '-o', filename, '-'
        ],
                               stdin=subprocess.PIPE,
                               stdout=devnull,
                               stderr=devnull,
                               preexec_fn=ignore_sigint)

        if ((pcmreader.channels <= 2) or (int(pcmreader.channel_mask) == 0)):
            transfer_framelist_data(pcmreader, sub.stdin.write)
        elif (pcmreader.channels <= 8):
            if (int(pcmreader.channel_mask) in (
                    0x7,  #FR, FC, FL
                    0x33,  #FR, FL, BR, BL
                    0x37,  #FR, FC, FL, BL, BR
                    0x3f,  #FR, FC, FL, BL, BR, LFE
                    0x70f,  #FL, FC, FR, SL, SR, BC, LFE
                    0x63f)  #FL, FC, FR, SL, SR, BL, BR, LFE
                ):
                standard_channel_mask = ChannelMask(pcmreader.channel_mask)
                vorbis_channel_mask = VorbisChannelMask(standard_channel_mask)
            else:
                raise UnsupportedChannelMask()

            transfer_framelist_data(
                ReorderedPCMReader(pcmreader, [
                    standard_channel_mask.channels().index(channel)
                    for channel in vorbis_channel_mask.channels()
                ]), sub.stdin.write)
        else:
            raise UnsupportedChannelMask()

        try:
            pcmreader.close()
        except DecodingError:
            raise EncodingError()
        sub.stdin.close()

        devnull.close()

        if (sub.wait() == 0):
            return VorbisAudio(filename)
        else:
            raise EncodingError(BIN['oggenc'])
Ejemplo n.º 4
0
    def __init__(self, aiff_file,
                 sample_rate, channels, channel_mask, bits_per_sample,
                 total_frames, process=None):
        """aiff_file should be a file-like object of aiff data

        sample_rate, channels, channel_mask and bits_per_sample are ints."""

        self.file = aiff_file
        self.sample_rate = sample_rate
        self.channels = channels
        self.channel_mask = channel_mask
        self.bits_per_sample = bits_per_sample
        self.remaining_frames = total_frames
        self.bytes_per_frame = self.channels * self.bits_per_sample / 8

        self.process = process

        from .bitstream import BitstreamReader

        #build a capped reader for the ssnd chunk
        aiff_reader = BitstreamReader(aiff_file, 0)
        try:
            (form, aiff) = aiff_reader.parse("4b 32p 4b")
            if (form != 'FORM'):
                raise InvalidAIFF(_(u"Not an AIFF file"))
            elif (aiff != 'AIFF'):
                raise InvalidAIFF(_(u"Invalid AIFF file"))

            while (True):
                (chunk_id, chunk_size) = aiff_reader.parse("4b 32u")
                if (chunk_id == 'SSND'):
                    #adjust for the SSND alignment
                    aiff_reader.skip(64)
                    break
                else:
                    aiff_reader.skip_bytes(chunk_size)
                    if (chunk_size % 2):
                        aiff_reader.skip(8)
        except IOError:
            self.read = self.read_error

        #handle AIFF unusual channel order
        standard_channel_mask = ChannelMask(self.channel_mask)
        aiff_channel_mask = AIFFChannelMask(standard_channel_mask)
        if (channels in (3, 4, 6)):
            self.channel_order = [aiff_channel_mask.channels().index(channel)
                                  for channel in
                                  standard_channel_mask.channels()]
        else:
            self.channel_order = None
Ejemplo n.º 5
0
                 0x70f,    # FL, FC, FR, SL, SR, BC, LFE
                 0x63f)):  # FL, FC, FR, SL, SR, BL, BR, LFE

                standard_channel_mask = ChannelMask(pcmreader.channel_mask)
                vorbis_channel_mask = VorbisChannelMask(standard_channel_mask)
            else:
                raise UnsupportedChannelMask(filename,
                                             int(pcmreader.channel_mask))

            try:
                from . import ReorderedPCMReader

                transfer_framelist_data(
                    ReorderedPCMReader(
                        pcmreader,
                        [standard_channel_mask.channels().index(channel)
                         for channel in vorbis_channel_mask.channels()]),
                    sub.stdin.write)
            except (IOError, ValueError), err:
                sub.stdin.close()
                sub.wait()
                cls.__unlink__(filename)
                raise EncodingError(str(err))
            except Exception, err:
                sub.stdin.close()
                sub.wait()
                cls.__unlink__(filename)
                raise err

        else:
            raise UnsupportedChannelMask(filename,
Ejemplo n.º 6
0
                0x37,  # FR, FC, FL, BL, BR
                0x3F,  # FR, FC, FL, BL, BR, LFE
                0x70F,  # FL, FC, FR, SL, SR, BC, LFE
                0x63F,
            ):  # FL, FC, FR, SL, SR, BL, BR, LFE

                standard_channel_mask = ChannelMask(pcmreader.channel_mask)
                vorbis_channel_mask = VorbisChannelMask(standard_channel_mask)
            else:
                raise UnsupportedChannelMask(filename, int(pcmreader.channel_mask))

            try:
                transfer_framelist_data(
                    ReorderedPCMReader(
                        pcmreader,
                        [standard_channel_mask.channels().index(channel) for channel in vorbis_channel_mask.channels()],
                    ),
                    sub.stdin.write,
                )
            except (IOError, ValueError), err:
                sub.stdin.close()
                sub.wait()
                cls.__unlink__(filename)
                raise EncodingError(str(err))
            except Exception, err:
                sub.stdin.close()
                sub.wait()
                cls.__unlink__(filename)
                raise err

        else:
Ejemplo n.º 7
0
        try:
            f = open(filename, 'wb')
        except IOError, msg:
            raise EncodingError(str(msg))

        if (int(pcmreader.channel_mask) in (
                0x4,  # FC
                0x3,  # FL, FR
                0x7,  # FL, FR, FC
                0x33,  # FL, FR, BL, BR
                0x707)):  # FL, SL, FC, FR, SR, BC
            standard_channel_mask = ChannelMask(pcmreader.channel_mask)
            aiff_channel_mask = AIFFChannelMask(standard_channel_mask)
            pcmreader = ReorderedPCMReader(pcmreader, [
                standard_channel_mask.channels().index(channel)
                for channel in aiff_channel_mask.channels()
            ])

        try:
            aiff_header = Con.Container(aiff_id='FORM',
                                        aiff_size=4,
                                        aiff_type='AIFF')

            comm_chunk = Con.Container(channels=pcmreader.channels,
                                       total_sample_frames=0,
                                       sample_size=pcmreader.bits_per_sample,
                                       sample_rate=float(
                                           pcmreader.sample_rate))

            ssnd_header = Con.Container(chunk_id='SSND', chunk_length=0)
Ejemplo n.º 8
0
    def from_pcm(cls, filename, pcmreader, compression=None):
        try:
            f = open(filename, 'wb')
        except IOError:
            raise EncodingError(None)

        if (int(pcmreader.channel_mask) in (
                0x4,  #FC
                0x3,  #FL, FR
                0x7,  #FL, FR, FC
                0x33,  #FL, FR, BL, BR
                0x707)  #FL, SL, FC, FR, SR, BC
            ):
            standard_channel_mask = ChannelMask(pcmreader.channel_mask)
            aiff_channel_mask = AIFFChannelMask(standard_channel_mask)
            pcmreader = ReorderedPCMReader(pcmreader, [
                standard_channel_mask.channels().index(channel)
                for channel in aiff_channel_mask.channels()
            ])

        try:
            aiff_header = construct.Container(aiff_id='FORM',
                                              aiff_size=4,
                                              aiff_type='AIFF')

            comm_chunk = construct.Container(
                channels=pcmreader.channels,
                total_sample_frames=0,
                sample_size=pcmreader.bits_per_sample,
                sample_rate=float(pcmreader.sample_rate))

            ssnd_header = construct.Container(chunk_id='SSND', chunk_length=0)
            ssnd_alignment = construct.Container(offset=0, blocksize=0)

            #skip ahead to the start of the SSND chunk
            f.seek(
                cls.AIFF_HEADER.sizeof() + cls.CHUNK_HEADER.sizeof() +
                cls.COMM_CHUNK.sizeof() + cls.CHUNK_HEADER.sizeof(), 0)

            #write the SSND alignment info
            f.write(cls.SSND_ALIGN.build(ssnd_alignment))

            #write big-endian samples to SSND chunk from pcmreader
            framelist = pcmreader.read(BUFFER_SIZE)
            total_pcm_frames = 0
            while (len(framelist) > 0):
                f.write(framelist.to_bytes(True, True))
                total_pcm_frames += framelist.frames
                framelist = pcmreader.read(BUFFER_SIZE)
            total_size = f.tell()

            #return to the start of the file
            f.seek(0, 0)

            #write AIFF header
            aiff_header.aiff_size = total_size - 8
            f.write(cls.AIFF_HEADER.build(aiff_header))

            #write COMM chunk
            comm_chunk.total_sample_frames = total_pcm_frames
            comm_chunk = cls.COMM_CHUNK.build(comm_chunk)
            f.write(
                cls.CHUNK_HEADER.build(
                    construct.Container(chunk_id='COMM',
                                        chunk_length=len(comm_chunk))))
            f.write(comm_chunk)

            #write SSND chunk header
            f.write(
                cls.CHUNK_HEADER.build(
                    construct.Container(
                        chunk_id='SSND',
                        chunk_length=(total_pcm_frames *
                                      (pcmreader.bits_per_sample / 8) *
                                      pcmreader.channels) +
                        cls.SSND_ALIGN.sizeof())))
            try:
                pcmreader.close()
            except DecodingError:
                raise EncodingError()
        finally:
            f.close()

        return cls(filename)