Audio record in R

瘦欲@ 提交于 2019-12-20 04:16:15

问题


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.


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/22619561/audio-record-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!