本文整理汇总了Python中audioop.ratecv方法的典型用法代码示例。如果您正苦于以下问题:Python audioop.ratecv方法的具体用法?Python audioop.ratecv怎么用?Python audioop.ratecv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类audioop
的用法示例。
在下文中一共展示了audioop.ratecv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_issue7673
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [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)
示例2: test_ratecv
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
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)
示例3: test_string
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [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: _write_frames_to_file
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def _write_frames_to_file(self, frames):
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(16000)
if self._input_rate == 16000:
wav_fp.writeframes(''.join(frames))
else:
wav_fp.writeframes(audioop.ratecv(''.join(frames),
int(self._input_bits/8),
self._input_channels,
self._input_rate,
16000,
None)
[0])
wav_fp.close()
f.seek(0)
yield f
示例5: mic_to_ws
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def mic_to_ws(): # uses stream
try:
print >> sys.stderr, "\nLISTENING TO MICROPHONE"
last_state = None
while True:
data = stream.read(self.chunk)
if self.audio_gate > 0:
rms = audioop.rms(data, 2)
if rms < self.audio_gate:
data = '\00' * len(data)
#if sample_chan == 2:
# data = audioop.tomono(data, 2, 1, 1)
if sample_rate != self.byterate:
(data, last_state) = audioop.ratecv(data, 2, 1, sample_rate, self.byterate, last_state)
self.send_data(data)
except IOError, e:
# usually a broken pipe
print e
示例6: resample_to_default_sample_rate
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def resample_to_default_sample_rate(self, pcm, sample_rate):
if sample_rate != self.sample_rate:
pcm, state = audioop.ratecv(pcm, 2, 1, sample_rate, self.sample_rate, None)
return pcm
示例7: _convert_file
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
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)
示例8: resample
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def resample(self, samplerate: int) -> 'Sample':
"""
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.
"""
if self.__locked:
raise RuntimeError("cannot modify a locked sample")
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
示例9: speed
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def speed(self, speed: float) -> 'Sample':
"""
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.
"""
if self.__locked:
raise RuntimeError("cannot modify a locked sample")
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
示例10: speech_to_text
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def speech_to_text(self, audio_frames):
with self.stt.start_utterance():
(resampled, _) = audioop.ratecv(audio_frames, self.WIDTH, self.CHANNELS, self.SAMPLE_RATE, self.TARGET_RATE, None)
self.stt.process_raw(resampled, False, False)
return self.stt.hypothesis()
示例11: main
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
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)))
示例12: testratecv
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def testratecv(data):
if verbose:
print 'ratecv'
state = None
d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
if d1 + d2 != '\000\000\001\001\002\001\000\000\001\001\002':
return 0
return 1
示例13: convert_framerate
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
def convert_framerate(fragment, width, nchannels, framerate_in, framerate_out):
"""
Convert framerate (sampling rate) of the input fragment.
Parameters
----------
fragment : bytes object
Specifies the original fragment.
width : int
Specifies the fragment's original sampwidth.
nchannels : int
Specifies the fragment's original nchannels.
framerate_in : int
Specifies the fragment's original framerate.
framerate_out : int
Specifies the fragment's desired framerate.
Returns
-------
bytes
Converted audio with the desired framerate 'framerate_out'.
"""
if framerate_in == framerate_out:
return fragment
new_fragment, _ = audioop.ratecv(fragment, width, nchannels, framerate_in, framerate_out, None)
return new_fragment
示例14: get_raw_data
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [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
示例15: test_ratecv
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import ratecv [as 别名]
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])
self.assertRaises(TypeError, audioop.ratecv, b'', 1, 1, 8000, 8000, 42)
self.assertRaises(TypeError, audioop.ratecv,
b'', 1, 1, 8000, 8000, (1, (42,)))