Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Activating the PC's speaker using C++ for until I will need to stop - how ? (Beep, thread, and more)...

Activating the PC's speaker using C++ for until I will need to stop - how ? (Beep, thread, and more)...

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdata-structurestutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitalonm
    wrote on last edited by
    #1

    Hi, I am trying to write a small application, using Visual C++. In the application's GUI, among other things, there will be 2 buttons: First button should turn the PC's internal speaker ON. Second button should turn the PC's internal speaker OFF. The GUI will have to be functional during all times. I see that in order to use the PC's internal speaker I need to use the Beep(int tone, int duration) function. (or is there any other way ???) The problem is that Beep function will play sound and stop the code's execution until duration mSec will be over. This is not good to me, as I will need the program to continue do other things in the background and I will need it to response to the OFF button as well. Using a flag to repeat the short Beeps again and again in a loop produces a glitch in the sound, every time that the Beep is executed (looped) again, so this is not elegant solution that I wold like to use. The solution that I came to is: ON button will start a thread, that will Beep endlessly. OFF button will Beep for 1 mSec, and this way will terminate sound that the Beep from the BeepThread started. Note that the Beep's thread called from ON button will never finish execution as it will be stack in it's Beep() line that has endless time as parameter. The problem in my solution is that even if the sound stops, the BeepThread that was started will never end, and pressing several times on the on/off buttons will produce several BeepThreads. Killing a thread from the outside is not recommended as well. Any ideas on how to deal with this issue ? I am open to change the whole way of my solution. Thanks.

    S 1 Reply Last reply
    0
    • A amitalonm

      Hi, I am trying to write a small application, using Visual C++. In the application's GUI, among other things, there will be 2 buttons: First button should turn the PC's internal speaker ON. Second button should turn the PC's internal speaker OFF. The GUI will have to be functional during all times. I see that in order to use the PC's internal speaker I need to use the Beep(int tone, int duration) function. (or is there any other way ???) The problem is that Beep function will play sound and stop the code's execution until duration mSec will be over. This is not good to me, as I will need the program to continue do other things in the background and I will need it to response to the OFF button as well. Using a flag to repeat the short Beeps again and again in a loop produces a glitch in the sound, every time that the Beep is executed (looped) again, so this is not elegant solution that I wold like to use. The solution that I came to is: ON button will start a thread, that will Beep endlessly. OFF button will Beep for 1 mSec, and this way will terminate sound that the Beep from the BeepThread started. Note that the Beep's thread called from ON button will never finish execution as it will be stack in it's Beep() line that has endless time as parameter. The problem in my solution is that even if the sound stops, the BeepThread that was started will never end, and pressing several times on the on/off buttons will produce several BeepThreads. Killing a thread from the outside is not recommended as well. Any ideas on how to deal with this issue ? I am open to change the whole way of my solution. Thanks.

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      Why dont you loop beep function with finite time inside the thread, say 5 seconds? This way when off is clicked the sound will stop immediately and thread will terminate in at most 5 seconds.

      void thread_func()
      {
      while(!done)
      {
      beep(5000);
      }
      }

      void off_func()
      {
      beep(1);
      done = false;
      }

      -Saurabh

      A 1 Reply Last reply
      0
      • S Saurabh Garg

        Why dont you loop beep function with finite time inside the thread, say 5 seconds? This way when off is clicked the sound will stop immediately and thread will terminate in at most 5 seconds.

        void thread_func()
        {
        while(!done)
        {
        beep(5000);
        }
        }

        void off_func()
        {
        beep(1);
        done = false;
        }

        -Saurabh

        A Offline
        A Offline
        amitalonm
        wrote on last edited by
        #3

        Thanks for your comment. As I wrote before, I did so, but the problem is that this caused a short noise every time interval. In the case of your suggestion, the noise will come out every 5 seconds. The noise is an outcome of the beep being stopped and immediately started again, so the tone is not continuously. This noise is a problem, as the tone that I produce should come out clean. Any other suggestions ? Thanks.

        A 1 Reply Last reply
        0
        • A amitalonm

          Thanks for your comment. As I wrote before, I did so, but the problem is that this caused a short noise every time interval. In the case of your suggestion, the noise will come out every 5 seconds. The noise is an outcome of the beep being stopped and immediately started again, so the tone is not continuously. This noise is a problem, as the tone that I produce should come out clean. Any other suggestions ? Thanks.

          A Offline
          A Offline
          amitalonm
          wrote on last edited by
          #4

          Hi, Can anyone help me here ? I would really appreciate getting ideas for my problem. -- Amit.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups