1 /* PROJECT:         ReactOS sndrec32
2  * LICENSE:         GPL - See COPYING in the top level directory
3  * FILE:            base/applications/sndrec32/audio_waveout.hpp
4  * PURPOSE:         Windows MM wave out abstraction
5  * PROGRAMMERS:     Marco Pagliaricci (irc: rendar)
6  */
7 
8 #ifndef _AUDIOWAVEOUT__H_
9 #define _AUDIOWAVEOUT__H_
10 
11 #include "audio_format.hpp"
12 #include "audio_producer.hpp"
13 
14 _AUDIO_NAMESPACE_START_
15 
16 enum audio_waveout_status
17 {
18     WAVEOUT_NOTREADY,
19     WAVEOUT_READY,
20     WAVEOUT_PLAYING,
21     WAVEOUT_FLUSHING,
22     WAVEOUT_PAUSED,
23     WAVEOUT_STOP,
24     WAVEOUT_ERR
25 };
26 
27 class audio_waveout
28 {
29     friend class audio_buffer;
30 
31     private:
32         static DWORD WINAPI playing_procedure(LPVOID);
33         HANDLE wakeup_playthread;
34 
35     protected:
36         WAVEFORMATEX wave_format;
37         WAVEHDR *wave_headers;
38         HWAVEOUT waveout_handle;
39 
40         const audio_format &aud_info;
41         audio_producer &audio_buf;
42 
43         /* Audio Playing Thread id */
44         DWORD playthread_id;
45 
46         audio_waveout_status status;
47         float buf_secs;
48 
49         /* The temporary buffers for the audio data outgoing to the waveout
50            device and its size, and its total number */
51 
52         /* base address for entire memory */
53         BYTE *main_buffer;
54 
55         /* size in bytes for the entire memory */
56         unsigned int mb_size;
57 
58         /* number of little buffers */
59         unsigned int buffers;
60 
61         /* Protected Functions */
62 
63         void init_(void);
64         void alloc_buffers_mem_(unsigned int, float);
65         void free_buffers_mem_(void);
66 
67         void init_headers_(void);
68         void prep_headers_(void);
69         void unprep_headers_(void);
70 
71     public:
72         /* Ctors */
73         audio_waveout(const audio_format &aud_fmt,
74                       audio_producer &a_buf) : wave_headers(0),
75                                                aud_info(aud_fmt),
76                                                audio_buf(a_buf),
77                                                status(WAVEOUT_NOTREADY),
78                                                main_buffer(0),
79                                                mb_size(0),
80                                                buffers(_AUDIO_DEFAULT_WAVEOUTBUFFERS)
81         {
82             /* Initializing internal wavein data */
83             init_();
84         }
85 
86         /* Dtor */
87         ~audio_waveout(void)
88         {
89         }
90 
91         /* Public Functions */
92 
93         void open(void);
94         void play(void);
95         void pause(void);
96         void stop(void);
97         void close(void);
98 
99         audio_waveout_status current_status(void)
100         {
101             return status;
102         }
103 
104         BYTE *buf(void)
105         {
106             return main_buffer;
107         }
108 
109         unsigned int bufsz(void)
110         {
111             return mb_size;
112         }
113 
114         unsigned int samplevalue_max(void)
115         {
116             if (aud_info.bits() == 16)
117                 return (unsigned int)65535;
118             else if (aud_info.bits() == 8)
119                 return (unsigned int)255;
120             else
121                 return 0;
122         }
123 
124         unsigned tot_samples_buf(void)
125         {
126             return aud_info.samples_in_bytes(mb_size);
127         }
128 
129         unsigned int nsample(unsigned int nsamp)
130         {
131             unsigned int svalue;
132 
133             if (aud_info.bits() == 16)
134                 svalue = (unsigned int)abs(*((short *)(main_buffer + aud_info.bytes_in_samples(nsamp))));
135             else if (aud_info.bits() == 8)
136                svalue = (unsigned int)((ptrdiff_t) *(main_buffer + aud_info.bytes_in_samples(nsamp)));
137             else
138                 svalue = 0;
139 
140             return svalue;
141         }
142 };
143 
144 _AUDIO_NAMESPACE_END_
145 
146 #endif /* _AUDIOWAVEOUT__H_ */
147