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)