本文整理汇总了Python中audiotools.bitstream.BitstreamRecorder类的典型用法代码示例。如果您正苦于以下问题:Python BitstreamRecorder类的具体用法?Python BitstreamRecorder怎么用?Python BitstreamRecorder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BitstreamRecorder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode_flac
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()
示例2: calculate_lpc_coefficients
def calculate_lpc_coefficients(pcmreader, options, sample_size, channel):
windowed = [s * t for s, t in zip(channel, tukey_window(len(channel), 0.5))]
autocorrelated = [sum([s1 * s2 for s1, s2 in zip(windowed, windowed[lag:])]) for lag in range(0, 9)]
assert len(autocorrelated) == 9
if autocorrelated[0] != 0.0:
lp_coefficients = compute_lp_coefficients(autocorrelated)
assert len(lp_coefficients) == 8
qlp_coefficients4 = quantize_coefficients(lp_coefficients, 4)
qlp_coefficients8 = quantize_coefficients(lp_coefficients, 8)
residuals4 = compute_residuals(sample_size, qlp_coefficients4[:], channel)
residuals8 = compute_residuals(sample_size, qlp_coefficients8[:], channel)
residual_block4 = BitstreamRecorder(0)
residual_block8 = BitstreamRecorder(0)
encode_residuals(residual_block4, options, sample_size, residuals4)
encode_residuals(residual_block8, options, sample_size, residuals8)
if residual_block4.bits() < residual_block8.bits():
return (qlp_coefficients4, residual_block4)
else:
return (qlp_coefficients8, residual_block8)
else:
qlp_coefficients = [0, 0, 0, 0]
residuals = compute_residuals(sample_size, qlp_coefficients[:], channel)
residual_block = BitstreamRecorder(0)
encode_residuals(residual_block, options, sample_size, residuals)
return (qlp_coefficients, residual_block)
示例3: build
def build(self, writer):
"""outputs an APEv2 tag to writer"""
from audiotools.bitstream import BitstreamRecorder
tags = BitstreamRecorder(1)
for tag in self.tags:
tag.build(tags)
if (self.contains_header):
writer.build(ApeTag.HEADER_FORMAT,
("APETAGEX", # preamble
2000, # version
tags.bytes() + 32, # tag size
len(self.tags), # item count
0, # read only
0, # encoding
1, # is header
not self.contains_footer, # no footer
self.contains_header)) # has header
tags.copy(writer)
if (self.contains_footer):
writer.build(ApeTag.HEADER_FORMAT,
("APETAGEX", # preamble
2000, # version
tags.bytes() + 32, # tag size
len(self.tags), # item count
0, # read only
0, # encoding
0, # is header
not self.contains_footer, # no footer
self.contains_header)) # has header
示例4: aiff_header
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")
示例5: encode_residual_partition
def encode_residual_partition(rice_parameter, residuals):
partition = BitstreamRecorder(0)
for residual in residuals:
if residual >= 0:
unsigned = residual << 1
else:
unsigned = ((-residual - 1) << 1) | 1
MSB = unsigned >> rice_parameter
LSB = unsigned - (MSB << rice_parameter)
partition.unary(1, MSB)
partition.write(rice_parameter, LSB)
return partition
示例6: update_metadata
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
"""
from audiotools import transfer_data, TemporaryFile
from audiotools.id3 import ID3v22Comment
from audiotools.bitstream import BitstreamRecorder
from audiotools.text import ERR_FOREIGN_METADATA
import os
if metadata is None:
return
elif not isinstance(metadata, ID3v22Comment):
raise ValueError(ERR_FOREIGN_METADATA)
elif not os.access(self.filename, os.W_OK):
raise IOError(self.filename)
# turn our ID3v2.2 tag into a raw binary chunk
id3_chunk = BitstreamRecorder(0)
metadata.build(id3_chunk)
# generate a temporary AIFF file in which our new ID3v2.2 chunk
# replaces the existing ID3v2.2 chunk
new_aiff = TemporaryFile(self.filename)
self.__class__.aiff_from_chunks(
new_aiff,
[(chunk if chunk.id != b"ID3 " else
AIFF_Chunk(b"ID3 ",
id3_chunk.bytes(),
id3_chunk.data())) for chunk in self.chunks()])
new_aiff.close()
示例7: set_metadata
def set_metadata(self, metadata):
"""takes a MetaData object and sets this track's metadata
this metadata includes track name, album name, and so on
raises IOError if unable to write the file"""
from audiotools.id3 import ID3v22Comment
if metadata is None:
return self.delete_metadata()
elif self.get_metadata() is not None:
# current file has metadata, so replace it with new metadata
self.update_metadata(ID3v22Comment.converted(metadata))
else:
# current file has no metadata, so append new ID3 block
import os
from audiotools.bitstream import BitstreamRecorder
from audiotools import transfer_data, TemporaryFile
if not os.access(self.filename, os.W_OK):
raise IOError(self.filename)
# turn our ID3v2.2 tag into a raw binary chunk
id3_chunk = BitstreamRecorder(0)
ID3v22Comment.converted(metadata).build(id3_chunk)
# generate a temporary AIFF file in which our new ID3v2.2 chunk
# is appended to the file's set of chunks
new_aiff = TemporaryFile(self.filename)
self.__class__.aiff_from_chunks(
new_aiff,
[c for c in self.chunks()] + [AIFF_Chunk(b"ID3 ",
id3_chunk.bytes(),
id3_chunk.data())])
new_aiff.close()
示例8: encode_wavpack
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()
示例9: write_block
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
#.........这里部分代码省略.........
示例10: wave_header_footer
def wave_header_footer(self):
"""returns a pair of data strings before and after PCM data
the first contains all data before the PCM content of the data chunk
the second containing all data after the data chunk
for example:
>>> w = audiotools.open("input.wav")
>>> (head, tail) = w.wave_header_footer()
>>> f = open("output.wav", "wb")
>>> f.write(head)
>>> audiotools.transfer_framelist_data(w.to_pcm(), f.write)
>>> f.write(tail)
>>> f.close()
should result in "output.wav" being identical to "input.wav"
"""
from audiotools.bitstream import BitstreamReader
from audiotools.bitstream import BitstreamRecorder
head = BitstreamRecorder(1)
tail = BitstreamRecorder(1)
current_block = head
fmt_found = False
with BitstreamReader(open(self.filename, 'rb'), 1) as wave_file:
# transfer the 12-byte "RIFFsizeWAVE" header to head
(riff, size, wave) = wave_file.parse("4b 32u 4b")
if riff != b'RIFF':
from audiotools.text import ERR_WAV_NOT_WAVE
raise ValueError(ERR_WAV_NOT_WAVE)
elif wave != b'WAVE':
from audiotools.text import ERR_WAV_INVALID_WAVE
raise ValueError(ERR_WAV_INVALID_WAVE)
else:
current_block.build("4b 32u 4b", (riff, size, wave))
total_size = size - 4
while total_size > 0:
# transfer each chunk header
(chunk_id, chunk_size) = wave_file.parse("4b 32u")
if not frozenset(chunk_id).issubset(self.PRINTABLE_ASCII):
from audiotools.text import ERR_WAV_INVALID_CHUNK
raise ValueError(ERR_WAV_INVALID_CHUNK)
else:
current_block.build("4b 32u", (chunk_id, chunk_size))
total_size -= 8
# and transfer the full content of non-audio chunks
if chunk_id != b"data":
if chunk_id == b"fmt ":
if not fmt_found:
fmt_found = True
else:
from audiotools.text import ERR_WAV_MULTIPLE_FMT
raise ValueError(ERR_WAV_MULTIPLE_FMT)
if chunk_size % 2:
current_block.write_bytes(
wave_file.read_bytes(chunk_size + 1))
total_size -= (chunk_size + 1)
else:
current_block.write_bytes(
wave_file.read_bytes(chunk_size))
total_size -= chunk_size
else:
wave_file.skip_bytes(chunk_size)
current_block = tail
if chunk_size % 2:
current_block.write_bytes(wave_file.read_bytes(1))
total_size -= (chunk_size + 1)
else:
total_size -= chunk_size
if fmt_found:
return (head.data(), tail.data())
else:
from audiotools.text import ERR_WAV_NO_FMT_CHUNK
raise ValueError(ERR_WAV_NO_FMT_CHUNK)
示例11: encode_subframe
def encode_subframe(writer, options, bits_per_sample, samples):
def all_identical(l):
if (len(l) == 1):
return True
else:
for i in l[1:]:
if (i != l[0]):
return False
else:
return True
def wasted(s):
w = 0
while ((s & 1) == 0):
w += 1
s >>= 1
return w
if (all_identical(samples)):
encode_constant_subframe(writer, bits_per_sample, samples[0])
else:
#account for wasted BPS, if any
wasted_bps = 2 ** 32
for sample in samples:
if (sample != 0):
wasted_bps = min(wasted_bps, wasted(sample))
if (wasted_bps == 0):
break
if (wasted_bps == 2 ** 32):
#all samples are 0
wasted_bps = 0
elif (wasted_bps > 0):
samples = [s >> wasted_bps for s in samples]
fixed_subframe = BitstreamRecorder(0)
encode_fixed_subframe(fixed_subframe,
options,
wasted_bps,
bits_per_sample,
samples)
if (options.max_lpc_order > 0):
(lpc_order,
qlp_coeffs,
qlp_shift_needed) = compute_lpc_coefficients(options,
wasted_bps,
bits_per_sample,
samples)
lpc_subframe = BitstreamRecorder(0)
encode_lpc_subframe(lpc_subframe,
options,
wasted_bps,
bits_per_sample,
lpc_order,
options.qlp_precision,
qlp_shift_needed,
qlp_coeffs,
samples)
if (((bits_per_sample * len(samples)) <
min(fixed_subframe.bits(), lpc_subframe.bits()))):
encode_verbatim_subframe(writer, wasted_bps,
bits_per_sample, samples)
elif (fixed_subframe.bits() < lpc_subframe.bits()):
fixed_subframe.copy(writer)
else:
lpc_subframe.copy(writer)
else:
if ((bits_per_sample * len(samples)) < fixed_subframe.bits()):
encode_verbatim_subframe(writer, wasted_bps,
bits_per_sample, samples)
else:
fixed_subframe.copy(writer)
示例12: encode_flac_frame
def encode_flac_frame(writer, pcmreader, options, frame_number, frame):
crc16 = CRC16()
writer.add_callback(crc16.update)
if ((pcmreader.channels == 2) and (options.adaptive_mid_side or
options.mid_side)):
# calculate average/difference
average = [(c0 + c1) // 2 for c0, c1 in zip(frame.channel(0),
frame.channel(1))]
difference = [c0 - c1 for c0, c1 in zip(frame.channel(0),
frame.channel(1))]
# try different subframes based on encoding options
left_subframe = BitstreamRecorder(0)
encode_subframe(left_subframe, options,
pcmreader.bits_per_sample, list(frame.channel(0)))
right_subframe = BitstreamRecorder(0)
encode_subframe(right_subframe, options,
pcmreader.bits_per_sample, list(frame.channel(1)))
average_subframe = BitstreamRecorder(0)
encode_subframe(average_subframe, options,
pcmreader.bits_per_sample, average)
difference_subframe = BitstreamRecorder(0)
encode_subframe(difference_subframe, options,
pcmreader.bits_per_sample + 1, difference)
# write best header/subframes to disk
if options.mid_side:
if ((left_subframe.bits() + right_subframe.bits()) <
min(left_subframe.bits() + difference_subframe.bits(),
difference_subframe.bits() + right_subframe.bits(),
average_subframe.bits() + difference_subframe.bits())):
write_frame_header(writer, pcmreader, frame_number, frame, 0x1)
left_subframe.copy(writer)
right_subframe.copy(writer)
elif (left_subframe.bits() <
min(right_subframe.bits(), difference_subframe.bits())):
write_frame_header(writer, pcmreader, frame_number, frame, 0x8)
left_subframe.copy(writer)
difference_subframe.copy(writer)
elif right_subframe.bits() < average_subframe.bits():
write_frame_header(writer, pcmreader, frame_number, frame, 0x9)
difference_subframe.copy(writer)
right_subframe.copy(writer)
else:
write_frame_header(writer, pcmreader, frame_number, frame, 0xA)
average_subframe.copy(writer)
difference_subframe.copy(writer)
else:
if (((left_subframe.bits() + right_subframe.bits()) <
(average_subframe.bits() + difference_subframe.bits()))):
write_frame_header(writer, pcmreader, frame_number, frame, 0x1)
left_subframe.copy(writer)
right_subframe.copy(writer)
else:
write_frame_header(writer, pcmreader, frame_number, frame, 0xA)
average_subframe.copy(writer)
difference_subframe.copy(writer)
else:
write_frame_header(writer, pcmreader, frame_number, frame,
pcmreader.channels - 1)
for i in range(frame.channels):
encode_subframe(writer,
options,
pcmreader.bits_per_sample,
list(frame.channel(i)))
writer.byte_align()
writer.pop_callback()
writer.write(16, int(crc16))
示例13: wave_header
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")
示例14: aiff_header_footer
def aiff_header_footer(self):
"""returns (header, footer) tuple of strings
containing all data before and after the PCM stream
if self.has_foreign_aiff_chunks() is False,
may raise ValueError if the file has no header and footer
for any reason"""
from audiotools.bitstream import BitstreamReader
from audiotools.bitstream import BitstreamRecorder
from audiotools.text import (ERR_AIFF_NOT_AIFF,
ERR_AIFF_INVALID_AIFF,
ERR_AIFF_INVALID_CHUNK_ID)
head = BitstreamRecorder(0)
tail = BitstreamRecorder(0)
current_block = head
with BitstreamReader(open(self.filename, 'rb'), False) as aiff_file:
# transfer the 12-byte "RIFFsizeWAVE" header to head
(form, size, aiff) = aiff_file.parse("4b 32u 4b")
if form != b'FORM':
raise InvalidAIFF(ERR_AIFF_NOT_AIFF)
elif aiff != b'AIFF':
raise InvalidAIFF(ERR_AIFF_INVALID_AIFF)
else:
current_block.build("4b 32u 4b", (form, size, aiff))
total_size = size - 4
while total_size > 0:
# transfer each chunk header
(chunk_id, chunk_size) = aiff_file.parse("4b 32u")
if not frozenset(chunk_id).issubset(self.PRINTABLE_ASCII):
raise InvalidAIFF(ERR_AIFF_INVALID_CHUNK_ID)
else:
current_block.build("4b 32u", (chunk_id, chunk_size))
total_size -= 8
# and transfer the full content of non-audio chunks
if chunk_id != b"SSND":
if chunk_size % 2:
current_block.write_bytes(
aiff_file.read_bytes(chunk_size + 1))
total_size -= (chunk_size + 1)
else:
current_block.write_bytes(
aiff_file.read_bytes(chunk_size))
total_size -= chunk_size
else:
# transfer alignment as part of SSND's chunk header
align = aiff_file.parse("32u 32u")
current_block.build("32u 32u", align)
aiff_file.skip_bytes(chunk_size - 8)
current_block = tail
if chunk_size % 2:
current_block.write_bytes(aiff_file.read_bytes(1))
total_size -= (chunk_size + 1)
else:
total_size -= chunk_size
return (head.data(), tail.data())
示例15: encode_subframe
def encode_subframe(writer, options, bits_per_sample, samples):
def all_identical(l):
if len(l) == 1:
return True
else:
for i in l[1:]:
if i != l[0]:
return False
else:
return True
def wasted(s):
w = 0
while (s & 1) == 0:
w += 1
s >>= 1
return w
if all_identical(samples):
encode_constant_subframe(writer, bits_per_sample, samples[0])
else:
# account for wasted BPS, if any
wasted_bps = 2 ** 32
for sample in samples:
if sample != 0:
wasted_bps = min(wasted_bps, wasted(sample))
if wasted_bps == 0:
break
if wasted_bps == 2 ** 32:
# all samples are 0
wasted_bps = 0
elif wasted_bps > 0:
samples = [s >> wasted_bps for s in samples]
fixed_subframe = BitstreamRecorder(0)
encode_fixed_subframe(fixed_subframe,
options,
wasted_bps,
bits_per_sample,
samples)
if options.max_lpc_order > 0:
lpc_subframe = BitstreamRecorder(0)
encode_lpc_subframe(lpc_subframe,
options,
wasted_bps,
bits_per_sample,
samples)
if (((bits_per_sample * len(samples)) <
min(fixed_subframe.bits(), lpc_subframe.bits()))):
encode_verbatim_subframe(writer, wasted_bps,
bits_per_sample, samples)
elif fixed_subframe.bits() < lpc_subframe.bits():
fixed_subframe.copy(writer)
else:
lpc_subframe.copy(writer)
else:
if (bits_per_sample * len(samples)) < fixed_subframe.bits():
encode_verbatim_subframe(writer, wasted_bps,
bits_per_sample, samples)
else:
fixed_subframe.copy(writer)