// File: /workspace/github_cpp_makefile_star2_O2/fgheng[P]keysound/src/Play.cc
// Function: play(Mixer*, unsigned short, unsigned short, unsigned int, unsigned short)
void play(Mixer *mixer, uint16_t frame_num, uint16_t channels,
        uint32_t sample_rate, uint16_t bits_per_sample) {

    pa_simple *s;
    pa_sample_spec ss;

    switch (bits_per_sample) {
        case 8:
            ss.format = PA_SAMPLE_U8;
            break;
        case 16:
            // wav的数据是小字端存储的		The data of wav is stored in small samples
            ss.format = PA_SAMPLE_S16LE;
            break;
        case 24:
            // 有符号?				Signed?
            ss.format = PA_SAMPLE_S24LE;
            break;
        case 32:
            // 有符号?				Signed?
            ss.format = PA_SAMPLE_S32LE;
            break;
        default:
            ss.format = PA_SAMPLE_INVALID;
            break;
    }
    ss.channels = channels;
    ss.rate = sample_rate;

    s = pa_simple_new(NULL, "keysound", PA_STREAM_PLAYBACK,
                    NULL, "music", &ss, NULL, NULL, NULL);

    if (!s) {
        std::cout << "error occured while connect audio" << std::endl;
        return;
    }

    // 每个frame拥有的字节数		The number of bytes in each frame
    uint16_t frame_bytes = channels * bits_per_sample / 8;
    // 一个buffer持续的时间		The duration of a buffer
    uint32_t sleep_time = (frame_num * frame_bytes * 1000) /
        (sample_rate * channels * bits_per_sample / 8);

    int error, ret;
    uint8_t buf[frame_num * frame_bytes];
    while (!stop) {
        std::memset(buf, 0, frame_num * frame_bytes);
        mixer->get_mix(buf, frame_num * frame_bytes);
        ret = pa_simple_write(s, buf, frame_num * frame_bytes, &error);
        if (ret < 0) {
            std::cout << "pulse write error" << std::endl;
            continue;
        }

        usleep(sleep_time*1000);
    }

    pa_simple_free(s);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %r12d
movl %ecx, %r15d
movl %edx, %r13d
movl %esi, %ebx
movq %rdi, -0x48(%rbp)
leal -0x8(%r12), %eax
roll $0x1d, %eax
cmpl $0x3, %eax
ja 0x869e
leaq 0x41a4(%rip), %rcx      # 0xc818
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
andl $0x0, -0x34(%rbp)
jmp 0x86a2
movl $0x9, -0x34(%rbp)
jmp 0x86a2
movl $0x7, -0x34(%rbp)
jmp 0x86a2
movl $0x3, -0x34(%rbp)
jmp 0x86a2
orl $-0x1, -0x34(%rbp)
leaq -0x34(%rbp), %r9
movb %r13b, 0x8(%r9)
movl %r15d, 0x4(%r9)
subq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x4162(%rip), %rsi      # 0xc828
leaq 0x4164(%rip), %r8       # 0xc831
pushq $0x1
popq %rdx
xorl %edi, %edi
xorl %ecx, %ecx
callq 0x32e0
addq $0x20, %rsp
testq %rax, %rax
je 0x87a8
movq %rax, %r14
movq %rsp, -0x40(%rbp)
movzwl %r13w, %ecx
imull %r12d, %r15d
imull %ecx, %r12d
shrl $0x3, %r12d
movzwl %bx, %eax
movzwl %r12w, %r12d
imull %eax, %r12d
imull $0x3e8, %r12d, %eax     # imm = 0x3E8
imull %ecx, %r15d
shrl $0x3, %r15d
xorl %edx, %edx
divl %r15d
leaq 0xf(%r12), %rcx
andq $-0x10, %rcx
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
imull $0x3e8, %eax, %r13d     # imm = 0x3E8
leaq -0x4c(%rbp), %rbx
cmpb $0x0, 0xc49a(%rip)      # 0x14bda
jne 0x879a
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x31b0
movq -0x48(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x8572
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x34d0
testl %eax, %eax
js 0x877d
movl %r13d, %edi
callq 0x3420
jmp 0x8739
movq 0x9834(%rip), %rdi      # 0x11fb8
leaq 0x40ce(%rip), %rsi      # 0xc859
callq 0x3310
movq %rax, %rdi
callq 0x3180
jmp 0x8739
movq %r14, %rdi
callq 0x3500
movq -0x40(%rbp), %rsp
jmp 0x87c3
movq 0x9809(%rip), %rdi      # 0x11fb8
leaq 0x4081(%rip), %rsi      # 0xc837
callq 0x3310
movq %rax, %rdi
callq 0x3180
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop