我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用audioop.ratecv()。
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_ratecv(self): for w in 1, 2, 4: self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None), (b'', (-1, ((0, 0),) * 5))) self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None), (b'', (-2, ((0, 0),)))) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0], datas[w]) state = None d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') for w in 1, 2, 4: d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None) d, state = b'', None for i in range(0, len(datas[w]), w): d1, state = audioop.ratecv(datas[w][i:i + w], w, 1, 8000, 16000, state) d += d1 self.assertEqual(d, d0) self.assertEqual(state, state0)
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 _write_frames_to_file(self, frames, framerate, volume): with tempfile.NamedTemporaryFile(mode='w+b') as f: wav_fp = wave.open(f, 'wb') wav_fp.setnchannels(self._input_channels) wav_fp.setsampwidth(int(self._input_bits / 8)) wav_fp.setframerate(framerate) if self._input_rate == framerate: fragment = ''.join(frames) else: fragment = audioop.ratecv(''.join(frames), int(self._input_bits / 8), self._input_channels, self._input_rate, framerate, None)[0] if volume is not None: maxvolume = audioop.minmax(fragment, self._input_bits / 8)[1] fragment = audioop.mul( fragment, int(self._input_bits / 8), volume * (2. ** 15) / maxvolume) wav_fp.writeframes(fragment) wav_fp.close() f.seek(0) yield f
def test_ratecv(self): state = None d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002')
def test_ratecv(self): for w in 1, 2, 4: self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None), (b'', (-1, ((0, 0),) * 5))) self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None), (b'', (-2, ((0, 0),)))) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0], datas[w]) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 1, 0)[0], datas[w]) state = None d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') for w in 1, 2, 4: d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None) d, state = b'', None for i in range(0, len(datas[w]), w): d1, state = audioop.ratecv(datas[w][i:i + w], w, 1, 8000, 16000, state) d += d1 self.assertEqual(d, d0) self.assertEqual(state, state0) expected = { 1: packs[1](0, 0x0d, 0x37, -0x26, 0x55, -0x4b, -0x14), 2: packs[2](0, 0x0da7, 0x3777, -0x2630, 0x5673, -0x4a64, -0x129a), 4: packs[4](0, 0x0da740da, 0x37777776, -0x262fc962, 0x56740da6, -0x4a62fc96, -0x1298bf26), } for w in 1, 2, 4: self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 3, 1)[0], expected[w]) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 30, 10)[0], expected[w])
def _convert_file(self, src, dest=None): """ convert wav into 8khz rate """ def convert(read,write): write.setparams((1, 2, 8000, 0,'NONE', 'not compressed')) o_fr = read.getframerate() o_chnl = read.getnchannels() t_fr = read.getnframes() data = read.readframes(t_fr) cnvrt = audioop.ratecv(data, 2, o_chnl, o_fr, 8000, None) if o_chnl != 1: mono = audioop.tomono(cnvrt[0], 2, 1, 0) write.writeframes(mono) else: write.writeframes(cnvrt[0]) read.close() write.close() if dest is None: temp = src + '.temp' os.rename(src, temp) read = wave.open(temp, 'r') write = wave.open(src, 'w') convert(read, write) os.remove(temp) else: read = wave.open(src, 'r') write = wave.open(dest, 'w') convert(read, write)
def _next_chunk(self): chunk = await self._src_block.__anext__() new_aud, self._state = audioop.ratecv(chunk.audio, 2, self._n_channels, chunk.freq, self._out_rate, self._state) return AudioChunk(chunk.start_time, new_aud, 2, self._out_rate)
def open_input(filename): stream = wave.open(filename,"rb") input_num_channels = stream.getnchannels() input_sample_rate = stream.getframerate() input_sample_width = stream.getsampwidth() input_num_frames = stream.getnframes() raw_data = stream.readframes(input_num_frames) # Returns byte data stream.close() total_samples = input_num_frames * input_num_channels print "Sample Width: {} ({}-bit)".format(input_sample_width, 8 * input_sample_width) print "Number of Channels: " + str(input_num_channels) print "Sample Rate " + str(input_sample_rate) print "Number of Samples: " + str(total_samples) print "Duration: {0:.2f}s".format(total_samples / float(input_sample_rate)) print "Raw Data Size: " + str(len(raw_data)) if input_sample_rate != SAMPLE_RATE: u_law = audioop.ratecv(raw_data, input_sample_width, input_num_channels, input_sample_rate, SAMPLE_RATE, None) u_law = audioop.lin2ulaw(u_law[0], input_sample_width) else: u_law = audioop.lin2ulaw(raw_data, input_sample_width) u_law = list(u_law) u_law = [ord(x)//Q_FACTOR for x in u_law] return np.asarray(u_law)
def main(): sample_rate = 48000 channels = 2 N = 4096 * 4 mic = Microphone(sample_rate, channels) window = np.hanning(N) sound_speed = 343.2 distance = 0.14 max_tau = distance / sound_speed def signal_handler(sig, num): print('Quit') mic.close() signal.signal(signal.SIGINT, signal_handler) for data in mic.read_chunks(N): buf = np.fromstring(data, dtype='int16') mono = buf[0::channels].tostring() if sample_rate != 16000: mono, _ = audioop.ratecv(mono, 2, 1, sample_rate, 16000, None) if vad.is_speech(mono): tau, _ = gcc_phat(buf[0::channels]*window, buf[1::channels]*window, fs=sample_rate, max_tau=max_tau) theta = math.asin(tau / max_tau) * 180 / math.pi print('\ntheta: {}'.format(int(theta)))
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_ratecv(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(bytearray(), w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(memoryview(b''), w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None), (b'', (-1, ((0, 0),) * 5))) self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None), (b'', (-2, ((0, 0),)))) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0], datas[w]) state = None d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') for w in 1, 2, 3, 4: d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None) d, state = b'', None for i in range(0, len(datas[w]), w): d1, state = audioop.ratecv(datas[w][i:i + w], w, 1, 8000, 16000, state) d += d1 self.assertEqual(d, d0) self.assertEqual(state, state0)
def resample(self, samplerate): """ Resamples to a different sample rate, without changing the pitch and duration of the sound. The algorithm used is simple, and it will cause a loss of sound quality. """ assert not self.__locked if samplerate == self.__samplerate: return self self.__frames = audioop.ratecv(self.__frames, self.samplewidth, self.nchannels, self.samplerate, samplerate, None)[0] self.__samplerate = samplerate return self
def speed(self, speed): """ Changes the playback speed of the sample, without changing the sample rate. This will change the pitch and duration of the sound accordingly. The algorithm used is simple, and it will cause a loss of sound quality. """ assert not self.__locked assert speed > 0 if speed == 1.0: return self rate = self.samplerate self.__frames = audioop.ratecv(self.__frames, self.samplewidth, self.nchannels, int(self.samplerate*speed), rate, None)[0] self.__samplerate = rate 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_ratecv(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(bytearray(), w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(memoryview(b''), w, 1, 8000, 8000, None), (b'', (-1, ((0, 0),)))) self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None), (b'', (-1, ((0, 0),) * 5))) self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None), (b'', (-2, ((0, 0),)))) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0], datas[w]) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 1, 0)[0], datas[w]) state = None d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') for w in 1, 2, 3, 4: d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None) d, state = b'', None for i in range(0, len(datas[w]), w): d1, state = audioop.ratecv(datas[w][i:i + w], w, 1, 8000, 16000, state) d += d1 self.assertEqual(d, d0) self.assertEqual(state, state0) expected = { 1: packs[1](0, 0x0d, 0x37, -0x26, 0x55, -0x4b, -0x14), 2: packs[2](0, 0x0da7, 0x3777, -0x2630, 0x5673, -0x4a64, -0x129a), 3: packs[3](0, 0x0da740, 0x377776, -0x262fca, 0x56740c, -0x4a62fd, -0x1298c0), 4: packs[4](0, 0x0da740da, 0x37777776, -0x262fc962, 0x56740da6, -0x4a62fc96, -0x1298bf26), } for w in 1, 2, 3, 4: self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 3, 1)[0], expected[w]) self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 30, 10)[0], expected[w])