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. Play and record audio at the same time
Forum Updated to NodeBB v4.3 + New Features

Play and record audio at the same time

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.5k 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.
  • Q Offline
    Q Offline
    QtAndrew
    wrote on 23 Sept 2021, 21:20 last edited by QtAndrew
    #1

    Hello, i want to play and record audio for a freeware application for impulse response extraction:

    // PLAY
    QMediaPlayer* player = new QMediaPlayer;
    QAudioOutput* audioOutput = new QAudioOutput;
    player->setAudioOutput(audioOutput);
    player->setSource(QUrl("qrc:/res/sweep.wav"));
    audioOutput->setVolume(100);
    player->play();
    
    // RECORD
    QMediaCaptureSession* session = new QMediaCaptureSession();
    QAudioInput* audioInput = new QAudioInput();
    QMediaRecorder* recorder = new QMediaRecorder();
    session->setAudioInput(audioInput);
    session->setRecorder(recorder);
    recorder->setMediaFormat(QMediaFormat::Wave);
    recorder->setOutputLocation(QUrl::fromLocalFile("test"));
    recorder->setAudioSampleRate(48000);
    recorder->setAudioChannelCount(1);
    recorder->record();
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), recorder, SLOT(stop()));
    timer->start(3500);
    

    The file is played, but the recorded file is created but has always the size zero. Any Idea?

    J 1 Reply Last reply 24 Sept 2021, 05:26
    0
    • Q Offline
      Q Offline
      QtAndrew
      wrote on 27 Sept 2021, 16:27 last edited by
      #15

      changed to Matlab --> https://github.com/Christoph-Lauer/Transfer-Function-Extractor-Matlab

      1 Reply Last reply
      0
      • Q QtAndrew
        23 Sept 2021, 21:20

        Hello, i want to play and record audio for a freeware application for impulse response extraction:

        // PLAY
        QMediaPlayer* player = new QMediaPlayer;
        QAudioOutput* audioOutput = new QAudioOutput;
        player->setAudioOutput(audioOutput);
        player->setSource(QUrl("qrc:/res/sweep.wav"));
        audioOutput->setVolume(100);
        player->play();
        
        // RECORD
        QMediaCaptureSession* session = new QMediaCaptureSession();
        QAudioInput* audioInput = new QAudioInput();
        QMediaRecorder* recorder = new QMediaRecorder();
        session->setAudioInput(audioInput);
        session->setRecorder(recorder);
        recorder->setMediaFormat(QMediaFormat::Wave);
        recorder->setOutputLocation(QUrl::fromLocalFile("test"));
        recorder->setAudioSampleRate(48000);
        recorder->setAudioChannelCount(1);
        recorder->record();
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), recorder, SLOT(stop()));
        timer->start(3500);
        

        The file is played, but the recorded file is created but has always the size zero. Any Idea?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 24 Sept 2021, 05:26 last edited by
        #2

        @QtAndrew Please add error handling as I suggested in your other thread.
        Also, this seems to be same question as https://forum.qt.io/topic/130551/record-audio-in-qt6 ?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Q Offline
          Q Offline
          QtAndrew
          wrote on 25 Sept 2021, 16:28 last edited by QtAndrew
          #3

          The project is now on GitHub: https://github.com/Christoph-Lauer/Impulse-Response-Extractor

          M 1 Reply Last reply 26 Sept 2021, 07:42
          0
          • Q QtAndrew
            25 Sept 2021, 16:28

            The project is now on GitHub: https://github.com/Christoph-Lauer/Impulse-Response-Extractor

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Sept 2021, 07:42 last edited by
            #4

            @QtAndrew

            Hi
            please hook up recorder's error signal to see if something is raised when you try to record.

            Q 1 Reply Last reply 26 Sept 2021, 09:20
            0
            • M mrjj
              26 Sept 2021, 07:42

              @QtAndrew

              Hi
              please hook up recorder's error signal to see if something is raised when you try to record.

              Q Offline
              Q Offline
              QtAndrew
              wrote on 26 Sept 2021, 09:20 last edited by
              #5

              @mrjj There is the other problem that the whole sound processing should be "Synthetic Sound Processing". This means that no files should be played but signal array should be generated, PLAYED and RECORDED simulaneously. And i have cannot find any examples....

              M 1 Reply Last reply 26 Sept 2021, 09:24
              0
              • Q QtAndrew
                26 Sept 2021, 09:20

                @mrjj There is the other problem that the whole sound processing should be "Synthetic Sound Processing". This means that no files should be played but signal array should be generated, PLAYED and RECORDED simulaneously. And i have cannot find any examples....

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 26 Sept 2021, 09:24 last edited by
                #6

                @QtAndrew

                Hi
                You mean an example of tone generation ?
                https://doc.qt.io/qt-5.12/qtmultimedia-multimedia-spectrum-app-tonegenerator-cpp.html

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QtAndrew
                  wrote on 26 Sept 2021, 09:25 last edited by
                  #7

                  No the generation and the signal processing is clear for me. I mean the technical handling of the SIMULTANEOUS PLAY and RECORD.

                  M 1 Reply Last reply 26 Sept 2021, 10:11
                  0
                  • Q QtAndrew
                    26 Sept 2021, 09:25

                    No the generation and the signal processing is clear for me. I mean the technical handling of the SIMULTANEOUS PLAY and RECORD.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 26 Sept 2021, 10:11 last edited by
                    #8

                    @QtAndrew
                    Hi
                    Well, it sounds like recording works if used alone.
                    but if you play and record its not. ?

                    But we don't know what error() says in this case so not much to guess on.

                    It can be it's simply not supported for the same hardware sound device, I mean
                    play and record at the same time on the same hardware.

                    Q 1 Reply Last reply 26 Sept 2021, 10:15
                    0
                    • M mrjj
                      26 Sept 2021, 10:11

                      @QtAndrew
                      Hi
                      Well, it sounds like recording works if used alone.
                      but if you play and record its not. ?

                      But we don't know what error() says in this case so not much to guess on.

                      It can be it's simply not supported for the same hardware sound device, I mean
                      play and record at the same time on the same hardware.

                      Q Offline
                      Q Offline
                      QtAndrew
                      wrote on 26 Sept 2021, 10:15 last edited by
                      #9

                      @mrjj I got it running for PLAY alone and RECORDING alone, both from File. 20 years ago this simultaneous play/record was a problem, bit got it running if set up a system command for plaing in background and recording. But that's not all what i want, It must run without the files, and i didnt find any examples for that, i spend so much time.

                      M 1 Reply Last reply 26 Sept 2021, 10:32
                      0
                      • Q QtAndrew
                        26 Sept 2021, 10:15

                        @mrjj I got it running for PLAY alone and RECORDING alone, both from File. 20 years ago this simultaneous play/record was a problem, bit got it running if set up a system command for plaing in background and recording. But that's not all what i want, It must run without the files, and i didnt find any examples for that, i spend so much time.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 26 Sept 2021, 10:32 last edited by
                        #10

                        @QtAndrew

                        Hi
                        Here they used a custom QIODevice
                        https://stackoverflow.com/questions/7776022/qt-how-to-record-and-play-sound-simultaneously
                        (last example)

                        What does QMediaRecorder error() says when you are playing and try to record?

                        Q 1 Reply Last reply 26 Sept 2021, 10:35
                        0
                        • M mrjj
                          26 Sept 2021, 10:32

                          @QtAndrew

                          Hi
                          Here they used a custom QIODevice
                          https://stackoverflow.com/questions/7776022/qt-how-to-record-and-play-sound-simultaneously
                          (last example)

                          What does QMediaRecorder error() says when you are playing and try to record?

                          Q Offline
                          Q Offline
                          QtAndrew
                          wrote on 26 Sept 2021, 10:35 last edited by QtAndrew
                          #11

                          @mrjj Hello, i know this stackoverflow entry, but they have no solution. There is no error, plaing and recording is always in series and never parallel. Could it be that it has something to with the cucrent loop thread? Because when I try to run the PLAY or RECORD code in a separate the Thread it stops working.

                          M 1 Reply Last reply 26 Sept 2021, 11:49
                          0
                          • Q QtAndrew
                            26 Sept 2021, 10:35

                            @mrjj Hello, i know this stackoverflow entry, but they have no solution. There is no error, plaing and recording is always in series and never parallel. Could it be that it has something to with the cucrent loop thread? Because when I try to run the PLAY or RECORD code in a separate the Thread it stops working.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 26 Sept 2021, 11:49 last edited by
                            #12

                            @QtAndrew
                            Hi
                            It sounds like it won't do both at the same time.
                            I'm not aware they shared a worker thread between the classes and hence only one can be active.

                            So the way using a custom QIODevice does not work anymore?
                            (the way the SO link shows)

                            Q 1 Reply Last reply 26 Sept 2021, 11:52
                            0
                            • M mrjj
                              26 Sept 2021, 11:49

                              @QtAndrew
                              Hi
                              It sounds like it won't do both at the same time.
                              I'm not aware they shared a worker thread between the classes and hence only one can be active.

                              So the way using a custom QIODevice does not work anymore?
                              (the way the SO link shows)

                              Q Offline
                              Q Offline
                              QtAndrew
                              wrote on 26 Sept 2021, 11:52 last edited by
                              #13

                              @mrjj i have currently no time to try it out.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 26 Sept 2021, 15:17 last edited by
                                #14

                                There are several issues with your code:

                                • Why are you using a static QAudioRecorder ?
                                • You are blocking your main thread with your sleep call and thus prevent the event loop from running.
                                • You are talking about synthetic sound yet you use an external application through "system" to play some sound file.

                                What it is exactly that you want to do ?
                                From the looks of it, you should check the new QMediaCaptureSession class.

                                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
                                • Q Offline
                                  Q Offline
                                  QtAndrew
                                  wrote on 27 Sept 2021, 16:27 last edited by
                                  #15

                                  changed to Matlab --> https://github.com/Christoph-Lauer/Transfer-Function-Extractor-Matlab

                                  1 Reply Last reply
                                  0

                                  10/15

                                  26 Sept 2021, 10:32

                                  • Login

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