1 /* PROJECT:         ReactOS sndrec32
2  * LICENSE:         GPL - See COPYING in the top level directory
3  * FILE:            base/applications/sndrec32/audio_membuffer.hpp
4  * PURPOSE:         Allocs audio buffer
5  * PROGRAMMERS:     Marco Pagliaricci (irc: rendar)
6  */
7 
8 #ifndef _AUDIOMEMBUFFER__H_
9 #define _AUDIOMEMBUFFER__H_
10 
11 #include "audio_receiver.hpp"
12 #include "audio_format.hpp"
13 #include "audio_producer.hpp"
14 
15 _AUDIO_NAMESPACE_START_
16 
17 class audio_membuffer : public audio_receiver, public audio_producer
18 {
19     protected:
20         BYTE * audio_data;
21         audio_format aud_info;
22         unsigned int buf_size;
23         unsigned int init_size;
24 
25         /* Protected Functions */
26 
27         /* allocs N bytes for the audio buffer */
28         void alloc_mem_(unsigned int);
29         /* frees memory */
30         void free_mem_(void);
31         /* resizes memory, and copies old audio data to new-size memory */
32         void resize_mem_(unsigned int);
33         /* truncates and discards unused memory. `buf_size' will be the same as `bytes_received' */
34         void truncate_(void);
35 
36     public:
37         void (* audio_arrival)(unsigned int);
38         void (* buffer_resized)(unsigned int);
39 
40         /* Ctors */
41         audio_membuffer(void) : audio_data(0),
42                                 aud_info(_AUDIO_DEFAULT_FORMAT),
43                                 buf_size(0),
44                                 init_size(0)
45         {
46             /* Allocs memory for at least 1 or some seconds of recording */
47             init_size = (unsigned int)((float)aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS);
48             alloc_mem_(init_size);
49         }
50 
51         audio_membuffer(audio_format aud_fmt) : audio_data(0),
52                                                 aud_info(aud_fmt),
53                                                 buf_size(0),
54                                                 init_size(0)
55         {
56             /* Allocs memory for at least 1 or some seconds of recording */
57             init_size = (unsigned int)((float)aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS);
58             alloc_mem_(init_size);
59         }
60 
61         audio_membuffer(audio_format aud_fmt, unsigned int seconds) : audio_data(0),
62                                                                       aud_info(aud_fmt),
63                                                                       buf_size(0),
64                                                                       init_size(0)
65         {
66             /* Allocs memory for audio recording the specified number of seconds */
67             init_size = aud_info.byte_rate() * seconds;
68             alloc_mem_(init_size);
69         }
70 
71         audio_membuffer(audio_format aud_fmt, float seconds) : audio_data(0),
72                                                                aud_info(aud_fmt),
73                                                                buf_size(0),
74                                                                init_size(0)
75         {
76             /* Allocs memory for audio recording the specified number of seconds */
77             init_size = (unsigned int)((float)aud_info.byte_rate() * seconds <= 0 ? 1 : seconds);
78             alloc_mem_(init_size);
79         }
80 
81         audio_membuffer(unsigned int bytes) : audio_data(0),
82                                               aud_info(_AUDIO_DEFAULT_FORMAT),
83                                               buf_size(0),
84                                               init_size(0)
85         {
86             /* Allocs memory for the specified bytes */
87             init_size = bytes;
88             alloc_mem_(init_size);
89         }
90 
91         /* Dtor */
92         virtual ~audio_membuffer(void)
93         {
94             /* Frees memory and reset values */
95             clear();
96         }
97 
98         /* Public functions */
99 
100         /* returns the audio buffer size in bytes */
101         unsigned int mem_size(void) const
102         {
103             return buf_size;
104         }
105 
106         /* returns how many audio data has been received, in bytes */
107         unsigned int bytes_recorded(void) const
108         {
109             return bytes_received;
110         }
111 
112         /* returns the integer number of seconds that the buffer can record */
113         unsigned int seconds_total(void) const
114         {
115             return buf_size / aud_info.byte_rate();
116         }
117 
118         /* returns the integer number of seconds that the buffer can record */
119         unsigned int seconds_recorded(void) const
120         {
121             return bytes_received / aud_info.byte_rate();
122         }
123 
124         /* returns the float number of seconds that the buffer can record */
125         float fseconds_total(void) const
126         {
127             return (float)((float) buf_size / (float)aud_info.byte_rate());
128         }
129 
130         /* returns the float number of seconds that has been recorded */
131         float fseconds_recorded(void) const
132         {
133             return (float)((float)bytes_received / (float)aud_info.byte_rate());
134         }
135 
136         unsigned int total_samples(void) const
137         {
138             return (aud_info.samples_in_seconds(fseconds_total()));
139         }
140 
141         unsigned int samples_received(void) const
142         {
143             return (aud_info.samples_in_bytes(bytes_received));
144         }
145 
146         /* returns a pointer to the audio buffer */
147         BYTE * audio_buffer(void) const
148         {
149             return audio_data;
150         }
151 
152         /* frees memory and resets values */
153         void clear(void);
154 
155         audio_format & audinfo(void)
156         {
157             return aud_info;
158         }
159 
160         /* discard audio data, resets values, but, instead of clear() which
161            frees memory, reset the memory to the initial size, ready for
162            receiving "new" audio data. */
163         void reset(void);
164 
165         /* truncates and discards unused memory. `buf_size' will be the same as `bytes_received' */
166         void truncate(void)
167         {
168             truncate_();
169         } /* TODO: fare truncate N bytes */
170 
171         /* if there is a buffer, discards current buffer memory and realloc
172            a new memory buffer with a new size expressed in bytes. */
173         void alloc_bytes(unsigned int);
174 
175         /* if there is a buffer, discards current buffer memory and realloc
176            a new memory buffer with a new size expressed in seconds, integer and float. */
177         void alloc_seconds(unsigned int);
178         void alloc_seconds(float);
179 
180         /* resizes in bytes the current buffer, without discarding
181            previously audio data received */
182         void resize_bytes(unsigned int);
183 
184         /* resizes in seconds the current buffer, without discarding
185            previously audio data received */
186         void resize_seconds( unsigned int );
187         void resize_seconds( float );
188 
189         /* Inherited Functions from `audio_receiver' */
190         void audio_receive(unsigned char *, unsigned int);
191 
192         /* Inherited Functions from `audio_buffer' */
193         unsigned int read(BYTE *, unsigned int);
194         bool finished(void);
195 };
196 
197 _AUDIO_NAMESPACE_END_
198 
199 #endif /* _AUDIOMEMBUFFER__H_ */
200