ReactOS  0.4.12-dev-51-ge94618b
audio_membuffer.hpp
Go to the documentation of this file.
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 
16 
18 {
19  protected:
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 
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 
198 
199 #endif /* _AUDIOMEMBUFFER__H_ */
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:47
unsigned int seconds_recorded(void) const
void(* audio_arrival)(unsigned int)
audio_membuffer(audio_format aud_fmt, unsigned int seconds)
unsigned int mem_size(void) const
unsigned int bytes_received
void resize_seconds(unsigned int)
virtual ~audio_membuffer(void)
audio_membuffer(audio_format aud_fmt, float seconds)
#define _AUDIO_NAMESPACE_START_
Definition: audio_def.hpp:24
void resize_mem_(unsigned int)
unsigned int buf_size
#define _AUDIO_DEFAULT_BUFSECS
Definition: audio_def.hpp:21
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
unsigned int byte_rate(void) const
unsigned int read(BYTE *, unsigned int)
void(* buffer_resized)(unsigned int)
void alloc_mem_(unsigned int)
BYTE * audio_buffer(void) const
audio_membuffer(audio_format aud_fmt)
audio_format & audinfo(void)
unsigned int samples_in_seconds(float seconds) const
unsigned int samples_received(void) const
unsigned int seconds_total(void) const
unsigned int init_size
audio_format aud_info
void truncate_(void)
void resize_bytes(unsigned int)
unsigned int total_samples(void) const
unsigned char BYTE
Definition: ntddk_ex.h:96
void alloc_bytes(unsigned int)
float fseconds_recorded(void) const
unsigned int bytes_recorded(void) const
#define _AUDIO_DEFAULT_FORMAT
Definition: audio_def.hpp:13
#define _AUDIO_NAMESPACE_END_
Definition: audio_def.hpp:25
unsigned int samples_in_bytes(unsigned int bytes) const
audio_membuffer(unsigned int bytes)
void free_mem_(void)
float fseconds_total(void) const
void audio_receive(unsigned char *, unsigned int)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:29
void alloc_seconds(unsigned int)