when running setPitch() on a sound, the sound's pitch does not actually change. it should be noted that there are no errors in build log or build messages, and the issue still persists after rebuilding
#include <math.h>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "leh");
note1.loadFromFile("notes/c.ogg");
//a 1 minute sound file containing note C at octave 0
pNote1.setBuffer(note1);
pNote1.setPitch(pow(2, 0));
//this should set the pitch of pNote1 to a c at octave 4
//the actual heard note is much lower
pNote1.play();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.display();
}
return 0;
}
is there an upper limit to how much i can multiply a sound's pitch by?
i am using SFML 2.5.1 for GCC 7.3.0 MinGW (SEH) - 64-bit on windows 10 with code::blocks 20.03
I hope the code is just an example and you do realize that 2^0 equals 1 which is the default pitch value :D
Anyways, I've put together a quick test and things are working as expected.
https://youtu.be/V80o9-3XZug
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "leh");
window.setFramerateLimit(30.f);
auto note1 = sf::SoundBuffer{};
if (!note1.loadFromFile("Air 063.ogg"))
{
return -1;
}
auto pitch = 1.f;
auto pNote1 = sf::Sound{};
pNote1.setBuffer(note1);
pNote1.play();
while (window.isOpen())
{
for (auto event = sf::Event{}; window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Up)
{
pitch += 0.1f;
}
else if (event.key.code == sf::Keyboard::Down && pitch > 0.1f)
{
pitch -= 0.1f;
}
pNote1.stop();
pNote1.setPitch(pitch);
pNote1.play();
}
}
window.display();
}
}
I hope the code is just an example and you do realize that 2^0 equals 1 which is the default pitch value :D
Anyways, I've put together a quick test and things are working as expected.
-snip-
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "leh");
window.setFramerateLimit(30.f);
auto note1 = sf::SoundBuffer{};
if (!note1.loadFromFile("Air 063.ogg"))
{
return -1;
}
auto pitch = 1.f;
auto pNote1 = sf::Sound{};
pNote1.setBuffer(note1);
pNote1.play();
while (window.isOpen())
{
for (auto event = sf::Event{}; window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Up)
{
pitch += 0.1f;
}
else if (event.key.code == sf::Keyboard::Down && pitch > 0.1f)
{
pitch -= 0.1f;
}
pNote1.stop();
pNote1.setPitch(pitch);
pNote1.play();
}
}
window.display();
}
}
oops, yes this is an example and i meant to put 2^4, not 2^0 (with the exponent being the octave i want to raise the note to)
regardless, it only seems to change the pitch until 2^2, after which there isnt any audible change in pitch
linking the libopenal32 library didnt seem to fix things