本文整理汇总了Python中audioop.getsample方法的典型用法代码示例。如果您正苦于以下问题:Python audioop.getsample方法的具体用法?Python audioop.getsample怎么用?Python audioop.getsample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类audioop
的用法示例。
在下文中一共展示了audioop.getsample方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_issue7673
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [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_string
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [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)
示例3: fadeout
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def fadeout(self, seconds: float, target_volume: float = 0.0) -> 'Sample':
"""Fade the end of the sample out to the target volume (usually zero) in the given time."""
if self.__locked:
raise RuntimeError("cannot modify a locked sample")
if not self.__frames:
return self
seconds = min(seconds, self.duration)
i = self.frame_idx(self.duration-seconds)
begin = self.__frames[:i]
end = self.__frames[i:] # we fade this chunk
numsamples = len(end)/self.__samplewidth
decrease = 1.0-target_volume
_sw = self.__samplewidth # optimization
_getsample = audioop.getsample # optimization
faded = Sample.get_array(_sw, [int(_getsample(end, _sw, i)*(1.0-i*decrease/numsamples)) for i in range(int(numsamples))])
end = faded.tobytes()
if sys.byteorder == "big":
end = audioop.byteswap(end, self.__samplewidth)
self.__frames = begin + end
return self
示例4: fadein
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def fadein(self, seconds: float, start_volume: float = 0.0) -> 'Sample':
"""Fade the start of the sample in from the starting volume (usually zero) in the given time."""
if self.__locked:
raise RuntimeError("cannot modify a locked sample")
if not self.__frames:
return self
seconds = min(seconds, self.duration)
i = self.frame_idx(seconds)
begin = self.__frames[:i] # we fade this chunk
end = self.__frames[i:]
numsamples = len(begin)/self.__samplewidth
increase = 1.0-start_volume
_sw = self.__samplewidth # optimization
_getsample = audioop.getsample # optimization
_incr = increase/numsamples # optimization
faded = Sample.get_array(_sw, [int(_getsample(begin, _sw, i)*(i*_incr+start_volume)) for i in range(int(numsamples))])
begin = faded.tobytes()
if sys.byteorder == "big":
begin = audioop.byteswap(begin, self.__samplewidth)
self.__frames = begin + end
return self
示例5: test_getsample
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def test_getsample(self):
for w in 1, 2, 4:
data = packs[w](0, 1, -1, maxvalues[w], minvalues[w])
self.assertEqual(audioop.getsample(data, w, 0), 0)
self.assertEqual(audioop.getsample(data, w, 1), 1)
self.assertEqual(audioop.getsample(data, w, 2), -1)
self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w])
self.assertEqual(audioop.getsample(data, w, 4), minvalues[w])
示例6: test_getsample
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def test_getsample(self):
for w in 1, 2, 3, 4:
data = packs[w](0, 1, -1, maxvalues[w], minvalues[w])
self.assertEqual(audioop.getsample(data, w, 0), 0)
self.assertEqual(audioop.getsample(bytearray(data), w, 0), 0)
self.assertEqual(audioop.getsample(memoryview(data), w, 0), 0)
self.assertEqual(audioop.getsample(data, w, 1), 1)
self.assertEqual(audioop.getsample(data, w, 2), -1)
self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w])
self.assertEqual(audioop.getsample(data, w, 4), minvalues[w])
示例7: gendata2
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def gendata2():
if verbose:
print 'getsample'
if audioop.getsample('\0\1', 2, 0) == 1:
return '\0\0\0\1\0\2'
else:
return '\0\0\1\0\2\0'
示例8: gendata4
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def gendata4():
if verbose:
print 'getsample'
if audioop.getsample('\0\0\0\1', 4, 0) == 1:
return '\0\0\0\0\0\0\0\1\0\0\0\2'
else:
return '\0\0\0\0\1\0\0\0\2\0\0\0'
示例9: testgetsample
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def testgetsample(data):
if verbose:
print 'getsample'
for i in range(3):
if audioop.getsample(data[0], 1, i) != i or \
audioop.getsample(data[1], 2, i) != i or \
audioop.getsample(data[2], 4, i) != i:
return 0
return 1
示例10: recordAudioSegments
# 需要导入模块: import audioop [as 别名]
# 或者: from audioop import getsample [as 别名]
def recordAudioSegments(RecordPath, BLOCKSIZE):
# This function is used for recording audio segments (until ctr+c is pressed)
# ARGUMENTS:
# - RecordPath: the path where the wav segments will be stored
# - BLOCKSIZE: segment recording size (in seconds)
#
# NOTE: filenames are based on clock() value
print "Press Ctr+C to stop recording"
RecordPath += os.sep
d = os.path.dirname(RecordPath)
if os.path.exists(d) and RecordPath!=".":
shutil.rmtree(RecordPath)
os.makedirs(RecordPath)
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
inp.setchannels(1)
inp.setrate(Fs)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(512)
midTermBufferSize = int(Fs*BLOCKSIZE)
midTermBuffer = []
curWindow = []
elapsedTime = "%08.3f" % (time.time())
while 1:
l,data = inp.read()
if l:
for i in range(len(data)/2):
curWindow.append(audioop.getsample(data, 2, i))
if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
else:
samplesToCopyToMidBuffer = len(curWindow)
midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer];
del(curWindow[0:samplesToCopyToMidBuffer])
if len(midTermBuffer) == midTermBufferSize:
# allData = allData + midTermBuffer
curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"
midTermBufferArray = numpy.int16(midTermBuffer)
wavfile.write(curWavFileName, Fs, midTermBufferArray)
print "AUDIO OUTPUT: Saved " + curWavFileName
midTermBuffer = []
elapsedTime = "%08.3f" % (time.time())