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

Author Topic: Piano fast in windows, slow in linux?  (Read 16631 times)

0 Members and 1 Guest are viewing this topic.

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« on: July 22, 2009, 11:43:01 am »
Hi I'm making a piano program(simple console application using sfml to play piano on the computer keyboard).

When I compile it in windows everything is superfast. When I press a button on my keyboard the sound plays instantly.

So yesterday I installed code::blocks on my linux mint gloria 7 installation on my laptop. I had some trouble setting everything up but in the end I got everything to work. The problem is, when I compile it in linux, with gcc compiler and linux settings(no .exe in wine :v:) it's slower!

I press a key on the keyboard, but the sound plays about a second later!

Here is the code:
Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

sf::Music Piano[100];

void PlayPiano(float pitch);
int width = sf::VideoMode::GetMode(0).Width;
int height = sf::VideoMode::GetMode(0).Height;

int main()
{
    //I know I shouldnt be doing this, but I dont know of any other way, Im still learning. Could you help me with this too?
    for (int i = 0;i < 100; i++){
        //Piano[i].OpenFromMemory(Piano[0]);
        Piano[i].OpenFromFile("piano.ogg");
    }

    // Create the main window
    sf::RenderWindow App(sf::VideoMode::GetMode(0), "Piano!", sf::Style::Fullscreen);
    // Piano picture
sf::Image pianopic;
pianopic.LoadFromFile("PianoPic.tga");


sf::Sprite PianoSprite;
PianoSprite.SetImage(pianopic);
    PianoSprite.SetPosition(0,0);
    PianoSprite.SetCenter(0, 0);
    PianoSprite.Resize(width, height);

App.Clear(sf::Color(0, 100, 0));
App.Draw(PianoSprite);
    App.Display();

    sf::Event PressKey;
    std::cout << "Start playing the piano!"<<std::endl << "Press escape to quit!" << std::endl;
    // Start main loopv
    bool running = true;
    while (running){
        while(App.GetEvent(PressKey)){
            if (PressKey.Type == sf::Event::KeyPressed) {

              //Toets ingedrukt
                switch(PressKey.Key.Code){
                    //I removed some cases here, I only left the escape and I button, but they are there in the real program.
                    case(sf::Key::I):
                        PlayPiano(1);
                        break;
                    case (sf::Key::Escape):
                        App.Close();
                        running = false;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    return EXIT_SUCCESS;
}

void PlayPiano(float pitch){
//If one piano sound is also playing, then play the next piano sound(this way you can use multiple tones at the same time)
    for (int i = 0; i < 100; i++){
        if (Piano[i].GetStatus() != Piano[i].Playing){
             Piano[i].Stop();
             Piano[i].SetPitch (pitch);
             std::cout << "PLAYING TONE" << std::endl;
             Piano[i].Play();
             break;
        }
    }
}


The windows code is about the same and it runs perfectly.

about the "PLAYING TONE" cout:
WHen I press the I button, I immediately see "PLAYING TONE" in the console screen, but the sound plays a second later!
I made some couts

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #1 on: July 22, 2009, 11:47:38 am »
Which OpenAL package/version is installed on your Linux?
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #2 on: July 22, 2009, 11:48:34 am »
I don't know, how do I find out?

Synaptic says I have 1:1.4.272-2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #3 on: July 22, 2009, 12:05:37 pm »
Try upgrading to the latest version.
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #4 on: July 22, 2009, 12:23:45 pm »
it IS the latest version:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #5 on: July 22, 2009, 12:29:58 pm »
Maybe you can get it from the official website (look for OpenAL-Soft, not OpenAL) and recompile the actual latest version, which is 1.8.466.

It's going to be hard to know if the problem is in SFML or in an external library :?
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #6 on: July 22, 2009, 12:43:08 pm »
Luckilly I found the the latest version and managed to compile + install it correctly.

however this has no effect! I still have the same problem!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #7 on: July 22, 2009, 01:12:38 pm »
You can try this: construct your sf::Music instances with a parameter such as 10000, instead of using the default constructor. You'll have to tweak you code, as you can only use the default constructor with static arrays.

Then tell me if the delay is shorter, or if it doesn't make any difference.
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #8 on: July 22, 2009, 02:05:59 pm »
I'm sorry, I'm new to C++, what exactly do I do with my code to do this? I can't seem to figure it out...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #9 on: July 22, 2009, 02:15:21 pm »
Here is a quick and dirty replacement:
Code: [Select]
...
std::vector<sf::Music*> Piano;
...

int main()
{
    Piano.resize(100, new sf::Music(10000));
    ...

    for (int i = 0; i < 100; ++i)
        delete Piano[i];

    return EXIT_SUCCESS;
}

Try different values instead of 10000, and tell me if it makes the delay shorter or longer.
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #10 on: July 22, 2009, 02:32:38 pm »
This line errors:
Piano.OpenFromFile("piano.ogg");

when I do that it errors:
/home/falco/C++/Projects/linux piano/linux piano/main.cpp|21|error: request for member ‘OpenFromFile’ in ‘Piano.std::vector<_Tp, _Alloc>::operator[] [with _Tp = sf::Music*, _Alloc = std::allocator<sf::Music*>](((unsigned int)i))’, which is of non-class type ‘sf::Music*’|

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #11 on: July 22, 2009, 02:40:33 pm »
The elements of your array are now pointers, so you have to replace every Piano. with Piano->
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #12 on: July 22, 2009, 02:46:06 pm »
Ok it works.

Changing the sample rate has no effect at all. I've tried 96000 44100 and 10000. All the same response time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Piano fast in windows, slow in linux?
« Reply #13 on: July 22, 2009, 02:59:45 pm »
Ok, thanks for your help. I'm really starting to think that this issue has nothing to do with SFML. I'll try your program at home to see whether I can reproduce the problem or not.

PS: it is not the sample rate, it is the size of the internal buffer ;)
Laurent Gomila - SFML developer

FPtje

  • Newbie
  • *
  • Posts: 14
    • View Profile
Piano fast in windows, slow in linux?
« Reply #14 on: July 22, 2009, 03:16:22 pm »
Ok thanks

What I noticed with the vector by the way is that I can't play multiple tones at the same time.

(I guess all the entries in the vector are pointing at the same sf::Music thing)

But that is my problem, I'll try to fix that myself :)

 

anything