Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sending wav over TCP, serializing problem?  (Read 1986 times)

0 Members and 1 Guest are viewing this topic.

elementalsc

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sending wav over TCP, serializing problem?
« on: February 28, 2018, 06:10:48 pm »
Hi!

I'm working on a voice chat built with boost::asio and when I'm on the receiving side, I hear a serie of quick "click!" instead of my original wav. I think the issue might be with how I'm marshalling/unmarshalling the data.

Could I have your opinion on this?

Here's how I split it when sending the samples:
  // Chopping SoundBuffer into buffer-length char[] filled into mByteBufferQueue
  Int16 * mSoundBufferRaw = const_cast<sf::Int16 *>(mSoundBuffer.getSamples());

  while(wBufferIndex < mSoundBuffer.getSampleCount())
  {
 
    // ... some calculation for mBlocksToConvert  

    // create char[] buffer
    for(unsigned int i = 0; i < mBlocksToConvert; ++i)
    {
      mByteBuffer[2*i]   = 0xFF & static_cast<char>(mSoundBufferRaw[wBufferIndex] >> 8);
      mByteBuffer[2*i+1] = 0xFF & static_cast<char>(mSoundBufferRaw[wBufferIndex]);
      ++wBufferIndex;
    }

    // fill queue
    mByteBufferQueue.push_back(mByteBuffer);
  }
 

This is how it's received and played:
part 1...
      std::vector<sf::Int16> wSamples;

      for (unsigned int i = 0; i < iBytesReceived; i += 2) {
        wSamples.push_back(static_cast<sf::Int16>( (mData[i] << 8) | (0x00FF & mData[i+1]) ));
      }

      mServerPtr->pushSamples(std::move(wSamples));
 
part 2...
    if((mAudioStream.getStatus() != AudioStream::Playing) && !mSamplesQueue.empty())
   {
      if(mSoundBuffer.loadFromSamples(
        reinterpret_cast<sf::Int16*>(mSamplesQueue.front().data()),
        mSamplesQueue.front().size(), 2, 48000)
      )
      {
        mAudioStream.load(mSoundBuffer);
        mAudioStream.play();
        mSamplesQueue.pop_front();
      }
    }
 
« Last Edit: February 28, 2018, 06:12:31 pm by elementalsc »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: Sending wav over TCP, serializing problem?
« Reply #1 on: February 28, 2018, 06:23:21 pm »
Get out Wireshark or log on both sides to a file and you'll see the difference. What's the point of complicating things instead of simply packing the bytes?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

elementalsc

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sending wav over TCP, serializing problem?
« Reply #2 on: February 28, 2018, 06:34:18 pm »
Quote
What's the point of complicating things instead of simply packing the bytes?

What do you mean by "simply packing the bytes"? I though that splitting int16 into two bytes and streaming them was simple enough... but I guess I'm missing something. How would you do it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sending wav over TCP, serializing problem?
« Reply #3 on: February 28, 2018, 06:53:53 pm »
Quote
What's the point of complicating things instead of simply packing the bytes?
Byte order?

Quote
Could I have your opinion on this?
I don't think it's the data, but rather how you handle it in mAudioStream. Would be interesting to see the implementation.
Laurent Gomila - SFML developer

elementalsc

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sending wav over TCP, serializing problem?
« Reply #4 on: February 28, 2018, 10:24:51 pm »
I took the Custom stream demo from the tutorial, it seems to work fine when loading and playing a buffer in the same program.

Maybe it's not working in the receiving end... other that the serializing thing, I was starting to ask myself if i was correctly "appending" SoundBuffer. I feel like I might simply be playing the same sample over and over.

    if((mAudioStream.getStatus() != AudioStream::Playing) && !mSamplesQueue.empty()) {
      if(mSoundBuffer.loadFromSamples(
        mSamplesQueue.front(),
        BUFFER_SIZE/2, 1, 44100) )
      {
        mAudioStream.load(mSoundBuffer);
        mAudioStream.play();
        mSamplesQueue.pop_front();
      }
    }
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sending wav over TCP, serializing problem?
« Reply #5 on: March 01, 2018, 08:18:21 am »
Quote
I took the Custom stream demo from the tutorial
It's not meant for real streaming, it's just a dummy example that shows what to implement in a derived sf::SoundStream. The "voip" example from the SFML SDK is a much better implementation of what you want to do.
Laurent Gomila - SFML developer

 

anything