Search code examples
alsalibalsa

aplay piping to arecord using a file instead of stdin and stdout


Below command will record the data from default device and output it on stdout and aplay will play the data from stdin.

arecord -D hw:0  | aplay -D hw:1 -

Why we prefer stdin and stdout instead of writing into a file and reading from it as below?

arecord -D hw:0 test.wav | aplay -D hw:1 test.wav

Solution

  • Using a pipe for this operation is more efficient and effective than using a file, simply because of the following reasons:

    1) Pipe (|) is an interprocess communication technique. The output of one process is directly sent to the input of another process using a kernel-based buffer. So this gives faster speed than writing something to a file in hard disk and reading it from it. This depends however on other factors also. Generally, the kernel also writes and reads small files from buffers and disk caches.

    2) Using interprocess communication technique also helps in getting concurrent operation. Instead, if you had to write something to a file and then read from it, it would have to be performed in steps, so you would have lost the concurrency.

    I assume you meant

    arecord -D hw:0 test.wav && aplay -D hw:1 test.wav
    

    instead of

    arecord -D hw:0 test.wav | aplay -D hw:1 test.wav