-
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?
-
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).
-
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?
-
Maybe most sounds start with a least a few milliseconds of silence? Yoiu should print values from the middle of the sample array.
-
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;
}
}
-
Thank you, it's working now!