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

Author Topic: Confusing error message in sf::SoundBuffer  (Read 3618 times)

0 Members and 3 Guests are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Confusing error message in sf::SoundBuffer
« on: September 26, 2013, 09:27:02 pm »
Hallo!
While I was working on the implementation of the capture device selection I noticed something that's a little weird. If you try to save an empty sf::SoundBuffer you'll get a confusion error message.
When you run this code:
#include <SFML\Audio.cpp>

int main()
{
    sf::SoundBuffer buffer;
    buffer.saveToFile("example.wav");
}
 
You will get this console output: Failed to create sound file "example.wav" (Format not recogized)
I tryed different formats (.wav, .ogg, ...) but they all failed. If there are sampled in the buffer the program runs as expected.
I fired up my debugger and saw that the error message is caused by the failure of sf_open() in sf::SoundFile::openWrite(). I checked the format that is passed to sf_open (fileInfos struct) and it matches the one for signed 16-bit WAV. I also noticed that channels and sampleRate are set to 0. So what I'm thinking is that sf_open fails with the error message "Format not recognized" eventhough the format is correct, but when the channel count and the sample rate is still at zero.

Wouldn't it be a good idea to check if the buffer contains samples before saving it? That way there could be a more discribtiv error message (i.e. "Nothing to record").

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Confusing error message in sf::SoundBuffer
« Reply #1 on: September 26, 2013, 10:15:20 pm »
I have a better solution: set an arbitrary channel count and sample rate, and let libsndfile produce a valid empty sound file, as one would expect when running the code that you posted.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Confusing error message in sf::SoundBuffer
« Reply #2 on: September 27, 2013, 12:00:35 am »
It's not as easy as it looks on first sight.
But adding the following line at the end of the constructor of sf::SoundBuffer fixes the problem and produces an empty file:
alCheck(alBufferData(m_buffer, priv::AudioDevice::getFormatFromChannelCount(2) , &m_samples[0], 0, 44100));
Would you like me to send a commit for that, so it's off your mind or do you want to add small stuff like that yourself?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Confusing error message in sf::SoundBuffer
« Reply #3 on: September 27, 2013, 07:39:44 am »
&m_samples[0] is an undefined behaviour if m_samples is empty.

And why do you need to actually generate data at OpenAL level, rather than just providing fake parameters to sf_open?

What exactly is not so easy?
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Confusing error message in sf::SoundBuffer
« Reply #4 on: September 27, 2013, 11:07:50 am »
Ah how could I've missed that! What an obvious stupid mistake.

Well the thing that doesn't make this so easy, is that sf::SoundBuffer doesn't store the channel count and the sample rate in a dedicated variable. So when sf::SoundFile::openWrite() is called the parameters passed to it are acquired by using getChannelCount() and getSampleRate(). These functions internally use alGetBufferi() and that requires that the Buffer data is set before hand to some default.

edit: Unless of course you check in saveToFile() if the buffer is empty and then call openWrite() with fake parameters. But I find the other way more generic.
« Last Edit: September 27, 2013, 11:09:47 am by Foaly »

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Confusing error message in sf::SoundBuffer
« Reply #5 on: October 19, 2013, 12:31:58 am »
What should we do about this?

 

anything