Simulate Microphone (virtual mic)

2020-08-17 08:22发布

问题:

I've got a problem where I need to "simulate" microphone output.

Data will be coming over the network, decoded into PCM and basically needs to be written into the mic - which then other programs can read/record/whatever.

I've been reading up on alsa but information is pretty sparse. The file plugin seemes promising - I was thinking of having a named pipe as "infile" which I could then deliver data to from my application. I can't get it to work however (vlc/audacity just segfault).

pcm.testing {
  type file
  slave {
    pcm {
      type hw
      card 0
      device 0
    }
  }
  infile "/dev/urandom"
  format "raw"
}

Are there any better ways of doing this? Any suggestions on alsa plug-ins (particularly the file plugin)?

回答1:

Your sound will come over the network and what would cache it until something wants to read? Or would data be discarded? In general something like the below (only barely tested) should work as a virtual mic, but I think that it will always read file from beginning when device opened and you need to check how does it handle end of file. Perhaps what you would try it using pipes but then caching/discarding incoming data needs to be handled by the app reading from network.

pcm.virtmic {
    type file
    format "raw"
    slave.pcm "default"
    file '/dev/null'
    infile '/dev/urandom'
}

See alsa docs for more options.

Again, not sure if this tool is what you really need for the task. It would have been really nifty if you could start a command with the 'infile' option, like you can with 'file' but unfortunately you can't...

Hope that helps.

UPDATE: slave.pcm must not be "null" but some real device. It seems that is used for timing or I don't know but using null causes the recorder process to block forever. This device could force you at a given sample rate though so be careful. Using "default" is a sane default value. infile needs to provide a raw sound data with the correct/matching format and rate. btw you can look at alsa server and jackd and other sound systems and libraries for alternative solutions for your task