Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Coupling signal into slot
Forum Updated to NodeBB v4.3 + New Features

Coupling signal into slot

Scheduled Pinned Locked Moved General and Desktop
signal & slot
8 Posts 5 Posters 2.2k Views 3 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.
  • O Offline
    O Offline
    ondra
    wrote on last edited by koahnig
    #1

    Hello programmers,

    I have a problem which i can't find a solution to. The situation is as follows:

    I am writing a program for sound visualisation. In my class which inherits QThread I am getting samples from my sound card, store them in a QVector and then I emit them for further processing. It look something like this:

    void AUDIO::run()
    {
    while(1){
     
    //funktions for getting the samples from the audio device
    emit amplitude_samples_signal(*bufferqt); //*bufferqt is a pointer to a QVector
                 }
    }
    

    And in my MainWindow constructor I connect this signal to a slot, which is a function that takes a QVector as an argument and then I do the visualisation from the data obtained in QVector. It looks like this:

    Constructor:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        in = new AUDIO();
           connect(in,SIGNAL(amplitude_samples_signal(QVector<double>)),this,SLOT(amplitude_samples_slot(QVector<double>)),Qt::QueuedConnection);
    }
    

    Slot

    void MainWindow::amplitude_samples_slot(QVector<double> value)
    {
           //take the QVector and do the visualisation
    }
    

    This solution works fine, however it takes a lot of computing power since I am using while(1) in AUDIO::run(). I want to use QTimer to get rid of the while and to trigger the recording lets say every 16ms. But then I can't propagate the Qtimer into a slot since I am already propagating the QVector. Is there any way how to propagate a couple of signals (in my example a signal from QTimer and a QVector) into one slot?

    Thank you very much for your answers.

    Ondra

    [edit: koahnig] code tags added

    K 1 Reply Last reply
    0
    • O ondra

      Hello programmers,

      I have a problem which i can't find a solution to. The situation is as follows:

      I am writing a program for sound visualisation. In my class which inherits QThread I am getting samples from my sound card, store them in a QVector and then I emit them for further processing. It look something like this:

      void AUDIO::run()
      {
      while(1){
       
      //funktions for getting the samples from the audio device
      emit amplitude_samples_signal(*bufferqt); //*bufferqt is a pointer to a QVector
                   }
      }
      

      And in my MainWindow constructor I connect this signal to a slot, which is a function that takes a QVector as an argument and then I do the visualisation from the data obtained in QVector. It looks like this:

      Constructor:

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          in = new AUDIO();
             connect(in,SIGNAL(amplitude_samples_signal(QVector<double>)),this,SLOT(amplitude_samples_slot(QVector<double>)),Qt::QueuedConnection);
      }
      

      Slot

      void MainWindow::amplitude_samples_slot(QVector<double> value)
      {
             //take the QVector and do the visualisation
      }
      

      This solution works fine, however it takes a lot of computing power since I am using while(1) in AUDIO::run(). I want to use QTimer to get rid of the while and to trigger the recording lets say every 16ms. But then I can't propagate the Qtimer into a slot since I am already propagating the QVector. Is there any way how to propagate a couple of signals (in my example a signal from QTimer and a QVector) into one slot?

      Thank you very much for your answers.

      Ondra

      [edit: koahnig] code tags added

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @ondra

      If I understand you correctly, you assume that you can have only one slot. However, you can have as many signal slot connections as you need. In principle each signal will be collected and stored until it can be processed. In some cases this might create a problem when you are creating lots of signals and/or the slot routines may take a lot of cpu power, but otherwise it should work.

      In your case you are putting a complete vector as parameter into the signal. Depending on the size of your vector this might create a problem, because your vector with all elements are copied. You might think of a different method here (e.g. a pointer).

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • sneubertS Offline
        sneubertS Offline
        sneubert
        wrote on last edited by
        #3

        I´m not sure this will solve your problem but take a short look at QSignalMapper

        http://doc.qt.io/qt-5/qsignalmapper.html

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

          Hi,

          How are you getting your data from the sound card ?

          How are you doing the processing ?

          Depending on what you want, there are audio libraries that do a lot of the heavy lifting, however, it might be wiser to use a secondary thread for your audio processing and then only send the result to your GUI

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

          O 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            How are you getting your data from the sound card ?

            How are you doing the processing ?

            Depending on what you want, there are audio libraries that do a lot of the heavy lifting, however, it might be wiser to use a secondary thread for your audio processing and then only send the result to your GUI

            O Offline
            O Offline
            ondra
            wrote on last edited by
            #5

            @SGaist Thanks for your reply. I am using ALSA library in Linux to get the data and QCustomPlot for the data visualisation. The actual function in ALSA is called snd_pcm_readi() . I am using two threads, at least I thing so (I am not an experienced programmer). But when I test the thread ID's with QThread::currentThreadId() , I get:

            From AUDIO thread: 0x7f13cffff700
            From GUI thread:  0x7f13f152f780
            

            So I am guessing that the GUI and audio processing run in a separate thread. However, the two threads are doing some heavy computing all the time because of the while loop I mentioned. And I want all the actions (first action is read the data from the sound card, next is visualise the data) to be triggered with a 16ms interval. And I don't know how to do it.

            Thank you for your replies! I really appreciate it.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DRoscoe
              wrote on last edited by DRoscoe
              #6

              Any while loop will tie up your processor if you don't put protections in, such as a sleep. An empty while loop will peg your processor even if it is doing nothing.

              In your main application, set a single-shot timer which is handled by the AUDIO::run method of your sampling thread. There, set up any signal/slot connections you want and then emit a signal which will be picked up by a slot on the audio thread to sample your card and exit the ::run method. When the signal is picked up by the slot, sample your data, and set a QTimer event which is handled by the same slot definition, emit a signal with your QVector payload with a slot to handle it on the visualization thread and exit. The visualization thread will pick up the signal and process the data. No timer is needed here since it will be slaved to the Audio event which is firing every 16ms

              When the Qtimer hits again, your audio thread will sample, and the process will repeat indefinitely until you signal a shutdown.

              You will need to take care to disable your timers when the application is about to terminate, or you could end up trying to process an event while the application is shutting down.

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

                Why 16ms ? And why not use QAudioInput ?

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

                O 1 Reply Last reply
                0
                • SGaistS SGaist

                  Why 16ms ? And why not use QAudioInput ?

                  O Offline
                  O Offline
                  ondra
                  wrote on last edited by
                  #8

                  @SGaist I'm reading batches of data - 256 samples (one frame) with a 16 kHz sampling frequency. The length of the frame is then 16ms. And I'm using ALSA because it's a requirement from a school for this project and I need to manually set all the HW audio parameters.
                  Thank you for all your answers. It helped me a lot.

                  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