// File: /workspace/github_cpp_makefile_star2_O1/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, %r15d
movl %ecx, %ebx
movl %edx, %r13d
movl %esi, %r12d
movq %rdi, -0x48(%rbp)
leal -0x8(%r15), %eax
roll $0x1d, %eax
cmpl $0x3, %eax
ja 0x9ab4
leaq 0x4cc9(%rip), %rcx      # 0xe750
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x34(%rbp)
jmp 0x9abb
movl $0x9, -0x34(%rbp)
jmp 0x9abb
movl $0x7, -0x34(%rbp)
jmp 0x9abb
movl $0x3, -0x34(%rbp)
jmp 0x9abb
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
leaq -0x34(%rbp), %r9
movb %r13b, 0x8(%r9)
movl %ebx, 0x4(%r9)
subq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x4c7e(%rip), %rsi      # 0xe760
leaq 0x4c80(%rip), %r8       # 0xe769
xorl %edi, %edi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x3320
addq $0x20, %rsp
testq %rax, %rax
je 0x9bf4
movq %rax, %r14
movq %rsp, -0x40(%rbp)
movzwl %r13w, %ecx
movl %r15d, %eax
imull %ecx, %eax
shrl $0x3, %eax
movzwl %r12w, %edx
movzwl %ax, %r12d
imull %edx, %r12d
leaq 0xf(%r12), %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
cmpb $0x0, 0xc0bd(%rip)      # 0x15bfa
jne 0x9be6
imull $0x3e8, %r12d, %eax     # imm = 0x3E8
imull %ebx, %ecx
imull %r15d, %ecx
shrl $0x3, %ecx
xorl %edx, %edx
divl %ecx
imull $0x3e8, %eax, %r15d     # imm = 0x3E8
movq 0x945a(%rip), %rbx      # 0x12fc0
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x31d0
movq -0x48(%rbp), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x99ae
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq -0x4c(%rbp), %rcx
callq 0x3530
testl %eax, %eax
js 0x9ba2
movl %r15d, %edi
callq 0x3470
jmp 0x9bdd
movl $0x11, %edx
movq %rbx, %rdi
leaq 0x4be0(%rip), %rsi      # 0xe791
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
cmpb $0x0, 0xc016(%rip)      # 0x15bfa
je 0x9b66
movq %r14, %rdi
callq 0x3560
movq -0x40(%rbp), %rsp
jmp 0x9c36
movq 0x93c5(%rip), %rbx      # 0x12fc0
leaq 0x4b6d(%rip), %rsi      # 0xe76f
movl $0x21, %edx
movq %rbx, %rdi
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)