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


Python BitstreamRecorder.reset方法代码示例

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


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

示例1: encode_flac

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import reset [as 别名]
def encode_flac(filename, pcmreader,
                block_size=4096,
                max_lpc_order=8,
                adaptive_mid_side=False,
                mid_side=True,
                exhaustive_model_search=False,
                max_residual_partition_order=5):

    options = Encoding_Options(block_size,
                               max_lpc_order,
                               adaptive_mid_side,
                               mid_side,
                               exhaustive_model_search,
                               max_residual_partition_order,
                               14 if pcmreader.bits_per_sample <= 16 else 30)

    streaminfo = STREAMINFO(block_size, block_size,
                            2 ** 32, 0,
                            pcmreader.sample_rate,
                            pcmreader.channels,
                            pcmreader.bits_per_sample,
                            0, md5())

    pcmreader = BufferedPCMReader(pcmreader)
    output_file = open(filename, "wb")
    writer = BitstreamWriter(output_file, 0)

    #write placeholder metadata blocks
    writer.write_bytes("fLaC")
    writer.build("1u 7u 24u", [1, 0, 34])
    streaminfo.write(writer)

    #walk through PCM reader's FrameLists
    frame_number = 0
    frame = pcmreader.read(block_size *
                           (pcmreader.bits_per_sample / 8) *
                           pcmreader.channels)

    flac_frame = BitstreamRecorder(0)

    while (len(frame) > 0):

        streaminfo.input_update(frame)

        flac_frame.reset()
        encode_flac_frame(flac_frame, pcmreader, options, frame_number, frame)

        streaminfo.output_update(flac_frame)

        flac_frame.copy(writer)

        frame_number += 1
        frame = pcmreader.read(block_size *
                               (pcmreader.bits_per_sample / 8) *
                               pcmreader.channels)

    #return to beginning of file and rewrite STREAMINFO block
    output_file.seek(8, 0)
    streaminfo.write(writer)
    writer.close()
开发者ID:XiaonuoGantan,项目名称:python-audio-tools,代码行数:62,代码来源:flac.py

示例2: write_block

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import reset [as 别名]
def write_block(writer, context, channels, block_index, first_block, last_block, parameters):
    """writer is a BitstreamWriter-compatible object
    context is an EncoderContext object
    channels[c][s] is sample "s" in channel "c"
    block_index is an integer of the block's offset in PCM frames
    first_block and last_block are flags indicating the block's sequence
    parameters is an EncodingParameters object
    """

    assert (len(channels) == 1) or (len(channels) == 2)

    if (len(channels) == 1) or (channels[0] == channels[1]):
        # 1 channel block or equivalent
        if len(channels) == 1:
            false_stereo = 0
        else:
            false_stereo = 1

        # calculate maximum magnitude of channel_0
        magnitude = max(map(bits, channels[0]))

        # determine wasted bits
        wasted = min(map(wasted_bps, channels[0]))
        if wasted == INFINITY:
            # all samples are 0
            wasted = 0

        # if wasted bits, remove them from channel_0
        if (wasted > 0) and (wasted != INFINITY):
            shifted = [[s >> wasted for s in channels[0]]]
        else:
            shifted = [channels[0]]

        # calculate CRC of shifted_0
        crc = calculate_crc(shifted)
    else:
        # 2 channel block
        false_stereo = 0

        # calculate maximum magnitude of channel_0/channel_1
        magnitude = max(max(map(bits, channels[0])), max(map(bits, channels[1])))

        # determine wasted bits
        wasted = min(min(map(wasted_bps, channels[0])), min(map(wasted_bps, channels[1])))
        if wasted == INFINITY:
            # all samples are 0
            wasted = 0

        # if wasted bits, remove them from channel_0/channel_1
        if wasted > 0:
            shifted = [[s >> wasted for s in channels[0]], [s >> wasted for s in channels[1]]]
        else:
            shifted = channels

        # calculate CRC of shifted_0/shifted_1
        crc = calculate_crc(shifted)

        # joint stereo conversion of shifted_0/shifted_1 to mid/side channels
        mid_side = joint_stereo(shifted[0], shifted[1])

    sub_blocks = BitstreamRecorder(1)
    sub_block = BitstreamRecorder(1)

    # if first block in file, write Wave header
    if not context.first_block_written:
        sub_block.reset()
        if context.wave_header is None:
            if context.wave_footer is None:
                write_wave_header(sub_block, context.pcmreader, 0, 0)
            else:
                write_wave_header(sub_block, context.pcmreader, 0, len(context.wave_footer))
        else:
            sub_block.write_bytes(context.wave_header)
        write_sub_block(sub_blocks, WV_WAVE_HEADER, 1, sub_block)
        context.first_block_written = True

    # if correlation passes, write three sub blocks of pass data
    if parameters.correlation_passes > 0:
        sub_block.reset()
        write_correlation_terms(
            sub_block,
            [p.term for p in parameters.correlation_parameters(false_stereo)],
            [p.delta for p in parameters.correlation_parameters(false_stereo)],
        )
        write_sub_block(sub_blocks, WV_TERMS, 0, sub_block)

        sub_block.reset()
        write_correlation_weights(sub_block, [p.weights for p in parameters.correlation_parameters(false_stereo)])
        write_sub_block(sub_blocks, WV_WEIGHTS, 0, sub_block)

        sub_block.reset()
        write_correlation_samples(
            sub_block,
            [p.term for p in parameters.correlation_parameters(false_stereo)],
            [p.samples for p in parameters.correlation_parameters(false_stereo)],
            2 if ((len(channels) == 2) and (not false_stereo)) else 1,
        )
        write_sub_block(sub_blocks, WV_SAMPLES, 0, sub_block)

    # if wasted bits, write extended integers sub block
