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

    int i,index;
    FILE *in,*out;
    char *source = 0;
    RRA *h;
    static int skip;

    argIndex = processOptions(argc,argv);

    if (argc-argIndex == 0)
        {
        in = stdin;
        out = stdout;
        }
    else if (argc-argIndex == 1)
        {
        source = argv[argIndex];
        in = OpenFile(source,"r");
        out = stdout;
        }
    else if (argc-argIndex == 2)
        {
        source = argv[argIndex];
        in = OpenFile(source,"r");
        out = OpenFile(argv[argIndex+1],"w");
        }
    else
        {
        printf("usage: %s [<input rra file> [<output wave 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]);

    /* allocate window and zeros */
    windowSize = (h->sampleRate / MinFrequency) * 10;
    window = malloc(sizeof(int) * windowSize);
    zeros = malloc(sizeof(int) * windowSize);
    if (window == 0 || zeros == 0) Fatal("out of memory\n");

    /* skip over the first part */

    skip = initialOffset * h->sampleRate;
    printf("skip is %d\n",skip);
    for (i = 0; i < skip; ++i)
        (void) readRRAAmplitude(in,out,h->bitsPerSample,outputComment);
        printf("skipping %d\n",readRRAAmplitude(in,out,h->bitsPerSample,outputComment));
    //printf("skipping %d\n",readRRAAmplitude(in,out,h->bitsPerSample,outputComment));

    /* read in a segment */

    printf("windowSize is %d\n",windowSize);
    for (i = 0; i < windowSize && !feof(in); ++i)
        {
        window[i] = readRRAAmplitude(in,out,h->bitsPerSample,outputComment);
        //printf("window[%d] is %d\n",i,window[i]);
        }

    printf("smoothing...\n");
    /* heavily smooth the data */
    for (i = 0; i < smoothings; ++i)
        smooth();

    FILE *fp = fopen("smoothed.rra","w");
    fprintf(fp,"RRAUDIO\n");
    fprintf(fp,"%%%%\n");
    for (i = 0; i < windowSize; ++i)
        fprintf(fp,"%d\n",window[i]);
    fclose(fp);

    printf("finding first upward zero crossing\n");
    /* find the first upward zero crossing */
    for (i = 100; i < windowSize-1; ++i)
        if (window[i] < 0 && window[i+1] >= 0)
            break;

    printf("zero at %d\n",i);
    printf("finding remaining upward zero crossings\n");
    /* find remaining upward zero crossings, recording length and count */
    int count = 0;
    int last = i;
    for (++i; i < windowSize-200; ++i)
        {
        if (window[i] < 0 && window[i+1] >= 0)
            {
            zeros[count++] = i - last;
            printf("zeros at %d = %d\n",count-1,zeros[count-1]);
            last = i;
            }
        }

    printf("computing average and score\n");
    /* compute the average and stddev */
    double total = 0.0;
    for (i = 0; i < count; ++i)
        total += zeros[i];
    printf("count is %d\n",count);
    printf("total is %f\n",total);
    double avg = total / count;
    total = 0.0;
    for (i = 0; i < count; ++i)
        total += pow(zeros[i]-avg,2);
    double stddev = pow(total / count,0.5);
    double freq = h->sampleRate / avg;

    printf("avg is %f\n",avg);

    index = -1;
    for (i = 0; index < 0 && i < 120; ++i)
        {
        if (freq >= freqs[i] && freq < freqs[i+1])
            {
            if (freq - freqs[i] >= freqs[i+1] - freq)
                index = i+1;
            else
                index = i;
            }
        }

    printf("index is %d\n",index);
    if (Pitch && source != 0)
        printf("rratune %f %s %s.repitch\n",
            freqs[index] / freq,source,source);
    else if (Pitch)
        printf("rratune %f\n", freqs[index] / freq);
    else if (Note)
        printf("%s\n", notes[index]);
    else if (Frequency)
        printf("%f\n", freq);
    else
        printf("closest note to %f is %s (freq = %f) -- error is %f\n",
            freq,notes[index],freqs[index],stddev);
            
    fclose(in);
    fclose(out);

    return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rdx
movl %edi, %ebx
pushq $0x1
popq %r15
leaq 0x2ca3(%rip), %r12      # 0x5020
movq %rsi, -0x30(%rbp)
movslq %r15d, %r13
cmpl %r15d, %ebx
jle 0x242c
movq (%rdx,%r13,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x242c
movb 0x2(%rsi), %r14b
testb %r14b, %r14b
je 0x23a9
leaq 0x2(%rsi), %rdi
jmp 0x23ae
movq 0x8(%rdx,%r13,8), %rdi
movzbl 0x1(%rsi), %eax
leal -0x66(%rax), %ecx
cmpl $0xa, %ecx
ja 0x23f7
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb $0x1, 0x6546(%rip)      # 0x8910
jmp 0x23f2
callq 0x2080
movq -0x30(%rbp), %rdx
movsd %xmm0, 0x64f3(%rip)     # 0x88d0
movb $0x1, %r13b
jmp 0x2415
movb $0x1, 0x6523(%rip)      # 0x890c
jmp 0x23f2
movb $0x1, 0x6516(%rip)      # 0x8908
xorl %r13d, %r13d
jmp 0x2415
cmpl $0x76, %eax
je 0x2a43
xorl %r13d, %r13d
leaq 0x2f78(%rip), %rdi      # 0x5382
xorl %eax, %eax
callq 0x2a80
movq -0x30(%rbp), %rdx
testb %r14b, %r14b
sete %al
andb %r13b, %al
movzbl %al, %eax
addl %eax, %r15d
incl %r15d
jmp 0x2381
subl %r15d, %ebx
jne 0x244f
movq 0x5ba0(%rip), %rax      # 0x7fd8
movq (%rax), %rbx
movq 0x5b8e(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq $0x0, -0x40(%rbp)
jmp 0x24ab
cmpl $0x2, %ebx
je 0x2480
cmpl $0x1, %ebx
jne 0x2a66
movq (%rdx,%r13,8), %rdi
leaq 0x3090(%rip), %rsi      # 0x54f8
movq %rdi, -0x40(%rbp)
callq 0x2bb0
movq %rax, %rbx
movq 0x5b55(%rip), %rax      # 0x7fd0
movq (%rax), %rax
jmp 0x24ab
movq (%rdx,%r13,8), %rdi
leaq 0x306d(%rip), %rsi      # 0x54f8
movq %rdi, -0x40(%rbp)
movq %rdx, %r14
callq 0x2bb0
movq %rax, %rbx
movq 0x8(%r14,%r13,8), %rdi
leaq 0x2d81(%rip), %rsi      # 0x5227
callq 0x2bb0
movq %rax, -0x48(%rbp)
callq 0x2e0c
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x3171
movq %r14, -0x38(%rbp)
cmpl $0x1, (%r14)
je 0x24e3
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x2d80(%rip), %rdi      # 0x525c
xorl %eax, %eax
callq 0x2a80
movq %rbx, %r12
movq -0x38(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
divsd 0x2b0c(%rip), %xmm0     # 0x5008
mulsd 0x2b0c(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %eax
movl %eax, 0x63e6(%rip)      # 0x88f4
movslq %eax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2190
movq %rax, %r13
movq %rax, 0x63d1(%rip)      # 0x88f8
movq %r15, %rdi
callq 0x2190
movsd -0x30(%rbp), %xmm0
movq %rax, 0x63c5(%rip)      # 0x8900
testq %r13, %r13
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x2562
leaq 0x2d29(%rip), %rdi      # 0x527b
xorl %eax, %eax
callq 0x2a80
movq -0x38(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm0
mulsd 0x6366(%rip), %xmm0     # 0x88d0
pushq $-0x1
popq %r13
cvttsd2si %xmm0, %esi
movl %esi, 0x6378(%rip)      # 0x88f0
leaq 0x2d0b(%rip), %rdi      # 0x528a
xorl %eax, %eax
callq 0x20c0
movq -0x48(%rbp), %rbx
movq %r12, %r15
movq -0x38(%rbp), %r12
movl 0x8(%r12), %edx
movl 0x6353(%rip), %r14d     # 0x88f0
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x1863(%rip), %rcx      # 0x3e0d
callq 0x3646
incl %r13d
cmpl %r14d, %r13d
jl 0x2591
leaq 0x2cd8(%rip), %rdi      # 0x5296
xorl %r13d, %r13d
movl %eax, %esi
xorl %eax, %eax
callq 0x20c0
movl 0x6324(%rip), %esi      # 0x88f4
leaq 0x2ccc(%rip), %rdi      # 0x52a3
xorl %eax, %eax
callq 0x20c0
leaq 0x1828(%rip), %r14      # 0x3e0d
movq %r15, %rbx
movslq 0x6305(%rip), %rax      # 0x88f4
cmpq %rax, %r13
jge 0x2624
movq %rbx, %rdi
callq 0x2170
testl %eax, %eax
jne 0x2624
movl 0x8(%r12), %edx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq %r14, %rcx
callq 0x3646
movq 0x62dd(%rip), %rcx      # 0x88f8
movl %eax, (%rcx,%r13,4)
incq %r13
jmp 0x25e8
leaq 0x2d71(%rip), %rdi      # 0x539c
callq 0x2070
movslq 0x62bd(%rip), %rcx      # 0x88f4
addq $-0x4, %rcx
xorl %esi, %esi
movq 0x62b4(%rip), %rdi      # 0x88f8
pushq $0x4
popq %r8
pushq $0x4b
popq %r9
cmpl $0x14, %esi
je 0x269e
movq %r8, %r10
cmpq %rcx, %r10
jge 0x269a
movl -0x10(%rdi,%r10,4), %eax
movl -0x8(%rdi,%r10,4), %edx
shll $0x5, %eax
imull $0x18, -0xc(%rdi,%r10,4), %r11d
addl %eax, %r11d
movl 0xc(%rdi,%r10,4), %eax
addl -0x4(%rdi,%r10,4), %eax
movl 0x8(%rdi,%r10,4), %r14d
leal (%r11,%rdx,8), %edx
addl (%rdi,%r10,4), %edx
leal (%rdx,%r14,2), %edx
leal (%rdx,%rax,4), %eax
cltd
idivl %r9d
movl %eax, (%rdi,%r10,4)
incq %r10
jmp 0x2654
incl %esi
jmp 0x264c
leaq 0x2c10(%rip), %rdi      # 0x52b5
leaq 0x2b7b(%rip), %rsi      # 0x5227
callq 0x21d0
movq %rax, %r15
leaq 0x2fcc(%rip), %rdi      # 0x5687
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2220
leaq 0x2bf2(%rip), %rsi      # 0x52c2
movq %r15, %rdi
xorl %eax, %eax
callq 0x2150
leaq 0x2f54(%rip), %r13      # 0x5635
xorl %r14d, %r14d
movslq 0x6209(%rip), %rax      # 0x88f4
cmpq %rax, %r14
jge 0x270d
movq 0x6201(%rip), %rax      # 0x88f8
movl (%rax,%r14,4), %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x2150
incq %r14
jmp 0x26e4
movq %r15, %rdi
callq 0x20a0
leaq 0x2c8d(%rip), %rdi      # 0x53a9
callq 0x2070
movslq 0x61cc(%rip), %rax      # 0x88f4
decq %rax
movq 0x61c6(%rip), %rcx      # 0x88f8
cmpl $0x65, %eax
pushq $0x64
popq %r14
cmovgel %eax, %r14d
pushq $0x64
popq %rdx
cmpq %rax, %rdx
jge 0x275a
cmpl $0x0, (%rcx,%rdx,4)
jns 0x2752
cmpl $0x0, 0x4(%rcx,%rdx,4)
jns 0x2757
incq %rdx
jmp 0x2740
movl %edx, %r14d
leaq 0x2b67(%rip), %rdi      # 0x52c8
movl %r14d, %esi
xorl %eax, %eax
callq 0x20c0
leaq 0x2c5a(%rip), %rdi      # 0x53cc
callq 0x2070
movl %r14d, %r15d
xorl %r13d, %r13d
movslq 0x6170(%rip), %rax      # 0x88f4
addq $-0xc8, %rax
movq 0x6167(%rip), %rcx      # 0x88f8
incq %r15
cmpq %rax, %r15
jge 0x27da
cmpl $0x0, (%rcx,%r15,4)
jns 0x2791
cmpl $0x0, 0x4(%rcx,%r15,4)
js 0x2791
movl %r15d, %edx
subl %r14d, %edx
movq 0x614b(%rip), %rax      # 0x8900
movq %r13, %r14
incq %r14
movl %r13d, %ecx
movl %edx, (%rax,%rcx,4)
leaq 0x2b0c(%rip), %rdi      # 0x52d4
movl %r13d, %esi
xorl %eax, %eax
callq 0x20c0
movq %r14, %r13
movl %r15d, %r14d
jmp 0x277d
leaq 0x2c13(%rip), %rdi      # 0x53f4
callq 0x2070
xorpd %xmm1, %xmm1
xorl %eax, %eax
movq 0x610d(%rip), %rcx      # 0x8900
cmpq %rax, %r13
je 0x2806
cvtsi2sdl (%rcx,%rax,4), %xmm0
addsd %xmm0, %xmm1
incq %rax
jmp 0x27f3
leaq 0x2ad9(%rip), %rdi      # 0x52e6
movl %r13d, %esi
xorl %eax, %eax
movsd %xmm1, -0x30(%rbp)
callq 0x20c0
leaq 0x2ad0(%rip), %rdi      # 0x52f3
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x20c0
movsd -0x30(%rbp), %xmm3
cvtsi2sd %r13d, %xmm1
divsd %xmm1, %xmm3
xorpd %xmm0, %xmm0
movq 0x60b8(%rip), %rax      # 0x8900
subq $0x1, %r13
jb 0x2864
cvtsi2sdl (%rax), %xmm2
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
addq $0x4, %rax
jmp 0x2848
divsd %xmm1, %xmm0
movsd 0x27a8(%rip), %xmm1     # 0x5018
movsd %xmm3, -0x30(%rbp)
callq 0x2090
movsd %xmm0, -0x50(%rbp)
cvtsi2sdl 0x4(%r12), %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
leaq 0x2a65(%rip), %rdi      # 0x5300
movb $0x1, %al
callq 0x20c0
movsd -0x38(%rbp), %xmm3
pushq $-0x1
popq %r15
xorl %eax, %eax
leaq 0x589c(%rip), %r12      # 0x8150
testl %r15d, %r15d
jns 0x28f6
cmpq $0x77, %rax
ja 0x28f6
movsd (%r12,%rax,8), %xmm0
ucomisd %xmm0, %xmm3
jb 0x28d8
movsd 0x8(%r12,%rax,8), %xmm1
ucomisd %xmm3, %xmm1
ja 0x28dd
incq %rax
jmp 0x28b4
movapd %xmm3, %xmm2
subsd %xmm0, %xmm2
subsd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
movl %eax, %r15d
sbbl $-0x1, %r15d
jmp 0x28d8
leaq 0x2a0e(%rip), %rdi      # 0x530b
movl %r15d, %esi
xorl %eax, %eax
callq 0x20c0
movb 0x5ffb(%rip), %al       # 0x8908
movl %eax, %ecx
notb %cl
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
sete %dl
orb %cl, %dl
testb $0x1, %dl
jne 0x2946
movslq %r15d, %rax
movsd (%r12,%rax,8), %xmm0
divsd -0x38(%rbp), %xmm0
leaq 0x29e1(%rip), %rdi      # 0x5318
movq %rsi, %rdx
movb $0x1, %al
callq 0x20c0
jmp 0x29cb
testb $0x1, %al
movsd -0x38(%rbp), %xmm2
je 0x296c
movslq %r15d, %rax
movsd (%r12,%rax,8), %xmm0
divsd %xmm2, %xmm0
leaq 0x29cf(%rip), %rdi      # 0x5332
movb $0x1, %al
callq 0x20c0
jmp 0x29cb
cmpb $0x1, 0x5f99(%rip)      # 0x890c
jne 0x298a
movslq %r15d, %rax
leaq 0x5b31(%rip), %rcx      # 0x84b0
movq (%rcx,%rax,8), %rdi
callq 0x2070
jmp 0x29cb
cmpb $0x1, 0x5f7f(%rip)      # 0x8910
jne 0x29a0
leaq 0x29a0(%rip), %rdi      # 0x533a
movapd %xmm2, %xmm0
jmp 0x2963
movslq %r15d, %rax
leaq 0x5b06(%rip), %rcx      # 0x84b0
movq (%rcx,%rax,8), %rsi
movsd (%r12,%rax,8), %xmm1
leaq 0x2983(%rip), %rdi      # 0x533e
movapd %xmm2, %xmm0
movsd -0x50(%rbp), %xmm2
movb $0x3, %al
callq 0x20c0
movq %rbx, %rdi
callq 0x20a0
movq -0x48(%rbp), %rdi
callq 0x20a0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2a1c(%rip), %rdi      # 0x5410
callq 0x2070
leaq 0x2a23(%rip), %rdi      # 0x5423
callq 0x2070
leaq 0x2a43(%rip), %rdi      # 0x544f
callq 0x2070
leaq 0x2a67(%rip), %rdi      # 0x547f
callq 0x2070
leaq 0x2a81(%rip), %rdi      # 0x54a5
callq 0x2070
leaq 0x2aa6(%rip), %rdi      # 0x54d6
callq 0x2070
leaq 0x2abe(%rip), %rdi      # 0x54fa
callq 0x2070
jmp 0x2a5f
movq 0x56f6(%rip), %rsi      # 0x8140
movq 0x56f7(%rip), %rdx      # 0x8148
leaq 0x291b(%rip), %rdi      # 0x5373
xorl %eax, %eax
callq 0x20c0
xorl %edi, %edi
callq 0x2210
movq (%rdx), %rsi
leaq 0x27b9(%rip), %rdi      # 0x5229
xorl %eax, %eax
callq 0x20c0
pushq $-0x1
popq %rdi
callq 0x2210
nop