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

Author Topic: Audio streaming problems  (Read 4118 times)

0 Members and 1 Guest are viewing this topic.

ronag

  • Newbie
  • *
  • Posts: 24
    • View Profile
Audio streaming problems
« on: September 03, 2010, 09:21:16 pm »
I've got a problem with the sf::SoundStream class where the OnGetData is not called often enough and the "externalChunks_" buffer is filled much faster than its emptied. Thus I get delayed and stuttering audio.

What am I doing wrong?

Code: [Select]
class SoundChannel : public sf::SoundStream
{
public:
SoundChannel() : internalChunks_(25) // Keep data for 25 frames = 1 sec
{
Initialize(2, 48000);
}

~SoundChannel()
{
Stop();
}

void Push(const AudioChunkPtr& pAudioChunk) // Called once per frame, push in audio data for one frame
{
externalChunks_.push(pAudioChunk); // pushes ALOT more than is popped
if(GetStatus() != Playing)
Play();
}

private:

bool OnGetData(sf::SoundStream::Chunk& data) // Should be called much more often
    {
AudioChunkPtr pChunk;
externalChunks_.pop(pChunk); // doesn't empty fast enough
internalChunks_.push_back(pChunk); // Keep memory allocated while the audio is played

data.Samples = reinterpret_cast<sf::Int16*>(pChunk->GetDataPtr());
               
data.NbSamples = 3840; // Each "AudioChunk" has enough audio data for one frame, 7680 bytes per frame in 25 fps, 1 sample = 2 bytes
        return true;
    }

boost::circular_buffer<AudioChunkPtr> internalChunks_;
tbb::concurrent_bounded_queue<AudioChunkPtr> externalChunks_;
};


EDIT:

If i change the samplerate to 24000 i get rid of the stuttering... however it sounds very wierd and is delayed.

Iprobably should mention im using SFML 1.6

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Audio streaming problems
« Reply #1 on: September 04, 2010, 03:21:31 am »
Isn't the sample rate supposed to be 44100 instead of 48000 ?

http://en.wikipedia.org/wiki/Sampling_rate#Audio
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Audio streaming problems
« Reply #2 on: September 04, 2010, 09:47:35 am »
Quote
I've got a problem with the sf::SoundStream class where the OnGetData is not called often enough and the "externalChunks_" buffer is filled much faster than its emptied. Thus I get delayed and stuttering audio.

I'll test that and I'll let you know if there's anything wrong.

Quote
Isn't the sample rate supposed to be 44100 instead of 48000 ?

The sound rate can be any value, 44100 is just the standard "CD" rate.
Laurent Gomila - SFML developer

ronag

  • Newbie
  • *
  • Posts: 24
    • View Profile
Audio streaming problems
« Reply #3 on: September 04, 2010, 11:06:55 am »
Quote from: "Ceylo"
Isn't the sample rate supposed to be 44100 instead of 48000 ?

http://en.wikipedia.org/wiki/Sampling_rate#Audio


I'm using ffmpeg and the file im trying to play says its 48000. I just hardcoded to make the example simpler.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Audio streaming problems
« Reply #4 on: September 05, 2010, 11:11:02 am »
Hi

I've found the source of the problem, and fixed it in SFML 2.

If you want to make it work in SFML 1.6 you will have to modify it. If you can't (or don't want to), the only thing that you can do is to send more samples (at least 1/10 of sample rate) in OnGetData.
Laurent Gomila - SFML developer

ronag

  • Newbie
  • *
  • Posts: 24
    • View Profile
Audio streaming problems
« Reply #5 on: September 05, 2010, 11:57:58 am »
I can modify 1.6. I'm working on a business critical application and would prefer to keep using 1.6 while 2.0 is maturing.

Could you send me the required changes?

Thank you.

EDIT:

Found it on SVN. Thx.

ronag

  • Newbie
  • *
  • Posts: 24
    • View Profile
Audio streaming problems
« Reply #6 on: September 05, 2010, 12:10:19 pm »
Wouldn't it be better if the Sleep time depended on the sample rate. Instead of a fixed value.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Audio streaming problems
« Reply #7 on: September 05, 2010, 12:35:44 pm »
Quote
Wouldn't it be better if the Sleep time depended on the sample rate. Instead of a fixed value.

Nop.

The sleep functions provided by the OS are not accurate enough, such a calculation wouldn't be reliable.

And the current solution is ok, I won't implement something more complicated unless it's really needed ;)
Laurent Gomila - SFML developer

  • Guest
Audio streaming problems
« Reply #8 on: September 28, 2010, 10:53:15 am »
I have liked your opinion on this kind post. I am going to subscribe your post and will come here again to know more. Thanks,