我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用audioop.lin2lin()。
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)
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))
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)
def normalize(self): """ 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. """ assert not self.__locked self.resample(self.norm_samplerate) if self.samplewidth != self.norm_samplewidth: # Convert to 16 bit sample size. self.__frames = audioop.lin2lin(self.__frames, self.samplewidth, self.norm_samplewidth) self.__samplewidth = self.norm_samplewidth if self.nchannels == 1: # convert to stereo self.__frames = audioop.tostereo(self.__frames, self.samplewidth, 1, 1) self.__nchannels = 2 return self
def test_lin2lin(self): # too simple: we test only the size for d1 in data: for d2 in data: got = len(d1)//3 wtd = len(d2)//3 self.assertEqual(len(audioop.lin2lin(d1, got, wtd)), len(d2))
def send_voice(self, wav, target, terminated): packet = bytearray() packet.append(self.outgoing_type.value << 5 | target.value) packet.extend(self._encode_varint(self.outgoing_sequence_number)) nchannels, sampwidth, framerate, n, _, _ = wav.getparams() logger.debug('Sending audio: %d channels, %d-bit, %dhz, %d frames', nchannels, sampwidth * 8, framerate, n) for i in range(n): pcm = wav.readframes(1) if sampwidth != 2: pcm = audioop.lin2lin(pcm, sampwidth, 2) if nchannels == 2: pcm = audioop.tomono(pcm, 2, 0.5, 0.5) if framerate != 48000: pcm, _ = audioop.ratecv(pcm, 2, 1, framerate, 48000, None) frame = self.outgoing_codec.encoder.encode(pcm) if self.outgoing_type == self.PacketType.VOICE_OPUS: #packet.extend(self._make_opus_header(frame)) # TODO: figure out opus pass else: packet.extend(self._make_celt_header(frame, True)) if i == n - 1 and terminated: packet.extend(self._make_celt_header(b'', False)) print(packet) self.transport.sendto(bytes(packet)) self.outgoing_sequence_number += 1
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))
def get_32bit_frames(self, scale_amplitude=True): """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) 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
def make_16bit(self, maximize_amplitude=True): """ 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. """ assert not self.__locked 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
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