Debian Codegraph @

mednafen/include/blip/Blip_Buffer.h

raw symbols SCIP
1 // Band-limited sound synthesis buffer
2 // Various changes and hacks for use in Mednafen.
3
4 #ifdef __GNUC__
5 #define blip_inline inline __attribute__((always_inline))
6 #else
7 #define blip_inline inline
8 #endif
9
10 #include <limits.h>
11 #include <stdint.h>
12
13 // Blip_Buffer 0.4.1
14 #ifndef BLIP_BUFFER_H
15 #define BLIP_BUFFER_H
16
17 // Internal
18 typedef int32_t blip_long;
19 typedef uint32_t blip_ulong;
20 typedef int64_t blip_s64;
21 typedef uint64_t blip_u64;
22
23 // Time unit at source clock rate
24 typedef blip_long blip_time_t;
25
26 // Output samples are 16-bit signed, with a range of -32768 to 32767
27 typedef short blip_sample_t;
28 enum { blip_sample_max = 32767 };
29
30 class Blip_Buffer {
31 public:
32 typedef const char* blargg_err_t;
33
34 // Set output sample rate and buffer length in milliseconds (1/1000 sec, defaults
35 // to 1/4 second), then clear buffer. Returns NULL on success, otherwise if there
36 // isn't enough memory, returns error without affecting current buffer setup.
37 blargg_err_t set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 );
38
39 // Set number of source time units per second
40 void clock_rate( long );
41
42 // End current time frame of specified duration and make its samples available
43 // (along with any still-unread samples) for reading with read_samples(). Begins
44 // a new time frame at the end of the current frame.
45 void end_frame( blip_time_t time );
46
47 // Read at most 'max_samples' out of buffer into 'dest', removing them from from
48 // the buffer. Returns number of samples actually read and removed. If stereo is
49 // true, increments 'dest' one extra time after writing each sample, to allow
50 // easy interleving of two channels into a stereo output buffer.
51 long read_samples( blip_sample_t* dest, long max_samples, int stereo = 0 );
52
53 // Additional optional features
54
55 // Current output sample rate
56 long sample_rate() const;
57
58 // Length of buffer, in milliseconds
59 int length() const;
60
61 // Number of source time units per second
62 long clock_rate() const;
63
64 // Set frequency high-pass filter frequency, where higher values reduce bass more
65 void bass_freq( int frequency );
66
67 // Number of samples delay from synthesis to samples read out
68 int output_latency() const;
69
70 // Remove all available samples and clear buffer to silence. If 'entire_buffer' is
71 // false, just clears out any samples waiting rather than the entire buffer.
72 void clear( int entire_buffer = 1 );
73
74 // Number of samples available for reading with read_samples()
75 long samples_avail() const;
76
77 // Remove 'count' samples from those waiting to be read
78 void remove_samples( long count );
79
80 // Experimental features
81
82 // Count number of clocks needed until 'count' samples will be available.
83 // If buffer can't even hold 'count' samples, returns number of clocks until
84 // buffer becomes full.
85 blip_time_t count_clocks( long count ) const;
86
87 // Number of raw samples that can be mixed within frame of specified duration.
88 long count_samples( blip_time_t duration ) const;
89
90 // not documented yet
91 void set_modified() { modified_ = 1; }
92 int clear_modified() { int b = modified_; modified_ = 0; return b; }
93 typedef blip_u64 blip_resampled_time_t;
94 void remove_silence( long count );
95 blip_resampled_time_t resampled_duration( int t ) const { return t * factor_; }
96 blip_resampled_time_t resampled_time( blip_time_t t ) const { return t * factor_ + offset_; }
97 blip_resampled_time_t clock_rate_factor( long clock_rate ) const;
98 public:
99 Blip_Buffer();
100 ~Blip_Buffer();
101
102 // Deprecated
103 typedef blip_resampled_time_t resampled_time_t;
104 blargg_err_t sample_rate( long r ) { return set_sample_rate( r ); }
105 blargg_err_t sample_rate( long r, int msec ) { return set_sample_rate( r, msec ); }
106 private:
107 // noncopyable
108 Blip_Buffer( const Blip_Buffer& );
109 Blip_Buffer& operator = ( const Blip_Buffer& );
110 public:
111 typedef blip_time_t buf_t_;
112 blip_u64 factor_;
113 blip_resampled_time_t offset_;
114 buf_t_* buffer_;
115 blip_long buffer_size_;
116 blip_long reader_accum_;
117 int bass_shift_;
118 private:
119 long sample_rate_;
120 long clock_rate_;
121 int bass_freq_;
122 int length_;
123 int modified_;
124 friend class Blip_Reader;
125 };
126
127 #ifdef HAVE_CONFIG_H
128 #include "config.h"
129 #endif
130
131 #define BLIP_BUFFER_ACCURACY 32
132 #define BLIP_PHASE_BITS 8
133
134 // Number of bits in resample ratio fraction. Higher values give a more accurate ratio
135 // but reduce maximum buffer size.
136 //#ifndef BLIP_BUFFER_ACCURACY
137 // #define BLIP_BUFFER_ACCURACY 16
138 //#endif
139
140 // Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in
141 // noticeable broadband noise when synthesizing high frequency square waves.
142 // Affects size of Blip_Synth objects since they store the waveform directly.
143 //#ifndef BLIP_PHASE_BITS
144 // #if BLIP_BUFFER_FAST
145 // #define BLIP_PHASE_BITS 8
146 // #else
147 // #define BLIP_PHASE_BITS 6
148 // #endif
149 //#endif
150
151 // Internal
152 typedef blip_u64 blip_resampled_time_t;
153 int const blip_widest_impulse_ = 16;
154 int const blip_buffer_extra_ = blip_widest_impulse_ + 2;
155 int const blip_res = 1 << BLIP_PHASE_BITS;
156 class blip_eq_t;
157
158 class Blip_Synth_Fast_ {
159 public:
160 Blip_Buffer* buf;
161 int last_amp;
162 int delta_factor;
163
164 void volume_unit( double );
165 Blip_Synth_Fast_();
166 void treble_eq( blip_eq_t const& ) { }
167 };
168
169 class Blip_Synth_ {
170 public:
171 Blip_Buffer* buf;
172 int last_amp;
173 int delta_factor;
174
175 void volume_unit( double );
176 Blip_Synth_( short* impulses, int width );
177 void treble_eq( blip_eq_t const& );
178 private:
179 double volume_unit_;
180 short* const impulses;
181 int const width;
182 blip_long kernel_unit;
183 int impulses_size() const { return blip_res / 2 * width + 1; }
184 void adjust_impulse();
185 };
186
187 // Quality level. Start with blip_good_quality.
188 const int blip_med_quality = 8;
189 const int blip_good_quality = 12;
190 const int blip_high_quality = 16;
191
192 // Range specifies the greatest expected change in amplitude. Calculate it
193 // by finding the difference between the maximum and minimum expected
194 // amplitudes (max - min).
195 template<int quality,int range>
196 class Blip_Synth {
197 public:
198 // Set overall volume of waveform
199 void volume( double v ) { impl.volume_unit( v * (1.0 / (range < 0 ? -range : range)) ); }
200
201 // Configure low-pass filter (see blip_buffer.txt)
202 void treble_eq( blip_eq_t const& eq ) { impl.treble_eq( eq ); }
203
204 // Get/set Blip_Buffer used for output
205 Blip_Buffer* output() const { return impl.buf; }
206 void output( Blip_Buffer* b ) { impl.buf = b; impl.last_amp = 0; }
207
208 // Update amplitude of waveform at given time. Using this requires a separate
209 // Blip_Synth for each waveform.
210 void update( blip_time_t time, int amplitude );
211
212 // Low-level interface
213
214 // Add an amplitude transition of specified delta, optionally into specified buffer
215 // rather than the one set with output(). Delta can be positive or negative.
216 // The actual change in amplitude is delta * (volume / range)
217 void offset( blip_time_t, int delta, Blip_Buffer* ) const;
218 void offset( blip_time_t t, int delta ) const { offset( t, delta, impl.buf ); }
219
220 // Works directly in terms of fractional output samples. Contact author for more info.
221 void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const;
222
223 // Same as offset(), except code is inlined for higher performance
224 void offset_inline( blip_time_t t, int delta, Blip_Buffer* buf ) const {
225 offset_resampled( t * buf->factor_ + buf->offset_, delta, buf );
226 }
227 void offset_inline( blip_time_t t, int delta ) const {
228 offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf );
229 }
230
231 private:
232 Blip_Synth_Fast_ impl;
233 };
234
235 // Low-pass equalization parameters
236 class blip_eq_t {
237 public:
238 // Logarithmic rolloff to treble dB at half sampling rate. Negative values reduce
239 // treble, small positive values (0 to 5.0) increase treble.
240 blip_eq_t( double treble_db = 0 );
241
242 // See blip_buffer.txt
243 blip_eq_t( double treble, long rolloff_freq, long sample_rate, long cutoff_freq = 0 );
244
245 private:
246 double treble;
247 long rolloff_freq;
248 long sample_rate;
249 long cutoff_freq;
250 void generate( float* out, int count ) const;
251 friend class Blip_Synth_;
252 };
253
254 int const blip_sample_bits = 30;
255
256 #define BLIP_RESTRICT
257
258 // Optimized reading from Blip_Buffer, for use in custom sample output
259
260 // Begin reading from buffer. Name should be unique to the current block.
261 #define BLIP_READER_BEGIN( name, blip_buffer ) \
262 const Blip_Buffer::buf_t_* BLIP_RESTRICT name##_reader_buf = (blip_buffer).buffer_;\
263 blip_long name##_reader_accum = (blip_buffer).reader_accum_
264
265 // Get value to pass to BLIP_READER_NEXT()
266 #define BLIP_READER_BASS( blip_buffer ) ((blip_buffer).bass_shift_)
267
268 // Constant value to use instead of BLIP_READER_BASS(), for slightly more optimal
269 // code at the cost of having no bass control
270 int const blip_reader_default_bass = 9;
271
272 // Current sample
273 #define BLIP_READER_READ( name ) (name##_reader_accum >> (blip_sample_bits - 16))
274
275 // Current raw sample in full internal resolution
276 #define BLIP_READER_READ_RAW( name ) (name##_reader_accum)
277
278 // Advance to next sample
279 #define BLIP_READER_NEXT( name, bass ) \
280 (void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass)))
281
282 // End reading samples from buffer. The number of samples read must now be removed
283 // using Blip_Buffer::remove_samples().
284 #define BLIP_READER_END( name, blip_buffer ) \
285 (void) ((blip_buffer).reader_accum_ = name##_reader_accum)
286
287
288 // Compatibility with older version
289 const long blip_unscaled = 65535;
290 const int blip_low_quality = blip_med_quality;
291 const int blip_best_quality = blip_high_quality;
292
293 // Deprecated; use BLIP_READER macros as follows:
294 // Blip_Reader r; r.begin( buf ); -> BLIP_READER_BEGIN( r, buf );
295 // int bass = r.begin( buf ) -> BLIP_READER_BEGIN( r, buf ); int bass = BLIP_READER_BASS( buf );
296 // r.read() -> BLIP_READER_READ( r )
297 // r.read_raw() -> BLIP_READER_READ_RAW( r )
298 // r.next( bass ) -> BLIP_READER_NEXT( r, bass )
299 // r.next() -> BLIP_READER_NEXT( r, blip_reader_default_bass )
300 // r.end( buf ) -> BLIP_READER_END( r, buf )
301 class Blip_Reader {
302 public:
303 int begin( Blip_Buffer& );
304 blip_long read() const { return accum >> (blip_sample_bits - 16); }
305 blip_long read_raw() const { return accum; }
306 void next( int bass_shift = 9 ) { accum += *buf++ - (accum >> bass_shift); }
307 void end( Blip_Buffer& b ) { b.reader_accum_ = accum; }
308
309 private:
310 const Blip_Buffer::buf_t_* buf;
311 blip_long accum;
312 };
313
314 // End of public interface
315
316 #include <assert.h>
317
318 template<int quality,int range>
319 blip_inline void Blip_Synth<quality,range>::offset_resampled( blip_resampled_time_t time,
320 int delta, Blip_Buffer* blip_buf ) const
321 {
322 // Fails if time is beyond end of Blip_Buffer, due to a bug in caller code or the
323 // need for a longer buffer as set by set_sample_rate().
324 assert( (blip_long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ );
325 delta *= impl.delta_factor;
326 blip_long* BLIP_RESTRICT buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY);
327 int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1));
328
329 blip_long left = buf [0] + delta;
330
331 // Kind of crappy, but doing shift after multiply results in overflow.
332 // Alternate way of delaying multiply by delta_factor results in worse
333 // sub-sample resolution.
334 blip_long right = (delta >> BLIP_PHASE_BITS) * phase;
335 left -= right;
336 right += buf [1];
337
338 buf [0] = left;
339 buf [1] = right;
340 }
341
342 #undef BLIP_FWD
343 #undef BLIP_REV
344
345 template<int quality,int range>
346 blip_inline void Blip_Synth<quality,range>::offset( blip_time_t t, int delta, Blip_Buffer* buf ) const
347 {
348 offset_resampled( t * buf->factor_ + buf->offset_, delta, buf );
349 }
350
351 template<int quality,int range>
352 blip_inline void Blip_Synth<quality,range>::update( blip_time_t t, int amp )
353 {
354 int delta = amp - impl.last_amp;
355 impl.last_amp = amp;
356 offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf );
357 }
358
359 blip_inline blip_eq_t::blip_eq_t( double t ) :
360 treble( t ), rolloff_freq( 0 ), sample_rate( 44100 ), cutoff_freq( 0 ) { }
361 blip_inline blip_eq_t::blip_eq_t( double t, long rf, long sr, long cf ) :
362 treble( t ), rolloff_freq( rf ), sample_rate( sr ), cutoff_freq( cf ) { }
363
364 blip_inline int Blip_Buffer::length() const { return length_; }
365 blip_inline long Blip_Buffer::samples_avail() const { return (long) (offset_ >> BLIP_BUFFER_ACCURACY); }
366 blip_inline long Blip_Buffer::sample_rate() const { return sample_rate_; }
367 blip_inline int Blip_Buffer::output_latency() const { return blip_widest_impulse_ / 2; }
368 blip_inline void Blip_Buffer::clock_rate( long cps ) { factor_ = clock_rate_factor( clock_rate_ = cps ); }
369
370 blip_inline int Blip_Reader::begin( Blip_Buffer& blip_buf )
371 {
372 buf = blip_buf.buffer_;
373 accum = blip_buf.reader_accum_;
374 return blip_buf.bass_shift_;
375 }
376
377 int const blip_max_length = 0;
378 int const blip_default_length = 250;
379
380 #endif