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. QTcpSocket & QAudioInput
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket & QAudioInput

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 3.0k Views 1 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.
  • A Offline
    A Offline
    aleskandro
    wrote on last edited by
    #1

    Hi all, i'm developing an app that allow to communicate between PCs by socket and I would to send audio from microphone.
    I asked myself if I can use QTcpSocket & QAudioInput
    So I tried this:

    @void Server::makeStream() {

    QAudioInput* audio;
    QAudioFormat format;
    
    format.setFrequency(8000);
    format.setChannels(1);
    format.setSampleSize(8);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);
    
    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    if (!info.isFormatSupported(format))  {
    qWarning()<<"default format not supported try to use nearest";
    format = info.nearestFormat(format);
    }
    
    audio = new QAudioInput(format, this);
    this->ToSocket("Listen Me");
    audio->start(this->SocketServer);
    

    }@

    So I think that maybe Server sends "audio", but I don't know how can I do to save data from socket to a .wav file on client pc.
    I tried this:
    @void Client::readSocket()
    {
    QDataStream in(Socket);
    in.setVersion(QDataStream::Qt_4_0);
    if (audioincoming)
    {
    QFile f;
    f.setFileName("/tmp/test.wav");
    f.open( QIODevice::Append );
    in >> f;
    f.close();
    return;
    }
    }@
    But I can't use >> operator.

    any suggestions?
    thanks all and sorry for my horrible english.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aleskandro
      wrote on last edited by
      #2

      I modified the Client::readSocket() function to this:
      @void Client::readSocket()
      {

      if (audioincoming)
      {
      
          QAudioFormat format;
          // Set up the format, eg.
          format.setFrequency(24000);
          format.setChannels(1);
          format.setSampleSize(8);
          format.setCodec("audio/pcm");
          format.setByteOrder(QAudioFormat::LittleEndian);
          format.setSampleType(QAudioFormat::UnSignedInt);
      
          QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
          if (!info.isFormatSupported(format)) {
              qWarning()<<"raw audio format not supported by backend, cannot play audio.";
              return;
          }
      
          audio = new QAudioOutput(format, this);
          connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
          audio->setBufferSize(512000);
          audio->start(Socket);
          return;
      }
      

      }@

      Server sends audio to the client but audio lags and is very horrible.
      after about 30 secs the transferring of audio unexpectedly stops, client and server remain connected but server doesn’t send audio, or client doesn’t play audio from socket.

      moreover i receive in the client warnings:
      @ QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1024
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -1536
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -2048
      QIODevice::seek: Invalid pos: -512@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aleskandro
        wrote on last edited by
        #3

        @Client::Client(QObject *parent) :
        QObject(parent)
        {
        Socket = new QTcpSocket(this);
        connect (this->Socket,SIGNAL(connected()),this, SLOT(SendTocToc()));
        connect (this->Socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(getError()));
        connect (this->Socket,SIGNAL(readyRead()), this, SLOT(readSocket()));
        QAudioFormat format;
        // Set up the format, eg.
        format.setFrequency(32000);
        format.setChannels(1);
        format.setSampleSize(8);
        format.setCodec("audio/pcm");
        format.setByteOrder(QAudioFormat::LittleEndian);
        format.setSampleType(QAudioFormat::UnSignedInt);

          QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
          if (!info.isFormatSupported(format)) {
              qWarning()<<"raw audio format not supported by backend, cannot play audio.";
              return;
          }
        
          audio = new QAudioOutput(format, this);
          connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
          audio->setBufferSize(512000);
        

        }
        void Client::readSocket()
        {
        if (audioincoming)
        {
        if (!audio->state() == 0 )
        QTimer::singleShot(3000,this,SLOT(startAudio()));
        return;
        }
        //[...]
        }
        void Client::startAudio()
        {
        audio->start(this->Socket);
        } @

        Now it works. I initialize QAudioOutput only on class constructor but I must delay a bit to "load buffer". So I tried to start QAudioOutput object after 3 secs, but I would to use a more efficient way. If I don't delay, audio lags and QAudioOutput crashes.

        Any suggestions?

        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