// File: /workspace/github_cpp_makefile_star2_O2/tobias93[P]piano/rtaudio/RtAudio.cpp
// Function: RtApiPulse::callbackEvent()
void RtApiPulse::callbackEvent( void )
{
  PulseAudioHandle *pah = static_cast<PulseAudioHandle *>( stream_.apiHandle );

  if ( stream_.state == STREAM_STOPPED ) {
    MUTEX_LOCK( &stream_.mutex );
    while ( !pah->runnable )
      pthread_cond_wait( &pah->runnable_cv, &stream_.mutex );

    if ( stream_.state != STREAM_RUNNING ) {
      MUTEX_UNLOCK( &stream_.mutex );
      return;
    }
    MUTEX_UNLOCK( &stream_.mutex );
  }

  if ( stream_.state == STREAM_CLOSED ) {
    errorText_ = "RtApiPulse::callbackEvent(): the stream is closed ... "
      "this shouldn't happen!";
    error( RtAudioError::WARNING );
    return;
  }

  RtAudioCallback callback = (RtAudioCallback) stream_.callbackInfo.callback;
  double streamTime = getStreamTime();
  RtAudioStreamStatus status = 0;
  int doStopStream = callback( stream_.userBuffer[OUTPUT], stream_.userBuffer[INPUT],
                               stream_.bufferSize, streamTime, status,
                               stream_.callbackInfo.userData );

  if ( doStopStream == 2 ) {
    abortStream();
    return;
  }

  MUTEX_LOCK( &stream_.mutex );
  void *pulse_in = stream_.doConvertBuffer[INPUT] ? stream_.deviceBuffer : stream_.userBuffer[INPUT];
  void *pulse_out = stream_.doConvertBuffer[OUTPUT] ? stream_.deviceBuffer : stream_.userBuffer[OUTPUT];

  if ( stream_.state != STREAM_RUNNING )
    goto unlock;

  int pa_error;
  size_t bytes;
  if (stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
    if ( stream_.doConvertBuffer[OUTPUT] ) {
        convertBuffer( stream_.deviceBuffer,
                       stream_.userBuffer[OUTPUT],
                       stream_.convertInfo[OUTPUT] );
        bytes = stream_.nDeviceChannels[OUTPUT] * stream_.bufferSize *
                formatBytes( stream_.deviceFormat[OUTPUT] );
    } else
        bytes = stream_.nUserChannels[OUTPUT] * stream_.bufferSize *
                formatBytes( stream_.userFormat );

    if ( pa_simple_write( pah->s_play, pulse_out, bytes, &pa_error ) < 0 ) {
      errorStream_ << "RtApiPulse::callbackEvent: audio write error, " <<
        pa_strerror( pa_error ) << ".";
      errorText_ = errorStream_.str();
      error( RtAudioError::WARNING );
    }
  }

  if ( stream_.mode == INPUT || stream_.mode == DUPLEX) {
    if ( stream_.doConvertBuffer[INPUT] )
      bytes = stream_.nDeviceChannels[INPUT] * stream_.bufferSize *
        formatBytes( stream_.deviceFormat[INPUT] );
    else
      bytes = stream_.nUserChannels[INPUT] * stream_.bufferSize *
        formatBytes( stream_.userFormat );
            
    if ( pa_simple_read( pah->s_rec, pulse_in, bytes, &pa_error ) < 0 ) {
      errorStream_ << "RtApiPulse::callbackEvent: audio read error, " <<
        pa_strerror( pa_error ) << ".";
      errorText_ = errorStream_.str();
      error( RtAudioError::WARNING );
    }
    if ( stream_.doConvertBuffer[INPUT] ) {
      convertBuffer( stream_.userBuffer[INPUT],
                     stream_.deviceBuffer,
                     stream_.convertInfo[INPUT] );
    }
  }

 unlock:
  MUTEX_UNLOCK( &stream_.mutex );
  RtApi::tickStreamTime();

  if ( doStopStream == 1 )
    stopStream();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x1b0(%rdi), %rbp
movl 0x1bc(%rdi), %eax
testl %eax, %eax
jne 0x598f
leaq 0x230(%rbx), %r14
movq %r14, %rdi
callq 0x42a0
leaq 0x18(%rbp), %r15
cmpb $0x0, 0x48(%rbp)
jne 0x5970
movq %r15, %rdi
movq %r14, %rsi
callq 0x42e0
jmp 0x595d
movl 0x1bc(%rbx), %r15d
movq %r14, %rdi
callq 0x4150
cmpl $0x2, %r15d
jne 0x5c3d
movl 0x1bc(%rbx), %eax
cmpl $-0x32, %eax
jne 0x59bf
leaq 0x180(%rbx), %rdi
leaq 0x3e77(%rip), %rsi      # 0x9819
callq 0x43e0
movq %rbx, %rdi
xorl %esi, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x532a
movq 0x268(%rbx), %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x58(%rax)
movq 0x1c0(%rbx), %rdi
movq 0x1c8(%rbx), %rsi
movl 0x1e4(%rbx), %edx
movq 0x270(%rbx), %r8
xorl %ecx, %ecx
callq *%r14
cmpl $0x2, %eax
jne 0x5a0b
movq (%rbx), %rax
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x50(%rax)
movl %eax, %r15d
leaq 0x230(%rbx), %r14
movq %r14, %rdi
callq 0x42a0
movq 0x1d0(%rbx), %rsi
cmpb $0x0, 0x1d9(%rbx)
movq %rsi, %r13
jne 0x5a37
movq 0x1c8(%rbx), %r13
movb 0x1d8(%rbx), %al
movq 0x1c0(%rbx), %rdx
testb %al, %al
movq %rdx, %r12
cmovneq %rsi, %r12
cmpl $0x2, 0x1bc(%rbx)
jne 0x5c1e
testl $0xfffffffd, 0x1b8(%rbx) # imm = 0xFFFFFFFD
jne 0x5b39
testb %al, %al
movq %r13, 0x10(%rsp)
je 0x5a8e
leaq 0x290(%rbx), %rcx
movq %rbx, %rdi
callq 0x5c4c
movl $0x220, %eax            # imm = 0x220
movl $0x1f4, %ecx            # imm = 0x1F4
jmp 0x5a98
movl $0x218, %eax            # imm = 0x218
movl $0x1ec, %ecx            # imm = 0x1EC
movl 0x1e4(%rbx), %r13d
imull (%rbx,%rcx), %r13d
movq (%rbx,%rax), %rsi
movq %rbx, %rdi
callq 0x5564
imull %eax, %r13d
movq (%rbp), %rdi
leaq 0xc(%rsp), %rcx
movq %r12, %rsi
movq %r13, %rdx
callq 0x4310
testl %eax, %eax
movq 0x10(%rsp), %r13
jns 0x5b39
leaq 0x8(%rbx), %rdi
leaq 0x3d8a(%rip), %rsi      # 0x9866
callq 0x41c0
movq %rax, %r12
movl 0xc(%rsp), %edi
callq 0x4040
movq %r12, %rdi
movq %rax, %rsi
callq 0x41c0
leaq 0x4190(%rip), %rsi      # 0x9c8f
movq %rax, %rdi
callq 0x41c0
leaq 0x10(%rbx), %rsi
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x4320
leaq 0x180(%rbx), %rdi
movq %r12, %rsi
callq 0x4250
movq %r12, %rdi
callq 0x40c0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x532a
movl 0x1b8(%rbx), %eax
decl %eax
cmpl $0x1, %eax
ja 0x5c1e
movzbl 0x1d9(%rbx), %eax
movl 0x1e4(%rbx), %r12d
imull 0x1f0(%rbx,%rax,8), %r12d
shll $0x4, %eax
movq 0x218(%rax,%rbx), %rsi
movq %rbx, %rdi
callq 0x5564
imull %eax, %r12d
movq 0x8(%rbp), %rdi
leaq 0xc(%rsp), %rcx
movq %r13, %rsi
movq %r12, %rdx
callq 0x42b0
testl %eax, %eax
jns 0x5bf8
leaq 0x8(%rbx), %rdi
leaq 0x3cfa(%rip), %rsi      # 0x9895
callq 0x41c0
movq %rax, %r12
movl 0xc(%rsp), %edi
callq 0x4040
movq %r12, %rdi
movq %rax, %rsi
callq 0x41c0
leaq 0x40d1(%rip), %rsi      # 0x9c8f
movq %rax, %rdi
callq 0x41c0
leaq 0x10(%rbx), %rsi
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x4320
leaq 0x180(%rbx), %rdi
movq %r12, %rsi
callq 0x4250
movq %r12, %rdi
callq 0x40c0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x532a
cmpb $0x1, 0x1d9(%rbx)
jne 0x5c1e
movq 0x1c8(%rbx), %rsi
movq 0x1d0(%rbx), %rdx
leaq 0x2e0(%rbx), %rcx
movq %rbx, %rdi
callq 0x5c4c
movq %r14, %rdi
callq 0x4150
movq %rbx, %rdi
callq 0x55dc
cmpl $0x1, %r15d
jne 0x5c3d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x48(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq