#include <SFML/Audio.hpp>
#include <iomanip>
#include <iostream>
int
main()
{
if
(sf::SoundRecorder::isAvailable() ==
false
)
{
std::cout <<
"Sorry, audio capture is not supported by your system"
<< std::endl;
return
EXIT_SUCCESS;
}
unsigned
int
sampleRate;
std::cout <<
"Please choose the sample rate for sound capture (44100 is CD quality) : "
;
std::cin >> sampleRate;
std::cin.ignore(10000,
'\n'
);
std::cout <<
"Press enter to start recording audio"
;
std::cin.ignore(10000,
'\n'
);
sf::SoundBufferRecorder recorder;
recorder.start(sampleRate);
std::cout <<
"Recording... press enter to stop"
;
std::cin.ignore(10000,
'\n'
);
recorder.stop();
const
sf::SoundBuffer& buffer = recorder.getBuffer();
std::cout <<
"Sound information :"
<< std::endl;
std::cout <<
" "
<< buffer.getDuration().asSeconds() <<
" seconds"
<< std::endl;
std::cout <<
" "
<< buffer.getSampleRate() <<
" samples / seconds"
<< std::endl;
std::cout <<
" "
<< buffer.getChannelCount() <<
" channels"
<< std::endl;
char
choice;
std::cout <<
"What do you want to do with captured sound (p = play, s = save) ? "
;
std::cin >> choice;
std::cin.ignore(10000,
'\n'
);
if
(choice ==
's'
)
{
std::string filename;
std::cout <<
"Choose the file to create : "
;
std::getline(std::cin, filename);
buffer.saveToFile(filename);
}
else
{
sf::Sound sound(buffer);
sound.play();
while
(sound.getStatus() == sf::Sound::Playing)
{
std::cout <<
"\rPlaying... "
<< std::fixed << std::setprecision(2) << sound.getPlayingOffset().asSeconds() <<
" sec"
;
std::cout << std::flush;
sf::sleep(sf::milliseconds(100));
}
}
std::cout << std::endl <<
"Done !"
<< std::endl;
std::cout <<
"Press enter to exit..."
<< std::endl;
std::cin.ignore(10000,
'\n'
);
return
EXIT_SUCCESS;
}