Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. hear only noises with QAudioOutput
Qt 6.12 LTS is out! See what's new in the release blog

hear only noises with QAudioOutput

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

    Hi, I work with audio capture from microphone.

    This is server

        QAudioRecorder* recorder = new QAudioRecorder();
        QAudioProbe* probe = new QAudioProbe;
    
        connect(probe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(processBuffer(QAudioBuffer)));
        
    
        QAudioEncoderSettings audioSettings;
        audioSettings.setCodec("audio/mpeg");
        audioSettings.setQuality(QMultimedia::HighQuality);
    
        recorder->setEncodingSettings(audioSettings);
    
        
        qDebug() << "probe ritorna " << probe->setSource(recorder); // Returns true, hopefully.
        //qDebug() << "" << recorder->setOutputLocation(QUrl::fromLocalFile("test"));
    
        recorder->record(); // Now we can do things like calculating levels or performing an FFT    
        
        myAudioServer = new MyAudioServer();
        myAudioServer->startServer();
    

    This is the way as trasfer to socket

    void QtVideoWidgetsIssueTrack::processBuffer(const QAudioBuffer& buffer){
        QByteArray byteArr;
        byteArr.append(buffer.constData<char>(), buffer.byteCount());
        QByteArray Data = byteArr;
        qDebug() << myAudioServer->isListening();
        QTcpSocket* myAudioClient = myAudioServer->getSocket();
        qDebug() << myAudioClient;
        qDebug() << "in processBuffer";
        if (myAudioClient != nullptr) {
            myAudioClient->write(Data, Data.count());
            myAudioClient->waitForBytesWritten();
        }
    }
    

    Now the client

    void MyThreadAudioTcpSocket::readyRead()
    {
    
        
    
        while (socket->bytesAvailable() > 0) {
    
            //fare il play da QByteArray
              // get default output device
            QByteArray* yourSoundData = new QByteArray(socket->readAll());
            QBuffer* buffer = new QBuffer;
            buffer->setData(yourSoundData->data(),yourSoundData->size());
            buffer->open(QBuffer::ReadOnly);
            
            QAudioFormat format;
            format.setSampleSize(16);
            format.setSampleRate(22050);
            format.setChannelCount(1);
            format.setCodec("audio/mpeg");
            format.setByteOrder(QAudioFormat::LittleEndian);
            format.setSampleType(QAudioFormat::UnSignedInt);
    
            QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
            if (!info.isFormatSupported(format)) {
                format = info.nearestFormat(format);
                qDebug() << "formato non supportato";
            }
           
            QAudioOutput *output = new QAudioOutput(format);
            output->moveToThread(this);
            output->start(buffer);
        }
    }
    

    I attach what I hear, only noise... !
    wav file

    Someone can You help me to understand where wrong in source code.

    Christian EhrlicherC 1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zabitqt
      wrote on last edited by
      #2

      Someone help me

      1 Reply Last reply
      0
      • Z zabitqt

        Hi, I work with audio capture from microphone.

        This is server

            QAudioRecorder* recorder = new QAudioRecorder();
            QAudioProbe* probe = new QAudioProbe;
        
            connect(probe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(processBuffer(QAudioBuffer)));
            
        
            QAudioEncoderSettings audioSettings;
            audioSettings.setCodec("audio/mpeg");
            audioSettings.setQuality(QMultimedia::HighQuality);
        
            recorder->setEncodingSettings(audioSettings);
        
            
            qDebug() << "probe ritorna " << probe->setSource(recorder); // Returns true, hopefully.
            //qDebug() << "" << recorder->setOutputLocation(QUrl::fromLocalFile("test"));
        
            recorder->record(); // Now we can do things like calculating levels or performing an FFT    
            
            myAudioServer = new MyAudioServer();
            myAudioServer->startServer();
        

        This is the way as trasfer to socket

        void QtVideoWidgetsIssueTrack::processBuffer(const QAudioBuffer& buffer){
            QByteArray byteArr;
            byteArr.append(buffer.constData<char>(), buffer.byteCount());
            QByteArray Data = byteArr;
            qDebug() << myAudioServer->isListening();
            QTcpSocket* myAudioClient = myAudioServer->getSocket();
            qDebug() << myAudioClient;
            qDebug() << "in processBuffer";
            if (myAudioClient != nullptr) {
                myAudioClient->write(Data, Data.count());
                myAudioClient->waitForBytesWritten();
            }
        }
        

        Now the client

        void MyThreadAudioTcpSocket::readyRead()
        {
        
            
        
            while (socket->bytesAvailable() > 0) {
        
                //fare il play da QByteArray
                  // get default output device
                QByteArray* yourSoundData = new QByteArray(socket->readAll());
                QBuffer* buffer = new QBuffer;
                buffer->setData(yourSoundData->data(),yourSoundData->size());
                buffer->open(QBuffer::ReadOnly);
                
                QAudioFormat format;
                format.setSampleSize(16);
                format.setSampleRate(22050);
                format.setChannelCount(1);
                format.setCodec("audio/mpeg");
                format.setByteOrder(QAudioFormat::LittleEndian);
                format.setSampleType(QAudioFormat::UnSignedInt);
        
                QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
                if (!info.isFormatSupported(format)) {
                    format = info.nearestFormat(format);
                    qDebug() << "formato non supportato";
                }
               
                QAudioOutput *output = new QAudioOutput(format);
                output->moveToThread(this);
                output->start(buffer);
            }
        }
        

        I attach what I hear, only noise... !
        wav file

        Someone can You help me to understand where wrong in source code.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @zabitqt said in hear only noises with QAudioOutput:

        void MyThreadAudioTcpSocket::readyRead()
        {

        while (socket->bytesAvailable() > 0) {
        
            //fare il play da QByteArray
              // get default output device
            QByteArray* yourSoundData = new QByteArray(socket->readAll());
            QBuffer* buffer = new QBuffer;
            buffer->setData(yourSoundData->data(),yourSoundData->size());
            buffer->open(QBuffer::ReadOnly);
            
            QAudioFormat format;
            format.setSampleSize(16);
            format.setSampleRate(22050);
            format.setChannelCount(1);
            format.setCodec("audio/mpeg");
            format.setByteOrder(QAudioFormat::LittleEndian);
            format.setSampleType(QAudioFormat::UnSignedInt);
        
            QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
            if (!info.isFormatSupported(format)) {
                format = info.nearestFormat(format);
                qDebug() << "formato non supportato";
            }
           
            QAudioOutput *output = new QAudioOutput(format);
            output->moveToThread(this);
            output->start(buffer);
        }
        

        }

        Are you serious? If there ould be a contest to create as much memleaks as possible this would be probably under the top ten.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Z 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @zabitqt said in hear only noises with QAudioOutput:

          void MyThreadAudioTcpSocket::readyRead()
          {

          while (socket->bytesAvailable() > 0) {
          
              //fare il play da QByteArray
                // get default output device
              QByteArray* yourSoundData = new QByteArray(socket->readAll());
              QBuffer* buffer = new QBuffer;
              buffer->setData(yourSoundData->data(),yourSoundData->size());
              buffer->open(QBuffer::ReadOnly);
              
              QAudioFormat format;
              format.setSampleSize(16);
              format.setSampleRate(22050);
              format.setChannelCount(1);
              format.setCodec("audio/mpeg");
              format.setByteOrder(QAudioFormat::LittleEndian);
              format.setSampleType(QAudioFormat::UnSignedInt);
          
              QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
              if (!info.isFormatSupported(format)) {
                  format = info.nearestFormat(format);
                  qDebug() << "formato non supportato";
              }
             
              QAudioOutput *output = new QAudioOutput(format);
              output->moveToThread(this);
              output->start(buffer);
          }
          

          }

          Are you serious? If there ould be a contest to create as much memleaks as possible this would be probably under the top ten.

          Z Offline
          Z Offline
          zabitqt
          wrote on last edited by
          #4

          @Christian-Ehrlicher Ok but I don't think the problem are memleaks... I don't realease Memory correctly and close connection but... Can you suggest me how improve code please. It's my first project in c++

          Christian EhrlicherC 1 Reply Last reply
          0
          • Z zabitqt

            @Christian-Ehrlicher Ok but I don't think the problem are memleaks... I don't realease Memory correctly and close connection but... Can you suggest me how improve code please. It's my first project in c++

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @zabitqt said in hear only noises with QAudioOutput:

            It's my first project in c++

            Then please start with something easier - the Qt examples are a good start

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1

            • Login

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