• Piano fast in windows, slow in linux?
    13 replies, posted
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 [b]instantly[/b]. 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: [cpp]#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; } } } [/cpp] The windows code is about the same and it runs perfectly. about the "PLAYING TONE" cout: WHen I press the I button, I [b]immediately[/b] see "PLAYING TONE" in the console screen, but the sound plays a second later! I made some couts
If no one here can help you, you might want to make a post on the official SFML forums about it. I've seen them be pretty helpful to people.
Posted on sfml forums: [url]http://www.sfml-dev.org/forum/viewforum.php?f=8[/url]
[cpp] case (sf::Key::Escape): App.Close(); running = false; break;//not needed default://not needed break;//not needed[/cpp] This won't help you at all, but you are still learning (I am a C++ newbie too) so I thought it could be useful for...... I don't know what.
[QUOTE=Robber;16194667][cpp] case (sf::Key::Escape): App.Close(); running = false; break;//not needed default://not needed break;//not needed[/cpp] This won't help you at all, but you are still learning (I am a C++ newbie too) so I thought it could be useful for...... I don't know what.[/QUOTE] No. Don't do that. Saving the typing of 6 characters isn't worth the potential time he'll spent looking for a bug if he adds another case.
[QUOTE=gparent;16195549]No. Don't do that. Saving the typing of 6 characters isn't worth the potential time he'll spent looking for a bug if he adds another case.[/QUOTE] Hm, didn't think of adding another case. I guess you are right. By the way, it's more than 6 characters. :downs: [B]Edit:[/B] The default case is still not needed. :smug:
[QUOTE=Robber;16195991]Hm, didn't think of adding another case. I guess you are right. By the way, it's more than 6 characters. :downs: [B]Edit:[/B] The default case is still not needed. :smug:[/QUOTE] I know, but if I don't add it I get warnings about some keys not being treated in the cases.
[QUOTE=FPtje;16196348]I know, but if I don't add it I get warnings about some keys not being treated in the cases.[/QUOTE] Oh ok. Didn't know that.
[cpp] sf::SoundBuffer PianoSound; if (!PianoSound.LoadFromFile("piano.ogg")) { std::cout << "File not found: piano.ogg" << std::endl; return 1; } sf::Sound Piano[100]; for(int i = 0; i < 100; i++) Piano[i].SetBuffer(PianoSound); [/cpp] Also, why are you stopping an sf::Music even though you know it isn't playing? (line 69)
[QUOTE=jA_cOp;16201193] Also, why are you stopping an sf::Music even though you know it isn't playing? (line 69)[/QUOTE] It fixed the "Failed to create thread" error when having pressed a lot of keys, I don't know why. And thanks a lot for the more efficient code!
Edit: just noticed that it was fixed in post 9, which would explain the failed to create thread, as the music system creates new threads to stream the audio files. (see Multi-threading at the bottom of this:[url]http://www.sfml-dev.org/tutorials/1.5/audio-streams.php[/url]) Shouldn't you be using an sf::Sound instead of making it stream a file every time you press a key?
[QUOTE=BMCHa;16202907]Edit: just noticed that it was fixed in post 9, which would explain the failed to create thread, as the music system creates new threads to stream the audio files. (see Multi-threading at the bottom of this:[url]http://www.sfml-dev.org/tutorials/1.5/audio-streams.php[/url]) Shouldn't you be using an sf::Sound instead of making it stream a file every time you press a key?[/QUOTE] If he used my example code, he already switched to sf::Sound :)
[QUOTE=Robber;16195991]By the way, it's more than 6 characters. :downs:[/QUOTE] I was counting "break;". Which is 6. Removing the default case causes a compiler warning. [QUOTE=Robber;16195991]The default case is still not needed. :smug:[/QUOTE] See above. :smug:
[QUOTE=jA_cOp;16203113]If he used my example code, he already switched to sf::Sound :)[/QUOTE] I did use your example, thanks a lot. But the problem is still there! the sound plays really late...
Sorry, you need to Log In to post a reply to this thread.