SFML community forums

Help => Audio => Topic started by: kirilzh on February 18, 2018, 08:07:53 pm

Title: What values should I expect to be returned from buffer.getSample()?
Post by: kirilzh on February 18, 2018, 08:07:53 pm
I have an array
const sf::Int16* samples = buffer.getSamples();
and I access it like
samples[i];
and I get values ranging from -4 to 4.

Is this what the output should be because samples are returned as 16 bits signed integers and I was looking for values ranging from −32,768:32,767?
Title: Re: What values should I expect to be returned from buffer.getSample()?
Post by: eXpl0it3r on February 18, 2018, 09:44:23 pm
The full range is a signed 16-bits integer. If you get only low values, your buffer simply isn't louder. The returned value is the amplitude at the given position. Additionally the channels are interleaved. So if you have for example stereo sound it would be LRLRLRLR... (not sure it it really starts with left, guess it just takes the higher channel count).
Title: Re: What values should I expect to be returned from buffer.getSample()?
Post by: kirilzh on February 20, 2018, 10:07:52 am
When I try to find the samples of other files I get only zeros. Here's my code. This https://freesound.org/people/reaktorplayer/sounds/99674/ (https://freesound.org/people/reaktorplayer/sounds/99674/) is the reaktor.wav I'm using.
  sf::SoundBuffer buffer;

  if(!buffer.loadFromFile("reaktor.wav")){
    std::cout << "didn't load properly";
    return -1;
  }

  const sf::Int16* samples = buffer.getSamples();

  for (int i = 0; i < 44; i++){
    std::cout << samples[i] << std::endl;
  }
 

Should I be doing something differently?
Title: Re: What values should I expect to be returned from buffer.getSample()?
Post by: Laurent on February 20, 2018, 12:55:42 pm
Maybe most sounds start with a least a few milliseconds of silence? Yoiu should print values from the middle of the sample array.
Title: Re: What values should I expect to be returned from buffer.getSample()?
Post by: eXpl0it3r on February 20, 2018, 01:43:17 pm
Yep, pretty sure it's what Laurent said. 45 samples is just the very, very beginning of the sound.

If you let it iterate over the whole array, you'll get lots of different values.

(https://i.imgur.com/TTjmp9I.png)

Updated code:

#include <iostream>

#include <SFML/Audio.hpp>

int main() {

        sf::SoundBuffer buffer;

        if (!buffer.loadFromFile("reaktor.wav")) {
                std::cout << "didn't load properly";
                return -1;
        }

        const auto samples = buffer.getSamples();

        for (std::size_t i = 0; i < buffer.getSampleCount(); i++) {
                std::cout << samples[i] << std::endl;
        }

}
Title: Re: What values should I expect to be returned from buffer.getSample()?
Post by: kirilzh on February 20, 2018, 02:34:09 pm
Thank you, it's working now!