Back to index

libsfml  1.6+dfsg2
SoundCapture.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Audio.hpp>
00006 #include <iomanip>
00007 #include <iostream>
00008 
00009 
00016 int main()
00017 {
00018     // Check that the device can capture audio
00019     if (sf::SoundRecorder::CanCapture() == false)
00020     {
00021         std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
00022         return EXIT_SUCCESS;
00023     }
00024 
00025     // Choose the sample rate
00026     unsigned int SampleRate;
00027     std::cout << "Please choose the sample rate for sound capture (44100 is CD quality) : ";
00028     std::cin  >> SampleRate;
00029     std::cin.ignore(10000, '\n');
00030 
00031     // Wait for user input...
00032     std::cout << "Press enter to start recording audio";
00033     std::cin.ignore(10000, '\n');
00034 
00035     // Here we'll use an integrated custom recorder, which saves the captured data into a SoundBuffer
00036     sf::SoundBufferRecorder Recorder;
00037 
00038     // Audio capture is done in a separate thread, so we can block the main thread while it is capturing
00039     Recorder.Start(SampleRate);
00040     std::cout << "Recording... press enter to stop";
00041     std::cin.ignore(10000, '\n');
00042     Recorder.Stop();
00043 
00044     // Get the buffer containing the captured data
00045     const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
00046 
00047     // Display captured sound informations
00048     std::cout << "Sound information :" << std::endl;
00049     std::cout << " " << Buffer.GetDuration()      << " seconds"           << std::endl;
00050     std::cout << " " << Buffer.GetSampleRate()    << " samples / seconds" << std::endl;
00051     std::cout << " " << Buffer.GetChannelsCount() << " channels"          << std::endl;
00052 
00053     // Choose what to do with the recorded sound data
00054     char Choice;
00055     std::cout << "What do you want to do with captured sound (p = play, s = save) ? ";
00056     std::cin  >> Choice;
00057     std::cin.ignore(10000, '\n');
00058 
00059     if (Choice == 's')
00060     {
00061         // Choose the filename
00062         std::string Filename;
00063         std::cout << "Choose the file to create : ";
00064         std::getline(std::cin, Filename);
00065 
00066         // Save the buffer
00067         Buffer.SaveToFile(Filename);
00068     }
00069     else
00070     {
00071         // Create a sound instance and play it
00072         sf::Sound Sound(Buffer);
00073         Sound.Play();
00074 
00075         // Wait until finished
00076         while (Sound.GetStatus() == sf::Sound::Playing)
00077         {
00078             // Display the playing position
00079             std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec";
00080 
00081             // Leave some CPU time for other threads
00082             sf::Sleep(0.1f);
00083         }
00084     }
00085 
00086     // Finished !
00087     std::cout << std::endl << "Done !" << std::endl;
00088 
00089     // Wait until the user presses 'enter' key
00090     std::cout << "Press enter to exit..." << std::endl;
00091     std::cin.ignore(10000, '\n');
00092 
00093     return EXIT_SUCCESS;
00094 }