我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用audioop.bias()。
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_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 test_bias(self): # Note: this test assumes that avg() works d1 = audioop.bias(data[0], 1, 100) d2 = audioop.bias(data[1], 2, 100) d4 = audioop.bias(data[2], 4, 100) self.assertEqual(audioop.avg(d1, 1), 101) self.assertEqual(audioop.avg(d2, 2), 101) self.assertEqual(audioop.avg(d4, 4), 101)
def test_bias(self): for w in 1, 2, 4: for bias in 0, 1, -1, 127, -128, 0x7fffffff, -0x80000000: self.assertEqual(audioop.bias(b'', w, bias), b'') self.assertEqual(audioop.bias(datas[1], 1, 1), b'\x01\x13\x46\xbc\x80\x81\x00') self.assertEqual(audioop.bias(datas[1], 1, -1), b'\xff\x11\x44\xba\x7e\x7f\xfe') self.assertEqual(audioop.bias(datas[1], 1, 0x7fffffff), b'\xff\x11\x44\xba\x7e\x7f\xfe') self.assertEqual(audioop.bias(datas[1], 1, -0x80000000), datas[1]) self.assertEqual(audioop.bias(datas[2], 2, 1), packs[2](1, 0x1235, 0x4568, -0x4566, -0x8000, -0x7fff, 0)) self.assertEqual(audioop.bias(datas[2], 2, -1), packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) self.assertEqual(audioop.bias(datas[2], 2, 0x7fffffff), packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) self.assertEqual(audioop.bias(datas[2], 2, -0x80000000), datas[2]) self.assertEqual(audioop.bias(datas[4], 4, 1), packs[4](1, 0x12345679, 0x456789ac, -0x456789aa, -0x80000000, -0x7fffffff, 0)) self.assertEqual(audioop.bias(datas[4], 4, -1), packs[4](-1, 0x12345677, 0x456789aa, -0x456789ac, 0x7ffffffe, 0x7fffffff, -2)) self.assertEqual(audioop.bias(datas[4], 4, 0x7fffffff), packs[4](0x7fffffff, -0x6dcba989, -0x3a987656, 0x3a987654, -2, -1, 0x7ffffffe)) self.assertEqual(audioop.bias(datas[4], 4, -0x80000000), packs[4](-0x80000000, -0x6dcba988, -0x3a987655, 0x3a987655, -1, 0, 0x7fffffff))
def bias(self, bias): """Add a bias constant to each sample value.""" assert not self.__locked self.__frames = audioop.bias(self.__frames, self.__samplewidth, bias) 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
def test_bias(self): for w in 1, 2, 3, 4: for bias in 0, 1, -1, 127, -128, 0x7fffffff, -0x80000000: self.assertEqual(audioop.bias(b'', w, bias), b'') self.assertEqual(audioop.bias(bytearray(), w, bias), b'') self.assertEqual(audioop.bias(memoryview(b''), w, bias), b'') self.assertEqual(audioop.bias(datas[1], 1, 1), b'\x01\x13\x46\xbc\x80\x81\x00') self.assertEqual(audioop.bias(datas[1], 1, -1), b'\xff\x11\x44\xba\x7e\x7f\xfe') self.assertEqual(audioop.bias(datas[1], 1, 0x7fffffff), b'\xff\x11\x44\xba\x7e\x7f\xfe') self.assertEqual(audioop.bias(datas[1], 1, -0x80000000), datas[1]) self.assertEqual(audioop.bias(datas[2], 2, 1), packs[2](1, 0x1235, 0x4568, -0x4566, -0x8000, -0x7fff, 0)) self.assertEqual(audioop.bias(datas[2], 2, -1), packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) self.assertEqual(audioop.bias(datas[2], 2, 0x7fffffff), packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) self.assertEqual(audioop.bias(datas[2], 2, -0x80000000), datas[2]) self.assertEqual(audioop.bias(datas[3], 3, 1), packs[3](1, 0x123457, 0x45678a, -0x456788, -0x800000, -0x7fffff, 0)) self.assertEqual(audioop.bias(datas[3], 3, -1), packs[3](-1, 0x123455, 0x456788, -0x45678a, 0x7ffffe, 0x7fffff, -2)) self.assertEqual(audioop.bias(datas[3], 3, 0x7fffffff), packs[3](-1, 0x123455, 0x456788, -0x45678a, 0x7ffffe, 0x7fffff, -2)) self.assertEqual(audioop.bias(datas[3], 3, -0x80000000), datas[3]) self.assertEqual(audioop.bias(datas[4], 4, 1), packs[4](1, 0x12345679, 0x456789ac, -0x456789aa, -0x80000000, -0x7fffffff, 0)) self.assertEqual(audioop.bias(datas[4], 4, -1), packs[4](-1, 0x12345677, 0x456789aa, -0x456789ac, 0x7ffffffe, 0x7fffffff, -2)) self.assertEqual(audioop.bias(datas[4], 4, 0x7fffffff), packs[4](0x7fffffff, -0x6dcba989, -0x3a987656, 0x3a987654, -2, -1, 0x7ffffffe)) self.assertEqual(audioop.bias(datas[4], 4, -0x80000000), packs[4](-0x80000000, -0x6dcba988, -0x3a987655, 0x3a987655, -1, 0, 0x7fffffff))
def __enter__(self): assert self.stream is None, "This audio source is already inside a context manager" try: # attempt to read the file as WAV self.audio_reader = wave.open(self.filename_or_fileobject, "rb") self.little_endian = True # RIFF WAV is a little-endian format (most ``audioop`` operations assume that the frames are stored in little-endian form) except wave.Error: try: # attempt to read the file as AIFF self.audio_reader = aifc.open(self.filename_or_fileobject, "rb") self.little_endian = False # AIFF is a big-endian format except aifc.Error: # attempt to read the file as FLAC if hasattr(self.filename_or_fileobject, "read"): flac_data = self.filename_or_fileobject.read() else: with open(self.filename_or_fileobject, "rb") as f: flac_data = f.read() # run the FLAC converter with the FLAC data to get the AIFF data flac_converter = get_flac_converter() process = subprocess.Popen([ flac_converter, "--stdout", "--totally-silent", # put the resulting AIFF file in stdout, and make sure it's not mixed with any program output "--decode", "--force-aiff-format", # decode the FLAC file into an AIFF file "-", # the input FLAC file contents will be given in stdin ], stdin=subprocess.PIPE, stdout=subprocess.PIPE) aiff_data, stderr = process.communicate(flac_data) aiff_file = io.BytesIO(aiff_data) try: self.audio_reader = aifc.open(aiff_file, "rb") except aifc.Error: assert False, "Audio file could not be read as WAV, AIFF, or FLAC; check if file is corrupted" self.little_endian = False # AIFF is a big-endian format assert 1 <= self.audio_reader.getnchannels() <= 2, "Audio must be mono or stereo" self.SAMPLE_WIDTH = self.audio_reader.getsampwidth() # 24-bit audio needs some special handling for old Python versions (workaround for https://bugs.python.org/issue12866) samples_24_bit_pretending_to_be_32_bit = False if self.SAMPLE_WIDTH == 3: # 24-bit audio try: audioop.bias(b"", self.SAMPLE_WIDTH, 0) # test whether this sample width 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) samples_24_bit_pretending_to_be_32_bit = True # while the ``AudioFile`` instance will outwardly appear to be 32-bit, it will actually internally be 24-bit self.SAMPLE_WIDTH = 4 # the ``AudioFile`` instance should present itself as a 32-bit stream now, since we'll be converting into 32-bit on the fly when reading self.SAMPLE_RATE = self.audio_reader.getframerate() self.CHUNK = 4096 self.FRAME_COUNT = self.audio_reader.getnframes() self.DURATION = self.FRAME_COUNT / float(self.SAMPLE_RATE) self.stream = AudioFile.AudioFileStream(self.audio_reader, self.little_endian, samples_24_bit_pretending_to_be_32_bit) return self