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"
16 #ifndef __EMSCRIPTEN__
18 static int OAL_capture_recording = 0;
20 struct capture_buffer {
21 uint samples_per_second;
30 static capture_buffer Capture;
32 static ALCdevice *al_capture_device = NULL;
33 static std::string CaptureDevice;
37 void oal_capture_release_buffer()
39 #ifndef __EMSCRIPTEN__
40 if (al_capture_device != NULL) {
41 alcCaptureCloseDevice(al_capture_device);
42 al_capture_device = NULL;
47 // create a capture buffer with the specified format
48 // exit: 0 -> buffer created successfully
49 // !0 -> error creating the buffer
50 int oal_capture_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds)
52 #ifndef __EMSCRIPTEN__
53 ALenum al_format = AL_FORMAT_MONO8;
54 ALsizei buf_size = freq * nseconds;
56 SDL_assert( (nchannels == 1) || (nchannels == 2) );
57 SDL_assert( (bits_per_sample == 8) || (bits_per_sample == 16) );
59 if ( !oal_is_initted() ) {
64 if (bits_per_sample == 8) {
65 al_format = AL_FORMAT_MONO8;
66 } else if (bits_per_sample == 16) {
67 al_format = AL_FORMAT_MONO16;
69 } else if (nchannels == 2) {
70 if (bits_per_sample == 8) {
71 al_format = AL_FORMAT_STEREO8;
72 } else if (bits_per_sample == 16) {
73 al_format = AL_FORMAT_STEREO16;
77 al_capture_device = alcCaptureOpenDevice(CaptureDevice.c_str(), freq, al_format, buf_size);
79 if (al_capture_device == NULL) {
83 // this gets around hang-on-close bug on Windows
84 alcCaptureStart(al_capture_device);
85 alcCaptureStop(al_capture_device);
87 if ( alcGetError(al_capture_device) != ALC_NO_ERROR ) {
88 alcCaptureCloseDevice(al_capture_device);
93 Capture.format = al_format;
94 Capture.bits_per_sample = bits_per_sample;
95 Capture.n_channels = nchannels;
96 Capture.samples_per_second = freq;
97 Capture.block_align = (nchannels * bits_per_sample) / 8;
105 void oal_capture_init()
107 #ifndef __EMSCRIPTEN__
108 const char *ptr = NULL;
109 ALCdevice *tdevice = NULL;
111 ptr = os_config_read_string("Audio", "CaptureDevice", "default");
113 if ( ptr && !SDL_strcasecmp(ptr, "default") ) {
117 tdevice = alcCaptureOpenDevice(ptr, 11025, AL_FORMAT_MONO8, 11025 * 2);
119 if (tdevice == NULL) {
120 tdevice = alcCaptureOpenDevice(NULL, 11025, AL_FORMAT_MONO8, 11025 * 2);
122 if (tdevice == NULL) {
123 mprintf((" Capture device : * Unavailable *\n"));
129 if ( alcGetError(tdevice) != ALC_NO_ERROR ) {
130 mprintf((" Capture device : * Unavailable *\n"));
131 alcCaptureCloseDevice(tdevice);
136 ptr = alcGetString(tdevice, ALC_CAPTURE_DEVICE_SPECIFIER);
139 mprintf((" Capture device : %s\n", ptr));
143 // this gets around hang-on-close bug on Windows
144 alcCaptureStart(tdevice);
145 alcCaptureStop(tdevice);
147 alcCaptureCloseDevice(tdevice);
151 int oal_capture_supported()
153 #ifndef __EMSCRIPTEN__
154 return oal_is_initted();
160 // start recording into the buffer
161 int oal_capture_start_record()
163 #ifndef __EMSCRIPTEN__
164 if ( !oal_is_initted() ) {
168 if (OAL_capture_recording) {
172 alcCaptureStart(al_capture_device);
174 OAL_capture_recording = 1;
176 // nprintf(("Alan","RTVOICE => start record\n"));
184 // stop recording into the buffer
185 int oal_capture_stop_record()
187 #ifndef __EMSCRIPTEN__
188 if ( !oal_is_initted() ) {
192 if ( !OAL_capture_recording ) {
196 alcCaptureStop(al_capture_device);
198 OAL_capture_recording = 0;
200 // nprintf(("Alan","RTVOICE => stop record\n"));
208 void oal_capture_close()
210 #ifndef __EMSCRIPTEN__
211 oal_capture_stop_record();
213 if (al_capture_device != NULL) {
214 alcCaptureCloseDevice(al_capture_device);
215 al_capture_device = NULL;
220 // return the max buffer size
221 int oal_capture_max_buffersize()
223 #ifndef __EMSCRIPTEN__
224 if ( !oal_is_initted() ) {
228 ALCsizei num_samples = 0;
230 alcGetIntegerv(al_capture_device, ALC_CAPTURE_SAMPLES, sizeof(ALCsizei), &num_samples);
232 if (alcGetError(al_capture_device) != ALC_NO_ERROR) {
236 return (num_samples * Capture.block_align);
242 // retrieve the recorded voice data
243 int oal_capture_get_raw_data(ubyte *outbuf, uint max_size)
245 #ifndef __EMSCRIPTEN__
246 if ( !oal_is_initted() ) {
250 if (outbuf == NULL) {
254 ALCsizei num_samples = 0;
256 alcGetIntegerv(al_capture_device, ALC_CAPTURE_SAMPLES, sizeof(ALCsizei), &num_samples);
258 if (num_samples <= 0) {
262 ALCsizei max_buf_size = SDL_min(num_samples, ALsizei(max_size / Capture.block_align));
264 alcCaptureSamples(al_capture_device, outbuf, max_buf_size);
266 if (alcGetError(al_capture_device) != ALC_NO_ERROR) {
270 return (int)max_buf_size * Capture.block_align;