def testavgpp(data): if verbose: print 'avgpp' if audioop.avgpp(data[0], 1) <> 0 or \ audioop.avgpp(data[1], 2) <> 0 or \ audioop.avgpp(data[2], 4) <> 0: return 0 return 1
def testavgpp(data): if verbose: print 'avgpp' if audioop.avgpp(data[0], 1) != 0 or \ audioop.avgpp(data[1], 2) != 0 or \ audioop.avgpp(data[2], 4) != 0: return 0 return 1
def test_avgpp(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.avgpp(b'', w), 0) self.assertEqual(audioop.avgpp(bytearray(), w), 0) self.assertEqual(audioop.avgpp(memoryview(b''), w), 0) self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0) self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) self.assertEqual(audioop.avgpp(datas[1], 1), 196) self.assertEqual(audioop.avgpp(datas[2], 2), 50534) self.assertEqual(audioop.avgpp(datas[3], 3), 12937096) self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002)
def test_avgpp(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.avgpp(b"", w), 0) self.assertEqual(audioop.avgpp(bytearray(), w), 0) self.assertEqual(audioop.avgpp(memoryview(b""), w), 0) self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0) self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) self.assertEqual(audioop.avgpp(datas[1], 1), 196) self.assertEqual(audioop.avgpp(datas[2], 2), 50534) self.assertEqual(audioop.avgpp(datas[3], 3), 12937096) self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002)
def run(self): while not self.done: data = self.queue.get() rms = audioop.rms(data, 2) rms_db = 20 * np.log10(rms) pp = 20 * np.log10(audioop.avgpp(data, 2)) x = np.fromstring(data, dtype='int16') nfft = x.shape[0] resolution = self.fs / nfft X = np.fft.rfft(x) self.on_data(20 * np.log10(np.abs(X))) base = self.ff / resolution F1 = np.abs(X[base]) Fh = 0 harmonic = 2 * base while harmonic < (nfft / 2): Fh += np.abs(X[harmonic]) harmonic += base thd = Fh / F1 print('RMS: {} dB, THD: {}, Peak: {} dB'.format(rms_db, thd, pp)) super(THD, self).put(data)
def levels(sound): buf = sound.tostring() rms = audioop.rms(buf, 2) / 32768.0 # signed peak = audioop.avgpp(buf, 2) / 32768.0 # signed r = [rms, rms] p = [peak, peak] d = [0, 0] return (r, p, d)
def obtenirAudioRAW(self, activat): print "comensanaudio" if activat: maxlen = 2 volumax = 3000 segonsilmax = 20 else: maxlen = 0.1 volumax = 5000 segonsilmax = 3 chunk = 2048 p = pyaudio.PyAudio() streamx = p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True,frames_per_buffer=chunk) audioenviar = [] rel = 16000/1024 #relacio bitrate i chunk, per saber quans segons d'audio agafar. prev_audio = deque(maxlen=maxlen * rel) comensat = False segonsilenci = 0 segonsilencimax = 0 while True: botostatus = self.db['BotoStatus'].find_one()['status'] if botostatus: break cur_data = streamx.read(1024) rmsTemp = audioop.avgpp(cur_data,2) if rmsTemp > volumax: if(not comensat): comensat = True audioenviar.append(cur_data) segonsilenci = 0 elif (comensat is True): if segonsilenci > segonsilmax: data = list(prev_audio) + audioenviar data = ''.join(data) break else: audioenviar.append(cur_data) segonsilenci = segonsilenci + 1 else: segonsilencimax = segonsilencimax + 1 prev_audio.append(cur_data) if activat: if segonsilencimax > 120: data = None break streamx.close() p.terminate() if botostatus: self.db['BotoStatus'].update({'id': None}, {'status': False}, upsert=True) self.interaccio = True self.getParaula() else: return data
def test_avgpp(self): for w in 1, 2, 4: self.assertEqual(audioop.avgpp(b'', w), 0) self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0) self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) self.assertEqual(audioop.avgpp(datas[1], 1), 196) self.assertEqual(audioop.avgpp(datas[2], 2), 50534) self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002)
def test_avgpp(self): for w in 1, 2, 4: self.assertEqual(audioop.avgpp(b"", w), 0) self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0) self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) self.assertEqual(audioop.avgpp(datas[1], 1), 196) self.assertEqual(audioop.avgpp(datas[2], 2), 50534) self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002)
def test_avgpp(self): self.assertEqual(audioop.avgpp(data[0], 1), 0) self.assertEqual(audioop.avgpp(data[1], 2), 0) self.assertEqual(audioop.avgpp(data[2], 4), 0)
def testavgpp(data): if audioop.avgpp(data[0], 1) <> 0 or \ audioop.avgpp(data[1], 2) <> 0 or \ audioop.avgpp(data[2], 4) <> 0: return 0 return 1
region_name='ap-south-1') bucket = s3.Bucket(input_bucket) # Downloading an audio file from s3 to local system bucket.download_file('****{wave file URL ob s3}****.wav', r'../path_for_local_system/recording.wav') with open(r'../../decibel_values.csv', 'a') as f: f.write('recording_name', 'sample_width', 'n_frames', 'avg', 'rms', 'avgpp', 'zero_crossings', 'maxpp', 'min_max') f.write( str(a, wav.getsampwidth(), wav.getnframes(), audioop.avg(string_wav, wav.getsampwidth()), audioop.rms(string_wav, wav.getsampwidth()), audioop.avgpp(string_wav, wav.getsampwidth()), audioop.cross(string_wav, wav.getsampwidth()), audioop.maxpp(string_wav, wav.getsampwidth()), audioop.minmax(string_wav, wav.getsampwidth()))) wav = wave.open(r'../../recordings.wav') string_wav = wav.readframes(wav.getnframes()) print('getsampwidth', wav.getsampwidth()) print('get n frmaes', wav.getnframes()) print('avg: ', audioop.avg(string_wav, wav.getsampwidth())) print('rms: ', audioop.rms(string_wav, wav.getsampwidth())) print('avgpp: ', audioop.avgpp(string_wav, wav.getsampwidth())) print('zero_crossings: ', audioop.cross(string_wav, wav.getsampwidth())) print('maxpp: ', audioop.maxpp(string_wav, wav.getsampwidth()))
from light_bar_controller import LightBarController from collections import deque bar = LightBarController() total_peak = 0 last = deque() logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) while True: var = sys.stdin.buffer.read(1024) peak = audioop.avgpp(var, 2) last.append(peak) if len(last) == 6: last.popleft() peak = int(sum(last) / len(last)) # In my testing, it's extreamly rare for an actual audio source to output # nothing. Thus, this is safe and we will only re-calibrate when switching # songs/audio sources if peak == 0 and total_peak != 0: total_peak = 0 logging.debug('0 / 0') bar.reset elif peak != 0: total_peak = max(peak, total_peak)
def calc_peak(buf): return audioop.avgpp(buf, 2) / 32768.0