1
2////////////////////////////////////////////////////////////
3// Headers
4////////////////////////////////////////////////////////////
5#include <SFML/Audio.hpp>
6#include <iostream>
7#include <string>
8
9
10////////////////////////////////////////////////////////////
11/// Play a sound
12///
13////////////////////////////////////////////////////////////
14void playSound()
15{
16 // Load a sound buffer from a wav file
17 sf::SoundBuffer buffer;
18 if (!buffer.loadFromFile("resources/canary.wav"))
19 return;
20
21 // Display sound informations
22 std::cout << "canary.wav:" << std::endl;
23 std::cout << " " << buffer.getDuration().asSeconds() << " seconds" << std::endl;
24 std::cout << " " << buffer.getSampleRate() << " samples / sec" << std::endl;
25 std::cout << " " << buffer.getChannelCount() << " channels" << std::endl;
26
27 // Create a sound instance and play it
28 sf::Sound sound(buffer);
29 sound.play();
30
31 // Loop while the sound is playing
32 while (sound.getStatus() == sf::Sound::Playing)
33 {
34 // Leave some CPU time for other processes
35 sf::sleep(sf::milliseconds(100));
36
37 // Display the playing position
38 std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec ";
39 std::cout << std::flush;
40 }
41 std::cout << std::endl << std::endl;
42}
43
44
45////////////////////////////////////////////////////////////
46/// Play a music
47///
48////////////////////////////////////////////////////////////
49void playMusic(const std::string& filename)
50{
51 // Load an ogg music file
52 sf::Music music;
53 if (!music.openFromFile("resources/" + filename))
54 return;
55
56 // Display music informations
57 std::cout << filename << ":" << std::endl;
58 std::cout << " " << music.getDuration().asSeconds() << " seconds" << std::endl;
59 std::cout << " " << music.getSampleRate() << " samples / sec" << std::endl;
60 std::cout << " " << music.getChannelCount() << " channels" << std::endl;
61
62 // Play it
63 music.play();
64
65 // Loop while the music is playing
66 while (music.getStatus() == sf::Music::Playing)
67 {
68 // Leave some CPU time for other processes
69 sf::sleep(sf::milliseconds(100));
70
71 // Display the playing position
72 std::cout << "\rPlaying... " << music.getPlayingOffset().asSeconds() << " sec ";
73 std::cout << std::flush;
74 }
75 std::cout << std::endl << std::endl;
76}
77
78
79////////////////////////////////////////////////////////////
80/// Entry point of application
81///
82/// \return Application exit code
83///
84////////////////////////////////////////////////////////////
85int main()
86{
87 // Play a sound
88 playSound();
89
90 // Play music from an ogg file
91 playMusic("orchestral.ogg");
92
93 // Play music from a flac file
94 playMusic("ding.flac");
95
96 // Wait until the user presses 'enter' key
97 std::cout << "Press enter to exit..." << std::endl;
98 std::cin.ignore(10000, '\n');
99
100 return EXIT_SUCCESS;
101}
102