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

Author Topic: setPitch() function only sets pitch up to a certain point  (Read 5237 times)

0 Members and 1 Guest are viewing this topic.

kant

  • Newbie
  • *
  • Posts: 4
    • View Profile
setPitch() function only sets pitch up to a certain point
« on: April 17, 2022, 05:44:31 am »
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
« Last Edit: April 17, 2022, 06:01:51 am by kant »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: setPitch() function only sets pitch up to a certain point
« Reply #1 on: April 17, 2022, 04:06:36 pm »
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.



#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();
    }
}
« Last Edit: April 17, 2022, 04:08:44 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kant

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: setPitch() function only sets pitch up to a certain point
« Reply #2 on: April 17, 2022, 08:01:49 pm »
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

kant

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: setPitch() function only sets pitch up to a certain point
« Reply #3 on: April 18, 2022, 08:43:58 am »
i tested the code on another system and as it turns out, the issue seems to only be on my system
i am not sure why this is the case, but i am glad to see that my code does work
i will however, leave this thread open until i find that the reason to setPitch() not setting pitch correctly can be attributed to cursed silicon magic

edit: re-linking my code on the other system to use SFML visual c++ 2017 rather than SFML for minGW 7.3.0 64 bit has strangely enough not broken anything

since this was my only idea as to why things may be breaking, i will be open to suggestions as to why SFML does not set pitch correctly on my system. otherwise, this is simply another case of cursed silicon magick
« Last Edit: April 18, 2022, 08:52:53 am by kant »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: setPitch() function only sets pitch up to a certain point
« Reply #4 on: April 18, 2022, 10:17:38 am »
The OpenAL specification says that the limit for pitch changing is implementation dependent.

What audio hardware does your system have?

Perhaps you could go the opposite way: start with a high octave in the sample and reduce the pitch as needed.

kant

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: setPitch() function only sets pitch up to a certain point
« Reply #5 on: April 18, 2022, 08:26:06 pm »
The OpenAL specification says that the limit for pitch changing is implementation dependent.

What audio hardware does your system have?

Perhaps you could go the opposite way: start with a high octave in the sample and reduce the pitch as needed.

my system doesnt have any special hardware, just what comes with the motherboard
it should be noted that the other system i tested the code on was a laptop, so i dont think that had any special sound equipment either

i will try your suggestion later, and report back with results