]> icculus.org git repositories - taylor/freespace2.git/blob - src/sound/oal_capture.cpp
fix faulty flag check
[taylor/freespace2.git] / src / sound / oal_capture.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * source.
7  *
8 */
9
10 #include "pstypes.h"
11 #include "oal.h"
12
13
14 static int OAL_capture_recording = 0;
15
16 struct capture_buffer {
17         uint samples_per_second;
18         uint bits_per_sample;
19         uint n_channels;
20         uint block_align;
21
22         ALenum format;
23         ALsizei buffer_size;
24 };
25
26 static capture_buffer Capture;
27
28 static ALCdevice *al_capture_device = NULL;
29
30
31 void oal_capture_release_buffer()
32 {
33         if (al_capture_device != NULL) {
34                 alcCaptureCloseDevice(al_capture_device);
35                 al_capture_device = NULL;
36         }
37 }
38
39 // create a capture buffer with the specified format
40 // exit:        0       ->              buffer created successfully
41 //                      !0      ->              error creating the buffer
42 int oal_capture_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds)
43 {
44         ALenum al_format = AL_FORMAT_MONO8;
45         ALsizei buf_size = freq * nseconds;
46
47         SDL_assert( (nchannels == 1) || (nchannels == 2) );
48         SDL_assert( (bits_per_sample == 8) || (bits_per_sample == 16) );
49
50         if ( !oal_is_initted() ) {
51                 return -1;
52         }
53
54         if (nchannels == 1) {
55                 if (bits_per_sample == 8)  {
56                         al_format = AL_FORMAT_MONO8;
57                 } else if (bits_per_sample == 16) {
58                         al_format = AL_FORMAT_MONO16;
59                 }
60         } else if (nchannels == 2) {
61                 if (bits_per_sample == 8) {
62                         al_format = AL_FORMAT_STEREO8;
63                 } else if (bits_per_sample == 16) {
64                         al_format = AL_FORMAT_STEREO16;
65                 }
66         }
67
68         al_capture_device = alcCaptureOpenDevice(NULL, freq, al_format, buf_size);
69
70         if (al_capture_device == NULL) {
71                 return -1;
72         }
73
74         if ( alcGetError(al_capture_device) != ALC_NO_ERROR ) {
75                 return -1;
76         }
77
78         Capture.format = al_format;
79         Capture.bits_per_sample = bits_per_sample;
80         Capture.n_channels = nchannels;
81         Capture.samples_per_second = freq;
82         Capture.block_align = (nchannels * bits_per_sample) / 8;
83
84         return 0;
85 }
86
87 int oal_capture_supported()
88 {
89         return oal_is_initted();
90 }
91
92 // start recording into the buffer
93 int oal_capture_start_record()
94 {
95         if ( !oal_is_initted() ) {
96                 return -1;
97         }
98
99         if (OAL_capture_recording) {
100                 return -1;
101         }
102
103         alcCaptureStart(al_capture_device);
104
105         OAL_capture_recording = 1;
106
107 //      nprintf(("Alan","RTVOICE => start record\n"));
108
109         return 0;
110 }
111
112 // stop recording into the buffer
113 int oal_capture_stop_record()
114 {
115         if ( !oal_is_initted() ) {
116                 return -1;
117         }
118
119         if ( !OAL_capture_recording ) {
120                 return -1;
121         }
122
123         alcCaptureStop(al_capture_device);
124
125         OAL_capture_recording = 0;
126
127 //      nprintf(("Alan","RTVOICE => stop record\n"));
128
129         return 0;
130 }
131
132 void oal_capture_close()
133 {
134         oal_capture_stop_record();
135
136         if (al_capture_device != NULL) {
137                 alcCaptureCloseDevice(al_capture_device);
138                 al_capture_device = NULL;
139         }
140 }
141
142 // return the max buffer size
143 int oal_capture_max_buffersize()
144 {
145         if ( !oal_is_initted() ) {
146                 return 0;
147         }
148
149         ALCsizei num_samples = 0;
150
151         alcGetIntegerv(al_capture_device, ALC_CAPTURE_SAMPLES, sizeof(ALCsizei), &num_samples);
152
153         if (alcGetError(al_capture_device) != ALC_NO_ERROR) {
154                 return 0;
155         }
156
157         return (num_samples * Capture.block_align);
158 }
159
160 // retrieve the recorded voice data
161 int oal_capture_get_raw_data(ubyte *outbuf, uint max_size)
162 {
163         if ( !oal_is_initted() ) {
164                 return 0;
165         }
166
167         if (outbuf == NULL) {
168                 return 0;
169         }
170
171         ALCsizei num_samples = 0;
172
173         alcGetIntegerv(al_capture_device, ALC_CAPTURE_SAMPLES, sizeof(ALCsizei), &num_samples);
174
175         if (num_samples <= 0) {
176                 return 0;
177         }
178
179         ALCsizei max_buf_size = min(num_samples, ALsizei(max_size / Capture.block_align));
180
181         alcCaptureSamples(al_capture_device, outbuf, max_buf_size);
182
183         if (alcGetError(al_capture_device) != ALC_NO_ERROR) {
184                 return 0;
185         }
186
187         return (int)max_buf_size * Capture.block_align;
188 }