Skip to content

Commit

Permalink
Removed WAV Buffer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AndrewCarluccio committed Aug 11, 2021
1 parent 5d48e62 commit b0e4afb
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions OSCLufs.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -41,39 +46,46 @@
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, \
input_device_index = micIndex,input = True, \
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)
Expand Down Expand Up @@ -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:")
Expand Down

0 comments on commit b0e4afb

Please sign in to comment.