Skip to content
Prev 389193 / 398506 Next

Handling interrupts in long-running R functions

Dear Ivan Krylov,


Re:
Just a crazy idea: the sound input of a Mac/PC might serve as an interrupt.
Record a short bit and assess the amplitude: moderate = signal, loud = break.
See my source code below. This works on my MacBook Air with Mojave.
When I snap my fingers, the amplitude is moderate (3 through 10), when tapping 
or scratching the microphone, amplitude is over 20.

Hoping this might help,
With best regards,


Franklin
------


R source code:


#  sound as interrupt.R
#  with package audio
#  15-09-2021
#  ====================
#  NB First switch audio input to the wanted device
#     (microphone, sound input or 3rd party device)
#  flick your fingers = signal (m about 3 through 10)
#  tap the microphone -> stop  (m > 20)
library(audio)
samp.rate = 44100
rectime = 1  #  second
nsamples = samp.rate * rectime
repeat {
	sample <- record(2*nsamples, rate=44100, channels=2)
	wait(sample) # wait for the recording to finish
	sound <- sample$data # get the result
	m = max(sound)
	print(m)
	if (m > 0.5) alarm()  #  or the desired routine
	if (m > 20) {save.wave(sound, "sound from audio input.wav"); break}   # stop program
	}
#  end




Franklin Bretschneider
Dept of Biology
Utrecht University
f.bretschneider at uu.nl