MCPcopy Create free account
hub / github.com/SFML/SFML / main

Function main

examples/sound_capture/SoundCapture.cpp:17–130  ·  view source on GitHub ↗

/////////////////////////////////////////////////////// Entry point of application \return Application exit code ///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

15///
16////////////////////////////////////////////////////////////
17int main()
18{
19 // Check that the device can capture audio
20 if (!sf::SoundRecorder::isAvailable())
21 {
22 std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
23 return EXIT_SUCCESS;
24 }
25
26 // List the available capture devices
27 auto devices = sf::SoundRecorder::getAvailableDevices();
28
29 std::cout << "Available capture devices:\n" << std::endl;
30
31 for (auto i = 0u; i < devices.size(); ++i)
32 std::cout << i << ": " << devices[i] << '\n';
33
34 std::cout << std::endl;
35
36 std::size_t deviceIndex = 0;
37
38 // Choose the capture device
39 if (devices.size() > 1)
40 {
41 deviceIndex = devices.size();
42 std::cout << "Please choose the capture device to use [0-" << devices.size() - 1 << "]: ";
43 do
44 {
45 std::cin >> deviceIndex;
46 std::cin.ignore(10'000, '\n');
47 } while (deviceIndex >= devices.size());
48 }
49
50 // Choose the sample rate
51 unsigned int sampleRate = 0;
52 std::cout << "Please choose the sample rate for sound capture (44100 is CD quality): ";
53 std::cin >> sampleRate;
54 std::cin.ignore(10'000, '\n');
55
56 // Wait for user input...
57 std::cout << "Press enter to start recording audio";
58 std::cin.ignore(10'000, '\n');
59
60 // Here we'll use an integrated custom recorder, which saves the captured data into a SoundBuffer
61 sf::SoundBufferRecorder recorder;
62
63 if (!recorder.setDevice(devices[deviceIndex]))
64 {
65 std::cerr << "Failed to set the capture device" << std::endl;
66 return EXIT_FAILURE;
67 }
68
69 // Audio capture is done in a separate thread, so we can block the main thread while it is capturing
70 if (!recorder.start(sampleRate))
71 {
72 std::cerr << "Failed to start recorder" << std::endl;
73 return EXIT_FAILURE;
74 }

Callers

nothing calls this directly

Calls 13

getAvailableDevicesFunction · 0.85
sleepFunction · 0.85
isAvailableFunction · 0.50
setDeviceMethod · 0.45
startMethod · 0.45
stopMethod · 0.45
getDurationMethod · 0.45
getSampleRateMethod · 0.45
getChannelCountMethod · 0.45
saveToFileMethod · 0.45
playMethod · 0.45
getStatusMethod · 0.45

Tested by

no test coverage detected