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. read from socket tcp and start play raw audio

read from socket tcp and start play raw audio

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.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.
  • Z Offline
    Z Offline
    zabitqt
    wrote on last edited by zabitqt
    #1

    Hi,

    when my client start I am not able to play row audio data received from socket server sent to client.
    Audio data received are captured from microphone and data from server is sent to client.

    I have sure data on client comes from server are correct .
    is there a mistake in format configuration of QAudioOutput?

    mytcpsocket.h

    #ifndef MYTCPSOCKET_H
    #define MYTCPSOCKET_H
    
    #include <QObject>
    #include <QTcpSocket>
    #include <QAbstractSocket>
    
    class MyTcpSocket : public QObject
    {
        Q_OBJECT
    public:
        explicit MyTcpSocket(QObject *parent = nullptr);
        void doConnect();
    
    signals:
    public slots:
        void connected();
        void disconnected();
        void bytesWritten(qint64 bytes);
        void readyRead();
    
    private:
         QTcpSocket *socket;
    };
    
    #endif // MYTCPSOCKET_H
    

    mytcpsocket.cpp

    #include "mytcpsocket.h"
    #include <QBuffer>
    #include <QMediaPlayer>
    #include <QMediaContent>
    #include <QAudioOutput>
    #include <QThread>
    
    
    MyTcpSocket::MyTcpSocket(QObject *parent) : QObject(parent)
    {
    
    }
    
    void MyTcpSocket::doConnect()
    {
        this->socket = new QTcpSocket(this);
    
        connect(this->socket, SIGNAL(connected()),this, SLOT(connected()));
        connect(this->socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
        connect(this->socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
        connect(this->socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
    
        qDebug() << "connecting...";
    
        socket->connectToHost("192.168.56.1", 12345);
    
        // we need to wait...
        if(!socket->waitForConnected(5000))
        {
            qDebug() << "Error: " << socket->errorString();
        }
    }
    
    void MyTcpSocket::connected()
    {
        qDebug() << "connected...";
    }
    
    void MyTcpSocket::disconnected()
    {
        qDebug() << "disconnected...";
    }
    
    void MyTcpSocket::bytesWritten(qint64 bytes)
    {
        qDebug() << bytes << " bytes written...";
    }
    
    void MyTcpSocket::readyRead()
    {
        qDebug() << "reading...";
    
        // read the data from the socket
        //qDebug() << socket->readAll();
    
        //CREATE A BUFFER OBJECT WITH BYTE ARRAY
        QByteArray data;
        data = socket->readAll();
    
    
        QAudioFormat format;
        format.setSampleRate(48000);
        format.setChannelCount(2);
        format.setSampleSize(16);
        format.setCodec("audio/pcm");
        format.setByteOrder(QAudioFormat::LittleEndian);
        format.setSampleType(QAudioFormat::SignedInt);
        QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
        if (!info.isFormatSupported(format)) {
            qWarning()<<"raw audio format not supported by backend, cannot play audio.";
            return;
        }
    
        QAudioOutput *qAudioOutput = new QAudioOutput(format, NULL);
        qAudioOutput->setBufferSize(16 * 48000);
        QIODevice *qAudioDevice = qAudioOutput->start();
        qAudioDevice->write(data,data.length());
    }
    

    main.cpp

    #include <QCoreApplication>
    #include "mytcpsocket.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        MyTcpSocket s;
        s.doConnect();
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      1. Why are you creating a new QAudioOutput object each time readyRead is called ?
      2. You do not take into account that data may not contain a full audio frame
      3. You do not take into account that you may have different audio formats between the server and the client

      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
      • Z Offline
        Z Offline
        zabitqt
        wrote on last edited by
        #3

        Ho, i edit my code, now qAudioOutput and qAudioDevice are in class scope as private attribuite.
        // In constructor
        qAudioOutput = new QAudioOutput(format, NULL);
        qAudioOutput->setBufferSize(16 * 48000);
        qAudioDevice = qAudioOutput->start();

        // In method
        data = socket->readAll();
        qAudioDevice->write(data,data.length());

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zabitqt
          wrote on last edited by
          #4

          Now my problem is that I hear rustle, a continue "shshshshsh" of micphone behind the speak. How can I solve it?

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

            Beside the noise, are you getting the rest correctly ?

            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
            1
            • Z Offline
              Z Offline
              zabitqt
              wrote on last edited by zabitqt
              #6

              Yes, the rest is ok. Over raw audio I hear a rustle "shshshsh" from my stereo speaker. How can I void this noise?

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

                You need to apply some noise cancellation on your input.

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

                SGaistS 1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bandkarde3
                  Banned
                  wrote on last edited by bandkarde3
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  1
                  • SGaistS SGaist

                    You need to apply some noise cancellation on your input.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @SGaist said in read from socket tcp and start play raw audio:

                    You need to apply some noise cancellation on your input.

                    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

                    • Login

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