2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Audio.hpp>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Play a sound
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void PlaySound()
|
|
|
|
{
|
|
|
|
// Load a sound buffer from a wav file
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::SoundBuffer buffer;
|
|
|
|
if (!buffer.LoadFromFile("datas/sound/footsteps.wav"))
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Display sound informations
|
|
|
|
std::cout << "footsteps.wav :" << std::endl;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << " " << buffer.GetDuration() << " sec" << std::endl;
|
|
|
|
std::cout << " " << buffer.GetSampleRate() << " samples / sec" << std::endl;
|
|
|
|
std::cout << " " << buffer.GetChannelsCount() << " channels" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Create a sound instance and play it
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Sound sound(buffer);
|
|
|
|
sound.Play();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Loop while the sound is playing
|
2009-07-12 06:17:24 +08:00
|
|
|
while (sound.GetStatus() == sf::Sound::Playing)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Leave some CPU time for other processes
|
|
|
|
sf::Sleep(0.1f);
|
2009-02-27 23:03:20 +08:00
|
|
|
|
|
|
|
// Display the playing position
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.GetPlayingOffset() << " sec ";
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
std::cout << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Play a music
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void PlayMusic()
|
|
|
|
{
|
|
|
|
// Load an ogg music file
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Music music;
|
|
|
|
if (!music.OpenFromFile("datas/sound/lepidoptera.ogg"))
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Display music informations
|
|
|
|
std::cout << "lepidoptera.ogg :" << std::endl;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << " " << music.GetDuration() << " sec" << std::endl;
|
|
|
|
std::cout << " " << music.GetSampleRate() << " samples / sec" << std::endl;
|
|
|
|
std::cout << " " << music.GetChannelsCount() << " channels" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Play it
|
2009-07-12 06:17:24 +08:00
|
|
|
music.Play();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Loop while the music is playing
|
2009-07-12 06:17:24 +08:00
|
|
|
while (music.GetStatus() == sf::Music::Playing)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Leave some CPU time for other processes
|
|
|
|
sf::Sleep(0.1f);
|
2009-02-27 23:03:20 +08:00
|
|
|
|
|
|
|
// Display the playing position
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.GetPlayingOffset() << " sec ";
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Play a sound
|
|
|
|
PlaySound();
|
|
|
|
|
|
|
|
// Play a music
|
|
|
|
PlayMusic();
|
|
|
|
|
|
|
|
// Wait until the user presses 'enter' key
|
|
|
|
std::cout << "Press enter to exit..." << std::endl;
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|