- Merge from trunk up to r45543
[reactos.git] / base / applications / sndrec32 / audio_wavein.hpp
1 /* PROJECT: ReactOS sndrec32
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/applications/sndrec32/audio_wavein.hpp
4 * PURPOSE: Windows MM wave in abstraction
5 * PROGRAMMERS: Marco Pagliaricci (irc: rendar)
6 */
7
8
9 #ifndef _AUDIOWAVEIN_H_
10 #define _AUDIOWAVEIN_H_
11
12
13
14 #include "audio_def.hpp"
15 #include "audio_format.hpp"
16 #include "audio_receiver.hpp"
17
18
19
20 _AUDIO_NAMESPACE_START_
21
22
23
24
25 enum audio_wavein_status { WAVEIN_NOTREADY, WAVEIN_READY,
26 WAVEIN_RECORDING, WAVEIN_ERR,
27 WAVEIN_STOP, WAVEIN_FLUSHING
28
29 };
30
31
32
33
34
35 class audio_wavein
36 {
37 private:
38
39
40
41 //
42 // The new recording thread sends message to this procedure
43 // about open recording, close, and sound data recorded
44 //
45
46 static DWORD WINAPI recording_procedure( LPVOID );
47
48 //
49 // When this event is signaled, then the previsiously created
50 // recording thread will wake up and start recording audio
51 // and will pass audio data to an `audio_receiver' object.
52 //
53
54 HANDLE wakeup_recthread;
55 HANDLE data_flushed_event;
56
57
58
59
60 protected:
61
62
63 //TODO: puts these structs in private?!
64
65
66
67
68 //
69 // Audio wavein device stuff
70 //
71
72 WAVEFORMATEX wave_format;
73 WAVEHDR * wave_headers;
74 HWAVEIN wavein_handle;
75
76
77
78
79
80 audio_format aud_info;
81
82 audio_receiver & audio_rcvd;
83
84
85
86 //
87 // Audio Recorder Thread id
88 //
89
90 DWORD recthread_id;
91
92
93
94
95 //
96 // Object status
97 //
98
99 audio_wavein_status status;
100
101
102
103
104
105
106
107 //
108 // How many seconds of audio
109 // can record the internal buffer
110 // before flushing audio data
111 // to the `audio_receiver' class?
112 //
113
114 float buf_secs;
115
116
117 //
118 // The temporary buffers for the audio
119 // data incoming from the wavein device
120 // and its size, and its total number.
121 //
122
123 BYTE * main_buffer;
124 unsigned int mb_size;
125
126 unsigned int buffers;
127
128
129
130
131
132 //
133 // Protected Functions
134 //
135
136
137 //initialize all structures and variables.
138 void init_( void );
139
140 void alloc_buffers_mem_( unsigned int, float );
141 void free_buffers_mem_( void );
142
143 void init_headers_( void );
144 void prep_headers_( void );
145 void unprep_headers_( void );
146 void add_buffers_to_driver_( void );
147
148
149
150
151
152
153
154 public:
155
156
157 //
158 // Ctors
159 //
160
161 audio_wavein(
162 const audio_format & a_info, audio_receiver & a_receiver )
163
164 : wave_headers( 0 ),
165 aud_info( a_info ), audio_rcvd( a_receiver ),
166 status( WAVEIN_NOTREADY ), main_buffer( 0 ), mb_size( 0 ),
167 buffers( _AUDIO_DEFAULT_WAVEINBUFFERS )
168 {
169
170 //
171 // Initializing internal wavein data
172 //
173
174
175 init_();
176
177 aud_info = a_info;
178 }
179
180
181
182
183
184
185
186 //
187 // Dtor
188 //
189
190 ~audio_wavein( void )
191 {
192
193 //close(); TODO!
194
195 }
196
197
198
199 //
200 // Public functions
201 //
202
203 void open( void );
204 void close ( void );
205
206
207 void start_recording( void );
208 void stop_recording( void );
209
210
211
212 audio_wavein_status current_status ( void ) const
213 {
214 return status;
215 }
216
217 float buffer_secs( void ) const
218 { return buf_secs; }
219
220
221 void buffer_secs( float bsecs )
222 {
223 //
224 // Some checking
225 //
226
227 if ( bsecs <= 0 )
228 return;
229
230
231 //
232 // Set seconds lenght for each
233 // buffer.
234 //
235
236 buf_secs = bsecs;
237 }
238
239
240 unsigned int total_buffers( void ) const
241 { return buffers; }
242
243
244
245 void total_buffers( unsigned int tot_bufs )
246 {
247
248 //
249 // Some checking
250 //
251
252 if ( tot_bufs == 0 )
253 return;
254
255
256 //
257 // Sets the number of total buffers.
258 //
259
260 buffers = tot_bufs;
261 }
262
263
264 audio_format format( void ) const
265 { return aud_info; }
266
267
268
269
270 BYTE * buf( void ) { return main_buffer; }
271 unsigned int bufsz( void ) { return mb_size; }
272
273
274 unsigned int samplevalue_max( void )
275 {
276
277 if ( aud_info.bits() == 16 )
278 return (unsigned int )65535;
279
280 else if ( aud_info.bits() == 8 )
281 return (unsigned int)255;
282
283 else
284 return 0;
285 }
286
287
288 unsigned tot_samples_buf( void )
289 {
290
291
292 return aud_info.samples_in_bytes( mb_size );
293
294
295 }
296
297 unsigned int nsample ( unsigned int nsamp )
298 {
299
300
301 unsigned int svalue;
302
303
304
305 if ( aud_info.bits() == 16 )
306 svalue = ( unsigned int ) abs( *(( short * ) (main_buffer + aud_info.bytes_in_samples( nsamp ))));
307 else if ( aud_info.bits() == 8 )
308 svalue = (unsigned int)(( unsigned char * ) *(main_buffer + aud_info.bytes_in_samples( nsamp )));
309
310 else
311 svalue = 0;
312
313 return svalue;
314
315 }
316
317
318 };
319
320
321
322
323 _AUDIO_NAMESPACE_END_
324
325
326
327
328 #endif //ifdef _AUDIOWAVEIN_H_