// File: fulhax[P]ncpamixer/src/pa.cpp
// Function: Pa::read_callback(pa_stream*, unsigned long, void*)
void Pa::read_callback(pa_stream *s, size_t length, void *instance)
{
    Pa *pa = reinterpret_cast<Pa *>(instance);
    std::lock_guard<std::mutex> lk(pa->inputMtx);
    const void *data;
    float v;

    if (pa_stream_peek(s, &data, &length) < 0) {
        return;
    }

    if (!data) {
        /*  NULL data means either a hole or empty buffer.
            Only drop the stream when there is a hole (length > 0) */
        if (length) {
            pa_stream_drop(s);
        }

        return;
    }

    assert(length > 0);
    assert(length % sizeof(float) == 0);

    v = ((const float *) data)[length / sizeof(float) - 1];

    pa_stream_drop(s);

    if (v < 0) {
        v = 0;
    }

    if (v > 1) {
        v = 1;
    }

    uint32_t input_idx = pa_stream_get_monitor_stream(s);

    if (input_idx != PA_INVALID_INDEX && pa->PA_INPUTS.find(input_idx) != pa->PA_INPUTS.end()) {
        pa->PA_INPUTS[input_idx]->peak = v;
    } else {
        pa->updatePeakByDeviceId(pa_stream_get_device_index(s), v);
    }

    pa->notify_update();
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rsi
addq $0x100, %rsi            # imm = 0x100
leaq 0x70(%rsp), %rdi
callq 0xd180
movq 0x90(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x73f0
movl %eax, 0x34(%rsp)
jmp 0xcad2
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
jge 0xcb05
movl $0x1, 0x50(%rsp)
jmp 0xcc99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x70(%rsp), %rdi
callq 0xd290
jmp 0xccab
cmpq $0x0, 0x68(%rsp)
jne 0xcb36
cmpq $0x0, 0x88(%rsp)
je 0xcb29
movq 0x90(%rsp), %rdi
callq 0x7ab0
jmp 0xcb27
jmp 0xcb29
movl $0x1, 0x50(%rsp)
jmp 0xcc99
movq 0x68(%rsp), %rcx
movq 0x88(%rsp), %rax
andq $-0x4, %rax
movss -0x4(%rax,%rcx), %xmm0
movss %xmm0, 0x64(%rsp)
movq 0x90(%rsp), %rdi
callq 0x7ab0
jmp 0xcb62
xorps %xmm0, %xmm0
ucomiss 0x64(%rsp), %xmm0
jbe 0xcb75
xorps %xmm0, %xmm0
movss %xmm0, 0x64(%rsp)
movss 0x64(%rsp), %xmm0
movss 0x1370d(%rip), %xmm1    # 0x20290
ucomiss %xmm1, %xmm0
jbe 0xcb96
movss 0x13700(%rip), %xmm0    # 0x20290
movss %xmm0, 0x64(%rsp)
movq 0x90(%rsp), %rdi
callq 0x72c0
movl %eax, 0x30(%rsp)
jmp 0xcba9
movl 0x30(%rsp), %eax
movl %eax, 0x4c(%rsp)
xorl %eax, %eax
cmpl $-0x1, 0x4c(%rsp)
movb %al, 0x2f(%rsp)
je 0xcc08
movq 0x78(%rsp), %rdi
addq $0x8, %rdi
leaq 0x4c(%rsp), %rsi
callq 0xd330
movq %rax, 0x20(%rsp)
jmp 0xcbd8
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x78(%rsp), %rdi
addq $0x8, %rdi
callq 0xd200
movq %rax, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xd1e0
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0xcc12
jmp 0xcc50
movss 0x64(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movq 0x78(%rsp), %rdi
addq $0x8, %rdi
leaq 0x4c(%rsp), %rsi
callq 0xd410
movq %rax, 0x18(%rsp)
jmp 0xcc38
movss 0x14(%rsp), %xmm0
movq 0x18(%rsp), %rax
movq (%rax), %rax
movss %xmm0, 0x228(%rax)
jmp 0xcc85
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %rdi
callq 0x7140
movl %eax, 0x10(%rsp)
jmp 0xcc6d
movl 0x10(%rsp), %esi
movq 0x8(%rsp), %rdi
movss 0x64(%rsp), %xmm0
callq 0xccc0
jmp 0xcc83
jmp 0xcc85
movq 0x78(%rsp), %rdi
callq 0xbe10
jmp 0xcc91
movl $0x0, 0x50(%rsp)
leaq 0x70(%rsp), %rdi
callq 0xd290
addq $0x98, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x7c40
nopw %cs:(%rax,%rax)