1 /** Sample buffer that resamples from input clock rate to output sample rate \file */
2 
3 /* blip_buf $vers */
4 #ifndef BLIP_BUF_H
5 #define BLIP_BUF_H
6 
7 #ifdef __cplusplus
8 	extern "C" {
9 #endif
10 
11 /** First parameter of most functions is blip_t*, or const blip_t* if nothing
12 is changed. */
13 typedef struct blip_t blip_t;
14 typedef struct blip_buffer_state_t blip_buffer_state_t;
15 
16 /** Creates new buffer that can hold at most sample_count samples. Sets rates
17 so that there are blip_max_ratio clocks per sample. Returns pointer to new
18 buffer, or NULL if insufficient memory. */
19 blip_t* blip_new( int sample_count );
20 
21 /** Sets approximate input clock rate and output sample rate. For every
22 clock_rate input clocks, approximately sample_rate samples are generated. */
23 void blip_set_rates( blip_t*, double clock_rate, double sample_rate );
24 
25 enum { /** Maximum clock_rate/sample_rate ratio. For a given sample_rate,
26 clock_rate must not be greater than sample_rate*blip_max_ratio. */
27 blip_max_ratio = 1 << 20 };
28 
29 /** Clears entire buffer. Afterwards, blip_samples_avail() == 0. */
30 void blip_clear( blip_t* );
31 
32 #ifndef BLIP_MONO
33 
34 /** Adds positive/negative deltas into stereo buffers at specified clock time. */
35 void blip_add_delta( blip_t*, unsigned time, int delta_l, int delta_r );
36 
37 /** Same as blip_add_delta(), but uses faster, lower-quality synthesis. */
38 void blip_add_delta_fast( blip_t*, unsigned time, int delta_l, int delta_r );
39 
40 #else
41 
42 /** Adds positive/negative delta into buffer at specified clock time. */
43 void blip_add_delta( blip_t*, unsigned clock_time, int delta );
44 
45 /** Same as blip_add_delta(), but uses faster, lower-quality synthesis. */
46 void blip_add_delta_fast( blip_t*, unsigned clock_time, int delta );
47 
48 #endif
49 
50 /** Length of time frame, in clocks, needed to make sample_count additional
51 samples available. */
52 int blip_clocks_needed( const blip_t*, int sample_count );
53 
54 enum { /** Maximum number of samples that can be generated from one time frame. */
55 blip_max_frame = 4000 };
56 
57 /** Makes input clocks before clock_duration available for reading as output
58 samples. Also begins new time frame at clock_duration, so that clock time 0 in
59 the new time frame specifies the same clock as clock_duration in the old time
60 frame specified. Deltas can have been added slightly past clock_duration (up to
61 however many clocks there are in two output samples). */
62 void blip_end_frame( blip_t*, unsigned clock_duration );
63 
64 /** Number of buffered samples available for reading. */
65 int blip_samples_avail( const blip_t* );
66 
67 /** Discards samples by moving the write pointer backwards directly,
68 leaving the audio buffer dirty. */
69 int blip_discard_samples_dirty(blip_t*, int count);
70 
71 /** Reads and removes at most 'count' samples and writes them to to every other
72 element of 'out', allowing easy interleaving of two buffers into a stereo sample
73 stream. Outputs 16-bit signed samples. Returns number of samples actually read.  */
74 int blip_read_samples( blip_t*, short out [], int count);
75 
76 /* Same as above function except sample is mixed from three blip buffers source */
77 int blip_mix_samples( blip_t* m1, blip_t* m2, blip_t* m3, short out [], int count);
78 
79 /** Frees buffer. No effect if NULL is passed. */
80 void blip_delete( blip_t* );
81 
82 /** Saves buffer state (samples, accumulators, and offset) to a blip_buffer_state structure */
83 void blip_save_buffer_state(const blip_t *buf, blip_buffer_state_t *state);
84 
85 /** Restores buffer state (samples, accumulators, and offset) from a blip_buffer_state structure */
86 void blip_load_buffer_state(blip_t *buf, const blip_buffer_state_t *state);
87 
88 /** Creates new blip_buffer_state. Returns pointer to new buffer,
89 or NULL if insufficient memory. */
90 blip_buffer_state_t* blip_new_buffer_state();
91 
92 /** Frees blip_buffer_state. No effect if NULL is passed. */
93 void blip_delete_buffer_state(blip_buffer_state_t *state);
94 
95 /* Deprecated */
96 typedef blip_t blip_buffer_t;
97 
98 #ifdef __cplusplus
99 	}
100 #endif
101 
102 #endif
103