| 1 | |
| 2 | //////////////////////////////////////////////////////////// |
| 3 | // Headers |
| 4 | //////////////////////////////////////////////////////////// |
| 5 | #include <SFML/Audio.hpp> |
| 6 | #include <iostream> |
| 7 | |
| 8 | |
| 9 | //////////////////////////////////////////////////////////// |
| 10 | /// Entry point of application |
| 11 | /// |
| 12 | /// \return Application exit code |
| 13 | /// |
| 14 | //////////////////////////////////////////////////////////// |
| 15 | int main() |
| 16 | { |
| 17 | // Check that the device can capture audio |
| 18 | if (sf::SoundRecorder::isAvailable() == false) |
| 19 | { |
| 20 | std::cout << "Sorry, audio capture is not supported by your system" << std::endl; |
| 21 | return EXIT_SUCCESS; |
| 22 | } |
| 23 | |
| 24 | // Choose the sample rate |
| 25 | unsigned int sampleRate; |
| 26 | std::cout << "Please choose the sample rate for sound capture (44100 is CD quality): " ; |
| 27 | std::cin >> sampleRate; |
| 28 | std::cin.ignore(10000, '\n'); |
| 29 | |
| 30 | // Wait for user input... |
| 31 | std::cout << "Press enter to start recording audio" ; |
| 32 | std::cin.ignore(10000, '\n'); |
| 33 | |
| 34 | // Here we'll use an integrated custom recorder, which saves the captured data into a SoundBuffer |
| 35 | sf::SoundBufferRecorder recorder; |
| 36 | |
| 37 | // Audio capture is done in a separate thread, so we can block the main thread while it is capturing |
| 38 | recorder.start(sampleRate); |
| 39 | std::cout << "Recording... press enter to stop" ; |
| 40 | std::cin.ignore(10000, '\n'); |
| 41 | recorder.stop(); |
| 42 | |
| 43 | // Get the buffer containing the captured data |
| 44 | const sf::SoundBuffer& buffer = recorder.getBuffer(); |
| 45 | |
| 46 | // Display captured sound informations |
| 47 | std::cout << "Sound information:" << std::endl; |
| 48 | std::cout << " " << buffer.getDuration().asSeconds() << " seconds" << std::endl; |
| 49 | std::cout << " " << buffer.getSampleRate() << " samples / seconds" << std::endl; |
| 50 | std::cout << " " << buffer.getChannelCount() << " channels" << std::endl; |
| 51 | |
| 52 | // Choose what to do with the recorded sound data |
| 53 | char choice; |
| 54 | std::cout << "What do you want to do with captured sound (p = play, s = save) ? " ; |
| 55 | std::cin >> choice; |
| 56 | std::cin.ignore(10000, '\n'); |
| 57 | |
| 58 | if (choice == 's') |
| 59 | { |
| 60 | // Choose the filename |
| 61 | std::string filename; |
| 62 | std::cout << "Choose the file to create: " ; |
| 63 | std::getline(std::cin, filename); |
| 64 | |
| 65 | // Save the buffer |
| 66 | buffer.saveToFile(filename); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Create a sound instance and play it |
| 71 | sf::Sound sound(buffer); |
| 72 | sound.play(); |
| 73 | |
| 74 | // Wait until finished |
| 75 | while (sound.getStatus() == sf::Sound::Playing) |
| 76 | { |
| 77 | // Display the playing position |
| 78 | std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec " ; |
| 79 | std::cout << std::flush; |
| 80 | |
| 81 | // Leave some CPU time for other threads |
| 82 | sf::sleep(sf::milliseconds(100)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Finished! |
| 87 | std::cout << std::endl << "Done!" << std::endl; |
| 88 | |
| 89 | // Wait until the user presses 'enter' key |
| 90 | std::cout << "Press enter to exit..." << std::endl; |
| 91 | std::cin.ignore(10000, '\n'); |
| 92 | |
| 93 | return EXIT_SUCCESS; |
| 94 | } |
| 95 | |