// File: tobias93[P]piano/rtaudio/tests/playsaw.cpp
// Function: main
int main( int argc, char *argv[] )
{
  unsigned int bufferFrames, fs, device = 0, offset = 0;

  // minimal command-line checking
  if (argc < 3 || argc > 6 ) usage();

  RtAudio dac;
  if ( dac.getDeviceCount() < 1 ) {
    std::cout << "\nNo audio devices found!\n";
    exit( 1 );
  }

  channels = (unsigned int) atoi( argv[1] );
  fs = (unsigned int) atoi( argv[2] );
  if ( argc > 3 )
    device = (unsigned int) atoi( argv[3] );
  if ( argc > 4 )
    offset = (unsigned int) atoi( argv[4] );
  if ( argc > 5 )
    nFrames = (unsigned int) (fs * atof( argv[5] ));
  if ( nFrames > 0 ) checkCount = true;

  double *data = (double *) calloc( channels, sizeof( double ) );

  // Let RtAudio print messages to stderr.
  dac.showWarnings( true );

  // Set our stream parameters for output only.
  bufferFrames = 512;
  RtAudio::StreamParameters oParams;
  oParams.deviceId = device;
  oParams.nChannels = channels;
  oParams.firstChannel = offset;

  if ( device == 0 )
    oParams.deviceId = dac.getDefaultOutputDevice();

  options.flags = RTAUDIO_HOG_DEVICE;
  options.flags |= RTAUDIO_SCHEDULE_REALTIME;
#if !defined( USE_INTERLEAVED )
  options.flags |= RTAUDIO_NONINTERLEAVED;
#endif
  try {
    dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options, &errorCallback );
    dac.startStream();
  }
  catch ( RtAudioError& e ) {
    e.printMessage();
    goto cleanup;
  }

  if ( checkCount ) {
    while ( dac.isStreamRunning() == true ) SLEEP( 100 );
  }
  else {
    char input;
    //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
    std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
    std::cin.get( input );

    try {
      // Stop the stream
      dac.stopStream();
    }
    catch ( RtAudioError& e ) {
      e.printMessage();
    }
  }

 cleanup:
  if ( dac.isStreamOpen() ) dac.closeStream();
  free( data );

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x8(%rbp)
jl 0x8993
cmpl $0x6, -0x8(%rbp)
jle 0x8998
callq 0x8690
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x9290
leaq -0x28(%rbp), %rdi
callq 0x8dd0
cmpl $0x1, %eax
jae 0x89e1
movq 0xf610(%rip), %rdi      # 0x17fc8
leaq 0xa7e7(%rip), %rsi      # 0x131a6
callq 0x8210
jmp 0x89c6
movl $0x1, %edi
callq 0x82e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x8cb7
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x83e0
movl %eax, 0xf8a8(%rip)      # 0x1829c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x83e0
movl %eax, -0x18(%rbp)
cmpl $0x3, -0x8(%rbp)
jle 0x8a1a
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x83e0
movl %eax, -0x1c(%rbp)
cmpl $0x4, -0x8(%rbp)
jle 0x8a30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x83e0
movl %eax, -0x20(%rbp)
cmpl $0x5, -0x8(%rbp)
jle 0x8a67
movl -0x18(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x8310
movaps %xmm0, %xmm1
movsd -0x70(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xf871(%rip)      # 0x182d8
cmpl $0x0, 0xf86a(%rip)      # 0x182d8
jbe 0x8a77
movb $0x1, 0xf85d(%rip)      # 0x182d4
movl 0xf81f(%rip), %eax      # 0x1829c
movl %eax, %edi
movl $0x8, %esi
callq 0x8150
movq %rax, -0x40(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x8e10
movl $0x200, -0x14(%rbp)     # imm = 0x200
leaq -0x4c(%rbp), %rdi
callq 0x8e50
jmp 0x8aad
movl -0x1c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl 0xf7e3(%rip), %eax      # 0x1829c
movl %eax, -0x48(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x8ad4
leaq -0x28(%rbp), %rdi
callq 0x8e80
movl %eax, -0x4c(%rbp)
movl $0x4, 0xf7c2(%rip)      # 0x182a0
leaq 0xf7bb(%rip), %rdx      # 0x182a0
movb 0xf7b5(%rip), %al       # 0x182a0
orb $0x8, %al
movb %al, 0xf7ad(%rip)       # 0x182a0
movb 0xf7a7(%rip), %al       # 0x182a0
orb $0x1, %al
movb %al, 0xf79f(%rip)       # 0x182a0
movl -0x18(%rbp), %r8d
movq -0x40(%rbp), %rcx
leaq -0x400(%rip), %rsi      # 0x8710
movq %rsp, %rax
movq %rsi, 0x18(%rax)
movq %rdx, 0x10(%rax)
movq %rcx, 0x8(%rax)
leaq -0x366(%rip), %rcx      # 0x87c0
movq %rcx, (%rax)
xorl %eax, %eax
movl %eax, %edx
leaq -0x28(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl $0x2, %ecx
leaq -0x14(%rbp), %r9
callq 0x94a0
jmp 0x8b45
leaq -0x28(%rbp), %rdi
callq 0x8ec0
jmp 0x8b50
jmp 0x8ba7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8cb7
movq -0x30(%rbp), %rdi
callq 0x80b0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movl $0x2, -0x5c(%rbp)
callq 0x8430
jmp 0x8b96
movl -0x5c(%rbp), %eax
subl $0x2, %eax
je 0x8c73
jmp 0x8ca2
testb $0x1, 0xf726(%rip)      # 0x182d4
je 0x8bd8
jmp 0x8bb2
leaq -0x28(%rbp), %rdi
callq 0x8ee0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8bd3
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x8370
jmp 0x8bd1
jmp 0x8bb2
jmp 0x8c71
movq 0xf3e9(%rip), %rdi      # 0x17fc8
leaq 0xa5da(%rip), %rsi      # 0x131c0
callq 0x8210
movq %rax, -0x78(%rbp)
jmp 0x8bf1
movq -0x78(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x8320
movq %rax, -0x80(%rbp)
jmp 0x8c03
movq -0x80(%rbp), %rdi
leaq 0xa5e5(%rip), %rsi      # 0x131f3
callq 0x8210
jmp 0x8c15
movq 0xf3bc(%rip), %rdi      # 0x17fd8
leaq -0x5d(%rbp), %rsi
callq 0x8350
jmp 0x8c27
leaq -0x28(%rbp), %rdi
callq 0x8f10
jmp 0x8c32
jmp 0x8c6f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8cb7
movq -0x30(%rbp), %rdi
callq 0x80b0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
callq 0x8430
jmp 0x8c6d
jmp 0x8c6f
jmp 0x8c71
jmp 0x8c73
leaq -0x28(%rbp), %rdi
callq 0x8f30
testb $0x1, %al
jne 0x8c82
jmp 0x8c8b
leaq -0x28(%rbp), %rdi
callq 0x8f60
movq -0x40(%rbp), %rdi
callq 0x82c0
movl $0x0, -0x4(%rbp)
movl $0x1, -0x5c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x9460
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x9460
movq -0x30(%rbp), %rdi
callq 0x8460
nopl (%rax)