Back to index

libsfml  1.6+dfsg2
Sound.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Audio.hpp>
00006 #include <iomanip>
00007 #include <iostream>
00008 
00009 
00014 void PlaySound()
00015 {
00016     // Load a sound buffer from a wav file
00017     sf::SoundBuffer Buffer;
00018     if (!Buffer.LoadFromFile("datas/sound/footsteps.wav"))
00019         return;
00020 
00021     // Display sound informations
00022     std::cout << "footsteps.wav :" << std::endl;
00023     std::cout << " " << Buffer.GetDuration()      << " sec"           << std::endl;
00024     std::cout << " " << Buffer.GetSampleRate()    << " samples / sec" << std::endl;
00025     std::cout << " " << Buffer.GetChannelsCount() << " channels"      << std::endl;
00026 
00027     // Create a sound instance and play it
00028     sf::Sound Sound(Buffer);
00029     Sound.Play();
00030 
00031     // Loop while the sound is playing
00032     while (Sound.GetStatus() == sf::Sound::Playing)
00033     {
00034         // Leave some CPU time for other processes
00035         sf::Sleep(0.1f);
00036 
00037         // Display the playing position
00038         std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec   ";
00039     }
00040     std::cout << std::endl << std::endl;
00041 }
00042 
00043 
00048 void PlayMusic()
00049 {
00050     // Load an ogg music file
00051     sf::Music Music;
00052     if (!Music.OpenFromFile("datas/sound/lepidoptera.ogg"))
00053         return;
00054 
00055     // Display music informations
00056     std::cout << "lepidoptera.ogg :" << std::endl;
00057     std::cout << " " << Music.GetDuration()      << " sec"           << std::endl;
00058     std::cout << " " << Music.GetSampleRate()    << " samples / sec" << std::endl;
00059     std::cout << " " << Music.GetChannelsCount() << " channels"      << std::endl;
00060 
00061     // Play it
00062     Music.Play();
00063 
00064     // Loop while the music is playing
00065     while (Music.GetStatus() == sf::Music::Playing)
00066     {
00067         // Leave some CPU time for other processes
00068         sf::Sleep(0.1f);
00069 
00070         // Display the playing position
00071         std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Music.GetPlayingOffset() << " sec   ";
00072     }
00073     std::cout << std::endl;
00074 }
00075 
00076 
00083 int main()
00084 {
00085     // Play a sound
00086     PlaySound();
00087 
00088     // Play a music
00089     PlayMusic();
00090 
00091     // Wait until the user presses 'enter' key
00092     std::cout << "Press enter to exit..." << std::endl;
00093     std::cin.ignore(10000, '\n');
00094 
00095     return EXIT_SUCCESS;
00096 }