audio_stream_resampled.h
1 /*************************************************************************/
2 /* audio_stream_resampled.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef AUDIO_STREAM_RESAMPLED_H
30 #define AUDIO_STREAM_RESAMPLED_H
31 
32 #include "scene/resources/audio_stream.h"
33 
34 #if 0
35 
36 class AudioStreamResampled : public AudioStream {
37  OBJ_TYPE(AudioStreamResampled,AudioStream);
38 
39  uint32_t rb_bits;
40  uint32_t rb_len;
41  uint32_t rb_mask;
42  uint32_t read_buff_len;
43  uint32_t channels;
44  uint32_t mix_rate;
45 
46  volatile int rb_read_pos;
47  volatile int rb_write_pos;
48 
49  int32_t offset; //contains the fractional remainder of the resampler
50  enum {
51  MIX_FRAC_BITS=13,
52  MIX_FRAC_LEN=(1<<MIX_FRAC_BITS),
53  MIX_FRAC_MASK=MIX_FRAC_LEN-1,
54  };
55 
56  int16_t *read_buf;
57  int16_t *rb;
58 
59 
60  template<int C>
61  uint32_t _resample(int32_t *p_dest,int p_todo,int32_t p_increment);
62 
63 
64 protected:
65 
66  _FORCE_INLINE_ int get_total() const {
67 
68  return rb_len;
69  }
70 
71  _FORCE_INLINE_ int get_todo() const { //return amount of frames to mix
72 
73  int todo;
74  int read_pos_cache=rb_read_pos;
75 
76  if (read_pos_cache==rb_write_pos) {
77  todo=rb_len-1;
78  } else if (read_pos_cache>rb_write_pos) {
79 
80  todo=read_pos_cache-rb_write_pos-1;
81  } else {
82 
83  todo=(rb_len-rb_write_pos)+read_pos_cache-1;
84  }
85 
86  return todo;
87  }
88 
89  //Stream virtual funcs
90  virtual int get_channel_count() const;
91  virtual bool mix(int32_t *p_dest, int p_frames);
92 
93  _FORCE_INLINE_ void _flush() {
94  rb_read_pos=0;
95  rb_write_pos=0;
96  }
97 
98  _FORCE_INLINE_ int16_t *get_write_buffer() { return read_buf; }
99  _FORCE_INLINE_ void write(uint32_t p_frames) {
100 
101  ERR_FAIL_COND(p_frames >= rb_len);
102 
103  switch(channels) {
104  case 1: {
105 
106  for(uint32_t i=0;i<p_frames;i++) {
107 
108  rb[ rb_write_pos ] = read_buf[i];
109  rb_write_pos=(rb_write_pos+1)&rb_mask;
110  }
111  } break;
112  case 2: {
113 
114  for(uint32_t i=0;i<p_frames;i++) {
115 
116  rb[ (rb_write_pos<<1)+0 ] = read_buf[(i<<1)+0];
117  rb[ (rb_write_pos<<1)+1 ] = read_buf[(i<<1)+1];
118  rb_write_pos=(rb_write_pos+1)&rb_mask;
119  }
120  } break;
121  case 4: {
122 
123  for(uint32_t i=0;i<p_frames;i++) {
124 
125  rb[ (rb_write_pos<<2)+0 ] = read_buf[(i<<2)+0];
126  rb[ (rb_write_pos<<2)+1 ] = read_buf[(i<<2)+1];
127  rb[ (rb_write_pos<<2)+2 ] = read_buf[(i<<2)+2];
128  rb[ (rb_write_pos<<2)+3 ] = read_buf[(i<<2)+3];
129  rb_write_pos=(rb_write_pos+1)&rb_mask;
130  }
131  } break;
132  case 6: {
133 
134  for(uint32_t i=0;i<p_frames;i++) {
135 
136  rb[ (rb_write_pos*6)+0 ] = read_buf[(i*6)+0];
137  rb[ (rb_write_pos*6)+1 ] = read_buf[(i*6)+1];
138  rb[ (rb_write_pos*6)+2 ] = read_buf[(i*6)+2];
139  rb[ (rb_write_pos*6)+3 ] = read_buf[(i*6)+3];
140  rb[ (rb_write_pos*6)+4 ] = read_buf[(i*6)+4];
141  rb[ (rb_write_pos*6)+5 ] = read_buf[(i*6)+5];
142  rb_write_pos=(rb_write_pos+1)&rb_mask;
143  }
144  } break;
145 
146 
147  }
148 
149  }
150 
151  virtual bool _can_mix() const =0;
152 
153  _FORCE_INLINE_ bool _is_ready() const{
154  return rb!=NULL;
155  }
156 
157  Error _setup(int p_channels,int p_mix_rate,int p_minbuff_needed=-1);
158  void _clear();
159 
160 public:
161  AudioStreamResampled();
162  ~AudioStreamResampled();
163 };
164 #endif
165 #endif // AUDIO_STREAM_RESAMPLED_H
Definition: audio_stream.h:68