From b0e4afbdd5d541ed8355b7aa081ee193698c9476 Mon Sep 17 00:00:00 2001 From: Andy Carluccio <45571628+AndrewCarluccio@users.noreply.github.com> Date: Wed, 11 Aug 2021 00:35:06 -0400 Subject: [PATCH] Removed WAV Buffer Using numpy, I removed the need for the WAV intermediary buffer on disk. Now PyAudio streams directly to PyLoudNorm, producing a much more stable / elegant workflow. --- OSCLufs.py | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/OSCLufs.py b/OSCLufs.py index f538926..8f9006a 100644 --- a/OSCLufs.py +++ b/OSCLufs.py @@ -1,9 +1,11 @@ #OSCLufs -#Originally Created by Programming for People -#Maintained and updated by Andy Carluccio -#Liminal Entertainment Technologies, LLC +#Office Hours Global Community Project +#Created and maintained by Andy Carluccio - Washington, D.C. -#Last updated 8/8/2021 +#Contributors: +#Juan C. Ramos - Mexico City, MX + +#Last updated 8/11/2021 #OSC variables & libraries from pythonosc import dispatcher @@ -15,10 +17,13 @@ import argparse import sys +#Numpy Library +import numpy as np + #Loudness Processing Library import pyloudnorm as pyln -#Files +#Files (No longer needed, but left for future use) import wave import soundfile @@ -41,12 +46,12 @@ micIndex = int(input()) #Other audio parameters -form_1 = pyaudio.paInt16 # 16-bit resolution +form_1 = pyaudio.paFloat32 # 16-bit resolution chans = 2 # 2 channel samp_rate = 44100 # 44.1kHz sampling rate chunk = 1024 # 2^12 samples for buffer -durr = 0.5 #durration of sample -file_name = "buffer.wav" #file name +durr = 0.6 #durration of sample +#file_name = "buffer.wav" #file name # create pyaudio stream stream = audio_stream.open(format = form_1,rate = samp_rate,channels = chans, \ @@ -54,26 +59,33 @@ frames_per_buffer=chunk) def getLufs(unused_addr): - #print("Running function!") data = stream.read(chunk) - + frames = [] for i in range(0, int(samp_rate / chunk * durr)): data = stream.read(chunk) frames.append(data) + total_data = b''.join(frames) + data_samples = np.frombuffer(total_data,dtype=np.float32) + meter = pyln.Meter(samp_rate) # create BS.1770 meter + loudness = meter.integrated_loudness(data_samples) # measure loudness + #conform the buffer to wav - waveFile = wave.open(file_name, 'wb') - waveFile.setnchannels(chans) - waveFile.setsampwidth(audio_stream.get_sample_size(form_1)) - waveFile.setframerate(samp_rate) - waveFile.writeframes(b''.join(frames)) - waveFile.close() + #waveFile = wave.open(file_name, 'wb') + #waveFile.setnchannels(chans) + #waveFile.setsampwidth(audio_stream.get_sample_size(form_1)) + #waveFile.setframerate(samp_rate) + #waveFile.writeframes(b''.join(frames)) + #waveFile.close() #pull in the wav for analysis - dat, rt = soundfile.read("buffer.wav") - meter = pyln.Meter(rt) # create BS.1770 meter - loudness = meter.integrated_loudness(dat) # measure loudness + #dat, rt = soundfile.read("buffer.wav") + #print("File Data") + #print(dat) + #meter = pyln.Meter(rt) # create BS.1770 meter + #loudness = meter.integrated_loudness(dat) # measure loudness + #send the loundess as OSC client.send_message("/OSCLufs/lufs", loudness) @@ -117,7 +129,7 @@ def getLufs(unused_addr): #Print API print("OSC Networking Established") print() - print("OSC API for Controlling OSCTranscribe:") + print("OSC API for Controlling OSCLufs:") print("/OSCLufs/getLufs: Request a lufs reply") print() print("OSC API for Receiving Text from OSCLufs:")