Search code examples
rubyshellpipesox

Controlling applications (sox)


When I call sox from Ruby using exec within my application, the command prompt does not let me return to my application until the selected file has completed playing.

If I use IO.popen, once I launched it, I loose control of the process.

I attempted to run the process with pipe, but the results to a file and return control to Ruby using the following command:

play '01\ -\ Planet\ Telex.mp3' > results.txt 2>&1 &

Sox uses the "I'm now a background process" command to pause itself, so as soon as the command above command runs, playback halts.

Maybe the solution would be to spawn a different shell from Ruby and operate the process inside of that. Does anyone have any better ideas?


Solution

  • I'm not sure what you mean by "loose control of the process", because IO.popen should do exactly what you want: spawn a subprocess and give you access to the input/output.

    Here is an example of using it to send input to sed and then read the result:

    IO.popen(["sed", "-e", "s/^/foo/"], "r+") do |f|
      f.puts "bar"
      f.close_write
      puts f.gets
    end
    # foobar
    

    If you want separate output/error streams, you can use popen3.