Audio record in R

2020-04-28 04:03发布

I am playing with some audio and sound packages in R for Windows (mine is Win7 x64). There is a problem when I tried to record from microphone using record() {audio} :

  • it could record only once then cannot record some more until restart the whole console
  • once sound is recorded, it could be save but cannot play()
  • file recorded from above cannot be read by audio, but tuneR due to 'incomplete wave file'
  • and the following "filename" does not work

    filename=paste0('abcd','.wav') save.wave(x,filename)

until type directly to the command like, this makes hard to write a record script/function

save.wave(x,'abc.wav')

I want to ask anyone used audio package in Win and another OS if you met the same problem. Thanks.

标签: r audio record
2条回答
萌系小妹纸
2楼-- · 2020-04-28 04:39

I just wrote a function for record. It works but after a running time, the program has to be closed and then open R again:

audiorec=function(kk,f){  # kk: time length in seconds; f: filename
if(f %in% list.files()) 
{file.remove(f); print('The former file has been replaced');}
require(audio)
s11 <- rep(NA_real_, 16000*kk) # rate=16000
record(s11, 16000, 1)  # record in mono mode
wait(kk)
save.wave(s11,f)
}

Still a problem of GUI. I tried with some other computer using Win7 but met the same error. There is some bugs, I haven't figured it out.

查看更多
太酷不给撩
3楼-- · 2020-04-28 04:49

You can simply use http://www.rforge.net/audio, the code should look like this:

# record 8000 samples at 8000Hz (1 sec), mono (1 channel)
a <- record(8000, 8000, 1)
wait(a) # wait for the recording to finish
x <- a$data # get the result
x[1:10] # show first ten samples
#sample rate: 8000Hz, mono, 16-bits
# [1] 0.018100981 0.017364085 0.016479610 0.013326526 0.010764275 0.011048204
# [7] 0.010541249 0.010892886 0.007960078 0.006703259
close(a); rm(a) # you can close the instance at this point
play(x) # play back the result
# amplify and crop the signal
y <- x * 2
y[y < -1] <- -1
y[y > 1] <- 1
# play the amplified signal
play(y)
查看更多
登录 后发表回答