我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用audioop.add()。
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_add(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.add(b'', b'', w), b'') self.assertEqual(audioop.add(bytearray(), bytearray(), w), b'') self.assertEqual(audioop.add(memoryview(b''), memoryview(b''), w), b'') self.assertEqual(audioop.add(datas[w], b'\0' * len(datas[w]), w), datas[w]) self.assertEqual(audioop.add(datas[1], datas[1], 1), b'\x00\x24\x7f\x80\x7f\x80\xfe') self.assertEqual(audioop.add(datas[2], datas[2], 2), packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2)) self.assertEqual(audioop.add(datas[3], datas[3], 3), packs[3](0, 0x2468ac, 0x7fffff, -0x800000, 0x7fffff, -0x800000, -2)) self.assertEqual(audioop.add(datas[4], datas[4], 4), packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000, 0x7fffffff, -0x80000000, -2))
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 mix(self, other, other_seconds=None, pad_shortest=True): """ Mix another sample into the current sample. You can limit the length taken from the other sample. When pad_shortest is False, no sample length adjustment is done. """ assert not self.__locked assert self.samplewidth == other.samplewidth assert self.samplerate == other.samplerate assert self.nchannels == other.nchannels frames1 = self.__frames if other_seconds: frames2 = other.__frames[:other.frame_idx(other_seconds)] else: frames2 = other.__frames if pad_shortest: if len(frames1) < len(frames2): frames1 += b"\0"*(len(frames2)-len(frames1)) elif len(frames2) < len(frames1): frames2 += b"\0"*(len(frames1)-len(frames2)) self.__frames = audioop.add(frames1, frames2, self.samplewidth) return self
def mix_at(self, seconds, other, other_seconds=None): """ Mix another sample into the current sample at a specific time point. You can limit the length taken from the other sample. """ if seconds == 0.0: return self.mix(other, other_seconds) assert not self.__locked assert self.samplewidth == other.samplewidth assert self.samplerate == other.samplerate assert self.nchannels == other.nchannels start_frame_idx = self.frame_idx(seconds) if other_seconds: other_frames = other.__frames[:other.frame_idx(other_seconds)] else: other_frames = other.__frames # Mix the frames. Unfortunately audioop requires splitting and copying the sample data, which is slow. pre, to_mix, post = self._mix_split_frames(len(other_frames), start_frame_idx) self.__frames = None # allow for garbage collection mixed = audioop.add(to_mix, other_frames, self.samplewidth) del to_mix # more garbage collection self.__frames = self._mix_join_frames(pre, mixed, post) return self
def test_add(self): data2 = [] for d in data: str = bytearray(len(d)) for i,b in enumerate(d): str[i] = 2*b data2.append(str) self.assertEqual(audioop.add(data[0], data[0], 1), data2[0]) self.assertEqual(audioop.add(data[1], data[1], 2), data2[1]) self.assertEqual(audioop.add(data[2], data[2], 4), data2[2])
def writeframes(self, data): import time from Carbon.Sound import bufferCmd, callBackCmd, extSH import struct import MacOS if not self._chan: from Carbon import Snd self._chan = Snd.SndNewChannel(5, 0, self._callback) nframes = len(data) / self._nchannels / self._sampwidth if len(data) != nframes * self._nchannels * self._sampwidth: raise error, 'data is not a whole number of frames' while self._gc and \ self.getfilled() + nframes > \ self._qsize / self._nchannels / self._sampwidth: time.sleep(0.1) if self._sampwidth == 1: import audioop data = audioop.add(data, '\x80'*len(data), 1) h1 = struct.pack('llHhllbbl', id(data)+MacOS.string_id_to_buffer, self._nchannels, self._outrate, 0, 0, 0, extSH, 60, nframes) h2 = 22*'\0' h3 = struct.pack('hhlll', self._sampwidth*8, 0, 0, 0, 0) header = h1+h2+h3 self._gc.append((header, data)) self._chan.SndDoCommand((bufferCmd, 0, header), 0) self._chan.SndDoCommand((callBackCmd, 0, 0), 0)
def test_add(self): for w in 1, 2, 4: self.assertEqual(audioop.add(b'', b'', w), b'') self.assertEqual(audioop.add(datas[w], b'\0' * len(datas[w]), w), datas[w]) self.assertEqual(audioop.add(datas[1], datas[1], 1), b'\x00\x24\x7f\x80\x7f\x80\xfe') self.assertEqual(audioop.add(datas[2], datas[2], 2), packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2)) self.assertEqual(audioop.add(datas[4], datas[4], 4), packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000, 0x7fffffff, -0x80000000, -2))