1
Audio / Piano fast in windows, slow in linux?
« on: August 01, 2009, 12:51:47 pm »
no it isn't
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
#include <iomanip>
#include <iostream>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
int a;
std::cin >> a;
std::cout << "TEST" << std::endl;
// Load a sound buffer from a wav file
sf::SoundBuffer Buffer;
if (!Buffer.LoadFromFile("sound.wav"))
return EXIT_FAILURE;
// Display sound informations
std::cout << "sound.wav :" << std::endl;
std::cout << " " << Buffer.GetDuration() << " sec" << std::endl;
std::cout << " " << Buffer.GetSampleRate() << " samples / sec" << std::endl;
std::cout << " " << Buffer.GetChannelsCount() << " channels" << std::endl;
// Create a sound instance and play it
sf::Sound Sound(Buffer);
Sound.Play();
// Loop while the sound is playing
while (Sound.GetStatus() == sf::Sound::Playing)
{
// Display the playing position
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec";
// Leave some CPU time for other threads
sf::Sleep(0.1f);
}
std::cout << std::endl;
// Wait until the user presses 'enter' key
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
return EXIT_SUCCESS;
}
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
sf::Sound Piano[100];
sf::SoundBuffer PianoSound;
void PlayPiano(float pitch);
int width = sf::VideoMode::GetMode(0).Width;
int height = sf::VideoMode::GetMode(0).Height;
int main()
{
PianoSound.LoadFromFile( "piano.wav");
//I know I shouldnt be doing this, but I dont know of any other way, Im still learning
for (int i = 0;i < 100; i++){
Piano[i].SetBuffer(PianoSound);
}
// 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){
// insert cases here.
case (sf::Key::Escape):
App.Close();
running = false;
break;
default:
break;
}
}
}
}
return EXIT_SUCCESS;
}
void PlayPiano(float pitch){
for (int i = 0; i < 100; i++){
if (Piano[i].GetStatus() != Piano[i].Playing){
Piano[i].Stop();
Piano[i].SetPitch (pitch);
Piano[i].Play();
break;
}
}
}
#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;
}
}
}