本文整理匯總了Python中audioop.lin2lin方法的典型用法代碼示例。如果您正苦於以下問題:Python audioop.lin2lin方法的具體用法?Python audioop.lin2lin怎麽用?Python audioop.lin2lin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類audioop
的用法示例。
在下文中一共展示了audioop.lin2lin方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_lin2lin
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def test_lin2lin(self):
for w in 1, 2, 4:
self.assertEqual(audioop.lin2lin(datas[w], w, w), datas[w])
self.assertEqual(audioop.lin2lin(datas[1], 1, 2),
packs[2](0, 0x1200, 0x4500, -0x4500, 0x7f00, -0x8000, -0x100))
self.assertEqual(audioop.lin2lin(datas[1], 1, 4),
packs[4](0, 0x12000000, 0x45000000, -0x45000000,
0x7f000000, -0x80000000, -0x1000000))
self.assertEqual(audioop.lin2lin(datas[2], 2, 1),
b'\x00\x12\x45\xba\x7f\x80\xff')
self.assertEqual(audioop.lin2lin(datas[2], 2, 4),
packs[4](0, 0x12340000, 0x45670000, -0x45670000,
0x7fff0000, -0x80000000, -0x10000))
self.assertEqual(audioop.lin2lin(datas[4], 4, 1),
b'\x00\x12\x45\xba\x7f\x80\xff')
self.assertEqual(audioop.lin2lin(datas[4], 4, 2),
packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1))
示例2: test_issue7673
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def test_issue7673(self):
state = None
for data, size in INVALID_DATA:
size2 = size
self.assertRaises(audioop.error, audioop.getsample, data, size, 0)
self.assertRaises(audioop.error, audioop.max, data, size)
self.assertRaises(audioop.error, audioop.minmax, data, size)
self.assertRaises(audioop.error, audioop.avg, data, size)
self.assertRaises(audioop.error, audioop.rms, data, size)
self.assertRaises(audioop.error, audioop.avgpp, data, size)
self.assertRaises(audioop.error, audioop.maxpp, data, size)
self.assertRaises(audioop.error, audioop.cross, data, size)
self.assertRaises(audioop.error, audioop.mul, data, size, 1.0)
self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5)
self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5)
self.assertRaises(audioop.error, audioop.add, data, data, size)
self.assertRaises(audioop.error, audioop.bias, data, size, 0)
self.assertRaises(audioop.error, audioop.reverse, data, size)
self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2)
self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state)
self.assertRaises(audioop.error, audioop.lin2ulaw, data, size)
self.assertRaises(audioop.error, audioop.lin2alaw, data, size)
self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state)
示例3: test_string
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def test_string(self):
data = 'abcd'
size = 2
self.assertRaises(TypeError, audioop.getsample, data, size, 0)
self.assertRaises(TypeError, audioop.max, data, size)
self.assertRaises(TypeError, audioop.minmax, data, size)
self.assertRaises(TypeError, audioop.avg, data, size)
self.assertRaises(TypeError, audioop.rms, data, size)
self.assertRaises(TypeError, audioop.avgpp, data, size)
self.assertRaises(TypeError, audioop.maxpp, data, size)
self.assertRaises(TypeError, audioop.cross, data, size)
self.assertRaises(TypeError, audioop.mul, data, size, 1.0)
self.assertRaises(TypeError, audioop.tomono, data, size, 0.5, 0.5)
self.assertRaises(TypeError, audioop.tostereo, data, size, 0.5, 0.5)
self.assertRaises(TypeError, audioop.add, data, data, size)
self.assertRaises(TypeError, audioop.bias, data, size, 0)
self.assertRaises(TypeError, audioop.reverse, data, size)
self.assertRaises(TypeError, audioop.lin2lin, data, size, size)
self.assertRaises(TypeError, audioop.ratecv, data, size, 1, 1, 1, None)
self.assertRaises(TypeError, audioop.lin2ulaw, data, size)
self.assertRaises(TypeError, audioop.lin2alaw, data, size)
self.assertRaises(TypeError, audioop.lin2adpcm, data, size, None)
示例4: normalize
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def normalize(self) -> 'Sample':
"""
Normalize the sample, meaning: convert it to the default samplerate, sample width and number of channels.
When mixing samples, they should all have the same properties, and this method is ideal to make sure of that.
"""
if self.__locked:
raise RuntimeError("cannot modify a locked sample")
self.resample(params.norm_samplerate)
if self.samplewidth != params.norm_samplewidth:
# Convert to desired sample size.
self.__frames = audioop.lin2lin(self.__frames, self.samplewidth, params.norm_samplewidth)
self.__samplewidth = params.norm_samplewidth
if params.norm_nchannels not in (1, 2):
raise ValueError("norm_nchannels has invalid value, can only be 1 or 2")
if self.nchannels == 1 and params.norm_nchannels == 2:
# convert to stereo
self.__frames = audioop.tostereo(self.__frames, self.samplewidth, 1, 1)
self.__nchannels = 2
elif self.nchannels == 2 and params.norm_nchannels == 1:
# convert to mono
self.__frames = audioop.tomono(self.__frames, self.__samplewidth, 1, 1)
self.__nchannels = 1
return self
示例5: test_lin2lin
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def test_lin2lin(self):
for w in 1, 2, 3, 4:
self.assertEqual(audioop.lin2lin(datas[w], w, w), datas[w])
self.assertEqual(audioop.lin2lin(bytearray(datas[w]), w, w),
datas[w])
self.assertEqual(audioop.lin2lin(memoryview(datas[w]), w, w),
datas[w])
self.assertEqual(audioop.lin2lin(datas[1], 1, 2),
packs[2](0, 0x1200, 0x4500, -0x4500, 0x7f00, -0x8000, -0x100))
self.assertEqual(audioop.lin2lin(datas[1], 1, 3),
packs[3](0, 0x120000, 0x450000, -0x450000,
0x7f0000, -0x800000, -0x10000))
self.assertEqual(audioop.lin2lin(datas[1], 1, 4),
packs[4](0, 0x12000000, 0x45000000, -0x45000000,
0x7f000000, -0x80000000, -0x1000000))
self.assertEqual(audioop.lin2lin(datas[2], 2, 1),
b'\x00\x12\x45\xba\x7f\x80\xff')
self.assertEqual(audioop.lin2lin(datas[2], 2, 3),
packs[3](0, 0x123400, 0x456700, -0x456700,
0x7fff00, -0x800000, -0x100))
self.assertEqual(audioop.lin2lin(datas[2], 2, 4),
packs[4](0, 0x12340000, 0x45670000, -0x45670000,
0x7fff0000, -0x80000000, -0x10000))
self.assertEqual(audioop.lin2lin(datas[3], 3, 1),
b'\x00\x12\x45\xba\x7f\x80\xff')
self.assertEqual(audioop.lin2lin(datas[3], 3, 2),
packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1))
self.assertEqual(audioop.lin2lin(datas[3], 3, 4),
packs[4](0, 0x12345600, 0x45678900, -0x45678900,
0x7fffff00, -0x80000000, -0x100))
self.assertEqual(audioop.lin2lin(datas[4], 4, 1),
b'\x00\x12\x45\xba\x7f\x80\xff')
self.assertEqual(audioop.lin2lin(datas[4], 4, 2),
packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1))
self.assertEqual(audioop.lin2lin(datas[4], 4, 3),
packs[3](0, 0x123456, 0x456789, -0x45678a,
0x7fffff, -0x800000, -1))
示例6: get_32bit_frames
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def get_32bit_frames(self, scale_amplitude: bool = True) -> bytes:
"""Returns the raw sample frames scaled to 32 bits. See make_32bit method for more info."""
if self.samplewidth == 4:
return self.__frames
frames = audioop.lin2lin(self.__frames, self.samplewidth, 4) # type: bytes
if not scale_amplitude:
# we need to scale back the sample amplitude to fit back into 24/16/8 bit range
factor = 1.0/2**(8*abs(self.samplewidth-4))
frames = audioop.mul(frames, 4, factor)
return frames
示例7: make_16bit
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def make_16bit(self, maximize_amplitude: bool = True) -> 'Sample':
"""
Convert to 16 bit sample width, usually by using a maximized amplification factor to
scale into the full 16 bit range without clipping or overflow.
This is used for example to downscale a 32 bits mixed sample back into 16 bit width.
"""
if self.__locked:
raise RuntimeError("cannot modify a locked sample")
assert self.samplewidth >= 2
if maximize_amplitude:
self.amplify_max()
if self.samplewidth > 2:
self.__frames = audioop.lin2lin(self.__frames, self.samplewidth, 2)
self.__samplewidth = 2
return self
示例8: testlin2lin
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def testlin2lin(data):
if verbose:
print 'lin2lin'
# too simple: we test only the size
for d1 in data:
for d2 in data:
got = len(d1)//3
wtd = len(d2)//3
if len(audioop.lin2lin(d1, got, wtd)) != len(d2):
return 0
return 1
示例9: convert_sampwidth
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def convert_sampwidth(fragment, sampwidth_in, sampwidth_out):
"""
Convert the sampwidth (byte width) of the input fragment between 1-, 2-, 3-, 4-byte formats.
Parameters
----------
fragment : bytes object
Specifies the original fragment.
sampwidth_in : int
Specifies the fragment's original sampwidth.
sampwidth_out : int
Specifies the fragment's desired sampwidth.
Returns
-------
bytes
Converted audio with the desired sampwidth 'sampwidth_out'.
"""
if sampwidth_in == sampwidth_out:
return fragment
# In .wav files, 16, 24, and 32 bit samples are signed, 8 bit samples are unsigned.
# So when converting from 8 bit wide samples, you need to also subtract 128 from the sample.
# Similarly, when converting to 8 bit wide samples, you need to also add 128 to the result.
if sampwidth_in == 1:
new_fragment = audioop.bias(fragment, 1, -128)
else:
new_fragment = fragment
new_fragment = audioop.lin2lin(new_fragment, sampwidth_in, sampwidth_out)
if sampwidth_out == 1:
new_fragment = audioop.bias(new_fragment, 1, 128)
return new_fragment
示例10: get_raw_data
# 需要導入模塊: import audioop [as 別名]
# 或者: from audioop import lin2lin [as 別名]
def get_raw_data(self, convert_rate = None, convert_width = None):
"""
Returns a byte string representing the raw frame data for the audio represented by the ``AudioData`` instance.
If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match.
If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match.
Writing these bytes directly to a file results in a valid `RAW/PCM audio file <https://en.wikipedia.org/wiki/Raw_audio_format>`__.
"""
assert convert_rate is None or convert_rate > 0, "Sample rate to convert to must be a positive integer"
assert convert_width is None or (convert_width % 1 == 0 and 1 <= convert_width <= 4), "Sample width to convert to must be between 1 and 4 inclusive"
raw_data = self.frame_data
# make sure unsigned 8-bit audio (which uses unsigned samples) is handled like higher sample width audio (which uses signed samples)
if self.sample_width == 1:
raw_data = audioop.bias(raw_data, 1, -128) # subtract 128 from every sample to make them act like signed samples
# resample audio at the desired rate if specified
if convert_rate is not None and self.sample_rate != convert_rate:
raw_data, _ = audioop.ratecv(raw_data, self.sample_width, 1, self.sample_rate, convert_rate, None)
# convert samples to desired sample width if specified
if convert_width is not None and self.sample_width != convert_width:
if convert_width == 3: # we're converting the audio into 24-bit (workaround for https://bugs.python.org/issue12866)
raw_data = audioop.lin2lin(raw_data, self.sample_width, 4) # convert audio into 32-bit first, which is always supported
try: audioop.bias(b"", 3, 0) # test whether 24-bit audio is supported (for example, ``audioop`` in Python 3.3 and below don't support sample width 3, while Python 3.4+ do)
except audioop.error: # this version of audioop doesn't support 24-bit audio (probably Python 3.3 or less)
raw_data = b"".join(raw_data[i + 1:i + 4] for i in range(0, len(raw_data), 4)) # since we're in little endian, we discard the first byte from each 32-bit sample to get a 24-bit sample
else: # 24-bit audio fully supported, we don't need to shim anything
raw_data = audioop.lin2lin(raw_data, self.sample_width, convert_width)
else:
raw_data = audioop.lin2lin(raw_data, self.sample_width, convert_width)
# if the output is 8-bit audio with unsigned samples, convert the samples we've been treating as signed to unsigned again
if convert_width == 1:
raw_data = audioop.bias(raw_data, 1, 128) # add 128 to every sample to make them act like unsigned samples again
return raw_data