2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Audio.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2023-04-24 20:13:52 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Check that the device can capture audio
|
2023-01-20 09:53:53 +08:00
|
|
|
if (!sf::SoundRecorder::isAvailable())
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-04-16 22:12:31 +08:00
|
|
|
// List the available capture devices
|
|
|
|
auto devices = sf::SoundRecorder::getAvailableDevices();
|
|
|
|
|
|
|
|
std::cout << "Available capture devices:\n" << std::endl;
|
|
|
|
|
|
|
|
for (auto i = 0u; i < devices.size(); ++i)
|
|
|
|
std::cout << i << ": " << devices[i] << '\n';
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::size_t deviceIndex = 0;
|
|
|
|
|
|
|
|
// Choose the capture device
|
|
|
|
if (devices.size() > 1)
|
|
|
|
{
|
|
|
|
deviceIndex = devices.size();
|
|
|
|
std::cout << "Please choose the capture device to use [0-" << devices.size() - 1 << "]: ";
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::cin >> deviceIndex;
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
} while (deviceIndex >= devices.size());
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Choose the sample rate
|
2024-04-27 14:44:27 +08:00
|
|
|
unsigned int sampleRate = 0;
|
2014-11-17 16:44:46 +08:00
|
|
|
std::cout << "Please choose the sample rate for sound capture (44100 is CD quality): ";
|
2022-07-05 00:20:58 +08:00
|
|
|
std::cin >> sampleRate;
|
2014-09-30 22:07:25 +08:00
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
// Wait for user input...
|
|
|
|
std::cout << "Press enter to start recording audio";
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
// Here we'll use an integrated custom recorder, which saves the captured data into a SoundBuffer
|
|
|
|
sf::SoundBufferRecorder recorder;
|
|
|
|
|
2023-04-16 22:12:31 +08:00
|
|
|
if (!recorder.setDevice(devices[deviceIndex]))
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to set the capture device" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Audio capture is done in a separate thread, so we can block the main thread while it is capturing
|
2021-12-14 21:04:15 +08:00
|
|
|
if (!recorder.start(sampleRate))
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to start recorder" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
std::cout << "Recording... press enter to stop";
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
recorder.stop();
|
|
|
|
|
|
|
|
// Get the buffer containing the captured data
|
|
|
|
const sf::SoundBuffer& buffer = recorder.getBuffer();
|
|
|
|
|
2023-05-22 02:47:07 +08:00
|
|
|
// Display captured sound information
|
2022-02-16 22:34:15 +08:00
|
|
|
std::cout << "Sound information:" << '\n'
|
2022-07-05 00:20:58 +08:00
|
|
|
<< " " << buffer.getDuration().asSeconds() << " seconds" << '\n'
|
|
|
|
<< " " << buffer.getSampleRate() << " samples / seconds" << '\n'
|
|
|
|
<< " " << buffer.getChannelCount() << " channels" << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Choose what to do with the recorded sound data
|
2024-04-27 14:44:27 +08:00
|
|
|
char choice = 0;
|
2014-09-30 22:07:25 +08:00
|
|
|
std::cout << "What do you want to do with captured sound (p = play, s = save) ? ";
|
2022-07-05 00:20:58 +08:00
|
|
|
std::cin >> choice;
|
2014-09-30 22:07:25 +08:00
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
if (choice == 's')
|
|
|
|
{
|
|
|
|
// Choose the filename
|
|
|
|
std::string filename;
|
|
|
|
std::cout << "Choose the file to create: ";
|
|
|
|
std::getline(std::cin, filename);
|
|
|
|
|
|
|
|
// Save the buffer
|
2021-12-14 21:04:15 +08:00
|
|
|
if (!buffer.saveToFile(filename))
|
|
|
|
std::cerr << "Could not save sound buffer to file" << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create a sound instance and play it
|
|
|
|
sf::Sound sound(buffer);
|
|
|
|
sound.play();
|
|
|
|
|
|
|
|
// Wait until finished
|
2024-04-28 17:11:02 +08:00
|
|
|
while (sound.getStatus() == sf::Sound::Status::Playing)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Display the playing position
|
2016-02-23 10:07:12 +08:00
|
|
|
std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec ";
|
2014-09-30 22:07:25 +08:00
|
|
|
std::cout << std::flush;
|
|
|
|
|
|
|
|
// Leave some CPU time for other threads
|
|
|
|
sf::sleep(sf::milliseconds(100));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished!
|
2022-02-16 22:34:15 +08:00
|
|
|
std::cout << '\n' << "Done!" << std::endl;
|
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');
|
|
|
|
}
|