Play and record audio at the same time
-
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?
-
changed to Matlab --> https://github.com/Christoph-Lauer/Transfer-Function-Extractor-Matlab
-
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?
@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 ? -
The project is now on GitHub: https://github.com/Christoph-Lauer/Impulse-Response-Extractor
-
The project is now on GitHub: https://github.com/Christoph-Lauer/Impulse-Response-Extractor
-
Hi
please hook up recorder's error signal to see if something is raised when you try to record.@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....
-
@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....
Hi
You mean an example of tone generation ?
https://doc.qt.io/qt-5.12/qtmultimedia-multimedia-spectrum-app-tonegenerator-cpp.html -
No the generation and the signal processing is clear for me. I mean the technical handling of the SIMULTANEOUS PLAY and RECORD.
@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. -
@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.@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.
-
@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.
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?
-
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?
@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.
-
@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.
@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) -
@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) -
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. -
changed to Matlab --> https://github.com/Christoph-Lauer/Transfer-Function-Extractor-Matlab