make sounds (beep) with c++

前端 未结 12 644
暗喜
暗喜 2020-11-29 04:08

How to make the hardware beep sound with c++?

Thanks

12条回答
  •  清酒与你
    2020-11-29 04:40

    I tried most things here, none worked on my Ubuntu VM.

    Here is a quick hack (credits goes here):

    #include 
    int main() {
      system("(speaker-test -t sine -f 1000)& pid=$!; sleep 1.0s; kill -9 $pid");
    }
    

    It will basically use system's speaker-test to produce the sound. This will not terminate quickly though, so the command runs it in background (the & part), then captures its process id (the pid=$1 part), sleeps for a certain amount that you can change (the sleep 1.0s part) and then it kills that process (the kill -9 $pid part).

    sine is the sound produced. You can change it to pink or to a wav file.

提交回复
热议问题