6 #include "cAudioCapture.h" 9 #include "cAudioSleep.h" 11 #include "cPluginManager.h" 17 cAudioCapture::cAudioCapture() : AudioThread(NULL), Frequency(22050), Format(EAF_16BIT_MONO),
18 InternalBufferSize(8192), SampleSize(2), Ready(false),
19 Capturing(false), CaptureDevice(NULL)
23 cAudioCapture::~cAudioCapture()
28 void cAudioCapture::run()
34 bool cAudioCapture::initOpenALDevice()
36 cAudioMutexBasicLock lock(Mutex);
38 shutdownOpenALDevice();
39 if(DeviceName.empty())
40 CaptureDevice = alcCaptureOpenDevice(NULL, Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize);
42 CaptureDevice = alcCaptureOpenDevice(toUTF8(DeviceName), Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize);
45 DeviceName = fromUTF8(alcGetString(CaptureDevice, ALC_CAPTURE_DEVICE_SPECIFIER));
57 void cAudioCapture::shutdownOpenALDevice()
59 cAudioMutexBasicLock lock(Mutex);
65 alcCaptureCloseDevice(CaptureDevice);
69 signalEvent(ON_RELEASE);
72 CaptureBuffer.clear();
86 shutdownOpenALDevice();
87 signalEvent(ON_RELEASE);
93 if(Capturing && CaptureDevice && Ready)
95 int AvailableSamples = 0;
96 alcGetIntegerv(CaptureDevice, ALC_CAPTURE_SAMPLES, 1, &AvailableSamples);
97 const unsigned int availbuffersize = AvailableSamples * SampleSize;
100 if(availbuffersize > InternalBufferSize / 2 || force)
103 if(availbuffersize > 0)
105 const unsigned int oldBufferSize = CaptureBuffer.size();
106 CaptureBuffer.resize(oldBufferSize + availbuffersize, 0);
107 alcCaptureSamples(CaptureDevice, &CaptureBuffer[oldBufferSize], AvailableSamples);
109 getLogger()->
logDebug(
"AudioCapture",
"Captured %i bytes of audio data.", availbuffersize);
110 signalEvent(ON_UPDATE);
121 CaptureBuffer.clear();
122 if(CaptureDevice && Ready)
124 alcCaptureStart(CaptureDevice);
127 signalEvent(ON_BEGINCAPTURE);
139 if(CaptureDevice && Ready)
141 alcCaptureStop(CaptureDevice);
145 signalEvent(ON_ENDCAPTURE);
160 unsigned int internalBufferSize = CaptureBuffer.size();
161 if(outputBuffer && outputBufferSize > 0 && internalBufferSize > 0)
163 int sizeToCopy = (outputBufferSize >= internalBufferSize) ? internalBufferSize : outputBufferSize;
164 memcpy(outputBuffer, &CaptureBuffer[0], sizeToCopy);
165 CaptureBuffer.erase(CaptureBuffer.begin(), CaptureBuffer.begin()+sizeToCopy);
167 getLogger()->
logDebug(
"AudioCapture",
"Copied out %i bytes of data out of %i bytes in the buffer at user request.", sizeToCopy, internalBufferSize);
168 signalEvent(ON_USERREQUESTEDBUFFER);
177 return CaptureBuffer.size();
183 Frequency = frequency;
184 shutdownOpenALDevice();
185 return initOpenALDevice();
192 if(Format == EAF_8BIT_MONO)
194 else if(Format == EAF_8BIT_STEREO)
196 else if(Format == EAF_16BIT_MONO)
201 shutdownOpenALDevice();
202 return initOpenALDevice();
208 InternalBufferSize = internalBufferSize;
210 shutdownOpenALDevice();
211 return initOpenALDevice();
217 DeviceName = fromUTF8(deviceName);
219 shutdownOpenALDevice();
220 return initOpenALDevice();
226 DeviceName = fromUTF8(deviceName);
227 Frequency = frequency;
228 InternalBufferSize = internalBufferSize;
231 if(Format == EAF_8BIT_MONO)
233 else if(Format == EAF_8BIT_STEREO)
235 else if(Format == EAF_16BIT_MONO)
240 shutdownOpenALDevice();
241 signalEvent(ON_INIT);
242 bool isInit = initOpenALDevice();
244 #ifdef CAUDIO_USE_INTERNAL_THREAD 254 bool cAudioCapture::checkError()
258 int error = alcGetError(CaptureDevice);
259 if (error != AL_NO_ERROR)
261 const char* errorString = alGetString(error);
269 ALenum cAudioCapture::convertAudioFormatEnum(
AudioFormats format)
274 return AL_FORMAT_MONO8;
276 return AL_FORMAT_MONO16;
277 case EAF_8BIT_STEREO:
278 return AL_FORMAT_STEREO8;
279 case EAF_16BIT_STEREO:
280 return AL_FORMAT_STEREO16;
282 return AL_FORMAT_MONO8;
290 eventHandlerList.push_back(handler);
298 eventHandlerList.remove(handler);
304 eventHandlerList.clear();
307 void cAudioCapture::signalEvent(Events sevent)
312 if(it != eventHandlerList.end()){
318 for(; it != eventHandlerList.end(); it++){
326 for(; it != eventHandlerList.end(); it++){
334 for(; it != eventHandlerList.end(); it++){
340 case ON_BEGINCAPTURE:
342 for(; it != eventHandlerList.end(); it++){
343 (*it)->onBeginCapture();
351 for(; it != eventHandlerList.end(); it++){
352 (*it)->onEndCapture();
357 case ON_USERREQUESTEDBUFFER:
359 for(; it != eventHandlerList.end(); it++){
360 (*it)->onUserRequestBuffer();
virtual void logError(const char *sender, const char *msg,...)=0
Used to log an error message to the logging system.
virtual bool setInternalBufferSize(unsigned int internalBufferSize)
Sets the internal buffer size that OpenAL will use to store captured audio between calls to getCaptur...
IThread * AudioThread
Our update thread.
virtual bool setFormat(AudioFormats format)
Sets the format that the captured audio will be at. Will cause the capture device to be reinitialized...
virtual unsigned int getCurrentCapturedAudioSize()
Returns the current size of the internal audio buffer in bytes.
virtual void updateCaptureBuffer(bool force=false)
Grabs samples from the OpenAL buffer into the capture buffer if the OpenAL buffer has reached half fu...
virtual void logDebug(const char *sender, const char *msg,...)=0
Used to log a debug message to the logging system.
Interface for recieving Capture Manager Events.
virtual void stopCapture()
Stops capturing audio data to an internal buffer.
virtual void unRegisterAllEventHandlers()
Removes all event handlers attached to this manager.
virtual AudioCaptureBuffer * getCapturedAudioBuffer()
this method is the same as getCapturedAudio but it returns an managed CaptureBuffer ...
virtual bool setFrequency(unsigned int frequency)
Sets the frequency that the captured audio will be at. Will cause the capture device to be reinitiali...
virtual bool setDevice(const char *deviceName)
Sets the audio device . Will cause the capture device to be reinitialized. Calling while in use will ...
virtual unsigned int getCapturedAudio(void *outputBuffer, unsigned int outputBufferSize)
Allows access to the audio data in the internal capture buffer.
virtual bool beginCapture()
Starts capturing audio data to an internal buffer. Will clear any old data in the buffer...
virtual bool initialize(const char *deviceName=0x0, unsigned int frequency=22050, AudioFormats format=EAF_16BIT_MONO, unsigned int internalBufferSize=8192)
Initializes the capture device to the selected settings.
virtual void shutdown()
Shuts down the capture device, clearing the internal buffer and setting the audio capture into an uni...
virtual void unRegisterEventHandler(ICaptureEventHandler *handler)
Removes the specified event handler from this manager.
CAUDIO_API ILogger * getLogger()
Gets the interface to the logger.
CAUDIO_API void cAudioSleep(unsigned int ms)
Causes the current thread to give up control for a certain duration.
AudioFormats
Enumeration of audio formats supported by the engine.
virtual void registerEventHandler(ICaptureEventHandler *handler)
Registers a new event handler to this manager.
Main namespace for the entire cAudio library.