-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio_conversion.c
844 lines (710 loc) · 19.6 KB
/
audio_conversion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
* MOC - music on console
* Copyright (C) 2005 Damian Pietras <daper@daper.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Code for conversion between float and fixed point types is based on
* libsamplerate:
* Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
*/
/* For future: audio conversion should be performed in order:
* channels -> rate -> format
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
/* for lrintf() */
#define _ISOC9X_SOURCE 1
#define _ISOC99_SOURCE 1
#define __USE_ISOC9X 1
#define __USE_ISOC99 1
#include <assert.h>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <strings.h>
#ifdef HAVE_SAMPLERATE
# include <samplerate.h>
#endif
#define DEBUG
#include "common.h"
#include "audio_conversion.h"
#include "log.h"
#include "options.h"
#include "compat.h"
/* Byte order conversion */
/* TODO: use functions from byteswap.h if available */
#define SWAP_INT16(l) ((int16_t) \
( (l & 0xff) << 8) | (((l) >> 8) & 0xff))
#define SWAP_INT32(l) ((int32_t) (\
(((l) & 0x000000ff) << 24) | \
(((l) & 0x0000ff00) << 8) | \
(((l) & 0x00ff0000) >> 8) | \
(((l) & 0xff000000) >> 24) \
) \
)
#if 0
#ifdef WORDS_BIGENDIAN
# define INT16_BE_TO_NE(l) (l)
# define INT16_LE_TO_NE(l) SWAP_INT16 (l)
# define INT32_BE_TO_NE(l) (l)
# define INT32_LE_TO_NE(l) SWAP_INT32 (l)
#else
# define INT16_BE_TO_NE(l) SWAP_INT16 (l)
# define INT16_LE_TO_NE(l) (l)
# define INT32_BE_TO_NE(l) SWAP_INT32 (l)
# define INT32_LE_TO_NE(l) (l)
#endif
/* The byte order conversion is symetric, so this is true: */
#define INT16_NE_TO_LE INT16_LE_TO_NE (l)
#define INT32_NE_TO_BE INT32_BE_TO_NE (l)
#endif
static void float_to_u8 (const float *in, unsigned char *out, const size_t samples)
{
size_t i;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++) {
float f = in[i] * INT32_MAX;
if (f >= INT32_MAX)
out[i] = UINT8_MAX;
else if (f <= INT32_MIN)
out[i] = 0;
else {
#ifdef HAVE_LRINTF
out[i] = (unsigned int)((lrintf(f) >> 24) - INT8_MIN);
#else
out[i] = (unsigned int)(((int)f >> 24) - INT8_MIN);
#endif
}
}
}
static void float_to_s8 (const float *in, char *out, const size_t samples)
{
size_t i;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++) {
float f = in[i] * INT32_MAX;
if (f >= INT32_MAX)
out[i] = INT8_MAX;
else if (f <= INT32_MIN)
out[i] = INT8_MIN;
else {
#ifdef HAVE_LRINTF
out[i] = lrintf(f) >> 24;
#else
out[i] = (int)f >> 24;
#endif
}
}
}
static void float_to_u16 (const float *in, unsigned char *out,
const size_t samples)
{
size_t i;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++) {
uint16_t *out_val = (uint16_t *)(out + i * sizeof (uint16_t));
float f = in[i] * INT32_MAX;
if (f >= INT32_MAX)
*out_val = UINT16_MAX;
else if (f <= INT32_MIN)
*out_val = 0;
else {
#ifdef HAVE_LRINTF
*out_val = (unsigned int)((lrintf(f) >> 16) - INT16_MIN);
#else
*out_val = (unsigned int)(((int)f >> 16) - INT16_MIN);
#endif
}
}
}
static void float_to_s16 (const float *in, char *out,
const size_t samples)
{
size_t i;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++) {
int16_t *out_val = (int16_t *)(out + i * sizeof (int16_t));
float f = in[i] * INT32_MAX;
if (f >= INT32_MAX)
*out_val = INT16_MAX;
else if (f <= INT32_MIN)
*out_val = INT16_MIN;
else {
#ifdef HAVE_LRINTF
*out_val = lrintf(f) >> 16;
#else
*out_val = ((int)f >> 16);
#endif
}
}
}
static void float_to_u32 (const float *in, unsigned char *out,
const size_t samples)
{
size_t i;
/* maximum and minimum values of 32-bit samples */
const unsigned int U32_MAX = (1 << 24) - 1;
const int S32_MAX = (1 << 23) - 1;
const int S32_MIN = -(1 << 23);
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++) {
uint32_t *out_val = (uint32_t *)(out + i * sizeof (uint32_t));
float f = in[i] * S32_MAX;
if (f >= S32_MAX)
*out_val = U32_MAX << 8;
else if (f <= S32_MIN)
*out_val = 0;
else {
#ifdef HAVE_LRINTF
*out_val = (uint32_t)(lrintf(f) - S32_MIN) << 8;
#else
*out_val = (uint32_t)((int32_t)f - S32_MIN) << 8;
#endif
}
}
}
static void float_to_s32 (const float *in, char *out,
const size_t samples)
{
size_t i;
/* maximum and minimum values of 32-bit samples */
const int S32_MAX = (1 << 23) - 1;
const int S32_MIN = -(1 << 23);
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++) {
int32_t *out_val = (int32_t *)(out + i * sizeof (int32_t));
float f = in[i] * S32_MAX;
if (f >= S32_MAX)
*out_val = S32_MAX << 8;
else if (f <= S32_MIN)
*out_val = S32_MIN << 8;
else {
#ifdef HAVE_LRINTF
*out_val = lrintf(f) << 8;
#else
*out_val = (int32_t)f << 8;
#endif
}
}
}
static void u8_to_float (const unsigned char *in, float *out,
const size_t samples)
{
size_t i;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++)
out[i] = (((int)*in++) + INT8_MIN) / (float)(INT8_MAX + 1);
}
static void s8_to_float (const char *in, float *out,
const size_t samples)
{
size_t i;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++)
out[i] = *in++ / (float)(INT8_MAX + 1);
}
static void u16_to_float (const unsigned char *in, float *out,
const size_t samples)
{
size_t i;
const uint16_t *in_16 = (uint16_t *)in;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++)
out[i] = ((int)*in_16++ + INT16_MIN) / (float)(INT16_MAX + 1);
}
static void s16_to_float (const char *in, float *out,
const size_t samples)
{
size_t i;
const int16_t *in_16 = (int16_t *)in;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++)
out[i] = *in_16++ / (float)(INT16_MAX + 1);
}
static void u32_to_float (const unsigned char *in, float *out,
const size_t samples)
{
size_t i;
const uint32_t *in_32 = (uint32_t *)in;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++)
out[i] = ((float)*in_32++ + (float)INT32_MIN) / ((float)INT32_MAX + 1.0);
}
static void s32_to_float (const char *in, float *out,
const size_t samples)
{
size_t i;
const int32_t *in_32 = (int32_t *)in;
assert (in != NULL);
assert (out != NULL);
for (i = 0; i < samples; i++)
out[i] = *in_32++ / ((float)INT32_MAX + 1.0);
}
/* Convert fixed point samples in format fmt (size in bytes) to float.
* Size of converted sound is put in new_size. Returned memory is malloc()ed. */
static float *fixed_to_float (const char *buf, const size_t size,
const long fmt, size_t *new_size)
{
float *out = NULL;
char fmt_name[SFMT_STR_MAX];
assert ((fmt & SFMT_MASK_FORMAT) != SFMT_FLOAT);
switch (fmt & SFMT_MASK_FORMAT) {
case SFMT_U8:
*new_size = sizeof(float) * size;
out = (float *)xmalloc (*new_size);
u8_to_float ((unsigned char *)buf, out, size);
break;
case SFMT_S8:
*new_size = sizeof(float) * size;
out = (float *)xmalloc (*new_size);
s8_to_float (buf, out, size);
break;
case SFMT_U16:
*new_size = sizeof(float) * size / 2;
out = (float *)xmalloc (*new_size);
u16_to_float ((unsigned char *)buf, out, size / 2);
break;
case SFMT_S16:
*new_size = sizeof(float) * size / 2;
out = (float *)xmalloc (*new_size);
s16_to_float (buf, out, size / 2);
break;
case SFMT_U32:
*new_size = sizeof(float) * size / 4;
out = (float *)xmalloc (*new_size);
u32_to_float ((unsigned char *)buf, out, size / 4);
break;
case SFMT_S32:
*new_size = sizeof(float) * size / 4;
out = (float *)xmalloc (*new_size);
s32_to_float (buf, out, size / 4);
break;
default:
error ("Can't convert from %s to float!",
sfmt_str (fmt, fmt_name, sizeof (fmt_name)));
abort ();
}
return out;
}
/* Convert float samples to fixed point format fmt. Returned samples of size
* new_size bytes is malloc()ed. */
static char *float_to_fixed (const float *buf, const size_t samples,
const long fmt, size_t *new_size)
{
char fmt_name[SFMT_STR_MAX];
char *new_snd = NULL;
assert ((fmt & SFMT_MASK_FORMAT) != SFMT_FLOAT);
switch (fmt & SFMT_MASK_FORMAT) {
case SFMT_U8:
*new_size = samples;
new_snd = (char *)xmalloc (samples);
float_to_u8 (buf, (unsigned char *)new_snd, samples);
break;
case SFMT_S8:
*new_size = samples;
new_snd = (char *)xmalloc (samples);
float_to_s8 (buf, new_snd, samples);
break;
case SFMT_U16:
*new_size = samples * 2;
new_snd = (char *)xmalloc (*new_size);
float_to_u16 (buf, (unsigned char *)new_snd, samples);
break;
case SFMT_S16:
*new_size = samples * 2;
new_snd = (char *)xmalloc (*new_size);
float_to_s16 (buf, new_snd, samples);
break;
case SFMT_U32:
*new_size = samples * 4;
new_snd = (char *)xmalloc (*new_size);
float_to_u32 (buf, (unsigned char *)new_snd, samples);
break;
case SFMT_S32:
*new_size = samples * 4;
new_snd = (char *)xmalloc (*new_size);
float_to_s32 (buf, new_snd, samples);
break;
default:
error ("Can't convert from float to %s!",
sfmt_str (fmt, fmt_name, sizeof (fmt_name)));
abort ();
}
return new_snd;
}
static void change_sign_8 (uint8_t *buf, const size_t samples)
{
size_t i;
for (i = 0; i < samples; i++)
*buf++ ^= 1 << 7;
}
static void change_sign_16 (uint16_t *buf, const size_t samples)
{
size_t i;
for (i = 0; i < samples; i++)
*buf++ ^= 1 << 15;
}
static void change_sign_32 (uint32_t *buf, const size_t samples)
{
size_t i;
for (i = 0; i < samples; i++)
*buf++ ^= 1 << 31;
}
/* Change the signs of samples in format *fmt. Also changes fmt to the new
* format. */
static void change_sign (char *buf, const size_t size, long *fmt)
{
char fmt_name[SFMT_STR_MAX];
switch (*fmt & SFMT_MASK_FORMAT) {
case SFMT_S8:
case SFMT_U8:
change_sign_8 ((uint8_t *)buf, size);
if (*fmt & SFMT_S8)
*fmt = sfmt_set_fmt (*fmt, SFMT_U8);
else
*fmt = sfmt_set_fmt (*fmt, SFMT_S8);
break;
case SFMT_S16:
case SFMT_U16:
change_sign_16 ((uint16_t *)buf, size / 2);
if (*fmt & SFMT_S16)
*fmt = sfmt_set_fmt (*fmt, SFMT_U16);
else
*fmt = sfmt_set_fmt (*fmt, SFMT_S16);
break;
case SFMT_S32:
case SFMT_U32:
change_sign_32 ((uint32_t *)buf, size/4);
if (*fmt & SFMT_S32)
*fmt = sfmt_set_fmt (*fmt, SFMT_U32);
else
*fmt = sfmt_set_fmt (*fmt, SFMT_S32);
break;
default:
error ("Request for changing sign of unknown format: %s",
sfmt_str (*fmt, fmt_name, sizeof (fmt_name)));
abort ();
}
}
static void int16_bswap_array (int16_t *buf, const size_t num)
{
size_t i;
for (i = 0; i < num; i++)
buf[i] = SWAP_INT16 (buf[i]);
}
static void int32_bswap_array (int32_t *buf, const size_t num)
{
size_t i;
for (i = 0; i < num; i++)
buf[i] = SWAP_INT32 (buf[i]);
}
/* Swap endianness of fixed point samples. */
static void swap_endian (char *buf, const size_t size, const long fmt)
{
if ((fmt & (SFMT_S8 | SFMT_U8 | SFMT_FLOAT)))
return;
switch (fmt & SFMT_MASK_FORMAT) {
case SFMT_S16:
case SFMT_U16:
int16_bswap_array ((int16_t *)buf, size / 2);
break;
case SFMT_S32:
case SFMT_U32:
int32_bswap_array ((int32_t *)buf, size / 4);
break;
default:
error ("Can't convert to native endian!");
abort (); /* we can't do anything smarter */
}
}
/* Initialize the audio_conversion structure for conversion between parameters
* from and to. Return 0 on error. */
int audio_conv_new (struct audio_conversion *conv,
const struct sound_params *from,
const struct sound_params *to)
{
assert (from->rate != to->rate || from->fmt != to->fmt
|| from->channels != to->channels);
if (from->channels != to->channels) {
/* the only conversion we can do */
if (!(from->channels == 1 && to->channels == 2)) {
error ("Can't change number of channels (%d to %d)!",
from->channels, to->channels);
return 0;
}
}
if (from->rate != to->rate) {
#ifdef HAVE_SAMPLERATE
int err;
int resample_type = -1;
char *method = options_get_str ("ResampleMethod");
if (!strcasecmp(method, "SincBestQuality"))
resample_type = SRC_SINC_BEST_QUALITY;
else if (!strcasecmp(method, "SincMediumQuality"))
resample_type = SRC_SINC_MEDIUM_QUALITY;
else if (!strcasecmp(method, "SincFastest"))
resample_type = SRC_SINC_FASTEST;
else if (!strcasecmp(method, "ZeroOrderHold"))
resample_type = SRC_ZERO_ORDER_HOLD;
else if (!strcasecmp(method, "Linear"))
resample_type = SRC_LINEAR;
else
fatal ("Bad ResampleMethod option: %s", method);
conv->src_state = src_new (resample_type, to->channels, &err);
if (!conv->src_state) {
error ("Can't resample from %dHz to %dHz: %s",
from->rate, to->rate, src_strerror (err));
return 0;
}
#else
error ("Resampling not supported!");
return 0;
#endif
}
#ifdef HAVE_SAMPLERATE
else
conv->src_state = NULL;
#endif
conv->from = *from;
conv->to = *to;
#ifdef HAVE_SAMPLERATE
conv->resample_buf = NULL;
conv->resample_buf_nsamples = 0;
#endif
return 1;
}
#ifdef HAVE_SAMPLERATE
static float *resample_sound (struct audio_conversion *conv, const float *buf,
const size_t samples, const int nchannels,
size_t *resampled_samples)
{
SRC_DATA resample_data;
float *output;
float *new_input_start;
int output_samples = 0;
resample_data.end_of_input = 0;
resample_data.src_ratio = conv->to.rate / (double)conv->from.rate;
resample_data.input_frames = samples / nchannels
+ conv->resample_buf_nsamples / nchannels;
resample_data.output_frames = resample_data.input_frames
* resample_data.src_ratio;
if (conv->resample_buf) {
conv->resample_buf = (float *)xrealloc (conv->resample_buf,
sizeof(float) * resample_data.input_frames
* nchannels);
new_input_start = conv->resample_buf
+ conv->resample_buf_nsamples;
}
else {
conv->resample_buf = (float *)xmalloc (sizeof(float)
* resample_data.input_frames
* nchannels);
new_input_start = conv->resample_buf;
}
output = (float *)xmalloc (sizeof(float) * resample_data.output_frames
* nchannels);
/*debug ("Resampling %lu bytes of data by ratio %f", (unsigned long)size,
resample_data.src_ratio);*/
memcpy (new_input_start, buf, samples * sizeof(float));
resample_data.data_in = conv->resample_buf;
resample_data.data_out = output;
do {
int err;
if ((err = src_process(conv->src_state, &resample_data))) {
error ("Can't resample: %s", src_strerror (err));
free (output);
return NULL;
}
resample_data.data_in += resample_data.input_frames_used
* nchannels;
resample_data.input_frames -= resample_data.input_frames_used;
resample_data.data_out += resample_data.output_frames_gen
* nchannels;
resample_data.output_frames -= resample_data.output_frames_gen;
output_samples += resample_data.output_frames_gen * nchannels;
} while (resample_data.input_frames && resample_data.output_frames_gen
&& resample_data.output_frames);
*resampled_samples = output_samples;
if (resample_data.input_frames) {
conv->resample_buf_nsamples = resample_data.input_frames
* nchannels;
if (conv->resample_buf != resample_data.data_in) {
float *new;
new = (float *)xmalloc (sizeof(float) *
conv->resample_buf_nsamples);
memcpy (new, resample_data.data_in, sizeof(float) *
conv->resample_buf_nsamples);
free (conv->resample_buf);
conv->resample_buf = new;
}
}
else {
free (conv->resample_buf);
conv->resample_buf = NULL;
conv->resample_buf_nsamples = 0;
}
return output;
}
#endif
/* Double the channels from */
static char *mono_to_stereo (const char *mono, const size_t size,
const long format)
{
int Bps = sfmt_Bps (format);
size_t i;
char *stereo;
stereo = (char *)xmalloc (size * 2);
for (i = 0; i < size; i += Bps) {
memcpy (stereo + (i * 2), mono + i, Bps);
memcpy (stereo + (i * 2 + Bps), mono + i, Bps);
}
return stereo;
}
static int16_t *s32_to_s16 (int32_t *in, const size_t samples)
{
size_t i;
int16_t *new;
new = (int16_t *)xmalloc (samples * 2);
for (i = 0; i < samples; i++)
new[i] = in[i] >> 16;
return new;
}
static uint16_t *u32_to_u16 (uint32_t *in, const size_t samples)
{
size_t i;
uint16_t *new;
new = (uint16_t *)xmalloc (samples * 2);
for (i = 0; i < samples; i++)
new[i] = in[i] >> 16;
return new;
}
/* Do the sound conversion. buf of length size is the sample buffer to
* convert and the size of the converted sound is put into *conv_len.
* Return the converted sound in malloc()ed memory. */
char *audio_conv (struct audio_conversion *conv, const char *buf,
const size_t size, size_t *conv_len)
{
char *curr_sound;
long curr_sfmt = conv->from.fmt;
*conv_len = size;
curr_sound = (char *)xmalloc (size);
memcpy (curr_sound, buf, size);
if (!(curr_sfmt & SFMT_NE)) {
swap_endian (curr_sound, *conv_len, curr_sfmt);
curr_sfmt = sfmt_set_endian (curr_sfmt, SFMT_NE);
}
/* Special case (optimization): if we only need to convert 32bit samples
* to 16bit, we can do it very simply and quickly. */
if ((curr_sfmt & (SFMT_S32 | SFMT_U32)) &&
(conv->to.fmt & (SFMT_S16 | SFMT_U16)) &&
conv->from.rate == conv->to.rate) {
char *new_sound;
if ((curr_sfmt & SFMT_MASK_FORMAT) == SFMT_S32) {
new_sound = (char *)s32_to_s16 ((int32_t *)curr_sound,
*conv_len / 4);
curr_sfmt = sfmt_set_fmt (curr_sfmt, SFMT_S16);
}
else {
new_sound = (char *)u32_to_u16 ((uint32_t *)curr_sound,
*conv_len / 4);
curr_sfmt = sfmt_set_fmt (curr_sfmt, SFMT_U16);
}
if (curr_sound != buf)
free (curr_sound);
curr_sound = new_sound;
*conv_len /= 2;
logit ("Fast conversion!");
}
/* convert to float if necessary */
if ((conv->from.rate != conv->to.rate
|| (conv->to.fmt & SFMT_MASK_FORMAT) == SFMT_FLOAT
|| !sfmt_same_bps(conv->to.fmt, curr_sfmt))
&& (conv->from.fmt & SFMT_MASK_FORMAT) != SFMT_FLOAT) {
char *new_sound;
new_sound = (char *)fixed_to_float (curr_sound, *conv_len,
curr_sfmt, conv_len);
curr_sfmt = sfmt_set_fmt (curr_sfmt, SFMT_FLOAT);
if (curr_sound != buf)
free (curr_sound);
curr_sound = new_sound;
}
#ifdef HAVE_SAMPLERATE
if (conv->from.rate != conv->to.rate) {
char *new_sound = (char *)resample_sound (conv,
(float *)curr_sound,
*conv_len / sizeof(float), conv->to.channels,
conv_len);
*conv_len *= sizeof(float);
if (curr_sound != buf)
free (curr_sound);
curr_sound = new_sound;
}
#endif
if ((curr_sfmt & SFMT_MASK_FORMAT)
!= (conv->to.fmt & SFMT_MASK_FORMAT)) {
if (sfmt_same_bps(curr_sfmt, conv->to.fmt))
change_sign (curr_sound, size, &curr_sfmt);
else {
char *new_sound;
assert (curr_sfmt & SFMT_FLOAT);
new_sound = float_to_fixed ((float *)curr_sound,
*conv_len / sizeof(float),
conv->to.fmt, conv_len);
curr_sfmt = sfmt_set_fmt (curr_sfmt, conv->to.fmt);
if (curr_sound != buf)
free (curr_sound);
curr_sound = new_sound;
}
}
if ((curr_sfmt & SFMT_MASK_ENDIANNESS)
!= (conv->to.fmt & SFMT_MASK_ENDIANNESS)) {
swap_endian (curr_sound, *conv_len, curr_sfmt);
curr_sfmt = sfmt_set_endian (curr_sfmt,
conv->to.fmt & SFMT_MASK_ENDIANNESS);
}
if (conv->from.channels == 1 && conv->to.channels == 2) {
char *new_sound;
new_sound = mono_to_stereo (curr_sound, *conv_len,
conv->from.fmt);
*conv_len *= 2;
if (curr_sound != buf)
free (curr_sound);
curr_sound = new_sound;
}
return curr_sound;
}
void audio_conv_destroy (struct audio_conversion *conv ATTR_UNUSED)
{
assert (conv != NULL);
#ifdef HAVE_SAMPLERATE
if (conv->resample_buf)
free (conv->resample_buf);
if (conv->src_state)
src_delete (conv->src_state);
#endif
}