Audio record in R

后端 未结 2 869
南旧
南旧 2021-01-27 02:06

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} :

相关标签:
2条回答
  • 2021-01-27 02:26

    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.

    0 讨论(0)
  • 2021-01-27 02:40

    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)
    
    0 讨论(0)
提交回复
热议问题