Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Audio recorder with Qt 4.5. How to tell the recording loop to stop recording using pushbutton?
QtWS25 Last Chance

Audio recorder with Qt 4.5. How to tell the recording loop to stop recording using pushbutton?

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 2.7k Views
  • 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.
  • W Offline
    W Offline
    wenc
    wrote on last edited by
    #1

    Hi,

    I am developing a program for embedded device (Blackfin BF548). The program needs an audio recorder build in it. Currently, I am able to record sounds into an audio file. But the problem is, I cannot stop recording when I need it to. I tried QThread using the method "(here)":http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/. But the thread will only quit after while loop ends, which is never. And terminate() is a bad idea.

    Any solutions?Or does anyone has similar example codes?

    Please note: The embedded version of Qt I have is Qt 4.5. Thanks.

    Kind regards,
    Wen

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      It would be better if you could post the code you are using to record the data. Otherwise it would be pretty much crystal ball guessing.

      Since you have an infinite loop, you have to add a stop slot where you set the condition to stop the loop.

      @
      void stop()
      {
      _continue = false;
      }

      void run()
      {
      while (!_continue) {
      // processing
      }
      }
      @

      This is just a basic idea, hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wenc
        wrote on last edited by
        #3

        Hi SGaist,

        Here are how my codes look like.
        

        @//======Recorder.cpp=========
        //Constructor...
        //Deconstructor...

        void Recorder::process() {

        for (;;)
        {
            //recording
        }
        

        emit finished();
        }@

        @//========MainWindow.cpp=========
        MainWindow::MainWindow()
        {
        QThread* thread = new QThread;
        Recorder* recorder = new Recorder();
        recorder->moveToThread(thread);
        connect(recorder, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
        connect(thread, SIGNAL(started()), recorder, SLOT(process()));
        connect(recorder, SIGNAL(finished()), thread, SLOT(quit()));
        }

        void MainWindow::On_pushButton_pressed()
        {
        thread->start();
        }

        void MainWindow::On_pushButton_release()
        {
        emit recorder->finished();
        }@

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You have to change your process() code to something like I showed in my example. Without a way to break your loop you won't be able to stop it.

          Also, are you sure finished is a signal ? Anyway, you can only emit a signal from within the object that declares it.

          On_pushButton_release() should call the method from Recorder that will end the loop, the rest will fall in place.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wenc
            wrote on last edited by
            #5

            The example was the first thing I tried. Both approaches have the same problem that, the ui is not responding unless all the functions in process() are performed. It means the button is always being pressed and I won't be able to release it until loop end.

            For example:

            @void process()
            {
            for(int i =0; i<60000; i++)
            {
            if (this->stop) break;

                 //record to file
            }
            cleanup();
            

            }
            //==MainWindow==
            void on_pushButton_release()
            {
            recorder->stop = true;
            recorder->quit();
            }
            @

            The pushbutton won't be released unitl i=59999 and finish cleanup(). Is there any method I can stop tell the loop to stop anytime before i=59999?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What is happening is that when started is called your infinite loop runs and "locks" the thread so it can't start it's own event loop.

              Are you doing any signal/slot communication from the process function ? If not, you should rather subclass QThread, reimplement run and put your infinite loop there.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wenc
                wrote on last edited by
                #7

                Yes, I link the thread.start() and recorder.process() together..But don't work very well.

                Referring to the link in my first post "(the link),":http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ Subclassing QThread is wrong..Please correct me if that is wrong.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Read this "followup":http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html article.

                  Most of the time you don't need to subclass but in some cases you have to

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved