简体   繁体   中英

C++ Recording Audio When a Certain Key Is Down Until it is Up

Basically I need a program that runs in Linux and records to a .wav or .flac when I hold alt. So far I have a program(in C++) where it recognizes when alt is up or down, but I need a way to record until I release it. Here's some pseudo code of what I've got so far:

while 1:
    if altChanged:
        if altIsDown:
            //Call system(arecord OPTIONS > /tmp/blah.wav) to record audio.
        end
        else
            //Get PID
            //Use system(kill PID) to fake Ctrl+C and stop recording
        end
    end
end
This doesn't work because I was too stupid to see that the program halts when I do the first system call to try and wait for arecord to end, which it never does since the program doesn't reach the kill. Do I need to figure out how to do threading? Or is there a library where I could cheat and do a record.start(); record.stop(); set of functions?

The system() function is not appropriate for much at all (and maybe even less than that). Your best bet to call external applications is to use fork() / execl() (or other exec functions) directly.

Since you're on Linux, your best bet is to pull the source to whatever external application you're currently calling system() on and figure out how to add the functionality into your own code.

Additionally you will probably want to have a dedicated thread for watching the events to start/stop recording.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM