我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用audioop.max()。
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 get_noise_value(self): """ Generate random value for all channels. """ new_left = uniform(self.min, self.max) new_right = uniform(self.min, self.max) new_mono = self.get_mono(new_left, new_right) left = self.get_channel(self.previous_left, new_left) right = self.get_channel(self.previous_right, new_right) mono = self.get_channel(self.previous_mono, new_mono) self.previous_left = new_left self.previous_right = new_right self.previous_mono = new_mono return (left, right, mono)
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 __db_level(self, rms_mode=False): """ Returns the average audio volume level measured in dB (range -60 db to 0 db) If the sample is stereo, you get back a tuple: (left_level, right_level) If the sample is mono, you still get a tuple but both values will be the same. This method is probably only useful if processed on very short sample fragments in sequence, so the db levels could be used to show a level meter for the duration of the sample. """ maxvalue = 2**(8*self.__samplewidth-1) if self.nchannels == 1: if rms_mode: peak_left = peak_right = (audioop.rms(self.__frames, self.__samplewidth)+1)/maxvalue else: peak_left = peak_right = (audioop.max(self.__frames, self.__samplewidth)+1)/maxvalue else: left_frames = audioop.tomono(self.__frames, self.__samplewidth, 1, 0) right_frames = audioop.tomono(self.__frames, self.__samplewidth, 0, 1) if rms_mode: peak_left = (audioop.rms(left_frames, self.__samplewidth)+1)/maxvalue peak_right = (audioop.rms(right_frames, self.__samplewidth)+1)/maxvalue else: peak_left = (audioop.max(left_frames, self.__samplewidth)+1)/maxvalue peak_right = (audioop.max(right_frames, self.__samplewidth)+1)/maxvalue # cut off at the bottom at -60 instead of all the way down to -infinity return max(20.0*math.log(peak_left, 10), -60.0), max(20.0*math.log(peak_right, 10), -60.0)
def modulate_amp(self, modulator): """ Perform amplitude modulation by another waveform or oscillator. You can use a Sample (or array of sample values) or an oscillator as modulator. If you use a Sample (or array), it will be cycled if needed and its maximum amplitude is scaled to be 1.0, effectively using it as if it was an oscillator. """ assert not self.__locked frames = self.get_frame_array() if isinstance(modulator, (Sample, list, array.array)): # modulator is a waveform, turn that into an 'oscillator' ran if isinstance(modulator, Sample): modulator = modulator.get_frame_array() biggest = max(max(modulator), abs(min(modulator))) modulator = (v/biggest for v in itertools.cycle(modulator)) else: modulator = iter(modulator) for i in range(len(frames)): frames[i] = int(frames[i] * next(modulator)) self.__frames = frames.tobytes() if sys.byteorder == "big": self.__frames = audioop.byteswap(self.__frames, self.__samplewidth) return self
def echo(self, length, amount, delay, decay): """ Adds the given amount of echos into the end of the sample, using a given length of sample data (from the end of the sample). The decay is the factor with which each echo is decayed in volume (can be >1 to increase in volume instead). If you use a very short delay the echos blend into the sound and the effect is more like a reverb. """ assert not self.__locked if amount > 0: length = max(0, self.duration - length) echo = self.copy() echo.__frames = self.__frames[self.frame_idx(length):] echo_amp = decay for _ in range(amount): if echo_amp < 1.0/(2**(8*self.__samplewidth-1)): # avoid computing echos that you can't hear break length += delay echo = echo.copy().amplify(echo_amp) self.mix_at(length, echo) echo_amp *= decay return self
def test_max(self): self.assertEqual(audioop.max(data[0], 1), 2) self.assertEqual(audioop.max(data[1], 2), 2) self.assertEqual(audioop.max(data[2], 4), 2)
def __init__(self, c): """ Initializer :param c: configuration dictionary """ config = c[DATA_SOURCE] self.left_channel = Queue(maxsize=1) self.right_channel = Queue(maxsize=1) self.mono_channel = Queue(maxsize=1) self.mono_algorithm = config[MONO_ALGORITHM] self.stereo_algorithm = config[STEREO_ALGORITHM] self.ds_type = config[TYPE] self.const = config[VOLUME_CONSTANT] self.bits = 16 self.pipe_name = config[PIPE_NAME] self.min = config[VOLUME_MIN] self.max = config[VOLUME_MAX] self.v = 0 self.step = config[STEP] self.pipe_size = config[PIPE_SIZE] self.rng = list(range(int(self.min), int(self.max))) self.double_rng = self.rng self.double_rng.extend(range(int(self.max) - 1, int(self.min), -1)) self.pipe = None if self.ds_type == SOURCE_PIPE: try: self.pipe = os.open(self.pipe_name, os.O_RDONLY) except: logging.debug("cannot open pipe") self.k = int(math.pow(2, self.bits)//2 - 1) self.previous_left = self.previous_right = self.previous_mono = 0.0 self.run_flag = True self.polling_interval = config[POLLING_INTERVAL] self.prev_time = None
def get_saw_value(self): """ Generate saw shape signal. """ s = (self.rng[self.v], self.rng[self.v], self.rng[self.v]) self.v = (self.v + self.step) % self.max return s
def get_triangle_value(self): """ Generate triangle shape signal. """ t = (self.double_rng[self.v], self.double_rng[self.v], self.double_rng[self.v]) self.v = (self.v + self.step) % (int(self.max * 2 - 1)) return t
def get_sine_value(self): """ Generate sine shape signal. """ a = int(self.max * ((1 + math.sin(math.radians(-90 + self.v)))/2)) s = (a, a, a) self.v = (self.v + self.step * 6) % 360 return s
def get_mono(self, left, right): """ Create mono signal from stereo using particular algorithm """ if self.mono_algorithm == MONO_ALGORITHM_MAXIMUM: mono = max(left, right) elif self.mono_algorithm == MONO_ALGORITHM_AVERAGE: mono = statistics.mean([left, right]) return mono
def test_max(self): for w in 1, 2, 4: self.assertEqual(audioop.max(b'', w), 0) p = packs[w] self.assertEqual(audioop.max(p(5), w), 5) self.assertEqual(audioop.max(p(5, -8, -1), w), 8) self.assertEqual(audioop.max(p(maxvalues[w]), w), maxvalues[w]) self.assertEqual(audioop.max(p(minvalues[w]), w), -minvalues[w]) self.assertEqual(audioop.max(datas[w], w), -minvalues[w])
def capture_audio(): inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NORMAL) inp.setchannels(1) inp.setrate(16000) inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) inp.setperiodsize(512) loops=290 silence_counter = 80 silence_thershold = 2500 rf = open(path + 'recording.raw', 'w') while loops > 0: loops -= 1 l, data = inp.read() print audioop.max(data,2) if audioop.max(data,2) < silence_thershold: silence_counter -= 1 if silence_counter == 0: print "Silence detected " break else: silence_counter=60 if l: rf.write(data) rf.close()
def test_max(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.max(b'', w), 0) self.assertEqual(audioop.max(bytearray(), w), 0) self.assertEqual(audioop.max(memoryview(b''), w), 0) p = packs[w] self.assertEqual(audioop.max(p(5), w), 5) self.assertEqual(audioop.max(p(5, -8, -1), w), 8) self.assertEqual(audioop.max(p(maxvalues[w]), w), maxvalues[w]) self.assertEqual(audioop.max(p(minvalues[w]), w), -minvalues[w]) self.assertEqual(audioop.max(datas[w], w), -minvalues[w])
def maximum(self): return audioop.max(self.__frames, self.samplewidth)
def amplify_max(self): """Amplify the sample to maximum volume without clipping or overflow happening.""" assert not self.__locked max_amp = audioop.max(self.__frames, self.samplewidth) max_target = 2 ** (8 * self.samplewidth - 1) - 2 if max_amp > 0: factor = max_target/max_amp self.__frames = audioop.mul(self.__frames, self.samplewidth, factor) return self