5 * Copyright (C) 2010-2013 Xiph.Org
7 * squishyball is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * squishyball is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with rtrecord; see the file COPYING. If not, write to the
19 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define _LARGEFILE_SOURCE
26 #define _LARGEFILE64_SOURCE
27 #define _FILE_OFFSET_BITS 64
33 #include <sys/types.h>
38 /* sample formatting helpers ********************************************************/
40 static inline int host_is_big_endian() {
43 unsigned char bytewise
[4];
45 m
.pattern
= 0xfeedface; /* deadbeef */
46 if (m
.bytewise
[0] == 0xfe) return 1;
50 static float get_clamp(pcm_t
*pcm
){
51 if(pcm
->nativebits
>=0 && pcm
->nativebits
<24)
52 return 1.f
- 1.f
/(1<<(pcm
->nativebits
-1));
54 return 1.f
- 1.f
/2147483648.f
;
57 float check_warn_clipping(pcm_t
*pcm
, int no_normalize
){
60 int s
= pcm
->size
/sizeof(float);
65 float *d
= (float *)pcm
->data
;
67 memset(flag
,0,sizeof(flag
));
70 fprintf(stderr
,"\rLoading %s: checking for clipping...",pcm
->name
);
72 clamp
= max
= get_clamp(pcm
);
75 for(i
=0;i
<s
;i
+=pcm
->ch
)
76 for(j
=0;j
<pcm
->ch
;j
++){
78 if(d
[i
+j
]<min
)min
=d
[i
+j
];
80 }else if(d
[i
+j
]>clamp
){
81 if(d
[i
+j
]>max
)max
=d
[i
+j
];
84 if(flag
[j
]>1)count
+=flag
[j
];
89 if(flag
[j
]>1)count
+=flag
[j
];
94 if(pcm
->nativebits
>0){
95 fprintf(stderr
,"CLIPPING WARNING: %ld probably clipped samples in %s;\n",(long)count
,pcm
->name
);
96 fprintf(stderr
," (can't be repaired with normalization)\n");
99 fprintf(stderr
,"CLIPPING WARNING: %ld clipped samples in %s;\n",(long)count
,pcm
->name
);
100 fprintf(stderr
," normalization disabled on command line.\n");
103 if(clamp
/max
< att
) att
=clamp
/max
;
105 fprintf(stderr
,"%ld overrange samples after decoding %s (peak %+0.1fdB)\n",(long)count
,pcm
->name
,todB(1./att
));
112 fprintf(stderr
,"\rLoading %s: done. \n",pcm
->name
);
118 /* input must be float */
119 float convert_to_mono(pcm_t
*pcm
){
122 int s
= pcm
->size
/sizeof(float);
123 float *d
= (float *)pcm
->data
;
127 float clamp
= get_clamp(pcm
);
129 if(pcm
->currentbits
!=-32){
130 fprintf(stderr
,"Internal error; non-float PCM passed to convert_to_mono.\n");
135 fprintf(stderr
,"Downmixing to mono... ");
150 if(pcm
->matrix
)free(pcm
->matrix
);
151 pcm
->matrix
=strdup("M");
152 if(min
<-1.f
) att
=-1./min
;
153 if(clamp
/max
< att
) att
=clamp
/max
;
157 fprintf(stderr
,"done. peak: %+0.1fdB\n",todB(1./att
));
159 fprintf(stderr
,"done.\n");
166 static const float left_mix
[33]={
182 static const float right_mix
[33]={
198 /* input must be float */
199 float convert_to_stereo(pcm_t
*pcm
){
202 int s
= pcm
->size
/sizeof(float);
203 float *d
= (float *)pcm
->data
;
207 float clamp
= get_clamp(pcm
);
211 if(pcm
->currentbits
!=-32){
212 fprintf(stderr
,"Internal error; non-float PCM passed to convert_to_mono.\n");
216 fprintf(stderr
,"Internal error; can't downmix mono to stereo.\n");
221 fprintf(stderr
,"Downmixing to stereo... ");
223 lmix
=calloc(cpf
,sizeof(*lmix
));
224 rmix
=calloc(cpf
,sizeof(*rmix
));
226 lmix
[j
] = left_mix
[pcm
->mix
[j
]-'A'];
227 rmix
[j
] = right_mix
[pcm
->mix
[j
]-'A'];
247 pcm
->size
=pcm
->size
/cpf
*2;
249 if(pcm
->matrix
)free(pcm
->matrix
);
250 pcm
->matrix
=strdup("L,R");
252 if(min
<-1.f
) att
=-1./min
;
253 if(clamp
/max
< att
) att
=clamp
/max
;
257 fprintf(stderr
,"done. peak: %+0.1fdB\n",todB(1./att
));
259 fprintf(stderr
,"done.\n");
265 static inline float triangle_ditherval(float *save
){
266 float r
= rand()/(float)RAND_MAX
-.5f
;
272 void convert_to_32(pcm_t
*pcm
){
273 unsigned char *d
= pcm
->data
;
274 float *f
= (float *)pcm
->data
;
277 fprintf(stderr
,"\rConverting %s to 32 bit... ",pcm
->name
);
278 for(j
=0;j
<pcm
->size
/sizeof(float);j
++){
279 float val
= rint(f
[j
]*2147483648.f
);
281 if(val
<-2147483648.f
) val
= -2147483648.f
;
282 if(val
> 2147483647.f
) val
= 2147483647.f
;
285 d
[j
*4+1]=(iv
>>8)&0xff;
286 d
[j
*4+2]=(iv
>>16)&0xff;
287 d
[j
*4+3]=(iv
>>24)&0xff;
290 fprintf(stderr
,"done.\n");
292 pcm
->size
/=sizeof(float);
296 void convert_to_24(pcm_t
*pcm
){
297 unsigned char *d
= pcm
->data
;
298 float *f
= (float *)pcm
->data
;
301 fprintf(stderr
,"\rConverting %s to 24 bit... ",pcm
->name
);
302 for(j
=0;j
<pcm
->size
/sizeof(float);j
++){
303 float val
= rint(f
[j
]*8388608.f
);
305 if(val
<-8388608.f
) val
= -8388608.f
;
306 if(val
> 8388607.f
) val
= 8388607.f
;
309 d
[j
*3+1]=(iv
>>8)&0xff;
310 d
[j
*3+2]=(iv
>>16)&0xff;
313 fprintf(stderr
,"done.\n");
315 pcm
->size
/=sizeof(float);
319 void convert_to_16(pcm_t
*pcm
, int dither
){
320 unsigned char *d
= pcm
->data
;
321 float *f
= (float *)pcm
->data
;
325 memset(t
,0,sizeof(t
));
328 fprintf(stderr
,"\r%s %s to 16 bit... ",
329 dither
?"Dithering":"Down-converting",pcm
->name
);
331 for(j
=0;j
<pcm
->size
/sizeof(float);j
++){
334 val
= rint(f
[j
]*32768.f
+ triangle_ditherval(t
+ch
));
338 val
= rint(f
[j
]*32768.f
);
344 }else if(val
<=-32768.f
){
350 d
[j
*2+1]=(iv
>>8)&0xff;
355 fprintf(stderr
,"done.\n");
358 pcm
->size
/=sizeof(float);
362 /* Channel map reconciliation helpers *********************************/
364 static const char *chlist
[]={"X","M","L","R","C","LFE","SL","SR","BC","BL","BR","CL","CR",NULL
};
366 static void tokenize_channels(char *matrix
,int *out
,int n
){
368 char *copy
= strdup(matrix
);
369 char *t
=strtok(copy
,",");
370 memset(out
,0,sizeof(*out
)*n
);
375 if(!strcmp(chlist
[j
],t
))break;
385 /* pre-permute sample ordering so that playback incurs ~equal
386 CPU/memory/etc load during playback */
387 /* A and B must have machine sample formats */
388 void reconcile_channel_maps(pcm_t
*A
, pcm_t
*B
){
389 /* arbitrary; match B to A */
390 int ai
[A
->ch
],bi
[A
->ch
];
393 int bps
= (B
->currentbits
+7)/8;
396 unsigned char temp
[bpf
];
399 tokenize_channels(A
->matrix
,ai
,A
->ch
);
400 tokenize_channels(B
->matrix
,bi
,A
->ch
);
403 fprintf(stderr
,"remapping channels in %s: ",B
->name
);
405 fprintf(stderr
,"%d%s",bi
[i
],i
+1==B
->ch
?"":", ");
406 fprintf(stderr
," -> ");
408 fprintf(stderr
,"%d%s",ai
[i
],i
+1==A
->ch
?"":", ");
409 fprintf(stderr
,"\n");
412 for(i
=0;i
<A
->ch
;i
++){
414 p
[i
*bps
+k
]=i
*bps
+k
; /* failsafe */
415 for(j
=0;j
<A
->ch
;j
++){
425 for(o
=0;o
<B
->size
;o
+=bpf
){
433 B
->matrix
= strdup(A
->matrix
);
436 /* fade and beep function generation **************************************/
438 void put_val(unsigned char *d
,int bps
,float v
){
446 float get_val(unsigned char *d
, int bps
){
448 short i
= d
[0] | (d
[1]<<8);
451 int32_t i
= ((d
[0]<<8) | (d
[1]<<16) | (d
[2]<<24))>>8;
456 int setup_windows(pcm_t
**pcm
, int test_files
,
457 float **fw1
, float **fw2
, float **fw3
,
458 float **b1
, float **b2
){
460 int fragsamples
= pcm
[0]->rate
/10; /* 100ms */
461 float mul
= (pcm
[0]->currentbits
==24 ? 8388608.f
: 32768.f
) * .0625;
462 int bps
=(pcm
[0]->currentbits
+7)/8;
465 int maxsamples
= pcm
[0]->size
/ bpf
;
466 if (fragsamples
* 3 > maxsamples
)
467 fragsamples
= maxsamples
/ 3;
468 /* precompute the fades/beeps */
469 float *fadewindow1
= *fw1
= calloc(fragsamples
,sizeof(*fadewindow1
));
470 float *fadewindow2
= *fw2
= calloc(fragsamples
,sizeof(*fadewindow2
));
471 float *fadewindow3
= *fw3
= calloc(fragsamples
,sizeof(*fadewindow3
));
472 float *beep1
= *b1
= calloc(fragsamples
,sizeof(*beep1
));
473 float *beep2
= *b2
= calloc(fragsamples
,sizeof(*beep2
));
482 /* fadewindow1 is a larger simple crossfade */
483 for(i
=0;i
<fragsamples
;i
++){
484 float val
= cosf(M_PI
*.5f
*(i
+.5f
)/fragsamples
);
485 fadewindow1
[i
] = val
*val
;
488 /* fadewindow2 goes to silence and back */
489 for(i
=0;i
<fragsamples
/3;i
++){
490 float val
= cosf(M_PI
*1.5f
*(i
+.5f
)/fragsamples
);
491 fadewindow2
[i
] = val
*val
;
493 for(;i
<fragsamples
;i
++)
494 fadewindow2
[i
] = 0.f
;
496 /* fadewindow3 is like fadewindow 2 but with briefer silence */
497 for(i
=0;i
<fragsamples
/2;i
++){
498 float val
= cosf(M_PI
*(i
+.5f
)/fragsamples
);
499 fadewindow3
[i
] = val
*val
;
501 for(;i
<fragsamples
;i
++)
502 fadewindow3
[i
] = 0.f
;
504 /* Single beep for flipping */
505 for(i
=0;i
<fragsamples
/4;i
++){
507 beep1
[fragsamples
-i
-1]=0.f
;
509 float base
= 3.14159f
*2.f
*1000./pcm
[0]->rate
;
510 for(;i
<fragsamples
*3/4;i
++){
511 float f
= i
-fragsamples
/4+.5f
;
512 float w
= cosf(3.14159f
*f
/fragsamples
);
523 /* Double beep for selection */
524 for(i
=0;i
<fragsamples
/4;i
++){
526 beep2
[fragsamples
-i
-1]=0.f
;
528 for(;i
<fragsamples
/2;i
++){
529 float f
= i
-fragsamples
/4+.5f
;
530 float w
= cosf(3.14159f
*2.f
*f
/fragsamples
);
540 base
= 3.14159f
*2.f
*1500./pcm
[0]->rate
;
541 for(;i
<fragsamples
*3/4;i
++){
542 float f
= i
-fragsamples
/2+.5f
;
543 float w
= cosf(3.14159f
*2.f
*f
/fragsamples
);
551 beep2
[i
] = w
*b
*mul
*2;
557 /* fragment is filled such that a crossloop never begins after
558 pcm->size-fragsize, and it always begins from the start of the
559 window, even if that means starting a crossloop late because the
561 void fill_fragment1(unsigned char *out
, pcm_t
*pcm
, off_t start
, off_t
*pos
, off_t end
, int *loop
,
562 int fragsamples
, float *fadewindow
){
563 int bps
= (pcm
->currentbits
+7)/8;
566 int fragsize
= fragsamples
*bpf
;
568 /* guard limits here */
569 if(end
<fragsize
*3)end
=fragsize
*3;
570 if(end
>pcm
->size
)end
=pcm
->size
;
572 if(start
>pcm
->size
-fragsize
*3)start
=pcm
->size
-fragsize
*3;
574 /* we fill a fragment from the data buffer of the passed in pcm_t.
575 It's possible we'll need to crossloop from the end of the sample,
576 and the start/end markers may have moved so that the cursor is
577 outside the strict sample bounds. */
579 /* if *loop>0, we're in the process of crosslapping at pos ><
580 start+(fragsize-*loop*bpf). Stay the course. */
584 unsigned char *A
= pcm
->data
+*pos
;
585 unsigned char *B
= pcm
->data
+start
+(fragsamples
-lp
)*bpf
;
586 for(i
=0;i
<fragsamples
;i
++){
588 float w
= fadewindow
[--lp
];
590 float val
= get_val(A
,bps
)*(1.f
-w
) + get_val(B
,bps
)*w
;
591 put_val(out
,bps
,val
);
597 /* crossloop finished, the rest is B */
606 /* no crossloop in progress... should one be? If the cursor is
607 before start, do nothing. If it's past end-fragsize, begin a
608 crossloop immediately. If the current fragment will extend
609 beyond end-fragsize, begin the crossloop at end-fragsize */
610 if(*pos
>pcm
->size
-fragsize
){
611 /* Error condition; should not be possible */
612 fprintf(stderr
,"Internal error; %ld>%ld, Monty fucked up.\n",(long)*pos
,(long)pcm
->size
-fragsize
);
614 }else if(*pos
+fragsize
>end
-fragsize
){
616 unsigned char *A
= pcm
->data
+*pos
;
617 unsigned char *B
= pcm
->data
+start
;
618 int lp
= (end
-*pos
)/bpf
;
619 if(lp
<fragsamples
)lp
=fragsamples
; /* If we're late, start immediately, but use full window */
621 for(i
=0;i
<fragsamples
;i
++){
622 if(--lp
>=fragsamples
){
623 /* still before crossloop begins */
629 float w
= fadewindow
[lp
];
631 float val
= get_val(A
,bps
)*(1.f
-w
) + get_val(B
,bps
)*w
;
632 put_val(out
,bps
,val
);
640 *pos
=(lp
<=0?B
-pcm
->data
:A
-pcm
->data
);
643 unsigned char *A
= pcm
->data
+*pos
;
644 memcpy(out
,A
,fragsize
);
651 /* fragment is filled such that a crossloop is always 'exactly on
652 schedule' even if that means beginning partway through the window. */
653 void fill_fragment2(unsigned char *out
, pcm_t
*pcm
, off_t start
, off_t
*pos
, off_t end
, int *loop
,
654 int fragsamples
, float *fadewindow
){
655 int bps
= (pcm
->currentbits
+7)/8;
658 int fragsize
=fragsamples
*bpf
;
660 /* guard limits here */
661 if(end
<fragsize
*3)end
=fragsize
*3;
662 if(end
>pcm
->size
)end
=pcm
->size
;
664 if(start
>pcm
->size
-fragsize
*3)start
=pcm
->size
-fragsize
*3;
666 /* loop is never in progress for a fill_fragment2; called only during a seek crosslap */
667 unsigned char *A
= pcm
->data
+*pos
;
668 if(end
-*pos
>=fragsize
*2){
670 memcpy(out
,A
,fragsize
);
672 *pos
=A
-pcm
->data
+fragsize
;
674 /* just before crossloop, in the middle of a crossloop, or just after crossloop */
676 int lp
= (end
-*pos
)/bpf
;
677 unsigned char *B
= pcm
->data
+start
;
678 if(lp
<fragsamples
)B
+=(fragsamples
-lp
)*bpf
;
680 for(i
=0;i
<fragsamples
;i
++){
683 /* not yet crosslooping */
688 /* now crosslooping */
689 float w
= fadewindow
[lp
];
691 float val
= get_val(A
,bps
)*(1.-w
) + get_val(B
,bps
)*w
;
692 put_val(out
,val
,bps
);
704 *loop
=(lp
>0?(lp
<fragsamples
?lp
:fragsamples
):0);
705 *pos
=(lp
>0?A
-pcm
->data
:B
-pcm
->data
);
709 /* playback ******************************************************/
711 ao_device
*setup_playback(int rate
, int ch
, int bits
, char *matrix
, char *device
){
712 ao_option aoe
={0,0,0};
719 sf
.byte_format
=AO_FMT_LITTLE
;
720 sf
.matrix
=(ch
>2?matrix
:0);
724 /* if we don't have an explicit device, defaults make this easy */
725 int id
= ao_default_driver_id();
729 ai
=ao_driver_info(id
);
732 aname
=ai
->short_name
;
734 fprintf(stderr
,"Opening [%s] %s for %d/%d and %d channel[s]...",aname
,"default",bits
,rate
,ch
);
735 ret
=ao_open_live(id
, &sf
, &aoe
);
738 fprintf(stderr
," errno %d\n",errno
);
740 fprintf(stderr
," ok!\n");
744 /* Otherwise... there's some hunting to do. */
745 /* Is the passed device a number or a name? */
748 ao_info
**info_list
=ao_driver_info_list(&count
);
749 int number
=strtol(device
,&test
,10);
752 if(!device
[0] || test
[0]) number
=-1;
754 fprintf(stderr
,"Scanning for a device driver that recognizes '%s'...\n",device
);
756 /* driver info list is sorted by priority */
757 for(i
=0;i
<count
;i
++){
759 ao_info
*info
=info_list
[i
];
760 ao_option ao
={0,0,0};
761 int id
= ao_driver_id(info
->short_name
);
764 sprintf(buf
,"%d",number
);
765 ao
.key
=(number
>=0?"id":"dev");
766 ao
.value
=(number
>=0?buf
:device
);
768 aname
=info
->short_name
;
770 /* don't try to open the driver if it doesn't have the device/id
771 option; it will ignore the option and try to open its default */
772 for(j
=0;j
<info
->option_count
;j
++)
773 if(!strcmp(info
->options
[j
],ao
.key
))break;
774 if(j
<info
->option_count
){
776 fprintf(stderr
," ...trying to open [%s] %s for %d/%d and %d channel[s]...",aname
,device
,bits
,rate
,ch
);
777 if((ret
=ao_open_live(id
,&sf
,&ao
))){
779 fprintf(stderr
," ok!\n");
783 fprintf(stderr
," errno %d\n",errno
);
787 if(ret
&& sb_verbose
)
788 fprintf(stderr
,"Opened %s%s audio device %s%sfor %d bit %d channel %d Hz...\n",
789 (device
?"":"default "),aname
,
790 (device
?device
:""),(device
?" ":""),bits
,ch
,rate
);