当前位置: 首页>>代码示例>>Python>>正文


Python BitstreamRecorder.build方法代码示例

本文整理汇总了Python中audiotools.bitstream.BitstreamRecorder.build方法的典型用法代码示例。如果您正苦于以下问题:Python BitstreamRecorder.build方法的具体用法?Python BitstreamRecorder.build怎么用?Python BitstreamRecorder.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在audiotools.bitstream.BitstreamRecorder的用法示例。


在下文中一共展示了BitstreamRecorder.build方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: aiff_header

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import build [as 别名]
def aiff_header(sample_rate,
                channels,
                bits_per_sample,
                total_pcm_frames):
    """given a set of integer stream attributes,
    returns header string of everything before an AIFF's PCM data

    may raise ValueError if the total size of the file is too large"""

    from audiotools.bitstream import (BitstreamRecorder, format_size)

    header = BitstreamRecorder(False)

    data_size = (bits_per_sample // 8) * channels * total_pcm_frames
    total_size = ((format_size("4b" + "4b 32u" +
                               "16u 32u 16u 1u 15u 64U" +
                               "4b 32u 32u 32u") // 8) +
                  data_size + (data_size % 2))

    if total_size < (2 ** 32):
        header.build("4b 32u 4b", (b"FORM", total_size, b"AIFF"))
        header.build("4b 32u", (b"COMM", 0x12))
        header.build("16u 32u 16u", (channels,
                                     total_pcm_frames,
                                     bits_per_sample))
        build_ieee_extended(header, sample_rate)
        header.build("4b 32u 32u 32u", (b"SSND", data_size + 8, 0, 0))

        return header.data()
    else:
        raise ValueError("total size too large for aiff file")
开发者ID:brigittebigi,项目名称:sppas,代码行数:33,代码来源:aiff.py

示例2: update_metadata

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import build [as 别名]
    def update_metadata(self, metadata):
        """takes this track's current MetaData object
        as returned by get_metadata() and sets this track's metadata
        with any fields updated in that object

        raises IOError if unable to write the file
        """

        import os
        from audiotools import TemporaryFile
        from audiotools.ogg import (PageReader,
                                    PacketReader,
                                    PageWriter,
                                    packet_to_pages,
                                    packets_to_pages)
        from audiotools.vorbiscomment import VorbisComment
        from audiotools.bitstream import BitstreamRecorder

        if (metadata is None):
            return
        elif (not isinstance(metadata, VorbisComment)):
            from .text import ERR_FOREIGN_METADATA
            raise ValueError(ERR_FOREIGN_METADATA)
        elif (not os.access(self.filename, os.W_OK)):
            raise IOError(self.filename)

        original_ogg = PacketReader(PageReader(file(self.filename, "rb")))
        new_ogg = PageWriter(TemporaryFile(self.filename))

        sequence_number = 0

        #transfer current file's identification packet in its own page
        identification_packet = original_ogg.read_packet()
        for (i, page) in enumerate(packet_to_pages(
                identification_packet,
                self.__serial_number__,
                starting_sequence_number=sequence_number)):
            page.stream_beginning = (i == 0)
            new_ogg.write(page)
            sequence_number += 1

        #discard the current file's comment packet
        comment_packet = original_ogg.read_packet()

        #generate new comment packet
        comment_writer = BitstreamRecorder(True)
        comment_writer.build("8u 6b", (3, "vorbis"))
        vendor_string = metadata.vendor_string.encode('utf-8')
        comment_writer.build("32u %db" % (len(vendor_string)),
                             (len(vendor_string), vendor_string))
        comment_writer.write(32, len(metadata.comment_strings))
        for comment_string in metadata.comment_strings:
            comment_string = comment_string.encode('utf-8')
            comment_writer.build("32u %db" % (len(comment_string)),
                                 (len(comment_string), comment_string))

        comment_writer.build("1u a", (1,))  # framing bit

        #transfer codebooks packet from original file to new file
        codebooks_packet = original_ogg.read_packet()

        for page in packets_to_pages(
                [comment_writer.data(), codebooks_packet],
                self.__serial_number__,
                starting_sequence_number=sequence_number):
            new_ogg.write(page)
            sequence_number += 1

        #transfer remaining pages after re-sequencing
        page = original_ogg.read_page()
        page.sequence_number = sequence_number
        sequence_number += 1
        new_ogg.write(page)
        while (not page.stream_end):
            page = original_ogg.read_page()
            page.sequence_number = sequence_number
            page.bitstream_serial_number = self.__serial_number__
            sequence_number += 1
            new_ogg.write(page)

        original_ogg.close()
        new_ogg.close()
开发者ID:SeverVladMuresan,项目名称:python-audio-tools,代码行数:84,代码来源:vorbis.py

示例3: wave_header

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import build [as 别名]
def wave_header(sample_rate,
                channels,
                channel_mask,
                bits_per_sample,
                total_pcm_frames):
    """given a set of integer stream attributes,
    returns header string of everything before a RIFF WAVE's PCM data

    may raise ValueError if the total size of the file is too large"""

    from audiotools.bitstream import (BitstreamRecorder, format_size)

    assert(isinstance(sample_rate, int))
    assert(isinstance(channels, int))
    assert(isinstance(channel_mask, int))
    assert(isinstance(bits_per_sample, int))
    assert(isinstance(total_pcm_frames, int) or
           isinstance(total_pcm_frames, long))

    header = BitstreamRecorder(True)

    avg_bytes_per_second = sample_rate * channels * (bits_per_sample // 8)
    block_align = channels * (bits_per_sample // 8)

    # build a regular or extended fmt chunk
    # based on the reader's attributes
    if ((channels <= 2) and (bits_per_sample <= 16)):
        fmt = "16u 16u 32u 32u 16u 16u"
        fmt_fields = (1,   # compression code
                      channels,
                      sample_rate,
                      avg_bytes_per_second,
                      block_align,
                      bits_per_sample)
    else:
        if channel_mask == 0:
            channel_mask = {1: 0x4,
                            2: 0x3,
                            3: 0x7,
                            4: 0x33,
                            5: 0x37,
                            6: 0x3F}.get(channels, 0)
        fmt = "16u 16u 32u 32u 16u 16u" + "16u 16u 32u 16b"
        fmt_fields = (0xFFFE,   # compression code
                      channels,
                      sample_rate,
                      avg_bytes_per_second,
                      block_align,
                      bits_per_sample,
                      22,       # CB size
                      bits_per_sample,
                      channel_mask,
                      b'\x01\x00\x00\x00\x00\x00\x10\x00' +
                      b'\x80\x00\x00\xaa\x00\x38\x9b\x71'  # sub format
                      )

    data_size = (bits_per_sample // 8) * channels * total_pcm_frames
    total_size = ((format_size("4b" + "4b 32u" + fmt + "4b 32u") // 8) +
                  data_size + (data_size % 2))

    if total_size < (2 ** 32):
        header.build("4b 32u 4b", (b"RIFF", total_size, b"WAVE"))
        header.build("4b 32u", (b"fmt ", format_size(fmt) // 8))
        header.build(fmt, fmt_fields)
        header.build("4b 32u", (b"data", data_size))

        return header.data()
    else:
        raise ValueError("total size too large for wave file")
开发者ID:KristoforMaynard,项目名称:python-audio-tools,代码行数:71,代码来源:wav.py

示例4: update_metadata

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import build [as 别名]
    def update_metadata(self, metadata):
        """takes this track's current MetaData object
        as returned by get_metadata() and sets this track's metadata
        with any fields updated in that object

        raises IOError if unable to write the file
        """

        import os
        from audiotools import TemporaryFile
        from audiotools.ogg import (PageReader, PacketReader,
                                    PageWriter, packet_to_pages)
        from audiotools.bitstream import BitstreamRecorder

        if (metadata is None):
            return
        elif (not isinstance(metadata, VorbisComment)):
            from .text import ERR_FOREIGN_METADATA
            raise ValueError(ERR_FOREIGN_METADATA)
        elif (not os.access(self.filename, os.W_OK)):
            raise IOError(self.filename)

        original_ogg = PacketReader(PageReader(file(self.filename, "rb")))
        new_ogg = PageWriter(TemporaryFile(self.filename))

        #transfer current file's identification page/packet
        #(the ID packet is always fixed size, and fits in one page)
        identification_page = original_ogg.read_page()
        new_ogg.write(identification_page)
        sequence_number = 1

        #discard the current file's comment packet
        original_ogg.read_packet()

        #write the new comment packet in its own page(s)
        comment_writer = BitstreamRecorder(True)
        comment_writer.write_bytes("OpusTags")
        vendor_string = metadata.vendor_string.encode('utf-8')
        comment_writer.build("32u %db" % (len(vendor_string)),
                             (len(vendor_string), vendor_string))
        comment_writer.write(32, len(metadata.comment_strings))
        for comment_string in metadata.comment_strings:
            comment_string = comment_string.encode('utf-8')
            comment_writer.build("32u %db" % (len(comment_string)),
                                 (len(comment_string), comment_string))

        for page in packet_to_pages(
                comment_writer.data(),
                identification_page.bitstream_serial_number,
                starting_sequence_number=sequence_number):
            new_ogg.write(page)
            sequence_number += 1

        #transfer remaining pages after re-sequencing
        page = original_ogg.read_page()
        page.sequence_number = sequence_number
        sequence_number += 1
        new_ogg.write(page)
        while (not page.stream_end):
            page = original_ogg.read_page()
            page.sequence_number = sequence_number
            sequence_number += 1
            new_ogg.write(page)

        original_ogg.close()
        new_ogg.close()
开发者ID:wuye9036,项目名称:python-audio-tools,代码行数:68,代码来源:opus.py


注:本文中的audiotools.bitstream.BitstreamRecorder.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。