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)
8 #ifndef _AUDIOMEMBUFFER__H_
9 #define _AUDIOMEMBUFFER__H_
11 #include "audio_receiver.hpp"
12 #include "audio_format.hpp"
13 #include "audio_producer.hpp"
15 _AUDIO_NAMESPACE_START_
17 class audio_membuffer : public audio_receiver, public audio_producer
21 audio_format aud_info;
22 unsigned int buf_size;
23 unsigned int init_size;
25 /* Protected Functions */
27 /* allocs N bytes for the audio buffer */
28 void alloc_mem_(unsigned int);
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' */
37 void (* audio_arrival)(unsigned int);
38 void (* buffer_resized)(unsigned int);
41 audio_membuffer(void) : audio_data(0),
42 aud_info(_AUDIO_DEFAULT_FORMAT),
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);
51 audio_membuffer(audio_format aud_fmt) : audio_data(0),
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);
61 audio_membuffer(audio_format aud_fmt, unsigned int seconds) : audio_data(0),
66 /* Allocs memory for audio recording the specified number of seconds */
67 init_size = aud_info.byte_rate() * seconds;
68 alloc_mem_(init_size);
71 audio_membuffer(audio_format aud_fmt, float seconds) : audio_data(0),
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);
81 audio_membuffer(unsigned int bytes) : audio_data(0),
82 aud_info(_AUDIO_DEFAULT_FORMAT),
86 /* Allocs memory for the specified bytes */
88 alloc_mem_(init_size);
92 virtual ~audio_membuffer(void)
94 /* Frees memory and reset values */
98 /* Public functions */
100 /* returns the audio buffer size in bytes */
101 unsigned int mem_size(void) const
106 /* returns how many audio data has been received, in bytes */
107 unsigned int bytes_recorded(void) const
109 return bytes_received;
112 /* returns the integer number of seconds that the buffer can record */
113 unsigned int seconds_total(void) const
115 return buf_size / aud_info.byte_rate();
118 /* returns the integer number of seconds that the buffer can record */
119 unsigned int seconds_recorded(void) const
121 return bytes_received / aud_info.byte_rate();
124 /* returns the float number of seconds that the buffer can record */
125 float fseconds_total(void) const
127 return (float)((float) buf_size / (float)aud_info.byte_rate());
130 /* returns the float number of seconds that has been recorded */
131 float fseconds_recorded(void) const
133 return (float)((float)bytes_received / (float)aud_info.byte_rate());
136 unsigned int total_samples(void) const
138 return (aud_info.samples_in_seconds(fseconds_total()));
141 unsigned int samples_received(void) const
143 return (aud_info.samples_in_bytes(bytes_received));
146 /* returns a pointer to the audio buffer */
147 BYTE * audio_buffer(void) const
152 /* frees memory and resets values */
155 audio_format & audinfo(void)
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. */
165 /* truncates and discards unused memory. `buf_size' will be the same as `bytes_received' */
169 } /* TODO: fare truncate N bytes */
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);
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);
180 /* resizes in bytes the current buffer, without discarding
181 previously audio data received */
182 void resize_bytes(unsigned int);
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 );
189 /* Inherited Functions from `audio_receiver' */
190 void audio_receive(unsigned char *, unsigned int);
192 /* Inherited Functions from `audio_buffer' */
193 unsigned int read(BYTE *, unsigned int);
197 _AUDIO_NAMESPACE_END_
199 #endif /* _AUDIOMEMBUFFER__H_ */