本文整理汇总了Python中audiotools.bitstream.BitstreamRecorder.data方法的典型用法代码示例。如果您正苦于以下问题:Python BitstreamRecorder.data方法的具体用法?Python BitstreamRecorder.data怎么用?Python BitstreamRecorder.data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类audiotools.bitstream.BitstreamRecorder
的用法示例。
在下文中一共展示了BitstreamRecorder.data方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: aiff_header
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [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")
示例2: update_metadata
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [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
"""
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()
示例3: set_metadata
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [as 别名]
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()
示例4: update_metadata
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [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()
示例5: wave_header_footer
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [as 别名]
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)
示例6: wave_header
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [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")
示例7: aiff_header_footer
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [as 别名]
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())
示例8: update_metadata
# 需要导入模块: from audiotools.bitstream import BitstreamRecorder [as 别名]
# 或者: from audiotools.bitstream.BitstreamRecorder import data [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()