Results 1 to 2 of 2

Thread: How to record and play sound simultaneously

  1. #1
    Join Date
    Oct 2011
    Location
    Dhaka, Bangladesh
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy How to record and play sound simultaneously

    Hi, I have posted this problem in two other places but found no solution. So I am posting it here.

    How can I implement full duplex audio communication in Qt? That is, I want to record sound from the microphone and at the same time I want to play the sound
    on the headphone/speaker.

    The code that I have tried so far is as below -

    Qt Code:
    1. void AudioRecorder::on_btnStart_clicked()
    2. {
    3. // Get audio format and search for nearest matching format of the device
    4. QAudioFormat format = this->getFormat();
    5.  
    6. // Star recording using the specified format
    7. this->audioInput = new QAudioInput(format, this);
    8. connect(this->audioInput,SIGNAL(stateChanged(QAudio::State)),
    9. SLOT(inputStateChanged(QAudio::State)));
    10. QIODevice *myBuffer = this->audioInput->start();
    11.  
    12. this->audioOutput = new QAudioOutput(format, this);
    13. connect(this->audioOutput,SIGNAL(stateChanged(QAudio::State)),
    14. SLOT(outputStateChanged(QAudio::State)));
    15. this->audioOutput->start(myBuffer);
    16. }
    17.  
    18. QAudioFormat AudioRecorder::getFormat()
    19. {
    20. QAudioFormat format;
    21. format.setFrequency(8000);
    22. format.setChannels(1);
    23. format.setSampleSize(8);
    24. format.setCodec("audio/pcm");
    25. format.setByteOrder(QAudioFormat::LittleEndian);
    26. format.setSampleType(QAudioFormat::UnSignedInt);
    27.  
    28. QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    29. if (!info.isFormatSupported(format))
    30. {
    31. this->log("Default format not supported. Trying to use the nearest format.....");
    32. format = info.nearestFormat(format);
    33. }
    34.  
    35. return format;
    36. }
    To copy to clipboard, switch view to plain text mode 

    The only problem is that in linux this code runs for some time but after that the output device faces an Underrun Error (I have checked that in the stateChanged slot) and in windows this program crashes just after it's run.

    How can I solve this problem? Is there any other method for full duplex audio communication?

  2. #2
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: How to record and play sound simultaneously

    start the input and output device like this


    Qt Code:
    1. m_output= m_audioOutput->start();
    2. m_input = m_audioInput->start();
    3. connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
    To copy to clipboard, switch view to plain text mode 

    and write the input sample to output in readMore()
    Qt Code:
    1. m_output->write(outdata, len);
    To copy to clipboard, switch view to plain text mode 

    Check this article in code project that record from microphone and play back to speaker simultaneously

    http://www.codeproject.com/Articles/...essing-Utility

Similar Threads

  1. Record sound
    By Aayush in forum General Programming
    Replies: 0
    Last Post: 20th July 2011, 21:00
  2. play sound repeatedly
    By ardisaz in forum Qt Programming
    Replies: 4
    Last Post: 4th December 2010, 12:10
  3. Play .amr sound
    By ahmdsd_ostora in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2010, 16:34
  4. Phonon Cannot Play Sound (without Error)
    By padawan in forum Qt Programming
    Replies: 5
    Last Post: 16th September 2009, 16:17
  5. MP3 Sound / OGG Sound Play on GUI
    By patrik08 in forum Newbie
    Replies: 3
    Last Post: 1st September 2006, 20:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.