read from socket tcp and start play raw audio
-
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(); }
-
Hi,
- Why are you creating a new QAudioOutput object each time readyRead is called ?
- You do not take into account that
data
may not contain a full audio frame - You do not take into account that you may have different audio formats between the server and the client
-
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()); -
Beside the noise, are you getting the rest correctly ?
-
You need to apply some noise cancellation on your input.
-
This post is deleted!
-
@SGaist said in read from socket tcp and start play raw audio:
You need to apply some noise cancellation on your input.