Debian Codegraph @

android/app/jni/extern/blip_buf/blip_buf.txt

raw
1 blip_buf $vers
2 --------------
3 Author : Shay Green <[email protected]>
4 Website : http://www.slack.net/~ant/
5 License : GNU Lesser General Public License (LGPL)
6
7 MODIFICATION DISCLAIMER
8 -----------------------
9 I have modified this library in order to add a function that disables the DC offset correction high-pass filter.
10 - tildearrow
11
12
13 Contents
14 --------
15 * Overview
16 * Buffer creation
17 * Waveform generation
18 * Time frames
19 * Complex waveforms
20 * Sample buffering
21 * Thanks
22
23
24 Overview
25 --------
26 This library resamples audio waveforms from input clock rate to output
27 sample rate. Usage follows this general pattern:
28
29 * Create buffer with blip_new().
30 * Set clock rate and sample rate with blip_set_rates().
31 * Waveform generation loop:
32 - Generate several clocks of waveform with blip_add_delta().
33 - End time frame with blip_end_frame().
34 - Read samples from buffer with blip_read_samples().
35 * Free buffer with blip_delete().
36
37
38 Buffer creation
39 ---------------
40 Before synthesis, a buffer must be created with blip_new(). Its size is
41 the maximum number of unread samples it can hold. For most uses, this
42 can be 1/10 the sample rate or less, since samples will usually be read
43 out immediately after being generated.
44
45 After the buffer is created, the input clock rate and output sample rate
46 must be set with blip_set_rates(). This determines how many input clocks
47 there are per second, and how many output samples are generated per
48 second.
49
50 If the compiler supports a 64-bit integer type, then the input-output
51 ratio is stored very accurately. If the compiler only supports a 32-bit
52 integer type, then the ratio is stored with only 20 fraction bits, so
53 some ratios cannot be represented exactly (for example, sample
54 rate=48000 and clock rate=48001). The ratio is internally rounded up, so
55 there will never be fewer than 'sample rate' samples per second. Having
56 too many per second is generally better than having too few.
57
58
59 Waveform generation
60 -------------------
61 Waveforms are generated at the input clock rate. Consider a simple
62 square wave with 8 clocks per cycle (4 clocks high, 4 clocks low):
63
64 |<-- 8 clocks ->|
65 +5| ._._._._ ._._._._ ._._._._ ._._
66 | | | | | | | |
67 Amp 0|._._._._ | | | | | |
68 | | | | | | |
69 -5| ._._._._ ._._._._ ._._._._
70 * . . . * . . . * . . . * . . . * . . . * . . . * . . . * .
71 Time 0 4 8 12 16 20 24 28
72
73 The wave changes amplitude at time points 0, 4, 8, 12, 16, etc.
74
75 The following generates the amplitude at every clock of above waveform
76 at the input clock rate:
77
78 int wave [30];
79
80 for ( int i = 4; i < 30; ++i )
81 {
82 if ( i % 8 < 4 )
83 wave [i] = -5;
84 else
85 wave [i] = +5;
86 }
87
88 Without this library, the wave array would then need to be resampled
89 from the input clock rate to the output sample rate. This library does
90 this resampling internally, so it won't be discussed further; waveform
91 generation code can focus entirely on the input clocks.
92
93 Rather than specify the amplitude at every clock, this library merely
94 needs to know the points where the amplitude CHANGES, referred to as a
95 delta. The time of a delta is specified with a clock count. The deltas
96 for this square wave are shown below the time points they occur at:
97
98 +5| ._._._._ ._._._._ ._._._._ ._._
99 | | | | | | | |
100 Amp 0|._._._._ | | | | | |
101 | | | | | | |
102 -5| ._._._._ ._._._._ ._._._._
103 * . . . * . . . * . . . * . . . * . . . * . . . * . . . * .
104 Time 0 4 8 12 16 20 24 28
105 Delta +5 -10 +10 -10 +10 -10 +10
106
107 The following calls generate the above waveform:
108
109 blip_add_delta( blip, 4, +5 );
110 blip_add_delta( blip, 8, -10 );
111 blip_add_delta( blip, 12, +10 );
112 blip_add_delta( blip, 16, -10 );
113 blip_add_delta( blip, 20, +10 );
114 blip_add_delta( blip, 24, -10 );
115 blip_add_delta( blip, 28, +10 );
116
117 In the examples above, the amplitudes are small for clarity. The 16-bit
118 sample range is -32768 to +32767, so actual waveform amplitudes would
119 need to be in the thousands to be audible (for example, -5000 to +5000).
120
121 This library allows waveform generation code to pay NO attention to the
122 output sample rate. It can focus ENTIRELY on the essence of the
123 waveform: the points where its amplitude changes. Since these points can
124 be efficiently generated in a loop, synthesis is efficient. Sound chip
125 emulation code can be structured to allow full accuracy down to a single
126 clock, with the emulated CPU being able to simply tell the sound chip to
127 "emulate from wherever you left off, up to clock time T within the
128 current time frame".
129
130
131 Time frames
132 -----------
133 Since time keeps increasing, if left unchecked, at some point it would
134 overflow the range of an integer. This library's solution to the problem
135 is to break waveform generation into time frames of moderate length.
136 Clock counts within a time frame are thus relative to the beginning of
137 the frame, where 0 is the beginning of the frame. When a time frame of
138 length T is ended, what was at time T in the old time frame is now at
139 time 0 in the new time frame. Breaking the above waveform into time
140 frames of 10 clocks each looks like this:
141
142 +5| ._._._._ ._._._._ ._._._._ ._._
143 | | | | | | | |
144 Amp 0|._._._._ | | | | | |
145 | | | | | | |
146 -5| ._._._._ ._._._._ ._._._._
147 * . . . * . . . * . . . * . . . * . . . * . . . * . . . * .
148 Time |0 4 8 | 2 6 |0 4 8 |
149 | first time frame | second time frame | third time frame |
150 |<--- 10 clocks --->|<--- 10 clocks --->|<--- 10 clocks --->|
151
152 The following calls generate the above waveform. After they execute, the
153 first 30 clocks of the waveform will have been resampled and be
154 available as output samples for reading with blip_read_samples().
155
156 blip_add_delta( blip, 4, +5 );
157 blip_add_delta( blip, 8, -10 );
158 blip_end_frame( blip, 10 );
159
160 blip_add_delta( blip, 2, +10 );
161 blip_add_delta( blip, 6, -10 );
162 blip_end_frame( blip, 10 );
163
164 blip_add_delta( blip, 0, +10 );
165 blip_add_delta( blip, 4, -10 );
166 blip_add_delta( blip, 8, +10 );
167 blip_end_frame( blip, 10 );
168 ...
169
170 Time frames can be a convenient length, and the length can vary from one
171 frame to the next. Once a time frame is ended, the resulting output
172 samples become available for reading immediately, and no more deltas can
173 be added to it.
174
175 There is a limit of about 4000 output samples per time frame. The number
176 of clocks depends on the clock rate. At common sample rates, this allows
177 time frames of at least 1/15 second, plenty for most uses. This limit
178 allows increased resampling ratio accuracy.
179
180 In an emulator, it is usually convenient to have audio time frames
181 correspond to video frames, where the CPU's clock counter is reset at
182 the beginning of each video frame and thus can be used directly as the
183 relative clock counts for audio time frames.
184
185
186 Complex waveforms
187 -----------------
188 Any sort of waveform can be generated, not just a square wave. For
189 example, a saw-like wave:
190
191 +5| ._._._._ ._._._._ ._._
192 | | | | | |
193 Amp 0|._._._._ | ._._._._ | ._._._._
194 | | | | |
195 -5| ._._._._ ._._._._
196 * . . . * . . . * . . . * . . . * . . . * . . . * . . . * .
197 Time 0 4 8 12 16 20 24 28
198 Delta +5 -10 +5 +5 -10 +5 +5
199
200 Code to generate above waveform:
201
202 blip_add_delta( blip, 4, +5 );
203 blip_add_delta( blip, 8, -10 );
204 blip_add_delta( blip, 12, +5 );
205 blip_add_delta( blip, 16, +5 );
206 blip_add_delta( blip, 20, +10 );
207 blip_add_delta( blip, 24, +5 );
208 blip_add_delta( blip, 28, +5 );
209
210 Similarly, multiple waveforms can be added within a time frame without
211 problem. It doesn't matter what order they're added, because all the
212 library needs are the deltas. The synthesis code doesn't need to know
213 all the waveforms at once either; it can calculate and add the deltas
214 for each waveform individually. Deltas don't need to be added in
215 chronological order either.
216
217
218 Sample buffering
219 ----------------
220 Sample buffering is very flexible. Once a time frame is ended, the
221 resampled waveforms become output samples that are immediately made
222 available for reading with blip_read_samples(). They don't have to be
223 read immediately; they can be allowed to accumulate in the buffer, with
224 each time frame appending more samples to the buffer. When reading, some
225 or all of the samples in can be read out, with the remaining unread
226 samples staying in the buffer for later. Usually a program will
227 immediately read all available samples after ending a time frame and
228 play them immediately. In some systems, a program needs samples in
229 fixed-length blocks; in that case, it would keep generating time frames
230 until some number of samples are available, then read only that many,
231 even if slightly more were available in the buffer.
232
233 In some systems, one wants to run waveform generation for exactly the
234 number of clocks necessary to generate some desired number of output
235 samples, and no more. In that case, use blip_clocks_needed( blip, N ) to
236 find out how many clocks are needed to generate N additional samples.
237 Ending a time frame with this value will result in exactly N more
238 samples becoming available for reading.
239
240
241 Thanks
242 ------
243 Thanks to Jsr (FamiTracker author), the Mednafen team (multi-system
244 emulator), ShizZie (Nhes GMB author), Marcel van Tongeren, Luke Molnar
245 (UberNES author), Fredrick Meunier (Fuse contributor) for using and
246 giving feedback for another similar library. Thanks to Disch for his
247 interest and discussions about the synthesis algorithm itself, and for
248 writing his own implementation of it (Schpune) rather than just using
249 mine. Thanks to Xodnizel for Festalon, whose sound quality got me
250 interested in video game sound emulation in the first place, and where I
251 first came up with the algorithm while optimizing its brute-force
252 filter.
253
254 --
255 Shay Green <[email protected]>