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

Author Topic: What values should I expect to be returned from buffer.getSample()?  (Read 3571 times)

0 Members and 1 Guest are viewing this topic.

kirilzh

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: What values should I expect to be returned from buffer.getSample()?
« Reply #1 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).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kirilzh

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: What values should I expect to be returned from buffer.getSample()?
« Reply #2 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/ 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?
« Last Edit: February 20, 2018, 10:09:39 am by kirilzh »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: What values should I expect to be returned from buffer.getSample()?
« Reply #3 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.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: What values should I expect to be returned from buffer.getSample()?
« Reply #4 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.



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;
        }

}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kirilzh

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: What values should I expect to be returned from buffer.getSample()?
« Reply #5 on: February 20, 2018, 02:34:09 pm »
Thank you, it's working now!