git ssb

0+

kode54 / syntrax-c



Commit f8137a6ba7c36d2d53855c2c3c4fdd0961c4d9b6

Changed resampler to pure mono, which improves its performance in this mono-only implementation threefold.

Christopher Snowhill authored on 2/16/2017, 5:38:10 AM
Christopher Snowhill committed on 6/13/2018, 12:10:58 AM
Parent: 6ea963e864a12e832081bf1ad32a479a16c7f523

Files changed

src/syntrax/resampler.cchanged
src/syntrax/resampler.hchanged
src/syntrax/syntrax.cchanged
src/syntrax/resampler.cView
@@ -61,13 +61,12 @@
6161 }
6262 }
6363
6464 enum { width = 32 };
65-enum { stereo = 2 };
6665 enum { max_res = 512 };
6766 enum { min_width = (width < 4 ? 4 : width) };
6867 enum { adj_width = min_width / 4 * 4 + 2 };
69-enum { write_offset = adj_width * stereo };
68+enum { write_offset = adj_width };
7069
7170 enum { buffer_size = 128 };
7271
7372 typedef struct _resampler
@@ -82,10 +81,10 @@
8281 int latency;
8382
8483 imp_t const* imp;
8584 imp_t impulses [max_res * (adj_width + 2 * (sizeof(imp_off_t) / sizeof(imp_t)))];
86- sample_t buffer_in[buffer_size * stereo * 2];
87- sample_t buffer_out[buffer_size * stereo];
85+ sample_t buffer_in[buffer_size * 2];
86+ sample_t buffer_out[buffer_size];
8887 } resampler;
8988
9089 void * resampler_create()
9190 {
@@ -171,9 +170,9 @@
171170 }
172171 rs->rate_ = ratio_;
173172
174173 /* how much of input is used for each output sample */
175- step = stereo * (int) floor( ratio_ );
174+ step = (int) floor( ratio_ );
176175 fraction = fmod( ratio_, 1.0 );
177176
178177 filter = (ratio_ < 1.0) ? 1.0 : 1.0 / ratio_;
179178 pos = 0.0;
@@ -192,12 +191,12 @@
192191 pos += fraction;
193192 if ( pos >= 0.9999999 )
194193 {
195194 pos -= 1.0;
196- cur_step += stereo;
195+ cur_step += 1;
197196 }
198197
199- ((imp_off_t*)out)[0] = (cur_step - rs->width_ * 2 + 4) * sizeof (sample_t);
198+ ((imp_off_t*)out)[0] = (cur_step - rs->width_ + 2) * sizeof (sample_t);
200199 ((imp_off_t*)out)[1] = 2 * sizeof (imp_t) + 2 * sizeof (imp_off_t);
201200 out += 2 * (sizeof(imp_off_t) / sizeof(imp_t));
202201 /*input_per_cycle += cur_step;*/
203202 }
@@ -209,47 +208,43 @@
209208
210209 int resampler_get_free(void *_r)
211210 {
212211 resampler *r = (resampler *)_r;
213- return buffer_size * stereo - r->infilled;
212+ return buffer_size - r->infilled;
214213 }
215214
216215 int resampler_get_min_fill(void *_r)
217216 {
218217 resampler *r = (resampler *)_r;
219- const int min_needed = write_offset + stereo;
218+ const int min_needed = write_offset + 1;
220219 const int latency = r->latency ? 0 : adj_width;
221220 int min_free = min_needed - r->infilled - latency;
222221 return min_free < 0 ? 0 : min_free;
223222 }
224223
225-void resampler_write_pair(void *_r, sample_t ls, sample_t rs)
224+void resampler_write_sample(void *_r, sample_t s)
226225 {
227226 resampler *r = (resampler *)_r;
228227
229228 if (!r->latency)
230229 {
231230 int i;
232231 for ( i = 0; i < adj_width / 2; ++i)
233232 {
234- r->buffer_in[r->inptr + 0] = 0;
235- r->buffer_in[r->inptr + 1] = 0;
236- r->buffer_in[buffer_size * stereo + r->inptr + 0] = 0;
237- r->buffer_in[buffer_size * stereo + r->inptr + 1] = 0;
238- r->inptr = (r->inptr + stereo) % (buffer_size * stereo);
239- r->infilled += stereo;
233+ r->buffer_in[r->inptr] = 0;
234+ r->buffer_in[buffer_size + r->inptr] = 0;
235+ r->inptr = (r->inptr + 1) % (buffer_size);
236+ r->infilled += 1;
240237 }
241238 r->latency = 1;
242239 }
243240
244- if (r->infilled < buffer_size * stereo)
241+ if (r->infilled < buffer_size)
245242 {
246- r->buffer_in[r->inptr + 0] = ls;
247- r->buffer_in[r->inptr + 1] = rs;
248- r->buffer_in[buffer_size * stereo + r->inptr + 0] = ls;
249- r->buffer_in[buffer_size * stereo + r->inptr + 1] = rs;
250- r->inptr = (r->inptr + stereo) % (buffer_size * stereo);
251- r->infilled += stereo;
243+ r->buffer_in[r->inptr] = s;
244+ r->buffer_in[buffer_size + r->inptr + 0] = s;
245+ r->inptr = (r->inptr + 1) % (buffer_size);
246+ r->infilled += 1;
252247 }
253248 }
254249
255250 #if defined(_MSC_VER) || defined(__GNUC__)
@@ -270,37 +265,32 @@
270265 {
271266 int n;
272267 /* accumulate in extended precision*/
273268 int pt = imp [0];
274- intermediate_t l = (intermediate_t)pt * (intermediate_t)(in [0]);
275- intermediate_t r = (intermediate_t)pt * (intermediate_t)(in [1]);
269+ intermediate_t s = (intermediate_t)pt * (intermediate_t)(in [0]);
276270 if ( out >= out_end )
277271 break;
278272 for ( n = (adj_width - 2) / 2; n; --n )
279273 {
280274 pt = imp [1];
281- l += (intermediate_t)pt * (intermediate_t)(in [2]);
282- r += (intermediate_t)pt * (intermediate_t)(in [3]);
275+ s += (intermediate_t)pt * (intermediate_t)(in [1]);
283276
284277 /* pre-increment more efficient on some RISC processors*/
285278 imp += 2;
286279 pt = imp [0];
287- r += (intermediate_t)pt * (intermediate_t)(in [5]);
288- in += 4;
289- l += (intermediate_t)pt * (intermediate_t)(in [0]);
280+ s += (intermediate_t)pt * (intermediate_t)(in [2]);
281+ in += 2;
290282 }
291283 pt = imp [1];
292- l += (intermediate_t)pt * (intermediate_t)(in [2]);
293- r += (intermediate_t)pt * (intermediate_t)(in [3]);
284+ s += (intermediate_t)pt * (intermediate_t)(in [1]);
294285
295286 /* these two "samples" after the end of the impulse give the
296287 * proper offsets to the next input sample and next impulse */
297288 in = (sample_t const*) ((char const*) in + ((imp_off_t*)(&imp [2]))[0]); /* some negative value */
298289 imp = (imp_t const*) ((char const*) imp + ((imp_off_t*)(&imp [2]))[1]); /* small positive or large negative */
299290
300- out [0] = (sample_t) (l >> 15);
301- out [1] = (sample_t) (r >> 15);
302- out += 2;
291+ out [0] = (sample_t) (s >> 15);
292+ out += 1;
303293 }
304294 while ( in < in_end );
305295
306296 r->imp = imp;
@@ -326,13 +316,13 @@
326316 while (!r->outfilled && r->infilled)
327317 {
328318 int inread;
329319
330- int writepos = ( r->outptr + r->outfilled ) % (buffer_size * stereo);
331- int writesize = (buffer_size * stereo) - writepos;
332- if ( writesize > ( buffer_size * stereo - r->outfilled ) )
333- writesize = buffer_size * stereo - r->outfilled;
334- inread = resampler_wrapper(r, &r->buffer_out[writepos], &writesize, &r->buffer_in[buffer_size * stereo + r->inptr - r->infilled], r->infilled);
320+ int writepos = ( r->outptr + r->outfilled ) % (buffer_size);
321+ int writesize = (buffer_size) - writepos;
322+ if ( writesize > ( buffer_size - r->outfilled ) )
323+ writesize = buffer_size - r->outfilled;
324+ inread = resampler_wrapper(r, &r->buffer_out[writepos], &writesize, &r->buffer_in[buffer_size + r->inptr - r->infilled], r->infilled);
335325 r->infilled -= inread;
336326 r->outfilled += writesize;
337327 if (!inread)
338328 break;
@@ -341,39 +331,37 @@
341331
342332 int resampler_get_avail(void *_r)
343333 {
344334 resampler *r = (resampler *)_r;
345- if (r->outfilled < stereo && r->infilled >= r->width_)
335+ if (r->outfilled < 1 && r->infilled >= r->width_)
346336 resampler_fill( r );
347337 return r->outfilled;
348338 }
349339
350-static void resampler_read_pair_internal( resampler *r, sample_t *ls, sample_t *rs, int advance )
340+static void resampler_read_sample_internal( resampler *r, sample_t *s, int advance )
351341 {
352- if (r->outfilled < stereo)
342+ if (r->outfilled < 1)
353343 resampler_fill( r );
354- if (r->outfilled < stereo)
344+ if (r->outfilled < 1)
355345 {
356- *ls = 0;
357- *rs = 0;
346+ *s = 0;
358347 return;
359348 }
360- *ls = r->buffer_out[r->outptr + 0];
361- *rs = r->buffer_out[r->outptr + 1];
349+ *s = r->buffer_out[r->outptr];
362350 if (advance)
363351 {
364- r->outptr = (r->outptr + 2) % (buffer_size * stereo);
365- r->outfilled -= stereo;
352+ r->outptr = (r->outptr + 1) % (buffer_size);
353+ r->outfilled -= 1;
366354 }
367355 }
368356
369-void resampler_read_pair( void *_r, sample_t *ls, sample_t *rs )
357+void resampler_read_sample( void *_r, sample_t *s )
370358 {
371359 resampler *r = (resampler *)_r;
372- resampler_read_pair_internal(r, ls, rs, 1);
360+ resampler_read_sample_internal(r, s, 1);
373361 }
374362
375-void resampler_peek_pair( void *_r, sample_t *ls, sample_t *rs )
363+void resampler_peek_sample( void *_r, sample_t *s )
376364 {
377365 resampler *r = (resampler *)_r;
378- resampler_read_pair_internal(r, ls, rs, 0);
366+ resampler_read_sample_internal(r, s, 0);
379367 }
src/syntrax/resampler.hView
@@ -60,14 +60,14 @@
6060
6161 int resampler_get_free(void *);
6262 int resampler_get_min_fill(void *);
6363
64-void resampler_write_pair(void *, sample_t ls, sample_t rs);
64+void resampler_write_sample(void *, sample_t s);
6565
6666 int resampler_get_avail(void *);
6767
68-void resampler_read_pair( void *, sample_t *ls, sample_t *rs );
69-void resampler_peek_pair( void *, sample_t *ls, sample_t *rs );
68+void resampler_read_sample( void *, sample_t *s );
69+void resampler_peek_sample( void *, sample_t *s );
7070
7171 #ifdef __cplusplus
7272 }
7373 #endif
src/syntrax/syntrax.cView
@@ -1879,9 +1879,9 @@
18791879 sample_t s;
18801880 while ( v->sampPos != -1 && resampler_get_min_fill(v->resampler[0]) )
18811881 {
18821882 s = v->waveBuff[v->sampPos];
1883- resampler_write_pair(v->resampler[0], s, s);
1883+ resampler_write_sample(v->resampler[0], s);
18841884 if ( v->isPlayingBackward )
18851885 {
18861886 v->sampPos--;
18871887 if ( v->sampPos <= v->smpLoopStart )
@@ -1916,9 +1916,9 @@
19161916 }
19171917 }
19181918
19191919 //smp = intp->interpSamp(v);
1920- resampler_read_pair(v->resampler[0], &s, &s);
1920+ resampler_read_sample(v->resampler[0], &s);
19211921 smp = s;
19221922
19231923 audioMainR += (smp * v->gainRight) >> 8;
19241924 audioMainL += (smp * v->gainLeft) >> 8;
@@ -1933,13 +1933,13 @@
19331933 sample_t s;
19341934 while ( resampler_get_min_fill(v->resampler[0]) )
19351935 {
19361936 s = v->waveBuff[v->synthPos];
1937- resampler_write_pair(v->resampler[0], s, s);
1937+ resampler_write_sample(v->resampler[0], s);
19381938 v->synthPos++;
19391939 v->synthPos &= v->wavelength;
19401940 }
1941- resampler_read_pair(v->resampler[0], &s, &s);
1941+ resampler_read_sample(v->resampler[0], &s);
19421942 smp = s;
19431943
19441944 audioMainR += (smp * v->gainRight) >> 8;
19451945 audioMainL += (smp * v->gainLeft) >> 8;
@@ -2025,9 +2025,9 @@
20252025 sample_t s;
20262026 while ( v->sampPos != -1 && resampler_get_min_fill(v->resampler[1]) )
20272027 {
20282028 s = v->waveBuff[v->sampPos];
2029- resampler_write_pair(v->resampler[1], s, s);
2029+ resampler_write_sample(v->resampler[1], s);
20302030 if ( v->isPlayingBackward )
20312031 {
20322032 v->sampPos--;
20332033 if ( v->sampPos <= v->smpLoopStart )
@@ -2060,9 +2060,9 @@
20602060 }
20612061 }
20622062 }
20632063 }
2064- resampler_read_pair(v->resampler[1], &s, &s);
2064+ resampler_read_sample(v->resampler[1], &s);
20652065 smp = s;
20662066
20672067 audioMainR += (smp * v->gainRight) >> 8;
20682068 audioMainL += (smp * v->gainLeft) >> 8;
@@ -2077,13 +2077,13 @@
20772077 sample_t s;
20782078 while ( resampler_get_min_fill(v->resampler[1]) )
20792079 {
20802080 s = v->waveBuff[v->synthPos];
2081- resampler_write_pair(v->resampler[1], s, s);
2081+ resampler_write_sample(v->resampler[1], s);
20822082 v->synthPos++;
20832083 v->synthPos &= v->wavelength;
20842084 }
2085- resampler_read_pair(v->resampler[1], &s, &s);
2085+ resampler_read_sample(v->resampler[1], &s);
20862086 smp = s;
20872087
20882088 audioMainR += (smp * v->gainRight) >> 8;
20892089 audioMainL += (smp * v->gainLeft) >> 8;

Built with git-ssb-web