Skip to content

Instantly share code, notes, and snippets.

@mailletf
Last active October 14, 2022 18:53
Show Gist options
  • Save mailletf/c49063d005dfc51a2df6 to your computer and use it in GitHub Desktop.
Save mailletf/c49063d005dfc51a2df6 to your computer and use it in GitHub Desktop.
Simplified version of real-time audio scoring for goal detection
import pyaudio
import librosa
import numpy as np
import requests
# ring buffer will keep the last 2 seconds worth of audio
ringBuffer = RingBuffer(2 * 22050)
def callback(in_data, frame_count, time_info, flag):
audio_data = np.fromstring(in_data, dtype=np.float32)
# we trained on audio with a sample rate of 22050 so we need to convert it
audio_data = librosa.resample(audio_data, 44100, 22050)
ringBuffer.extend(audio_data)
# machine learning model takes wavform as input and
# decides if the last 2 seconds of audio contains a goal
if model.is_goal(ringBuffer.get()):
# GOAL!! Trigger light show
requests.get("http://127.0.0.1:8082/goal")
return (in_data, pyaudio.paContinue)
# function that finds the index of the Soundflower
# input device and HDMI output device
dev_indexes = findAudioDevices()
stream = pa.open(format = pyaudio.paFloat32,
channels = 1,
rate = 44100,
output = True,
input = True,
input_device_index = dev_indexes['input'],
output_device_index = dev_indexes['output'],
stream_callback = callback)
# start the stream
stream.start_stream()
while stream.is_active():
sleep(0.25)
stream.close()
pa.terminate()
@RFHO-BDSS
Copy link

RFHO-BDSS commented Oct 10, 2017

Hi Dude,

I was just looking to play around with your code a bit but there is no definition for RingBuffer and findAudioDevices, where can I find the implementations?

@mpariente
Copy link

Given the name of the class and its methods, I would say it comes from here, without credit.

@cleebp
Copy link

cleebp commented Nov 23, 2018

It appears that link is now dead, any idea where the RingBuffer specifically is from @mailletf ?

@BG4WCE
Copy link

BG4WCE commented Oct 16, 2020

Given the name of the class and its methods, I would say it comes from here, without credit.

You saved me a couple of hours. Thanks.

@jammerxd
Copy link

What is the http://127.0.0.1:8082/goal referring to to 'trigger the lightshow' - how is this setup?

@mailletf
Copy link
Author

It would call something like this https://gist.github.com/mailletf/024e87ccb5d907f45f8a#file-lightshow-py, wrapped behind a Flask endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment