Results 1 to 15 of 15

Thread: QAudioInput && QTcpSocket help !

  1. #1
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QAudioInput && QTcpSocket help !

    i am writing the application to send voice by lan network using...
    i am having trouble because code doesn't work
    below is the class ThreadInputAudio using thread to constantly send my voice
    threadinputaudio.h
    Qt Code:
    1. #ifndef THREADINPUTAUDIO_H
    2. #define THREADINPUTAUDIO_H
    3.  
    4. #include <QThread>
    5. #include <QByteArray>
    6. #include <QAudioInput>
    7. #include <QTcpSocket>
    8. #include <QVariant>
    9.  
    10. class ThreadInputAudio : public QThread
    11. {
    12. Q_OBJECT
    13. public:
    14. ThreadInputAudio();
    15. void stop();
    16. QTcpSocket* socket;
    17. protected:
    18. void run();
    19. private:
    20. bool stopped;
    21. QByteArray m_buffer;
    22. QAudioInput *m_audioInput;
    23. QAudioFormat m_format;
    24. QIODevice *m_input;
    25. void stopRecording();
    26.  
    27. private slots:
    28.  
    29. void readMore();
    30. };
    31.  
    32. #endif // THREADINPUTAUDIO_H
    To copy to clipboard, switch view to plain text mode 

    threadinputaudio.cpp
    Qt Code:
    1. #include "threadinputaudio.h"
    2.  
    3. ThreadInputAudio::ThreadInputAudio()
    4. :m_audioInput(0)
    5. ,m_input(0)
    6. {
    7. stopped = false;
    8.  
    9. }
    10. void ThreadInputAudio::run(){
    11. m_format.setFrequency(8000);
    12. m_format.setChannels(1);
    13. m_format.setSampleSize(16);
    14. m_format.setSampleType(QAudioFormat::SignedInt);
    15. m_format.setByteOrder(QAudioFormat::LittleEndian);
    16. m_format.setCodec("audio/pcm");
    17. QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
    18. if (!info.isFormatSupported(m_format)) {
    19. qDebug() << "Default format not supported - trying to use nearest";
    20. m_format = info.nearestFormat(m_format);
    21. }
    22. while (!stopped){
    23. m_audioInput = new QAudioInput(m_format);
    24. m_input = m_audioInput->start();
    25. connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
    26. QThread::sleep(500);
    27. stopRecording();
    28. }
    29. stopped = false;
    30. }
    31. void ThreadInputAudio::stop(){
    32. stopped = true;
    33. }
    34. void ThreadInputAudio::stopRecording(){
    35. m_audioInput->stop();
    36. delete m_audioInput;
    37. }
    38. void ThreadInputAudio::readMore(){
    39. qDebug() << "start send voice";
    40. if(!m_audioInput)
    41. return;
    42. qint64 len = m_audioInput->bytesReady();
    43. qint64 l = m_input->read(m_buffer.data(), len);
    44. if(l > 0) {
    45. if(socket == NULL) return;
    46. if(socket->state() != QAbstractSocket::ConnectedState ) return;
    47. QString msg = "VOICE";
    48. QVariant data = QVariant(m_buffer);
    49. QByteArray block;
    50. QDataStream out(&block, QIODevice::WriteOnly);
    51. out.setVersion(QDataStream::Qt_4_2);
    52. out << (quint32) 0;
    53. out << msg;
    54. out << data;
    55. out.device()->seek(0);
    56. out << (quint32)(block.size() - sizeof(quint32));
    57.  
    58. socket->write(block);
    59. socket->flush();
    60.  
    61. qDebug() << "send voice ok";
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 
    i can't figure out why the method readmore() is never done
    help me to decide whether the code on receiving the data is correct or not? how can i fix it.
    Qt Code:
    1. void MainWindow::onVoiceMessage(const QByteArray &data){
    2. qDebug() << "nhan duoc am thanh";
    3. QAudioFormat format;
    4. inputFile->write(data.constData(),(qint64)data.size());
    5. format.setFrequency(8000);
    6. format.setChannels(1);
    7. format.setSampleSize(8);
    8. format.setCodec("audio/pcm");
    9. format.setByteOrder(QAudioFormat::LittleEndian);
    10. format.setSampleType(QAudioFormat::UnSignedInt);
    11.  
    12. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    13. if (!info.isFormatSupported(format)) {
    14. qDebug() <<"raw audio format not supported by backend, cannot play audio.";
    15. return;
    16. }
    17.  
    18. audio = new QAudioOutput(format);
    19. connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
    20. audio->start(inputFile);
    21. }
    22.  
    23. void MainWindow::finishedPlaying(QAudio::State state)
    24. {
    25. if(state == QAudio::IdleState) {
    26. audio->stop();
    27. delete audio;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioInput && QTcpSocket help !

    i can't figure out why the method readmore() is never done
    What are the symptoms you are having?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    in class mainwindow i declare an object ThreadInputAudio tIA;
    when socket already connected to server, i added tIA.start();
    as usual, the method readmore() of class ThreadInputAudio is supposed to be processed by the statement connect(m_input, SIGNAL(readyRead()), SLOT(readMore())); in the method run() of the class ThreadInputAudio but readmore() is not processed.


    help me check whether my method is correct or not (input >>> send >>> reic >>> output ).

    thank in advance .

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioInput && QTcpSocket help !

    Ok, try this:
    Qt Code:
    1. while (!stopped){ //set a break point here - does it stop here?
    2. m_audioInput = new QAudioInput(m_format); //step through, do you get in here?
    3. m_input = m_audioInput->start();
    4. connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
    5. QThread::sleep(500);
    6. stopRecording();
    7. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QAudioInput && QTcpSocket help !

    Again, this is the classic thread affinity problem of signals, slots and QThread.

    You can't just subclass QThread and add slots to it and call these slots from within another thread.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioInput && QTcpSocket help !

    You can't just subclass QThread and add slots to it and call these slots from within another thread.
    Maybe I misunderstand, but you can - if you use queued connection, which if I remember right is the default between threads.

    But I agree that the code above is asking for trouble.

    Also - in his code, he is connecting from an object in the new thread, so the emitting object has the current thread affinity.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    i fix my code into this:
    threadinputaudio.cpp
    Qt Code:
    1. #include "threadinputaudio.h"
    2.  
    3. ThreadInputAudio::ThreadInputAudio()
    4. :m_audioInput(0)
    5. ,m_input(0)
    6. {
    7. stopped = false;
    8.  
    9. }
    10. void ThreadInputAudio::run(){
    11. m_format.setFrequency(8000);
    12. m_format.setChannels(1);
    13. m_format.setSampleSize(16);
    14. m_format.setSampleType(QAudioFormat::SignedInt);
    15. m_format.setByteOrder(QAudioFormat::LittleEndian);
    16. m_format.setCodec("audio/pcm");
    17. QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
    18. if (!info.isFormatSupported(m_format)) {
    19. qDebug() << "Default format not supported - trying to use nearest";
    20. m_format = info.nearestFormat(m_format);
    21. }
    22.  
    23. while (!stopped){
    24. m_audioInput = new QAudioInput(m_format);
    25. m_input = m_audioInput->start();
    26. QThread::sleep(0.5);
    27.  
    28. m_audioInput->stop();
    29. readMore();
    30. stopRecording();
    31.  
    32. }
    33.  
    34. stopped = false;
    35. }
    36. void ThreadInputAudio::stop(){
    37. stopped = true;
    38. }
    39. void ThreadInputAudio::stopRecording(){
    40. delete m_audioInput;
    41. }
    42. void ThreadInputAudio::readMore(){
    43.  
    44. if(!m_audioInput)
    45. return;
    46.  
    47. qint64 len = m_audioInput->bytesReady();
    48. qint64 l = m_input->read(m_buffer.data(), len);
    49. if(l > 0) {
    50. [B]// not work[/B]
    51. if(socket == NULL) return;
    52. if(socket->state() != QAbstractSocket::ConnectedState ) return;
    53. QString msg = "VOICE";
    54. QVariant data = QVariant(m_buffer);
    55. QByteArray block;
    56. QDataStream out(&block, QIODevice::WriteOnly);
    57. out.setVersion(QDataStream::Qt_4_2);
    58. out << (quint32) 0;
    59. out << msg;
    60. out << data;
    61. out.device()->seek(0);
    62. out << (quint32)(block.size() - sizeof(quint32));
    63.  
    64. socket->write(block);
    65. socket->flush();
    66.  
    67.  
    68. }
    69. }
    To copy to clipboard, switch view to plain text mode 
    the part in if statement is not processed...the bytes which are read..
    what 's wrong with my code

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioInput && QTcpSocket help !

    i fix my code into this:
    Please note in next posts what the changes are, preferably via comments in the code, otherwise its hard to follow.

    Try to get the state() of the device before your read form it, maybe it is in error state...
    Last edited by high_flyer; 14th February 2011 at 15:08.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    who can help me fix my class ThreadInputAudio to be able to send Audio Input.
    That's necessary for me .

  10. #10
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    up ... please help me :-S

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QAudioInput && QTcpSocket help !

    Did you try getting the state as I suggested to you?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    I've modified my code.

    threadinputaudio.h
    Qt Code:
    1. #ifndef THREADINPUTAUDIO_H
    2. #define THREADINPUTAUDIO_H
    3.  
    4. #include <QThread>
    5. #include <QByteArray>
    6. #include <QAudioInput>
    7. #include <QTcpSocket>
    8. #include <QVariant>
    9.  
    10. class AudioInfo : public QIODevice
    11. {
    12. Q_OBJECT
    13. public:
    14. AudioInfo(QObject *parent);
    15. ~AudioInfo();
    16.  
    17. void start();
    18. void stop();
    19.  
    20. qint64 readData(char *data, qint64 maxlen);
    21. qint64 writeData(const char *data, qint64 len);
    22. };
    23.  
    24.  
    25.  
    26. class ThreadInputAudio : public QThread
    27. {
    28. Q_OBJECT
    29. public:
    30. ThreadInputAudio();
    31. QTcpSocket* socket;
    32. protected:
    33. void run();
    34. private:
    35. QByteArray m_buffer;
    36. QAudioInput *m_audioInput;
    37. QAudioFormat m_format;
    38. QIODevice *m_input;
    39. AudioInfo *m_audioInfo;
    40.  
    41.  
    42. private slots:
    43. void stopRecording();
    44. void readMore();
    45. };
    46.  
    47. #endif // THREADINPUTAUDIO_H
    To copy to clipboard, switch view to plain text mode 
    threadinputaudio.cpp
    Qt Code:
    1. #include "threadinputaudio.h"
    2.  
    3. const int BufferSize = 4096;
    4.  
    5. AudioInfo::AudioInfo(QObject *parent)
    6. : QIODevice(parent)
    7.  
    8. {
    9. }
    10.  
    11. AudioInfo::~AudioInfo()
    12. {
    13. }
    14.  
    15. void AudioInfo::start()
    16. {
    17. open(QIODevice::WriteOnly);
    18. }
    19.  
    20. void AudioInfo::stop()
    21. {
    22. close();
    23. }
    24. qint64 AudioInfo::readData(char *data, qint64 maxlen)
    25. {
    26. Q_UNUSED(data)
    27. Q_UNUSED(maxlen)
    28.  
    29. return 0;
    30. }
    31.  
    32. qint64 AudioInfo::writeData(const char *data, qint64 len)
    33. {
    34. Q_UNUSED(data)
    35. Q_UNUSED(len)
    36.  
    37. return 0;
    38. }
    39.  
    40.  
    41.  
    42. ThreadInputAudio::ThreadInputAudio()
    43. :m_audioInput(0)
    44. ,m_input(0)
    45. ,m_buffer(BufferSize, 0)
    46. {
    47. }
    48. void ThreadInputAudio::run(){
    49. connect(socket, SIGNAL(disconnected()), SLOT(stopRecording()));
    50. m_format.setFrequency(8000);
    51. m_format.setChannels(1);
    52. m_format.setSampleSize(16);
    53. m_format.setSampleType(QAudioFormat::SignedInt);
    54. m_format.setByteOrder(QAudioFormat::LittleEndian);
    55. m_format.setCodec("audio/pcm");
    56. QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
    57. if (!info.isFormatSupported(m_format)) {
    58. qDebug() << "Default format not supported - trying to use nearest";
    59. m_format = info.nearestFormat(m_format);
    60. }
    61. m_audioInfo = new AudioInfo(this);
    62. m_audioInput = new QAudioInput(m_format);
    63. m_audioInfo->start();
    64. m_audioInput->start(m_audioInfo);
    65.  
    66. //m_input = m_audioInput->start();
    67. //Q_ASSERT(m_input);
    68. qDebug() << m_audioInput->state();
    69. connect(m_audioInfo, SIGNAL(readyRead()), SLOT(readMore()));
    70. }
    71. void ThreadInputAudio::stopRecording(){
    72. m_audioInfo->stop();
    73. delete m_audioInfo;
    74. delete m_audioInput;
    75. }
    76. void ThreadInputAudio::readMore(){
    77.  
    78. if(!m_audioInput)
    79. return;
    80. qDebug() << m_audioInput->state(); // display 0
    81. qint64 len = m_audioInput->bytesReady();
    82. m_input->seek(0);
    83. qint64 l = m_input->read(m_buffer.data(), len);
    84. if(l > 0) {
    85. // not work
    86. qDebug() << "not work.";
    87. if(socket == NULL) return;
    88. if(socket->state() != QAbstractSocket::ConnectedState ) return;
    89. QString msg = "VOICE";
    90. QVariant data = QVariant(m_buffer);
    91. QByteArray block;
    92. QDataStream out(&block, QIODevice::WriteOnly);
    93. out.setVersion(QDataStream::Qt_4_2);
    94. out << (quint32) 0;
    95. out << msg;
    96. out << data;
    97. out.device()->seek(0);
    98. out << (quint32)(block.size() - sizeof(quint32));
    99.  
    100. socket->write(block);
    101. socket->flush();
    102.  
    103.  
    104. }
    105. }
    To copy to clipboard, switch view to plain text mode 


    I tried to do on your own. và state == 0.

    please help me.

  13. #13
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    help me :-((

  14. #14
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QAudioInput && QTcpSocket help !

    The problem is already described.
    You can't just subclass QThread and create slots.

  15. #15
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioInput && QTcpSocket help !

    It mean 1 subclass of QThread couldn't create 1 slot which execute subclass? So, do you have the way to solve my problem? I want sending from QaudioInput input data continuous by QTcpsocket. Please,

    help me!.

    thanks .

Similar Threads

  1. QAudioInput Frequency and Amplitude
    By stormtrooper5 in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2011, 18:18
  2. QAudioInput example strange behavior
    By m15ch4 in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2010, 06:55
  3. QAudioInput and QtcpSocket help!!!
    By XavierQT in forum General Programming
    Replies: 1
    Last Post: 13th July 2010, 19:15
  4. QTcpSocket
    By pdoria in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2009, 17:52
  5. QTcpSocket and Qt 4.2.3
    By slcotter in forum Qt Programming
    Replies: 4
    Last Post: 9th May 2007, 16:14

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.