// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x1b0(%rax), %rcx
movq %rcx, -0x10(%rbp)
cmpl $0x0, 0x1bc(%rax)
jne 0xab58
movq -0x90(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
addq $0x88, %rdi
callq 0x8380
movq -0x10(%rbp), %rax
movb 0x48(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xaaeb
jmp 0xab0f
movq -0x90(%rbp), %rsi
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
addq $0x1a8, %rsi            # imm = 0x1A8
addq $0x88, %rsi
callq 0x83b0
jmp 0xaadc
movq -0x90(%rbp), %rax
cmpl $0x2, 0x1bc(%rax)
je 0xab3e
movq -0x90(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
addq $0x88, %rdi
callq 0x8190
jmp 0xb00b
movq -0x90(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
addq $0x88, %rdi
callq 0x8190
movq -0x90(%rbp), %rax
cmpl $-0x32, 0x1bc(%rax)
jne 0xab95
movq -0x90(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
leaq 0x8bac(%rip), %rsi      # 0x13729
callq 0x8330
movq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x9f60
jmp 0xb00b
movq -0x90(%rbp), %rdi
movq 0x268(%rdi), %rax
movq %rax, -0x18(%rbp)
movq (%rdi), %rax
callq *0x58(%rax)
movq -0x90(%rbp), %r8
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq 0x1c0(%r8), %rdi
movq 0x1c8(%r8), %rsi
movl 0x1e4(%r8), %edx
movsd -0x20(%rbp), %xmm0
movl -0x24(%rbp), %ecx
movq 0x270(%r8), %r8
callq *%rax
movl %eax, -0x28(%rbp)
cmpl $0x2, -0x28(%rbp)
jne 0xac05
movq -0x90(%rbp), %rdi
movq (%rdi), %rax
callq *0x50(%rax)
jmp 0xb00b
movq -0x90(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
addq $0x88, %rdi
callq 0x8380
movq -0x90(%rbp), %rax
testb $0x1, 0x1d9(%rax)
je 0xac46
movq -0x90(%rbp), %rax
movq 0x1d0(%rax), %rax
movq %rax, -0x98(%rbp)
jmp 0xac5b
movq -0x90(%rbp), %rax
movq 0x1c8(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, -0x30(%rbp)
testb $0x1, 0x1d8(%rax)
je 0xac8d
movq -0x90(%rbp), %rax
movq 0x1d0(%rax), %rax
movq %rax, -0xa0(%rbp)
jmp 0xaca2
movq -0x90(%rbp), %rax
movq 0x1c0(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq %rcx, -0x38(%rbp)
cmpl $0x2, 0x1bc(%rax)
je 0xacc2
jmp 0xafd2
movq -0x90(%rbp), %rax
cmpl $0x0, 0x1b8(%rax)
je 0xace6
movq -0x90(%rbp), %rax
cmpl $0x2, 0x1b8(%rax)
jne 0xae3a
movq -0x90(%rbp), %rax
testb $0x1, 0x1d8(%rax)
je 0xad5a
movq -0x90(%rbp), %rdi
movq 0x1d0(%rdi), %rsi
movq 0x1c0(%rdi), %rdx
movq %rdi, %rcx
addq $0x1a8, %rcx            # imm = 0x1A8
addq $0xe8, %rcx
callq 0xb020
movq -0x90(%rbp), %rdi
movl 0x1f4(%rdi), %eax
imull 0x1e4(%rdi), %eax
movl %eax, -0xa4(%rbp)
movq 0x220(%rdi), %rsi
callq 0xa3d0
movl %eax, %ecx
movl -0xa4(%rbp), %eax
imull %ecx, %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0xad91
movq -0x90(%rbp), %rdi
movl 0x1ec(%rdi), %eax
imull 0x1e4(%rdi), %eax
movl %eax, -0xa8(%rbp)
movq 0x218(%rdi), %rsi
callq 0xa3d0
movl %eax, %ecx
movl -0xa8(%rbp), %eax
imull %ecx, %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
callq 0x83e0
cmpl $0x0, %eax
jge 0xae38
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
leaq 0x89b2(%rip), %rsi      # 0x13776
callq 0x8210
movq %rax, -0xb0(%rbp)
movl -0x3c(%rbp), %edi
callq 0x8060
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x8210
movq %rax, %rdi
leaq 0x8def(%rip), %rsi      # 0x13be0
callq 0x8210
movq -0x90(%rbp), %rsi
addq $0x8, %rsi
leaq -0x68(%rbp), %rdi
callq 0x8240
movq -0x90(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
leaq -0x68(%rbp), %rsi
callq 0x8300
leaq -0x68(%rbp), %rdi
callq 0x8140
movq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x9f60
jmp 0xae3a
movq -0x90(%rbp), %rax
cmpl $0x1, 0x1b8(%rax)
je 0xae5e
movq -0x90(%rbp), %rax
cmpl $0x2, 0x1b8(%rax)
jne 0xafd0
movq -0x90(%rbp), %rax
testb $0x1, 0x1d9(%rax)
je 0xaea7
movq -0x90(%rbp), %rdi
movl 0x1f8(%rdi), %eax
imull 0x1e4(%rdi), %eax
movl %eax, -0xb4(%rbp)
movq 0x228(%rdi), %rsi
callq 0xa3d0
movl %eax, %ecx
movl -0xb4(%rbp), %eax
imull %ecx, %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0xaede
movq -0x90(%rbp), %rdi
movl 0x1f0(%rdi), %eax
imull 0x1e4(%rdi), %eax
movl %eax, -0xb8(%rbp)
movq 0x218(%rdi), %rsi
callq 0xa3d0
movl %eax, %ecx
movl -0xb8(%rbp), %eax
imull %ecx, %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
callq 0x8390
cmpl $0x0, %eax
jge 0xaf8f
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
leaq 0x8893(%rip), %rsi      # 0x137a5
callq 0x8210
movq %rax, -0xc0(%rbp)
movl -0x3c(%rbp), %edi
callq 0x8060
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x8210
movq %rax, %rdi
leaq 0x8ca1(%rip), %rsi      # 0x13be0
callq 0x8210
movq -0x90(%rbp), %rsi
addq $0x8, %rsi
leaq -0x88(%rbp), %rdi
callq 0x8240
movq -0x90(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
leaq -0x88(%rbp), %rsi
callq 0x8300
leaq -0x88(%rbp), %rdi
callq 0x8140
movq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x9f60
movq -0x90(%rbp), %rax
testb $0x1, 0x1d9(%rax)
je 0xafce
movq -0x90(%rbp), %rdi
movq 0x1c8(%rdi), %rsi
movq 0x1d0(%rdi), %rdx
movq %rdi, %rcx
addq $0x1a8, %rcx            # imm = 0x1A8
addq $0xe8, %rcx
addq $0x50, %rcx
callq 0xb020
jmp 0xafd0
jmp 0xafd2
movq -0x90(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
addq $0x88, %rdi
callq 0x8190
movq -0x90(%rbp), %rdi
callq 0xa4e0
cmpl $0x1, -0x28(%rbp)
jne 0xb00b
movq -0x90(%rbp), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)