Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Specifically taking a USB device, which normally allows only one process to open it for reading, and clone/duplicate/replicate it so that multiple processes can open and read the input. Something along these lines:

# Duplicate two streams from microphone
arecord -D [mic] | tee /dev/[#1] > /dev/[#2] &

# Set up process 1
apply-live-effects -filter lo-pass < /dev/[#1] > output1.raw &

# Set up process 2
apply-live-effects -filter hi-pass < /dev/[#2] > output2.raw &

With /dev/[#1] and /dev/[#2] being the devices to open for reading. I thought I might be able to use the /dev/loop# devices. I tried playing around with these but no luck.

share|improve this question
    
Might help give you an idea of what to do: carthick.wordpress.com/2007/11/26/… –  slm Jun 3 '13 at 23:58

1 Answer 1

up vote 1 down vote accepted

There is no general concept of duplicating a device. Even restricting to the case of an input-only device, it still returns different values at different times. If two programs access the same device at different times (even if it's a matter of microseconds), they might get completely different data.

The loop devices give access to block devices. Block devices behave differently: if you read from the same position twice with no write in between, you get the same data. This doesn't apply to other types of devices, thus there is no concept of loop devices for character devices.

You are considering a specific kind of device which returns a stream — you're making the assumption that the data read by the program depends solely on when it starts and stops reading. The kernel won't help you there, it has no special support for this special case.

What you can do is read from the device once, and replicate the data in userland. Why are you attempting to feed the data back into a device? You've got it, so read it. You'll need to pass the output of tee to multiple programs, so either use a named pipe or process substitution.

mkfifo low high
apply-live-effects -filter lo-pass <low >output1.raw
apply-live-effects -filter ho-pass <high >output2.raw
arecord -D [mic] | tee low >high

or

arecord -D [mic] | tee >(apply-live-effects -filter lo-pass >output1.raw) >(apply-live-effects -filter hi-pass >output2.raw)

Beware that if the filters are too slow, tee may not be able to read the output from arecord as fast as it should, which may result in arecord not being able to read from the device as fast as it should, causing lost frames. This can be solved with multitee, which does its best not to block.

arecord -D [mic] | multitee 0-3,4 3> >(apply-live-effects -filter lo-pass >output1.raw) 4> >(apply-live-effects -filter hi-pass >output2.raw)
share|improve this answer
    
Thank you. It's okay to lose some samples, not a big deal. I tried with the pipes, just simple command line redirection, and it seems to get "Broken pipe" errors or just hangs whenever it tries to read anything, like head -c 32 low | hexdump -C or head -c 32 high | hexdump -C. I think the program reading the data might be expecting the input to be a device of some sort and not a file stream. For example, I use ALSADEV=plughw:1,0 to specify the microphone input source, that's why I was hoping to be able to clone it as a device. Don't know if that makes any sense. –  Yimin Rong Jun 5 '13 at 14:25
    
@YiminRong Have you tried with multitee? If it doesn't work with multitee, see if the program attempts ioctl calls: run strace -o apply-live-effects.strace apply-live-effects … and see if there are ioctl calls to file descriptor 0 that would only make sense on a sound device. –  Gilles Jun 5 '13 at 14:29
    
There aren't but there is a fundamental flaw with apply-live-effects in that if the input is stdin, it treats it as a file and tries to read in everything before processing. Bizarre but documented behaviour. I guess what I need is something like esd. I will ask in a separate question. –  Yimin Rong Jun 5 '13 at 16:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.