2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Audio.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Play a sound
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void playSound()
|
|
|
|
{
|
|
|
|
// Load a sound buffer from a wav file
|
2024-05-19 05:37:06 +08:00
|
|
|
const auto buffer = sf::SoundBuffer::loadFromFile("resources/killdeer.wav").value();
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2023-05-22 02:47:07 +08:00
|
|
|
// Display sound information
|
2022-02-16 22:34:15 +08:00
|
|
|
std::cout << "killdeer.wav:" << '\n'
|
2022-07-05 00:20:58 +08:00
|
|
|
<< " " << buffer.getDuration().asSeconds() << " seconds" << '\n'
|
|
|
|
<< " " << buffer.getSampleRate() << " samples / sec" << '\n'
|
2024-02-05 22:27:39 +08:00
|
|
|
<< " " << buffer.getChannelCount() << " channels" << '\n';
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create a sound instance and play it
|
|
|
|
sf::Sound sound(buffer);
|
|
|
|
sound.play();
|
|
|
|
|
|
|
|
// Loop while the sound is playing
|
2024-04-28 17:11:02 +08:00
|
|
|
while (sound.getStatus() == sf::Sound::Status::Playing)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Leave some CPU time for other processes
|
|
|
|
sf::sleep(sf::milliseconds(100));
|
|
|
|
|
|
|
|
// Display the playing position
|
2024-02-05 22:27:39 +08:00
|
|
|
std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec " << std::flush;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
2024-02-05 22:27:39 +08:00
|
|
|
|
|
|
|
std::cout << '\n' << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Play a music
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2022-03-15 09:32:26 +08:00
|
|
|
void playMusic(const std::filesystem::path& filename)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Load an ogg music file
|
|
|
|
sf::Music music;
|
2022-03-15 09:32:26 +08:00
|
|
|
if (!music.openFromFile("resources" / filename))
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
|
|
|
|
2023-05-22 02:47:07 +08:00
|
|
|
// Display music information
|
2022-02-16 22:34:15 +08:00
|
|
|
std::cout << filename << ":" << '\n'
|
2022-07-05 00:20:58 +08:00
|
|
|
<< " " << music.getDuration().asSeconds() << " seconds" << '\n'
|
|
|
|
<< " " << music.getSampleRate() << " samples / sec" << '\n'
|
2024-02-05 22:27:39 +08:00
|
|
|
<< " " << music.getChannelCount() << " channels" << '\n';
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Play it
|
|
|
|
music.play();
|
|
|
|
|
|
|
|
// Loop while the music is playing
|
2024-04-28 17:11:02 +08:00
|
|
|
while (music.getStatus() == sf::Music::Status::Playing)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Leave some CPU time for other processes
|
|
|
|
sf::sleep(sf::milliseconds(100));
|
|
|
|
|
|
|
|
// Display the playing position
|
2024-02-05 22:27:39 +08:00
|
|
|
std::cout << "\rPlaying... " << music.getPlayingOffset().asSeconds() << " sec " << std::flush;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
2024-02-05 22:27:39 +08:00
|
|
|
|
2022-02-16 22:34:15 +08:00
|
|
|
std::cout << '\n' << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Play a sound
|
|
|
|
playSound();
|
|
|
|
|
2015-03-10 07:44:06 +08:00
|
|
|
// Play music from an ogg file
|
2020-12-13 04:40:52 +08:00
|
|
|
playMusic("doodle_pop.ogg");
|
2015-04-01 10:08:23 +08:00
|
|
|
|
2015-03-10 07:44:06 +08:00
|
|
|
// Play music from a flac file
|
|
|
|
playMusic("ding.flac");
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2021-12-06 19:32:36 +08:00
|
|
|
// Play music from a mp3 file
|
|
|
|
playMusic("ding.mp3");
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Wait until the user presses 'enter' key
|
|
|
|
std::cout << "Press enter to exit..." << std::endl;
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
}
|