Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real-time audio analysis with R

Tags:

r

real-time

audio

Is it possible to use R to perform a real-time (or with a minimal delay) analysis of audio recorded through the computer's microphone?

While R has some well rounded packages for analysing audio files (such as package seewave) or to record sound (package audio), I could not find how to access microphone data in real-time.

The end goal is to analyse the frequency mix and amplitude of the recorded signal, as done here using Python: https://stackoverflow.com/a/40157297/4862822

A MWE will be provided as an edit if I can find something to start with.

like image 915
JohnBee Avatar asked Apr 22 '20 18:04

JohnBee


1 Answers

You can use the package audio to asynchronously fill a buffer in real-time from the computer's microphone: https://cran.r-project.org/web/packages/audio/

Although clunky, this can be used to do real-time audio analysis using R. Below is an example that records into a buffer from the microphone over 5 seconds. At 1 second intervals, the maximum decibels full-scale (dB FS) recorded during the previous 1 second is output to the console. For the purposes of this simple example I just used Sys.sleep() to pause until the next interval:

library(audio)

sample_rate <- 44100
seconds_to_record <- 5
num_channels <- 1
reporting_interval_seconds <- 1

# allocate buffer to record into
buffer <- rep(NA_real_, sample_rate * seconds_to_record)

# start recording into the buffer
rec <- record(buffer, sample_rate, num_channels)

for (reports in seq(reporting_interval_seconds, seconds_to_record, reporting_interval_seconds)){
  Sys.sleep(reporting_interval_seconds)
  analysis_samples <- (((reports - 1) * sample_rate) + 1) : (reports * sample_rate)
  abs_max <- max(abs(buffer[analysis_samples]), na.rm = TRUE)
  print(sprintf('Max Value = %2.3f dB FS', 20*log10(abs_max) ))
}

# play back the buffer
play(buffer)
like image 196
DMR Avatar answered Nov 09 '22 23:11

DMR