Beast - Music Synthesizer and Composer  0.11.1+10.g2da35
gsldatautils.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 #ifndef __GSL_DATA_UTILS_H__
3 #define __GSL_DATA_UTILS_H__
4 
5 #include <bse/bsemath.hh>
6 #include <bse/gsldatahandle.hh>
7 
8 /* --- structures --- */
9 #define GSL_DATA_HANDLE_PEEK_BUFFER (2048)
10 typedef struct
11 {
12  gint dir; /* initialize direction to -1 or +1 (or 0 for random access) */
13  GslLong start; /* initialize to 0 */
14  GslLong end; /* initialize to 0 */
15  gfloat data[GSL_DATA_HANDLE_PEEK_BUFFER];
17 typedef struct
18 {
19  GslLong head_skip; /* FIXME: remove this */
20  GslLong tail_cut;
21  GslLong min_loop;
22  GslLong max_loop;
23 } GslLoopSpec; /* rename this to GslData... */
24 
25 
26 /* --- data utils --- */
27 gboolean gsl_data_detect_signal (GslDataHandle *handle,
28  GslLong *sigstart,
29  GslLong *sigend);
30 GslLong gsl_data_find_sample (GslDataHandle *dhandle,
31  gfloat min_value,
32  gfloat max_value,
33  GslLong start_offset,
34  gint direction);
35 gboolean gsl_data_find_tailmatch (GslDataHandle *dhandle,
36  const GslLoopSpec *lspec,
37  GslLong *loop_start_p,
38  GslLong *loop_end_p);
39 GslLong gsl_data_find_block (GslDataHandle *handle,
40  guint n_values,
41  const gfloat *values,
42  gfloat epsilon);
43 gfloat* gsl_data_make_fade_ramp (GslDataHandle *handle,
44  GslLong min_pos, /* *= 0.0 + delta */
45  GslLong max_pos, /* *= 1.0 - delta */
46  GslLong *length_p);
47 double gsl_data_find_min_max (GslDataHandle *handle,
48  double *dmin,
49  double *dmax);
50 
51 
52 /* --- data handle utils --- */
53 static inline gfloat gsl_data_handle_peek_value (GslDataHandle *dhandle,
54  GslLong position,
55  GslDataPeekBuffer *peekbuf);
56 gint /* errno */ gsl_data_handle_dump (GslDataHandle *dhandle,
57  gint fd,
58  GslWaveFormatType format,
59  guint byte_order);
60 gint /* errno */ gsl_data_handle_dump_wav (GslDataHandle *dhandle,
61  gint fd,
62  guint n_bits,
63  guint n_channels,
64  guint sample_freq);
65 void gsl_data_handle_dump_wstore(GslDataHandle *dhandle,
66  SfiWStore *wstore,
67  GslWaveFormatType format,
68  guint byte_order);
69 gint /* errno */ bse_wave_file_dump_header (gint fd,
70  guint n_data_bytes,
71  guint n_bits,
72  guint n_channels,
73  guint sample_freq);
74 gint /* errno */ bse_wave_file_patch_length (gint fd,
75  guint n_data_bytes);
76 gint /* errno */ bse_wave_file_dump_data (gint fd,
77  guint n_bits,
78  guint n_values,
79  const gfloat *values);
80 gint /* errno */ bse_wave_file_from_fbuffer (const char *file_name,
81  guint n_bits,
82  guint n_channels,
83  guint sample_freq,
84  guint n_values,
85  const gfloat *values);
86 gint /* errno */ bse_wave_file_from_dbuffer (const char *file_name,
87  guint n_bits,
88  guint n_channels,
89  guint sample_freq,
90  guint n_values,
91  const gdouble *values);
92 
93 /* --- conversion utils --- */
94 static inline guint gsl_conv_from_float (GslWaveFormatType format,
95  guint byte_order,
96  const gfloat *src,
97  gpointer dest,
98  guint n_values);
99 static inline guint gsl_conv_from_float_clip (GslWaveFormatType format,
100  guint byte_order,
101  const gfloat *src,
102  gpointer dest,
103  guint n_values);
104 static inline void gsl_conv_to_float (GslWaveFormatType format,
105  guint byte_order,
106  gconstpointer src,
107  gfloat *dest,
108  guint n_values);
109 static inline guint gsl_conv_from_double (GslWaveFormatType format,
110  guint byte_order,
111  const gdouble *src,
112  gpointer dest,
113  guint n_values);
114 static inline guint gsl_conv_from_double_clip (GslWaveFormatType format,
115  guint byte_order,
116  const gdouble *src,
117  gpointer dest,
118  guint n_values);
119 static inline void gsl_conv_to_double (GslWaveFormatType format,
120  guint byte_order,
121  gconstpointer src,
122  gdouble *dest,
123  guint n_values);
124 static inline gint16 gsl_alaw_to_pcm (gint8 alawv);
125 static inline gint16 gsl_ulaw_to_pcm (gint8 ulawv);
126 
127 
128 /* --- clipping --- */
129 typedef struct
130 {
131  guint produce_info : 1;
132  gfloat threshold; /* 0..+1 */
133  guint head_samples;
134  guint tail_samples;
135  guint fade_samples;
136  guint pad_samples;
137  guint tail_silence;
139 typedef struct
140 {
141  GslDataHandle *dhandle;
142  guint clipped_to_0length : 1; /* no data above threshold */
143  guint head_detected : 1; /* found head_samples silence */
144  guint tail_detected : 1; /* found tail_samples silence */
145  guint clipped_head : 1;
146  guint clipped_tail : 1;
147  Bse::Error error;
149 
150 Bse::Error gsl_data_clip_sample (GslDataHandle *dhandle,
151  GslDataClipConfig *cconfig,
152  GslDataClipResult *result);
153 
154 
155 /* --- misc implementations --- */
156 gfloat gsl_data_peek_value_f (GslDataHandle *dhandle,
157  GslLong pos,
158  GslDataPeekBuffer *peekbuf);
159 
160 static inline gfloat
161 gsl_data_handle_peek_value (GslDataHandle *dhandle,
162  GslLong position,
163  GslDataPeekBuffer *peekbuf)
164 {
165  return (position >= peekbuf->start && position < peekbuf->end ?
166  peekbuf->data[position - peekbuf->start] :
167  gsl_data_peek_value_f (dhandle, position, peekbuf));
168 }
169 
170 #define GSL_CONV_FORMAT(format, endian_flag) (((endian_flag) << 16) | ((format) & 0xffff))
171 
172 static inline guint /* returns number of bytes used in dest */
173 gsl_conv_from_float (GslWaveFormatType format,
174  guint byte_order,
175  const gfloat *src,
176  gpointer dest,
177  guint n_values)
178 {
179  gint8 *i8 = (gint8*) dest;
180  guint8 *u8 = (guint8*) dest;
181  gint16 *i16 = (gint16*) dest;
182  guint16 *u16 = (guint16*) dest;
183  gint32 *i32 = (gint32*) dest;
184  guint32 *u32dest = (guint32*) dest;
185  const gfloat *bound = src + n_values;
186  const guint32 *u32src = (guint32*) src, *u32bound = (const guint32*) bound;
187 
188  if (!n_values)
189  return 0;
190 
191  switch (GSL_CONV_FORMAT (format, byte_order == G_BYTE_ORDER))
192  {
193  BseFpuState fpu;
194  gfloat v;
195  gint16 vi16;
196  guint16 vu16;
197  guint32 vu32;
198  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
199  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
200  do
201  *u8++ = bse_dtoi (*src++ * 128. + 128);
202  while (src < bound);
203  return n_values;
204  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
205  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
206  bse_fpu_setround (&fpu);
207  do
208  {
209  v = *src++;
210  v *= 128.;
211  *i8++ = bse_dtoi (v);
212  }
213  while (src < bound);
214  bse_fpu_restore (fpu);
215  return n_values;
216  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
217  do
218  *u16++ = bse_dtoi (*src++ * 2048. + 2048);
219  while (src < bound);
220  return n_values << 1;
221  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
222  do
223  {
224  vu16 = bse_dtoi (*src++ * 2048. + 2048);
225  *u16++ = GUINT16_SWAP_LE_BE (vu16);
226  }
227  while (src < bound);
228  return n_values << 1;
229  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
230  bse_fpu_setround (&fpu);
231  do
232  {
233  v = *src++;
234  v *= 2048.;
235  *i16++ = bse_dtoi (v);
236  }
237  while (src < bound);
238  bse_fpu_restore (fpu);
239  return n_values << 1;
240  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
241  bse_fpu_setround (&fpu);
242  do
243  {
244  v = *src++;
245  v *= 2048.;
246  vi16 = bse_dtoi (v);
247  *i16++ = GUINT16_SWAP_LE_BE (vi16);
248  }
249  while (src < bound);
250  bse_fpu_restore (fpu);
251  return n_values << 1;
252  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
253  do
254  *u16++ = bse_dtoi (*src++ * 32768. + 32768);
255  while (src < bound);
256  return n_values << 1;
257  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
258  do
259  {
260  vu16 = bse_dtoi (*src++ * 32768. + 32768);
261  *u16++ = GUINT16_SWAP_LE_BE (vu16);
262  }
263  while (src < bound);
264  return n_values << 1;
265  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
266  bse_fpu_setround (&fpu);
267  do
268  {
269  v = *src++;
270  v *= 32768.;
271  *i16++ = bse_dtoi (v);
272  }
273  while (src < bound);
274  bse_fpu_restore (fpu);
275  return n_values << 1;
276  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
277  bse_fpu_setround (&fpu);
278  do
279  {
280  v = *src++;
281  v *= 32768.;
282  vi16 = bse_dtoi (v);
283  *i16++ = GUINT16_SWAP_LE_BE (vi16);
284  }
285  while (src < bound);
286  bse_fpu_restore (fpu);
287  return n_values << 1;
288  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER == G_BYTE_ORDER):
289  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER != G_BYTE_ORDER):
290  bse_fpu_setround (&fpu);
291  if (byte_order == G_LITTLE_ENDIAN)
292  do
293  {
294  v = *src++;
295  v *= 8388608.;
296  gint32 vi32 = bse_dtoi (v);
297  *u8++ = vi32 >> 0;
298  *u8++ = vi32 >> 8;
299  *((gint8*) u8) = vi32 >> 16;
300  u8++;
301  }
302  while (src < bound);
303  else /* G_BIG_ENDIAN */
304  do
305  {
306  v = *src++;
307  v *= 8388608.;
308  gint32 vi32 = bse_dtoi (v);
309  *((gint8*) u8) = vi32 >> 16;
310  u8++;
311  *u8++ = vi32 >> 8;
312  *u8++ = vi32 >> 0;
313  }
314  while (src < bound);
315  bse_fpu_restore (fpu);
316  return n_values * 3;
317  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER == G_BYTE_ORDER):
318  bse_fpu_setround (&fpu);
319  do
320  {
321  v = *src++;
322  v *= 8388608.;
323  *i32++ = bse_dtoi (v);
324  }
325  while (src < bound);
326  bse_fpu_restore (fpu);
327  return n_values * 4;
328  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER != G_BYTE_ORDER):
329  bse_fpu_setround (&fpu);
330  do
331  {
332  v = *src++;
333  v *= 8388608.;
334  gint32 vi32 = bse_dtoi (v);
335  *i32++ = GUINT32_SWAP_LE_BE (vi32);
336  }
337  while (src < bound);
338  bse_fpu_restore (fpu);
339  return n_values * 4;
340  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER == G_BYTE_ORDER):
341  bse_fpu_setround (&fpu);
342  do
343  {
344  v = *src++;
345  v *= 2147483648.;
346  *i32++ = bse_dtoi (v);
347  }
348  while (src < bound);
349  bse_fpu_restore (fpu);
350  return n_values * 4;
351  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER != G_BYTE_ORDER):
352  bse_fpu_setround (&fpu);
353  do
354  {
355  v = *src++;
356  v *= 2147483648.;
357  gint32 vi32 = bse_dtoi (v);
358  *i32++ = GUINT32_SWAP_LE_BE (vi32);
359  }
360  while (src < bound);
361  bse_fpu_restore (fpu);
362  return n_values * 4;
363  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER == G_BYTE_ORDER):
364  return n_values * 4;
365  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER != G_BYTE_ORDER):
366  do
367  {
368  vu32 = *u32src++;
369  *u32dest++ = GUINT32_SWAP_LE_BE (vu32);
370  }
371  while (u32src < u32bound);
372  return n_values * 4;
373  default:
375  return 0;
376  }
377 }
378 
379 static inline guint /* returns number of bytes used in dest */
380 gsl_conv_from_float_clip (GslWaveFormatType format,
381  guint byte_order,
382  const gfloat *src,
383  gpointer dest,
384  guint n_values)
385 {
386  gint8 *i8 = (gint8*) dest;
387  guint8 *u8 = (guint8*) dest;
388  gint16 *i16 = (gint16*) dest;
389  guint16 *u16 = (guint16*) dest;
390  gint32 *i32 = (gint32*) dest;
391  guint32 *u32dest = (guint32*) dest;
392  const gfloat *bound = src + n_values;
393  const guint32 *u32src = (const guint32*) src, *u32bound = (const guint32*) bound;
394 
395  if (!n_values)
396  return 0;
397 
398  switch (GSL_CONV_FORMAT (format, byte_order == G_BYTE_ORDER))
399  {
400  BseFpuState fpu;
401  gfloat v;
402  guint32 vu32;
403  gint32 vi32;
404  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
405  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
406  do
407  {
408  vi32 = bse_dtoi (*src++ * 128. + 128);
409  *u8++ = CLAMP (vi32, 0, 255);
410  }
411  while (src < bound);
412  return n_values;
413  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
414  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
415  bse_fpu_setround (&fpu);
416  do
417  {
418  v = *src++;
419  v *= 128.;
420  vi32 = bse_dtoi (v);
421  *i8++ = CLAMP (vi32, -128, 127);
422  }
423  while (src < bound);
424  bse_fpu_restore (fpu);
425  return n_values;
426  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
427  do
428  {
429  vi32 = bse_dtoi (*src++ * 2048. + 2048);
430  *u16++ = CLAMP (vi32, 0, 4095);
431  }
432  while (src < bound);
433  return n_values << 1;
434  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
435  do
436  {
437  vi32 = bse_dtoi (*src++ * 2048. + 2048);
438  vi32 = CLAMP (vi32, 0, 4095);
439  *u16++ = GUINT16_SWAP_LE_BE (vi32);
440  }
441  while (src < bound);
442  return n_values << 1;
443  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
444  bse_fpu_setround (&fpu);
445  do
446  {
447  v = *src++;
448  v *= 2048.;
449  vi32 = bse_dtoi (v);
450  *i16++ = CLAMP (vi32, -2048, 2047);
451  }
452  while (src < bound);
453  bse_fpu_restore (fpu);
454  return n_values << 1;
455  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
456  bse_fpu_setround (&fpu);
457  do
458  {
459  v = *src++;
460  v *= 2048.;
461  vi32 = bse_dtoi (v);
462  vi32 = CLAMP (vi32, -2048, 2047);
463  *i16++ = GUINT16_SWAP_LE_BE (vi32);
464  }
465  while (src < bound);
466  bse_fpu_restore (fpu);
467  return n_values << 1;
468  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
469  do
470  {
471  vi32 = bse_dtoi (*src++ * 32768. + 32768);
472  *u16++ = CLAMP (vi32, 0, 65535);
473  }
474  while (src < bound);
475  return n_values << 1;
476  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
477  do
478  {
479  vi32 = bse_dtoi (*src++ * 32768. + 32768);
480  vi32 = CLAMP (vi32, 0, 65535);
481  *u16++ = GUINT16_SWAP_LE_BE (vi32);
482  }
483  while (src < bound);
484  return n_values << 1;
485  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
486  bse_fpu_setround (&fpu);
487  do
488  {
489  v = *src++;
490  v *= 32768.;
491  vi32 = bse_dtoi (v);
492  vi32 = CLAMP (vi32, -32768, 32767);
493  *i16++ = vi32;
494  }
495  while (src < bound);
496  bse_fpu_restore (fpu);
497  return n_values << 1;
498  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
499  bse_fpu_setround (&fpu);
500  do
501  {
502  v = *src++;
503  v *= 32768.;
504  vi32 = bse_dtoi (v);
505  vi32 = CLAMP (vi32, -32768, 32767);
506  *i16++ = GUINT16_SWAP_LE_BE (vi32);
507  }
508  while (src < bound);
509  bse_fpu_restore (fpu);
510  return n_values << 1;
511  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER == G_BYTE_ORDER):
512  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER != G_BYTE_ORDER):
513  bse_fpu_setround (&fpu);
514  if (byte_order == G_LITTLE_ENDIAN)
515  do
516  {
517  v = *src++;
518  v *= 8388608.;
519  gint32 vi32 = bse_dtoi (v);
520  vi32 = CLAMP (vi32, -8388608, 8388607);
521  *u8++ = vi32 >> 0;
522  *u8++ = vi32 >> 8;
523  *((gint8*) u8) = vi32 >> 16;
524  u8++;
525  }
526  while (src < bound);
527  else /* G_BIG_ENDIAN */
528  do
529  {
530  v = *src++;
531  v *= 8388608.;
532  gint32 vi32 = bse_dtoi (v);
533  vi32 = CLAMP (vi32, -8388608, 8388607);
534  *((gint8*) u8) = vi32 >> 16;
535  u8++;
536  *u8++ = vi32 >> 8;
537  *u8++ = vi32 >> 0;
538  }
539  while (src < bound);
540  bse_fpu_restore (fpu);
541  return n_values * 3;
542  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER == G_BYTE_ORDER):
543  bse_fpu_setround (&fpu);
544  do
545  {
546  v = *src++;
547  v *= 8388608.;
548  vi32 = bse_dtoi (v);
549  vi32 = CLAMP (vi32, -8388608, 8388607);
550  *i32++ = vi32;
551  }
552  while (src < bound);
553  bse_fpu_restore (fpu);
554  return n_values * 4;
555  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER != G_BYTE_ORDER):
556  bse_fpu_setround (&fpu);
557  do
558  {
559  v = *src++;
560  v *= 8388608.;
561  vi32 = bse_dtoi (v);
562  vi32 = CLAMP (vi32, -8388608, 8388607);
563  *i32++ = GUINT32_SWAP_LE_BE (vi32);
564  }
565  while (src < bound);
566  bse_fpu_restore (fpu);
567  return n_values * 4;
568  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER == G_BYTE_ORDER):
569  bse_fpu_setround (&fpu);
570  do
571  {
572  v = *src++;
573  v *= 2147483648.;
574  vi32 = bse_dtoi (v);
575  // vi32 = CLAMP (vi32, -2147483648, 2147483647);
576  *i32++ = vi32;
577  }
578  while (src < bound);
579  bse_fpu_restore (fpu);
580  return n_values * 4;
581  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER != G_BYTE_ORDER):
582  bse_fpu_setround (&fpu);
583  do
584  {
585  v = *src++;
586  v *= 2147483648.;
587  vi32 = bse_dtoi (v);
588  // vi32 = CLAMP (vi32, -2147483648, 2147483647);
589  *i32++ = GUINT32_SWAP_LE_BE (vi32);
590  }
591  while (src < bound);
592  bse_fpu_restore (fpu);
593  return n_values * 4;
594  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER == G_BYTE_ORDER):
595  return n_values << 2;
596  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER != G_BYTE_ORDER):
597  do
598  {
599  vu32 = *u32src++;
600  *u32dest++ = GUINT32_SWAP_LE_BE (vu32);
601  }
602  while (u32src < u32bound);
603  return n_values << 2;
604  default:
606  return 0;
607  }
608 }
609 
610 #define GSL_ALAW_MAX (0x7e00)
611 static inline gint16
612 gsl_alaw_to_pcm (gint8 alawv)
613 {
614  static const short alaw2pcm_table[128] = {
615  0x1580, 0x1480, 0x1780, 0x1680, 0x1180, 0x1080, 0x1380, 0x1280,
616  0x1d80, 0x1c80, 0x1f80, 0x1e80, 0x1980, 0x1880, 0x1b80, 0x1a80,
617  0x0ac0, 0x0a40, 0x0bc0, 0x0b40, 0x08c0, 0x0840, 0x09c0, 0x0940,
618  0x0ec0, 0x0e40, 0x0fc0, 0x0f40, 0x0cc0, 0x0c40, 0x0dc0, 0x0d40,
619  0x5600, 0x5200, 0x5e00, 0x5a00, 0x4600, 0x4200, 0x4e00, 0x4a00,
620  0x7600, 0x7200, 0x7e00, 0x7a00, 0x6600, 0x6200, 0x6e00, 0x6a00,
621  0x2b00, 0x2900, 0x2f00, 0x2d00, 0x2300, 0x2100, 0x2700, 0x2500,
622  0x3b00, 0x3900, 0x3f00, 0x3d00, 0x3300, 0x3100, 0x3700, 0x3500,
623  0x0158, 0x0148, 0x0178, 0x0168, 0x0118, 0x0108, 0x0138, 0x0128,
624  0x01d8, 0x01c8, 0x01f8, 0x01e8, 0x0198, 0x0188, 0x01b8, 0x01a8,
625  0x0058, 0x0048, 0x0078, 0x0068, 0x0018, 0x0008, 0x0038, 0x0028,
626  0x00d8, 0x00c8, 0x00f8, 0x00e8, 0x0098, 0x0088, 0x00b8, 0x00a8,
627  0x0560, 0x0520, 0x05e0, 0x05a0, 0x0460, 0x0420, 0x04e0, 0x04a0,
628  0x0760, 0x0720, 0x07e0, 0x07a0, 0x0660, 0x0620, 0x06e0, 0x06a0,
629  0x02b0, 0x0290, 0x02f0, 0x02d0, 0x0230, 0x0210, 0x0270, 0x0250,
630  0x03b0, 0x0390, 0x03f0, 0x03d0, 0x0330, 0x0310, 0x0370, 0x0350,
631  };
632  return alawv < 0 ? alaw2pcm_table[128 + alawv] : -alaw2pcm_table[alawv];
633 }
634 
635 #define GSL_ULAW_MAX (0x7d7c)
636 static inline gint16
637 gsl_ulaw_to_pcm (gint8 ulawv)
638 {
639  static const short ulaw2pcm_table[128] = {
640  0x7d7c, 0x797c, 0x757c, 0x717c, 0x6d7c, 0x697c, 0x657c, 0x617c,
641  0x5d7c, 0x597c, 0x557c, 0x517c, 0x4d7c, 0x497c, 0x457c, 0x417c,
642  0x3e7c, 0x3c7c, 0x3a7c, 0x387c, 0x367c, 0x347c, 0x327c, 0x307c,
643  0x2e7c, 0x2c7c, 0x2a7c, 0x287c, 0x267c, 0x247c, 0x227c, 0x207c,
644  0x1efc, 0x1dfc, 0x1cfc, 0x1bfc, 0x1afc, 0x19fc, 0x18fc, 0x17fc,
645  0x16fc, 0x15fc, 0x14fc, 0x13fc, 0x12fc, 0x11fc, 0x10fc, 0x0ffc,
646  0x0f3c, 0x0ebc, 0x0e3c, 0x0dbc, 0x0d3c, 0x0cbc, 0x0c3c, 0x0bbc,
647  0x0b3c, 0x0abc, 0x0a3c, 0x09bc, 0x093c, 0x08bc, 0x083c, 0x07bc,
648  0x075c, 0x071c, 0x06dc, 0x069c, 0x065c, 0x061c, 0x05dc, 0x059c,
649  0x055c, 0x051c, 0x04dc, 0x049c, 0x045c, 0x041c, 0x03dc, 0x039c,
650  0x036c, 0x034c, 0x032c, 0x030c, 0x02ec, 0x02cc, 0x02ac, 0x028c,
651  0x026c, 0x024c, 0x022c, 0x020c, 0x01ec, 0x01cc, 0x01ac, 0x018c,
652  0x0174, 0x0164, 0x0154, 0x0144, 0x0134, 0x0124, 0x0114, 0x0104,
653  0x00f4, 0x00e4, 0x00d4, 0x00c4, 0x00b4, 0x00a4, 0x0094, 0x0084,
654  0x0078, 0x0070, 0x0068, 0x0060, 0x0058, 0x0050, 0x0048, 0x0040,
655  0x0038, 0x0030, 0x0028, 0x0020, 0x0018, 0x0010, 0x0008, 0x0000,
656  };
657  return ulawv < 0 ? ulaw2pcm_table[128 + ulawv] : -ulaw2pcm_table[ulawv];
658 }
659 
660 static inline void
661 gsl_conv_to_float (GslWaveFormatType format,
662  guint byte_order,
663  gconstpointer src,
664  gfloat *dest,
665  guint n_values)
666 {
667  const guint8 *u8 = (guint8*) src;
668  const gint8 *i8 = (gint8*) src;
669  const guint16 *u16 = (guint16*) src;
670  const gint16 *i16 = (gint16*) src;
671  const gint32 *i32 = (gint32*) src;
672  const guint32 *u32src = (guint32*) src;
673  gfloat *bound = dest + n_values;
674  guint32 *u32dest = (guint32*) dest, *u32bound = (guint32*) bound;
675 
676  if (!n_values)
677  return;
678 
679  switch (GSL_CONV_FORMAT (format, byte_order == G_BYTE_ORDER))
680  {
681  gint16 vi16;
682  guint16 vu16;
683  guint32 vu32;
684  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
685  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
686  do
687  *dest++ = (*u8++ - 128) * (1. / 128.);
688  while (dest < bound);
689  break;
690  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
691  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
692  do
693  *dest++ = *i8++ * (1. / 128.);
694  while (dest < bound);
695  break;
696  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ALAW, G_BYTE_ORDER == G_BYTE_ORDER):
697  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ALAW, G_BYTE_ORDER != G_BYTE_ORDER):
698  do
699  *dest++ = gsl_alaw_to_pcm (*i8++) * (1.0 / GSL_ALAW_MAX);
700  while (dest < bound);
701  break;
702  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ULAW, G_BYTE_ORDER == G_BYTE_ORDER):
703  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ULAW, G_BYTE_ORDER != G_BYTE_ORDER):
704  do
705  *dest++ = gsl_ulaw_to_pcm (*i8++) * (1.0 / GSL_ULAW_MAX);
706  while (dest < bound);
707  break;
708  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
709  do
710  *dest++ = ((*u16++ & 0x0fff) - 2048) * (1. / 2048.);
711  while (dest < bound);
712  break;
713  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
714  do
715  {
716  vu16 = *u16++;
717  *dest++ = ((GUINT16_SWAP_LE_BE (vu16) & 0x0fff) - 2048) * (1. / 2048.);
718  }
719  while (dest < bound);
720  break;
721  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
722  do
723  {
724  vi16 = *i16++;
725  *dest++ = CLAMP (vi16, -2048, 2048) * (1. / 2048.);
726  }
727  while (dest < bound);
728  break;
729  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
730  do
731  {
732  vi16 = *i16++;
733  vi16 = GUINT16_SWAP_LE_BE (vi16);
734  *dest++ = CLAMP (vi16, -2048, 2048) * (1. / 2048.);
735  }
736  while (dest < bound);
737  break;
738  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
739  do
740  *dest++ = (*u16++ - 32768) * (1. / 32768.);
741  while (dest < bound);
742  break;
743  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
744  do
745  {
746  vu16 = *u16++;
747  *dest++ = (GUINT16_SWAP_LE_BE (vu16) - 32768) * (1. / 32768.);
748  }
749  while (dest < bound);
750  break;
751  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
752  do
753  *dest++ = *i16++ * (1. / 32768.);
754  while (dest < bound);
755  break;
756  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
757  do
758  {
759  vi16 = *i16++;
760  *dest++ = GUINT16_SWAP_LE_BE (vi16) * (1. / 32768.);
761  }
762  while (dest < bound);
763  break;
764  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER == G_BYTE_ORDER):
765  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER != G_BYTE_ORDER):
766  if (byte_order == G_LITTLE_ENDIAN)
767  do
768  {
769  gint32 v32 = *u8++;
770  v32 |= *u8++ << 8;
771  v32 |= *((gint8*) u8) << 16;
772  u8++;
773  *dest++ = v32 * (1. / 8388608.);
774  }
775  while (dest < bound);
776  else /* G_BIG_ENDIAN */
777  do
778  {
779  gint32 v32 = *((gint8*) u8) << 16;
780  u8++;
781  v32 |= *u8++ << 8;
782  v32 |= *u8++;
783  *dest++ = v32 * (1. / 8388608.);
784  }
785  while (dest < bound);
786  break;
787  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER == G_BYTE_ORDER):
788  do
789  *dest++ = *i32++ * (1. / 8388608.);
790  while (dest < bound);
791  break;
792  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER != G_BYTE_ORDER):
793  do
794  {
795  gint32 vi32 = *i32++;
796  *dest++ = GUINT32_SWAP_LE_BE (vi32) * (1. / 8388608.);
797  }
798  while (dest < bound);
799  break;
800  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER == G_BYTE_ORDER):
801  do
802  *dest++ = *i32++ * (1. / 2147483648.);
803  while (dest < bound);
804  break;
805  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER != G_BYTE_ORDER):
806  do
807  {
808  gint32 vi32 = *i32++;
809  *dest++ = GUINT32_SWAP_LE_BE (vi32) * (1. / 2147483648.);
810  }
811  while (dest < bound);
812  break;
813  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER == G_BYTE_ORDER):
814  break;
815  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER != G_BYTE_ORDER):
816  do
817  {
818  vu32 = *u32src++;
819  *u32dest++ = GUINT32_SWAP_LE_BE (vu32);
820  }
821  while (u32dest < u32bound);
822  break;
823  default:
825  }
826 }
827 
828 /* same as above with s/float/double */
829 static inline guint /* returns number of bytes used in dest */
830 gsl_conv_from_double (GslWaveFormatType format,
831  guint byte_order,
832  const gdouble *src,
833  gpointer dest,
834  guint n_values)
835 {
836  gint8 *i8 = (gint8*) dest;
837  guint8 *u8 = (guint8*) dest;
838  gint16 *i16 = (gint16*) dest;
839  guint16 *u16 = (guint16*) dest;
840  guint32 *u32dest = (guint32*) dest;
841  gint32 *i32 = (gint32*) dest;
842  const gdouble *bound = src + n_values;
843  const guint32 *u32src = (const guint32*) src, *u32bound = (const guint32*) bound;
844 
845  if (!n_values)
846  return 0;
847 
848  switch (GSL_CONV_FORMAT (format, byte_order == G_BYTE_ORDER))
849  {
850  BseFpuState fpu;
851  gdouble v;
852  gint16 vi16;
853  guint16 vu16;
854  guint32 vu32;
855  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
856  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
857  do
858  *u8++ = bse_dtoi (*src++ * 128. + 128);
859  while (src < bound);
860  return n_values;
861  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
862  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
863  bse_fpu_setround (&fpu);
864  do
865  {
866  v = *src++;
867  v *= 128.;
868  *i8++ = bse_dtoi (v);
869  }
870  while (src < bound);
871  bse_fpu_restore (fpu);
872  return n_values;
873  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
874  do
875  *u16++ = bse_dtoi (*src++ * 2048. + 2048);
876  while (src < bound);
877  return n_values << 1;
878  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
879  do
880  {
881  vu16 = bse_dtoi (*src++ * 2048. + 2048);
882  *u16++ = GUINT16_SWAP_LE_BE (vu16);
883  }
884  while (src < bound);
885  return n_values << 1;
886  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
887  bse_fpu_setround (&fpu);
888  do
889  {
890  v = *src++;
891  v *= 2048.;
892  *i16++ = bse_dtoi (v);
893  }
894  while (src < bound);
895  bse_fpu_restore (fpu);
896  return n_values << 1;
897  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
898  bse_fpu_setround (&fpu);
899  do
900  {
901  v = *src++;
902  v *= 2048.;
903  vi16 = bse_dtoi (v);
904  *i16++ = GUINT16_SWAP_LE_BE (vi16);
905  }
906  while (src < bound);
907  bse_fpu_restore (fpu);
908  return n_values << 1;
909  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
910  do
911  *u16++ = bse_dtoi (*src++ * 32768. + 32768);
912  while (src < bound);
913  return n_values << 1;
914  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
915  do
916  {
917  vu16 = bse_dtoi (*src++ * 32768. + 32768);
918  *u16++ = GUINT16_SWAP_LE_BE (vu16);
919  }
920  while (src < bound);
921  return n_values << 1;
922  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
923  bse_fpu_setround (&fpu);
924  do
925  {
926  v = *src++;
927  v *= 32768.;
928  *i16++ = bse_dtoi (v);
929  }
930  while (src < bound);
931  bse_fpu_restore (fpu);
932  return n_values << 1;
933  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
934  bse_fpu_setround (&fpu);
935  do
936  {
937  v = *src++;
938  v *= 32768.;
939  vi16 = bse_dtoi (v);
940  *i16++ = GUINT16_SWAP_LE_BE (vi16);
941  }
942  while (src < bound);
943  bse_fpu_restore (fpu);
944  return n_values << 1;
945  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER == G_BYTE_ORDER):
946  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER != G_BYTE_ORDER):
947  bse_fpu_setround (&fpu);
948  if (byte_order == G_LITTLE_ENDIAN)
949  do
950  {
951  v = *src++;
952  v *= 8388608.;
953  gint32 vi32 = bse_dtoi (v);
954  *u8++ = vi32 >> 0;
955  *u8++ = vi32 >> 8;
956  *((gint8*) u8) = vi32 >> 16;
957  u8++;
958  }
959  while (src < bound);
960  else /* G_BIG_ENDIAN */
961  do
962  {
963  v = *src++;
964  v *= 8388608.;
965  gint32 vi32 = bse_dtoi (v);
966  *((gint8*) u8) = vi32 >> 16;
967  u8++;
968  *u8++ = vi32 >> 8;
969  *u8++ = vi32 >> 0;
970  }
971  while (src < bound);
972  bse_fpu_restore (fpu);
973  return n_values * 3;
974  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER == G_BYTE_ORDER):
975  bse_fpu_setround (&fpu);
976  do
977  {
978  v = *src++;
979  v *= 8388608.;
980  *i32++ = bse_dtoi (v);
981  }
982  while (src < bound);
983  bse_fpu_restore (fpu);
984  return n_values * 4;
985  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER != G_BYTE_ORDER):
986  bse_fpu_setround (&fpu);
987  do
988  {
989  v = *src++;
990  v *= 8388608.;
991  gint32 vi32 = bse_dtoi (v);
992  *i32++ = GUINT32_SWAP_LE_BE (vi32);
993  }
994  while (src < bound);
995  bse_fpu_restore (fpu);
996  return n_values * 4;
997  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER == G_BYTE_ORDER):
998  bse_fpu_setround (&fpu);
999  do
1000  {
1001  v = *src++;
1002  v *= 2147483648.;
1003  *i32++ = bse_dtoi (v);
1004  }
1005  while (src < bound);
1006  bse_fpu_restore (fpu);
1007  return n_values * 4;
1008  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER != G_BYTE_ORDER):
1009  bse_fpu_setround (&fpu);
1010  do
1011  {
1012  v = *src++;
1013  v *= 2147483648.;
1014  gint32 vi32 = bse_dtoi (v);
1015  *i32++ = GUINT32_SWAP_LE_BE (vi32);
1016  }
1017  while (src < bound);
1018  bse_fpu_restore (fpu);
1019  return n_values * 4;
1020  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER == G_BYTE_ORDER):
1021  return n_values << 2;
1022  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER != G_BYTE_ORDER):
1023  do
1024  {
1025  vu32 = *u32src++;
1026  *u32dest++ = GUINT32_SWAP_LE_BE (vu32);
1027  }
1028  while (u32src < u32bound);
1029  return n_values << 2;
1030  default:
1032  return 0;
1033  }
1034 }
1035 
1036 static inline guint /* returns number of bytes used in dest */
1037 gsl_conv_from_double_clip (GslWaveFormatType format,
1038  guint byte_order,
1039  const gdouble *src,
1040  gpointer dest,
1041  guint n_values)
1042 {
1043  gint8 *i8 = (gint8*) dest;
1044  guint8 *u8 = (guint8*) dest;
1045  gint16 *i16 = (gint16*) dest;
1046  guint16 *u16 = (guint16*) dest;
1047  guint32 *u32dest = (guint32*) dest;
1048  gint32 *i32 = (gint32*) dest;
1049  const gdouble *bound = src + n_values;
1050  const guint32 *u32src = (const guint32*) src, *u32bound = (const guint32*) bound;
1051 
1052  if (!n_values)
1053  return 0;
1054 
1055  switch (GSL_CONV_FORMAT (format, byte_order == G_BYTE_ORDER))
1056  {
1057  BseFpuState fpu;
1058  gdouble v;
1059  guint32 vu32;
1060  gint32 vi32;
1061  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
1062  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
1063  do
1064  {
1065  vi32 = bse_dtoi (*src++ * 128. + 128);
1066  *u8++ = CLAMP (vi32, 0, 255);
1067  }
1068  while (src < bound);
1069  return n_values;
1070  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
1071  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
1072  bse_fpu_setround (&fpu);
1073  do
1074  {
1075  v = *src++;
1076  v *= 128.;
1077  vi32 = bse_dtoi (v);
1078  *i8++ = CLAMP (vi32, -128, 127);
1079  }
1080  while (src < bound);
1081  bse_fpu_restore (fpu);
1082  return n_values;
1083  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
1084  do
1085  {
1086  vi32 = bse_dtoi (*src++ * 2048. + 2048);
1087  *u16++ = CLAMP (vi32, 0, 4095);
1088  }
1089  while (src < bound);
1090  return n_values << 1;
1091  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
1092  do
1093  {
1094  vi32 = bse_dtoi (*src++ * 2048. + 2048);
1095  vi32 = CLAMP (vi32, 0, 4095);
1096  *u16++ = GUINT16_SWAP_LE_BE (vi32);
1097  }
1098  while (src < bound);
1099  return n_values << 1;
1100  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
1101  bse_fpu_setround (&fpu);
1102  do
1103  {
1104  v = *src++;
1105  v *= 2048.;
1106  vi32 = bse_dtoi (v);
1107  *i16++ = CLAMP (vi32, -2048, 2047);
1108  }
1109  while (src < bound);
1110  bse_fpu_restore (fpu);
1111  return n_values << 1;
1112  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
1113  bse_fpu_setround (&fpu);
1114  do
1115  {
1116  v = *src++;
1117  v *= 2048.;
1118  vi32 = bse_dtoi (v);
1119  vi32 = CLAMP (vi32, -2048, 2047);
1120  *i16++ = GUINT16_SWAP_LE_BE (vi32);
1121  }
1122  while (src < bound);
1123  bse_fpu_restore (fpu);
1124  return n_values << 1;
1125  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
1126  do
1127  {
1128  vi32 = bse_dtoi (*src++ * 32768. + 32768);
1129  *u16++ = CLAMP (vi32, 0, 65535);
1130  }
1131  while (src < bound);
1132  return n_values << 1;
1133  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
1134  do
1135  {
1136  vi32 = bse_dtoi (*src++ * 32768. + 32768);
1137  vi32 = CLAMP (vi32, 0, 65535);
1138  *u16++ = GUINT16_SWAP_LE_BE (vi32);
1139  }
1140  while (src < bound);
1141  return n_values << 1;
1142  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
1143  bse_fpu_setround (&fpu);
1144  do
1145  {
1146  v = *src++;
1147  v *= 32768.;
1148  vi32 = bse_dtoi (v);
1149  vi32 = CLAMP (vi32, -32768, 32767);
1150  *i16++ = vi32;
1151  }
1152  while (src < bound);
1153  bse_fpu_restore (fpu);
1154  return n_values << 1;
1155  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
1156  bse_fpu_setround (&fpu);
1157  do
1158  {
1159  v = *src++;
1160  v *= 32768.;
1161  vi32 = bse_dtoi (v);
1162  vi32 = CLAMP (vi32, -32768, 32767);
1163  *i16++ = GUINT16_SWAP_LE_BE (vi32);
1164  }
1165  while (src < bound);
1166  bse_fpu_restore (fpu);
1167  return n_values << 1;
1168  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER == G_BYTE_ORDER):
1169  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER != G_BYTE_ORDER):
1170  bse_fpu_setround (&fpu);
1171  if (byte_order == G_LITTLE_ENDIAN)
1172  do
1173  {
1174  v = *src++;
1175  v *= 8388608.;
1176  gint32 vi32 = bse_dtoi (v);
1177  vi32 = CLAMP (vi32, -8388608, 8388607);
1178  *u8++ = vi32 >> 0;
1179  *u8++ = vi32 >> 8;
1180  *((gint8*) u8) = vi32 >> 16;
1181  u8++;
1182  }
1183  while (src < bound);
1184  else /* G_BIG_ENDIAN */
1185  do
1186  {
1187  v = *src++;
1188  v *= 8388608.;
1189  gint32 vi32 = bse_dtoi (v);
1190  vi32 = CLAMP (vi32, -8388608, 8388607);
1191  *((gint8*) u8) = vi32 >> 16;
1192  u8++;
1193  *u8++ = vi32 >> 8;
1194  *u8++ = vi32 >> 0;
1195  }
1196  while (src < bound);
1197  bse_fpu_restore (fpu);
1198  return n_values * 3;
1199  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER == G_BYTE_ORDER):
1200  bse_fpu_setround (&fpu);
1201  do
1202  {
1203  v = *src++;
1204  v *= 8388608.;
1205  vi32 = bse_dtoi (v);
1206  vi32 = CLAMP (vi32, -8388608, 8388607);
1207  *i32++ = vi32;
1208  }
1209  while (src < bound);
1210  bse_fpu_restore (fpu);
1211  return n_values * 4;
1212  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER != G_BYTE_ORDER):
1213  bse_fpu_setround (&fpu);
1214  do
1215  {
1216  v = *src++;
1217  v *= 8388608.;
1218  vi32 = bse_dtoi (v);
1219  vi32 = CLAMP (vi32, -8388608, 8388607);
1220  *i32++ = GUINT32_SWAP_LE_BE (vi32);
1221  }
1222  while (src < bound);
1223  bse_fpu_restore (fpu);
1224  return n_values * 4;
1225  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER == G_BYTE_ORDER):
1226  bse_fpu_setround (&fpu);
1227  do
1228  {
1229  v = *src++;
1230  v *= 2147483648.;
1231  vi32 = bse_dtoi (v);
1232  // vi32 = CLAMP (vi32, -2147483648, 2147483647);
1233  *i32++ = vi32;
1234  }
1235  while (src < bound);
1236  bse_fpu_restore (fpu);
1237  return n_values * 4;
1238  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER != G_BYTE_ORDER):
1239  bse_fpu_setround (&fpu);
1240  do
1241  {
1242  v = *src++;
1243  v *= 2147483648.;
1244  vi32 = bse_dtoi (v);
1245  // vi32 = CLAMP (vi32, -2147483648, 2147483647);
1246  *i32++ = GUINT32_SWAP_LE_BE (vi32);
1247  }
1248  while (src < bound);
1249  bse_fpu_restore (fpu);
1250  return n_values * 4;
1251  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER == G_BYTE_ORDER):
1252  return n_values << 2;
1253  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER != G_BYTE_ORDER):
1254  do
1255  {
1256  vu32 = *u32src++;
1257  *u32dest++ = GUINT32_SWAP_LE_BE (vu32);
1258  }
1259  while (u32src < u32bound);
1260  return n_values << 2;
1261  default:
1263  return 0;
1264  }
1265 }
1266 
1267 static inline void
1268 gsl_conv_to_double (GslWaveFormatType format,
1269  guint byte_order,
1270  gconstpointer src,
1271  gdouble *dest,
1272  guint n_values)
1273 {
1274  const guint8 *u8 = (guint8*) src;
1275  const gint8 *i8 = (gint8*) src;
1276  const guint16 *u16 = (guint16*) src;
1277  const gint16 *i16 = (gint16*) src;
1278  const guint32 *u32src = (guint32*) src;
1279  const gint32 *i32 = (gint32*) src;
1280  gdouble *bound = dest + n_values;
1281  guint32 *u32dest = (guint32*) dest, *u32bound = (guint32*) bound;
1282 
1283  if (!n_values)
1284  return;
1285 
1286  switch (GSL_CONV_FORMAT (format, byte_order == G_BYTE_ORDER))
1287  {
1288  gint16 vi16;
1289  guint16 vu16;
1290  guint32 vu32;
1291  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
1292  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
1293  do
1294  *dest++ = (*u8++ - 128) * (1. / 128.);
1295  while (dest < bound);
1296  break;
1297  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER == G_BYTE_ORDER):
1298  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_8, G_BYTE_ORDER != G_BYTE_ORDER):
1299  do
1300  *dest++ = *i8++ * (1. / 128.);
1301  while (dest < bound);
1302  break;
1303  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ALAW, G_BYTE_ORDER == G_BYTE_ORDER):
1304  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ALAW, G_BYTE_ORDER != G_BYTE_ORDER):
1305  do
1306  *dest++ = gsl_alaw_to_pcm (*i8++) * (1.0 / GSL_ALAW_MAX);
1307  while (dest < bound);
1308  break;
1309  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ULAW, G_BYTE_ORDER == G_BYTE_ORDER):
1310  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_ULAW, G_BYTE_ORDER != G_BYTE_ORDER):
1311  do
1312  *dest++ = gsl_ulaw_to_pcm (*i8++) * (1.0 / GSL_ULAW_MAX);
1313  while (dest < bound);
1314  break;
1315  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
1316  do
1317  *dest++ = ((*u16++ & 0x0fff) - 2048) * (1. / 2048.);
1318  while (dest < bound);
1319  break;
1320  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
1321  do
1322  {
1323  vu16 = *u16++;
1324  *dest++ = ((GUINT16_SWAP_LE_BE (vu16) & 0x0fff) - 2048) * (1. / 2048.);
1325  }
1326  while (dest < bound);
1327  break;
1328  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER == G_BYTE_ORDER):
1329  do
1330  {
1331  vi16 = *i16++;
1332  *dest++ = CLAMP (vi16, -2048, 2048) * (1. / 2048.);
1333  }
1334  while (dest < bound);
1335  break;
1336  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_12, G_BYTE_ORDER != G_BYTE_ORDER):
1337  do
1338  {
1339  vi16 = *i16++;
1340  vi16 = GUINT16_SWAP_LE_BE (vi16);
1341  *dest++ = CLAMP (vi16, -2048, 2048) * (1. / 2048.);
1342  }
1343  while (dest < bound);
1344  break;
1345  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
1346  do
1347  *dest++ = (*u16++ - 32768) * (1. / 32768.);
1348  while (dest < bound);
1349  break;
1350  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_UNSIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
1351  do
1352  {
1353  vu16 = *u16++;
1354  *dest++ = (GUINT16_SWAP_LE_BE (vu16) - 32768) * (1. / 32768.);
1355  }
1356  while (dest < bound);
1357  break;
1358  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER == G_BYTE_ORDER):
1359  do
1360  *dest++ = *i16++ * (1. / 32768.);
1361  while (dest < bound);
1362  break;
1363  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_16, G_BYTE_ORDER != G_BYTE_ORDER):
1364  do
1365  {
1366  vi16 = *i16++;
1367  *dest++ = GUINT16_SWAP_LE_BE (vi16) * (1. / 32768.);
1368  }
1369  while (dest < bound);
1370  break;
1371  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER == G_BYTE_ORDER):
1372  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24, G_BYTE_ORDER != G_BYTE_ORDER):
1373  if (byte_order == G_LITTLE_ENDIAN)
1374  do
1375  {
1376  gint32 v32 = *u8++;
1377  v32 |= *u8++ << 8;
1378  v32 |= *((gint8*) u8) << 16;
1379  u8++;
1380  *dest++ = v32 * (1. / 8388608.);
1381  }
1382  while (dest < bound);
1383  else /* G_BIG_ENDIAN */
1384  do
1385  {
1386  gint32 v32 = *((gint8*) u8) << 16;
1387  u8++;
1388  v32 |= *u8++ << 8;
1389  v32 |= *u8++;
1390  *dest++ = v32 * (1. / 8388608.);
1391  }
1392  while (dest < bound);
1393  break;
1394  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER == G_BYTE_ORDER):
1395  do
1396  *dest++ = *i32++ * (1. / 8388608.);
1397  while (dest < bound);
1398  break;
1399  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_24_PAD4, G_BYTE_ORDER != G_BYTE_ORDER):
1400  do
1401  {
1402  gint32 vi32 = *i32++;
1403  *dest++ = GUINT32_SWAP_LE_BE (vi32) * (1. / 8388608.);
1404  }
1405  while (dest < bound);
1406  break;
1407  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER == G_BYTE_ORDER):
1408  do
1409  *dest++ = *i32++ * (1. / 2147483648.);
1410  while (dest < bound);
1411  break;
1412  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_SIGNED_32, G_BYTE_ORDER != G_BYTE_ORDER):
1413  do
1414  {
1415  gint32 vi32 = *i32++;
1416  *dest++ = GUINT32_SWAP_LE_BE (vi32) * (1. / 2147483648.);
1417  }
1418  while (dest < bound);
1419  break;
1420  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER == G_BYTE_ORDER):
1421  break;
1422  case GSL_CONV_FORMAT (GSL_WAVE_FORMAT_FLOAT, G_BYTE_ORDER != G_BYTE_ORDER):
1423  do
1424  {
1425  vu32 = *u32src++;
1426  *u32dest++ = GUINT32_SWAP_LE_BE (vu32);
1427  }
1428  while (u32dest < u32bound);
1429  break;
1430  default:
1432  }
1433 }
1434 
1435 #endif /* __GSL_DATA_UTILS_H__ */
1436 
1437 /* vim:set ts=8 sts=2 sw=2: */
Bse::Error gsl_data_clip_sample(GslDataHandle *dhandle, GslDataClipConfig *cconfig, GslDataClipResult *result)
Definition: gsldatautils.cc:755
gfloat * gsl_data_make_fade_ramp(GslDataHandle *handle, GslLong min_pos, GslLong max_pos, GslLong *length_p)
Definition: gsldatautils.cc:700
Definition: gsldatautils.hh:139
Definition: gsldatautils.hh:10
#define RAPICORN_ASSERT_UNREACHED()
Definition: gsldatautils.hh:17
Definition: sfistore.hh:12
GslLong gsl_data_find_block(GslDataHandle *handle, guint n_values, const gfloat *values, gfloat epsilon)
Definition: gsldatautils.cc:654
Definition: gsldatautils.hh:129