// File: /workspace/github_c_makefile_star2_O0/pipde[P]deos-songlib/utils/silence/silence.c
// Function: main
int
main(int argc,char **argv)
    {
    int argIndex = 1;

    int i,maxValue;
    int threshold;
    FILE *in,*out;
    RRA *h;

    argIndex = processOptions(argc,argv);

    if (argc-argIndex == 0)
        {
        in = stdin;
        out = stdout;
        }
    else if (argc-argIndex == 1)
        {
        in = OpenFile(argv[argIndex],"r");
        out = stdout;
        }
    else if (argc-argIndex == 2)
        {
        in = OpenFile(argv[argIndex],"r");
        out = OpenFile(argv[argIndex+1],"w");
        }
    else
        {
        printf("usage: %s -fN [<input rra file> [<output rra file>]]\n",
            argv[0]);
        exit(-1);
        }

    h = newRRAHeader();
    
    readRRAHeader(in,h,0);

    if (h->channels != 1)
        Fatal("%s requires a mono audio file\n",argv[0]);

    //printf("%f,%f\n",pow(2,h->bitsPerSample-1)-1,pow(factor,thresholdDb));

    maxValue = pow(2,h->bitsPerSample-1)-1;
    threshold = maxValue * pow(factor,thresholdDb);

    double volumeUpDelta =  1.0 / secondsToSamples(volumeUpRamp);
    double volumeDownDelta =  1.0 / secondsToSamples(volumeDownRamp);
    int volumeUpWaitSamples =  secondsToSamples(volumeUpWait);
    int volumeDownWaitSamples =  secondsToSamples(volumeDownWait);

    window = makeWindow();

    fprintf(stderr,"threshold: %d\n",threshold);
    fprintf(stderr,"amplify; %f\n",amplify);
    fprintf(stderr,"volume down wait; %d\n",volumeDownWaitSamples);
    fprintf(stderr,"volume down delta; %f\n",volumeDownDelta);

    writeRRAHeader(out,h,"modifiedBy: silence",0);

    int reducing = 0;
    double transitionAttenuation = 1;
    int transitionSamples = 0;
    int bottomMessage = 1;
    int topMessage = 1;

    for (i = 0; !feof(in); ++i)
        {
        int amp = readRRAAmplitude(in,out,h->bitsPerSample,outputComment);

        if (rms(i,amp) < threshold && i > windowSize)
            {
            if (!reducing) 
                {
                reducing = 1;
                transitionSamples = 0;
                fprintf(stderr,"waiting to reduce volume at %d samples\n",
                    i);
                fprintf(stderr,"attenuation is %f\n",transitionAttenuation);
                bottomMessage = 0;
                //getchar();
                }
            else if (transitionSamples < volumeDownWaitSamples)
                {
                ++transitionSamples;
                }
            else
                {
                transitionAttenuation -= volumeDownDelta;
                //fprintf(stderr,"reducing volume to %f at %d samples\n",
                    //transitionAttenuation,i);
                }
            }
        else if (i > windowSize)
            {
            if (reducing) 
                {
                reducing = 0;
                transitionSamples = 0;
                fprintf(stderr,"starting to increasing volume at %d samples\n",
                    i);
                fprintf(stderr,"attenuation is %f\n",transitionAttenuation);
                topMessage = 0;
                //getchar();
                }
            else if (transitionSamples < volumeUpWaitSamples)
                {
                ++transitionSamples;
                }
            else
                {
                transitionAttenuation += volumeUpDelta;
                //fprintf(stderr,"increasing volume to %f at %d samples\n",
                    //transitionAttenuation,i);
                }
            }
        if (transitionAttenuation > 1)
            {
            if (!topMessage)
                {
                fprintf(stderr,"at volume top\n");
                topMessage = 1;
                //getchar();
                }
            transitionAttenuation = 1;
            }
        else if (transitionAttenuation < 0)
            {
            if (!bottomMessage)
                {
                fprintf(stderr,"at volume bottom\n");
                bottomMessage = 1;
                //getchar();
                }
            transitionAttenuation = 0;
            }

        fprintf(out,"%d\n",(int) (amplify * amp * transitionAttenuation));
        }

    if (i != h->samples + 1)
        fprintf(stderr,"WARNING: %d samples found, %d expected\n",i,h->samples);

    fclose(in);
    fclose(out);

    return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x2900
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x241b
movq 0xebd7(%rip), %rax      # 0x10fd8
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq 0xebc1(%rip), %rax      # 0x10fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x24bf
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x1, %eax
jne 0x2452
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xab1f(%rip), %rsi      # 0xcf58
callq 0x90d0
movq %rax, -0x28(%rbp)
movq 0xeb87(%rip), %rax      # 0x10fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x24bd
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x2, %eax
jne 0x249c
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xaae8(%rip), %rsi      # 0xcf58
callq 0x90d0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x9c68(%rip), %rsi      # 0xc0f9
callq 0x90d0
movq %rax, -0x30(%rbp)
jmp 0x24bb
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x9c51(%rip), %rdi      # 0xc0fb
movb $0x0, %al
callq 0x20c0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2270
jmp 0x24bd
jmp 0x24bf
callq 0x95b0
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x9ab0
movq -0x38(%rbp), %rax
cmpl $0x1, (%rax)
je 0x24f7
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x9c41(%rip), %rdi      # 0xc131
movb $0x0, %al
callq 0x8f80
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x9b0b(%rip), %xmm0     # 0xc018
callq 0x2080
movsd 0x9aee(%rip), %xmm1     # 0xc008
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x1c(%rbp)
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
cvtsi2sdl 0xec49(%rip), %xmm1 # 0x11180
movsd 0x9ad1(%rip), %xmm0     # 0xc010
callq 0x2080
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x20(%rbp)
movsd 0xec29(%rip), %xmm0     # 0x11188
callq 0x6ed0
movaps %xmm0, %xmm1
movsd 0x9a99(%rip), %xmm0     # 0xc008
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0xec10(%rip), %xmm0     # 0x11190
callq 0x6ed0
movaps %xmm0, %xmm1
movsd 0x9a78(%rip), %xmm0     # 0xc008
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd 0xebf7(%rip), %xmm0     # 0x11198
callq 0x6ed0
cvttsd2si %xmm0, %eax
movl %eax, -0x4c(%rbp)
movsd 0xebeb(%rip), %xmm0     # 0x111a0
callq 0x6ed0
cvttsd2si %xmm0, %eax
movl %eax, -0x50(%rbp)
callq 0x2cc0
movq %rax, 0xee6b(%rip)      # 0x11438
movq 0xea24(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0x9b6f(%rip), %rsi      # 0xc150
movb $0x0, %al
callq 0x2190
movq 0xea09(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movsd 0xebae(%rip), %xmm0     # 0x111a8
leaq 0x9b5e(%rip), %rsi      # 0xc15f
movb $0x1, %al
callq 0x2190
movq 0xe9e9(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl -0x50(%rbp), %edx
leaq 0x9b50(%rip), %rsi      # 0xc16c
movb $0x0, %al
callq 0x2190
movq 0xe9ce(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movsd -0x48(%rbp), %xmm0
leaq 0x9b49(%rip), %rsi      # 0xc182
movb $0x1, %al
callq 0x2190
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq 0x9b4a(%rip), %rdx      # 0xc199
xorl %eax, %eax
movl %eax, %ecx
callq 0xaa50
movl $0x0, -0x54(%rbp)
movsd 0x99a1(%rip), %xmm0     # 0xc008
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x1, -0x68(%rbp)
movl $0x1, -0x6c(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x28(%rbp), %rdi
callq 0x21b0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x26a2
jmp 0x28b0
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x87d8(%rip), %rcx      # 0xae90
callq 0xa1e0
movl %eax, -0x70(%rbp)
movl -0x18(%rbp), %edi
movl -0x70(%rbp), %esi
callq 0x2d20
movaps %xmm0, %xmm1
cvtsi2sdl -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x276e
movl -0x18(%rbp), %eax
cmpl 0xeaca(%rip), %eax      # 0x111b0
jle 0x276e
cmpl $0x0, -0x54(%rbp)
jne 0x2741
movl $0x1, -0x54(%rbp)
movl $0x0, -0x64(%rbp)
movq 0xe8f1(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
leaq 0x9a99(%rip), %rsi      # 0xc1ad
movb $0x0, %al
callq 0x2190
movq 0xe8d6(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movsd -0x60(%rbp), %xmm0
leaq 0x9aa4(%rip), %rsi      # 0xc1d5
movb $0x1, %al
callq 0x2190
movl $0x0, -0x68(%rbp)
jmp 0x2769
movl -0x64(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x2754
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2767
movsd -0x48(%rbp), %xmm1
movsd -0x60(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x2769
jmp 0x27f6
movl -0x18(%rbp), %eax
cmpl 0xea39(%rip), %eax      # 0x111b0
jle 0x27f4
cmpl $0x0, -0x54(%rbp)
je 0x27ce
movl $0x0, -0x54(%rbp)
movl $0x0, -0x64(%rbp)
movq 0xe864(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
leaq 0x9a47(%rip), %rsi      # 0xc1e8
movb $0x0, %al
callq 0x2190
movq 0xe849(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movsd -0x60(%rbp), %xmm0
leaq 0x9a17(%rip), %rsi      # 0xc1d5
movb $0x1, %al
callq 0x2190
movl $0x0, -0x6c(%rbp)
jmp 0x27f2
movl -0x64(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x27e1
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x27f0
movsd -0x40(%rbp), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x27f2
jmp 0x27f4
jmp 0x27f6
movsd -0x60(%rbp), %xmm0
movsd 0x9805(%rip), %xmm1     # 0xc008
ucomisd %xmm1, %xmm0
jbe 0x283d
cmpl $0x0, -0x6c(%rbp)
jne 0x282e
movq 0xe7e2(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x99f5(%rip), %rsi      # 0xc215
movb $0x0, %al
callq 0x2190
movl $0x1, -0x6c(%rbp)
movsd 0x97d2(%rip), %xmm0     # 0xc008
movsd %xmm0, -0x60(%rbp)
jmp 0x2876
xorps %xmm0, %xmm0
ucomisd -0x60(%rbp), %xmm0
jbe 0x2874
cmpl $0x0, -0x68(%rbp)
jne 0x286c
movq 0xe7a4(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x99c6(%rip), %rsi      # 0xc224
movb $0x0, %al
callq 0x2190
movl $0x1, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x2876
movq -0x30(%rbp), %rdi
movsd 0xe926(%rip), %xmm0     # 0x111a8
cvtsi2sdl -0x70(%rbp), %xmm1
mulsd %xmm1, %xmm0
mulsd -0x60(%rbp), %xmm0
cvttsd2si %xmm0, %edx
leaq 0x98c0(%rip), %rsi      # 0xc15b
movb $0x0, %al
callq 0x2190
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2688
movl -0x18(%rbp), %eax
movq -0x38(%rbp), %rcx
movl 0xc(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
je 0x28e3
movq 0xe730(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movq -0x38(%rbp), %rax
movl 0xc(%rax), %ecx
leaq 0x995a(%rip), %rsi      # 0xc236
movb $0x0, %al
callq 0x2190
movq -0x28(%rbp), %rdi
callq 0x2090
movq -0x30(%rbp), %rdi
callq 0x2090
xorl %eax, %eax
addq $0x80, %rsp
popq %rbp
retq