.. index:: pair: class; EE::Audio::SoundRecorder .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder: class EE::Audio::SoundRecorder ============================== .. toctree:: :hidden: Overview ~~~~~~~~ Abstract base class for capturing sound data. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include class SoundRecorder: private :ref:`EE::Audio::AlResource` { public: // construction virtual :target:`~SoundRecorder`(); // methods bool :ref:`start`(unsigned int sampleRate = 44100); void :ref:`stop`(); unsigned int :ref:`getSampleRate`() const; bool :ref:`setDevice`(const std::string& name); const std::string& :ref:`getDevice`() const; void :ref:`setChannelCount`(unsigned int channelCount); unsigned int :ref:`getChannelCount`() const; bool :target:`isCapturing`(); static std::vector :ref:`getAvailableDevices`(); static std::string :ref:`getDefaultDevice`(); static bool :ref:`isAvailable`(); }; .. _details-class_e_e_1_1_audio_1_1_sound_recorder: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Abstract base class for capturing sound data. :ref:`SoundBuffer ` provides a simple interface to access the audio recording capabilities of the computer (the microphone). As an abstract base class, it only cares about capturing sound samples, the task of making something useful with them is left to the derived class. Note that EEPP provides a built-in specialization for saving the captured data to a sound buffer (see :ref:`SoundBufferRecorder `). A derived class has only one virtual function to override: * onProcessSamples provides the new chunks of audio samples while the capture happens Moreover, two additional virtual functions can be overridden as well if necessary: * onStart is called before the capture happens, to perform custom initializations * onStop is called after the capture ends, to perform custom cleanup A derived class can also control the frequency of the onProcessSamples calls, with the setProcessingInterval protected function. The default interval is chosen so that recording thread doesn't consume too much CPU, but it can be changed to a smaller value if you need to process the recorded data in real time, for example. The audio capture feature may not be supported or activated on every platform, thus it is recommended to check its availability with the :ref:`isAvailable() ` function. If it returns false, then any attempt to use an audio recorder will fail. If you have multiple sound input devices connected to your computer (for example: microphone, external soundcard, webcam mic, ...) you can get a list of all available devices through the :ref:`getAvailableDevices() ` function. You can then select a device by calling :ref:`setDevice() ` with the appropriate device. Otherwise the default capturing device will be used. By default the recording is in 16-bit mono. Using the setChannelCount method you can change the number of channels used by the audio capture device to record. Note that you have to decide whether you want to record in mono or stereo before starting the recording. It is important to note that the audio capture happens in a separate thread, so that it doesn't block the rest of the program. In particular, the onProcessSamples virtual function (but not onStart and not onStop) will be called from this separate thread. It is important to keep this in mind, because you may have to take care of synchronization issues if you share data between threads. Another thing to bear in mind is that you must call :ref:`stop() ` in the destructor of your derived class, so that the recording thread finishes before your object is destroyed. Usage example: .. ref-code-block:: cpp class CustomRecorder : public SoundRecorder { ~CustomRecorder() { // Make sure to stop the recording thread stop(); } virtual bool onStart() // optional { // Initialize whatever has to be done before the capture starts ... // Return true to start playing return true; } virtual bool onProcessSamples(const Int16* samples, std::size_t sampleCount) { // Do something with the new chunk of samples (store them, send them, ...) ... // Return true to continue playing return true; } virtual void onStop() // optional { // Clean up whatever has to be done after the capture ends ... } } // Usage if (CustomRecorder::isAvailable()) { CustomRecorder recorder; if (!recorder.start()) return -1; ... recorder.stop(); } .. rubric:: See also: :ref:`SoundBufferRecorder ` Methods ------- .. index:: pair: function; start .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1aa66957284ecc845dfd90aedbbebd4d74: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool start(unsigned int sampleRate = 44100) Start the capture. The *sampleRate* parameter defines the number of audio samples captured per second. The higher, the better the quality (for example, 44100 samples/sec is CD quality). This function uses its own thread so that it doesn't block the rest of the program while the capture runs. Please note that only one capture can happen at the same time. You can select which capture device will be used, by passing the name to the :ref:`setDevice() ` method. If none was selected before, the default capture device will be used. You can get a list of the names of all available capture devices by calling :ref:`getAvailableDevices() `. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - sampleRate - Desired capture rate, in number of samples per second .. rubric:: Returns: True, if start of capture was successful .. rubric:: See also: :ref:`stop `, :ref:`getAvailableDevices ` .. index:: pair: function; stop .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a8f03d4de40b629b7e7d140c4dff55e6a: .. ref-code-block:: cpp :class: doxyrest-title-code-block void stop() Stop the capture. .. rubric:: See also: :ref:`start ` .. index:: pair: function; getSampleRate .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a13131a3158fed6aecc0caccef68da496: .. ref-code-block:: cpp :class: doxyrest-title-code-block unsigned int getSampleRate() const Get the sample rate. The sample rate defines the number of audio samples captured per second. The higher, the better the quality (for example, 44100 samples/sec is CD quality). .. rubric:: Returns: Sample rate, in samples per second .. index:: pair: function; setDevice .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a3b7dc2a49853f0c5fefd0d8aea13921b: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool setDevice(const std::string& name) Set the audio capture device. This function sets the audio capture device to the device with the given *name*. It can be called on the fly (i.e: while recording). If you do so while recording and opening the device fails, it stops the recording. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - name - The name of the audio capture device .. rubric:: Returns: True, if it was able to set the requested device .. rubric:: See also: :ref:`getAvailableDevices `, :ref:`getDefaultDevice ` .. index:: pair: function; getDevice .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a09cf53751c3933d6aa9d81b2d60a6cea: .. ref-code-block:: cpp :class: doxyrest-title-code-block const std::string& getDevice() const Get the name of the current audio capture device. .. rubric:: Returns: The name of the current audio capture device .. index:: pair: function; setChannelCount .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a49a3f76da40f1fa6289562de2969494f: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setChannelCount(unsigned int channelCount) Set the channel count of the audio capture device. This method allows you to specify the number of channels used for recording. Currently only 16-bit mono and 16-bit stereo are supported. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - channelCount - Number of channels. Currently only mono (1) and stereo (2) are supported. .. rubric:: See also: :ref:`getChannelCount ` .. index:: pair: function; getChannelCount .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a83dd93647fce8f253f47078bb1cb1c99: .. ref-code-block:: cpp :class: doxyrest-title-code-block unsigned int getChannelCount() const Get the number of channels used by this recorder. Currently only mono and stereo are supported, so the value is either 1 (for mono) or 2 (for stereo). .. rubric:: Returns: Number of channels .. rubric:: See also: :ref:`setChannelCount ` .. index:: pair: function; getAvailableDevices .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a9650b8cf3919f5555b1e96d0a41e2042: .. ref-code-block:: cpp :class: doxyrest-title-code-block static std::vector getAvailableDevices() Get a list of the names of all available audio capture devices. This function returns a vector of strings, containing the names of all available audio capture devices. .. rubric:: Returns: A vector of strings containing the names .. index:: pair: function; getDefaultDevice .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1addd5b0224f47faf63adf7bc99a672574: .. ref-code-block:: cpp :class: doxyrest-title-code-block static std::string getDefaultDevice() Get the name of the default audio capture device. This function returns the name of the default audio capture device. If none is available, an empty string is returned. .. rubric:: Returns: The name of the default audio capture device .. index:: pair: function; isAvailable .. _doxid-class_e_e_1_1_audio_1_1_sound_recorder_1a5ac9b9116b1935a0273e509348d3818c: .. ref-code-block:: cpp :class: doxyrest-title-code-block static bool isAvailable() Check if the system supports audio capture. This function should always be called before using the audio capture features. If it returns false, then any attempt to use :ref:`SoundRecorder ` or one of its derived classes will fail. .. rubric:: Returns: True if audio capture is supported, false otherwise