#.........这里部分代码省略.........
开发者ID:hzlf,项目名称:python-audio-tools,代码行数:103,代码来源:wavpack.py

示例3: encode_wavpack

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import reset [as 别名]
def encode_wavpack(filename, pcmreader, block_size, correlation_passes=0, wave_header=None, wave_footer=None):
    pcmreader = BufferedPCMReader(pcmreader)
    output_file = open(filename, "wb")
    writer = BitstreamWriter(output_file, 1)
    context = EncoderContext(
        pcmreader,
        block_parameters(pcmreader.channels, pcmreader.channel_mask, correlation_passes),
        wave_header,
        wave_footer,
    )

    block_index = 0

    # walk through PCM reader's FrameLists
    frame = pcmreader.read(block_size * (pcmreader.bits_per_sample / 8) * pcmreader.channels)
    while len(frame) > 0:
        context.total_frames += frame.frames
        context.md5sum.update(frame.to_bytes(False, pcmreader.bits_per_sample >= 16))

        c = 0
        for parameters in context.block_parameters:
            if parameters.channel_count == 1:
                channel_data = [list(frame.channel(c))]
            else:
                channel_data = [list(frame.channel(c)), list(frame.channel(c + 1))]

            first_block = parameters is context.block_parameters[0]
            last_block = parameters is context.block_parameters[-1]

            context.block_offsets.append(output_file.tell())
            write_block(writer, context, channel_data, block_index, first_block, last_block, parameters)

            c += parameters.channel_count

        block_index += frame.frames
        frame = pcmreader.read(block_size * (pcmreader.bits_per_sample / 8) * pcmreader.channels)

    # write MD5 sum and optional Wave footer in final block
    sub_blocks = BitstreamRecorder(1)
    sub_block = BitstreamRecorder(1)

    sub_block.reset()
    sub_block.write_bytes(context.md5sum.digest())
    write_sub_block(sub_blocks, WV_MD5, 1, sub_block)

    # write Wave footer in final block, if present
    if context.wave_footer is not None:
        sub_block.reset()
        sub_block.write_bytes(context.wave_footer)
        write_sub_block(sub_blocks, WV_WAVE_FOOTER, 1, sub_block)

    write_block_header(
        writer,
        sub_blocks.bytes(),
        0xFFFFFFFF,
        0,
        pcmreader.bits_per_sample,
        1,
        0,
        0,
        0,
        1,
        1,
        0,
        pcmreader.sample_rate,
        0,
        0xFFFFFFFF,
    )
    sub_blocks.copy(writer)

    # update Wave header's "data" chunk size, if generated
    if context.wave_header is None:
        output_file.seek(32 + 2)
        if context.wave_footer is None:
            write_wave_header(writer, context.pcmreader, context.total_frames, 0)
        else:
            write_wave_header(writer, context.pcmreader, context.total_frames, len(context.wave_footer))

    # go back and populate block headers with total samples
    for block_offset in context.block_offsets:
        output_file.seek(block_offset + 12, 0)
        writer.write(32, block_index)

    writer.close()
开发者ID:hzlf,项目名称:python-audio-tools,代码行数:86,代码来源:wavpack.py

示例4: encode_flac

# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import reset [as 别名]
def encode_flac(filename,
                pcmreader,
                block_size=4096,
                max_lpc_order=8,
                min_residual_partition_order=0,
                max_residual_partition_order=5,
                mid_side=True,
                adaptive_mid_side=False,
                exhaustive_model_search=False,
                disable_verbatim_subframes=False,
                disable_constant_subframes=False,
                disable_fixed_subframes=False,
                disable_lpc_subframes=False,
                padding_size=4096):

    frame_sizes = []

    options = Encoding_Options(block_size,
                               max_lpc_order,
                               adaptive_mid_side,
                               mid_side,
                               exhaustive_model_search,
                               max_residual_partition_order,
                               14 if pcmreader.bits_per_sample <= 16 else 30)

    streaminfo = STREAMINFO(block_size,
                            block_size,
                            (2 ** 24) - 1,
                            0,
                            pcmreader.sample_rate,
                            pcmreader.channels,
                            pcmreader.bits_per_sample,
                            0, md5())

    pcmreader = BufferedPCMReader(pcmreader)
    output_file = open(filename, "wb")
    writer = BitstreamWriter(output_file, False)

    # write placeholder metadata blocks such as STREAMINFO and PADDING
    writer.write_bytes("fLaC")
    writer.build("1u 7u 24u", [0, 0, 34])
    streaminfo_start = writer.getpos()
    streaminfo.write(writer)

    writer.build("1u 7u 24u", [1, 1, padding_size])
    writer.write_bytes(b"\x00" * padding_size)

    # walk through PCM reader's FrameLists
    frame_number = 0
    frame = pcmreader.read(block_size)

    flac_frame = BitstreamRecorder(0)

    while len(frame) > 0:
        streaminfo.input_update(frame)

        flac_frame.reset()
        encode_flac_frame(flac_frame, pcmreader, options, frame_number, frame)
        frame_sizes.append((flac_frame.bytes(), frame.frames))
        streaminfo.output_update(flac_frame)

        flac_frame.copy(writer)

        frame_number += 1
        frame = pcmreader.read(block_size)

    # return to beginning of file and rewrite STREAMINFO block
    writer.setpos(streaminfo_start)
    streaminfo.write(writer)
    writer.flush()
    writer.close()

    return frame_sizes
开发者ID:brigittebigi,项目名称:sppas,代码行数:75,代码来源:flac.py


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