2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on the
14 #include "osregistry.h"
17 static int OAL_capture_recording = 0;
19 struct capture_buffer {
20 uint samples_per_second;
29 static capture_buffer Capture;
31 static ALCdevice *al_capture_device = NULL;
32 static std::string CaptureDevice;
35 void oal_capture_release_buffer()
37 if (al_capture_device != NULL) {
38 alcCaptureCloseDevice(al_capture_device);
39 al_capture_device = NULL;
43 // create a capture buffer with the specified format
44 // exit: 0 -> buffer created successfully
45 // !0 -> error creating the buffer
46 int oal_capture_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds)
48 ALenum al_format = AL_FORMAT_MONO8;
49 ALsizei buf_size = freq * nseconds;
51 SDL_assert( (nchannels == 1) || (nchannels == 2) );
52 SDL_assert( (bits_per_sample == 8) || (bits_per_sample == 16) );
54 if ( !oal_is_initted() ) {
59 if (bits_per_sample == 8) {
60 al_format = AL_FORMAT_MONO8;
61 } else if (bits_per_sample == 16) {
62 al_format = AL_FORMAT_MONO16;
64 } else if (nchannels == 2) {
65 if (bits_per_sample == 8) {
66 al_format = AL_FORMAT_STEREO8;
67 } else if (bits_per_sample == 16) {
68 al_format = AL_FORMAT_STEREO16;
72 al_capture_device = alcCaptureOpenDevice(CaptureDevice.c_str(), freq, al_format, buf_size);
74 if (al_capture_device == NULL) {
78 if ( alcGetError(al_capture_device) != ALC_NO_ERROR ) {
79 alcCaptureCloseDevice(al_capture_device);
84 Capture.format = al_format;
85 Capture.bits_per_sample = bits_per_sample;
86 Capture.n_channels = nchannels;
87 Capture.samples_per_second = freq;
88 Capture.block_align = (nchannels * bits_per_sample) / 8;
93 void oal_capture_init()
95 const char *ptr = NULL;
96 ALCdevice *tdevice = NULL;
98 ptr = os_config_read_string("Audio", "CaptureDevice", "default");
100 if ( ptr && !SDL_strcasecmp(ptr, "default") ) {
104 tdevice = alcCaptureOpenDevice(ptr, 11025, AL_FORMAT_MONO8, 11025 * 2);
106 if (tdevice == NULL) {
107 tdevice = alcCaptureOpenDevice(NULL, 11025, AL_FORMAT_MONO8, 11025 * 2);
109 if (tdevice == NULL) {
110 mprintf((" Capture device : * Unavailable *\n"));
116 if ( alcGetError(tdevice) != ALC_NO_ERROR ) {
117 mprintf((" Capture device : * Unavailable *\n"));
118 alcCaptureCloseDevice(tdevice);
123 ptr = alcGetString(tdevice, ALC_CAPTURE_DEVICE_SPECIFIER);
126 mprintf((" Capture device : %s\n", ptr));
130 alcCaptureCloseDevice(tdevice);
133 int oal_capture_supported()
135 return oal_is_initted();
138 // start recording into the buffer
139 int oal_capture_start_record()
141 if ( !oal_is_initted() ) {
145 if (OAL_capture_recording) {
149 alcCaptureStart(al_capture_device);
151 OAL_capture_recording = 1;
153 // nprintf(("Alan","RTVOICE => start record\n"));
158 // stop recording into the buffer
159 int oal_capture_stop_record()
161 if ( !oal_is_initted() ) {
165 if ( !OAL_capture_recording ) {
169 alcCaptureStop(al_capture_device);
171 OAL_capture_recording = 0;
173 // nprintf(("Alan","RTVOICE => stop record\n"));
178 void oal_capture_close()
180 oal_capture_stop_record();
182 if (al_capture_device != NULL) {
183 alcCaptureCloseDevice(al_capture_device);
184 al_capture_device = NULL;
188 // return the max buffer size
189 int oal_capture_max_buffersize()
191 if ( !oal_is_initted() ) {
195 ALCsizei num_samples = 0;
197 alcGetIntegerv(al_capture_device, ALC_CAPTURE_SAMPLES, sizeof(ALCsizei), &num_samples);
199 if (alcGetError(al_capture_device) != ALC_NO_ERROR) {
203 return (num_samples * Capture.block_align);
206 // retrieve the recorded voice data
207 int oal_capture_get_raw_data(ubyte *outbuf, uint max_size)
209 if ( !oal_is_initted() ) {
213 if (outbuf == NULL) {
217 ALCsizei num_samples = 0;
219 alcGetIntegerv(al_capture_device, ALC_CAPTURE_SAMPLES, sizeof(ALCsizei), &num_samples);
221 if (num_samples <= 0) {
225 ALCsizei max_buf_size = min(num_samples, ALsizei(max_size / Capture.block_align));
227 alcCaptureSamples(al_capture_device, outbuf, max_buf_size);
229 if (alcGetError(al_capture_device) != ALC_NO_ERROR) {
233 return (int)max_buf_size * Capture.block_align;