// File: tobias93[P]piano/rtaudio/tests/record.cpp
// Function: main
int main( int argc, char *argv[] )
{
  unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
  double time = 2.0;
  FILE *fd;

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

  RtAudio adc;
  if ( adc.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 )
    time = (double) atof( argv[3] );
  if ( argc > 4 )
    device = (unsigned int) atoi( argv[4] );
  if ( argc > 5 )
    offset = (unsigned int) atoi( argv[5] );

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

  // Set our stream parameters for input only.
  bufferFrames = 512;
  RtAudio::StreamParameters iParams;
  if ( device == 0 )
    iParams.deviceId = adc.getDefaultInputDevice();
  else
    iParams.deviceId = device;
  iParams.nChannels = channels;
  iParams.firstChannel = offset;

  InputData data;
  data.buffer = 0;
  try {
    adc.openStream( NULL, &iParams, FORMAT, fs, &bufferFrames, &input, (void *)&data );
  }
  catch ( RtAudioError& e ) {
    std::cout << '\n' << e.getMessage() << '\n' << std::endl;
    goto cleanup;
  }

  data.bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
  data.totalFrames = (unsigned long) (fs * time);
  data.frameCounter = 0;
  data.channels = channels;
  unsigned long totalBytes;
  totalBytes = data.totalFrames * channels * sizeof( MY_TYPE );

  // Allocate the entire data buffer before starting stream.
  data.buffer = (MY_TYPE*) malloc( totalBytes );
  if ( data.buffer == 0 ) {
    std::cout << "Memory allocation error ... quitting!\n";
    goto cleanup;
  }

  try {
    adc.startStream();
  }
  catch ( RtAudioError& e ) {
    std::cout << '\n' << e.getMessage() << '\n' << std::endl;
    goto cleanup;
  }

  std::cout << "\nRecording for " << time << " seconds ... writing file 'record.raw' (buffer frames = " << bufferFrames << ")." << std::endl;
  while ( adc.isStreamRunning() ) {
    SLEEP( 100 ); // wake every 100 ms to check if we're done
  }

  // Now write the entire data to the file.
  fd = fopen( "record.raw", "wb" );
  fwrite( data.buffer, sizeof( MY_TYPE ), data.totalFrames * channels, fd );
  fclose( fd );

 cleanup:
  if ( adc.isStreamOpen() ) adc.closeStream();
  if ( data.buffer ) free( data.buffer );

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movsd 0xa7b9(%rip), %xmm0     # 0x13008
movsd %xmm0, -0x30(%rbp)
cmpl $0x3, -0x8(%rbp)
jl 0x8860
cmpl $0x6, -0x8(%rbp)
jle 0x8865
callq 0x86b0
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x9110
leaq -0x40(%rbp), %rdi
callq 0x8d90
cmpl $0x1, %eax
jae 0x88ae
movq 0xf74b(%rip), %rdi      # 0x17fd0
leaq 0xa8ce(%rip), %rsi      # 0x1315a
callq 0x8220
jmp 0x8893
movl $0x1, %edi
callq 0x8310
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8d68
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x8410
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x8410
movl %eax, -0x18(%rbp)
cmpl $0x3, -0x8(%rbp)
jle 0x88e6
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x8340
movsd %xmm0, -0x30(%rbp)
cmpl $0x4, -0x8(%rbp)
jle 0x88fc
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x8410
movl %eax, -0x20(%rbp)
cmpl $0x5, -0x8(%rbp)
jle 0x8912
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x8410
movl %eax, -0x24(%rbp)
leaq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x8dd0
movl $0x200, -0x1c(%rbp)     # imm = 0x200
leaq -0x58(%rbp), %rdi
callq 0x8e10
jmp 0x8932
cmpl $0x0, -0x20(%rbp)
jne 0x8946
leaq -0x40(%rbp), %rdi
callq 0x8e40
movl %eax, -0x58(%rbp)
jmp 0x894c
movl -0x20(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x50(%rbp)
movq $0x0, -0x80(%rbp)
movl -0x18(%rbp), %r8d
movq %rsp, %rax
leaq -0x80(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x246(%rip), %rcx      # 0x8730
movq %rcx, (%rax)
movq $0x0, 0x18(%rax)
movq $0x0, 0x10(%rax)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rdx
movl $0x2, %ecx
leaq -0x1c(%rbp), %r9
callq 0x9320
jmp 0x89a5
jmp 0x8a84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8d68
movq -0x48(%rbp), %rdi
callq 0x80b0
movq %rax, -0x88(%rbp)
movq 0xf5f3(%rip), %rdi      # 0x17fd0
movl $0xa, %esi
callq 0x82c0
movq %rax, -0xa8(%rbp)
jmp 0x89f0
movq -0x88(%rbp), %rdi
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0x81f0
movq %rax, -0xb0(%rbp)
jmp 0x8a18
movq -0xb0(%rbp), %rdi
movl $0xa, %esi
callq 0x82c0
movq %rax, -0xb8(%rbp)
jmp 0x8a32
movq -0xb8(%rbp), %rdi
movq 0xf578(%rip), %rsi      # 0x17fb8
callq 0x8270
jmp 0x8a47
movl $0x2, -0x8c(%rbp)
callq 0x8460
jmp 0x8a6b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x8460
jmp 0x8a7f
movl -0x8c(%rbp), %eax
subl $0x2, %eax
je 0x8d1a
jmp 0x8d53
jmp 0x8d68
movl -0x1c(%rbp), %eax
movl -0x14(%rbp), %ecx
imull %ecx, %eax
addq %rax, %rax
movq %rax, -0x78(%rbp)
movl -0x18(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd -0x30(%rbp), %xmm1
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0xa554(%rip), %xmm1     # 0x13010
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x70(%rbp), %rax
movl -0x14(%rbp), %ecx
imulq %rcx, %rax
shlq %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x8350
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x8b23
movq 0xf4c0(%rip), %rdi      # 0x17fd0
leaq 0xa65d(%rip), %rsi      # 0x13174
callq 0x8220
jmp 0x8b1e
jmp 0x8d1a
leaq -0x40(%rbp), %rdi
callq 0x8e90
jmp 0x8b2e
jmp 0x8c0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8d68
movq -0x48(%rbp), %rdi
callq 0x80b0
movq %rax, -0xa0(%rbp)
movq 0xf46a(%rip), %rdi      # 0x17fd0
movl $0xa, %esi
callq 0x82c0
movq %rax, -0xc0(%rbp)
jmp 0x8b79
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x81f0
movq %rax, -0xc8(%rbp)
jmp 0x8ba1
movq -0xc8(%rbp), %rdi
movl $0xa, %esi
callq 0x82c0
movq %rax, -0xd0(%rbp)
jmp 0x8bbb
movq -0xd0(%rbp), %rdi
movq 0xf3ef(%rip), %rsi      # 0x17fb8
callq 0x8270
jmp 0x8bd0
movl $0x2, -0x8c(%rbp)
callq 0x8460
jmp 0x8bf4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x8460
jmp 0x8c08
movl -0x8c(%rbp), %eax
subl $0x2, %eax
je 0x8d1a
jmp 0x8d53
jmp 0x8d68
movq 0xf3bc(%rip), %rdi      # 0x17fd0
leaq 0xa580(%rip), %rsi      # 0x1319b
callq 0x8220
movq %rax, -0xd8(%rbp)
jmp 0x8c29
movq -0xd8(%rbp), %rdi
movsd -0x30(%rbp), %xmm0
callq 0x84f0
movq %rax, -0xe0(%rbp)
jmp 0x8c43
movq -0xe0(%rbp), %rdi
leaq 0xa55a(%rip), %rsi      # 0x131ab
callq 0x8220
movq %rax, -0xe8(%rbp)
jmp 0x8c5f
movq -0xe8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x8360
movq %rax, -0xf0(%rbp)
jmp 0x8c77
movq -0xf0(%rbp), %rdi
leaq 0xa55f(%rip), %rsi      # 0x131e4
callq 0x8220
movq %rax, -0xf8(%rbp)
jmp 0x8c93
movq -0xf8(%rbp), %rdi
movq 0xf317(%rip), %rsi      # 0x17fb8
callq 0x8270
jmp 0x8ca8
jmp 0x8caa
leaq -0x40(%rbp), %rdi
callq 0x8eb0
testb $0x1, %al
jne 0x8cb9
jmp 0x8cc7
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x83a0
jmp 0x8cc5
jmp 0x8caa
leaq 0xa519(%rip), %rdi      # 0x131e7
leaq 0xa51d(%rip), %rsi      # 0x131f2
callq 0x82e0
movq %rax, -0x100(%rbp)
jmp 0x8ce3
movq -0x100(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x80(%rbp), %rdi
movq -0x70(%rbp), %rdx
movl -0x14(%rbp), %eax
imulq %rax, %rdx
movq -0x38(%rbp), %rcx
movl $0x2, %esi
callq 0x8510
jmp 0x8d0d
movq -0x38(%rbp), %rdi
callq 0x8230
jmp 0x8d18
jmp 0x8d1a
leaq -0x40(%rbp), %rdi
callq 0x8ee0
testb $0x1, %al
jne 0x8d29
jmp 0x8d32
leaq -0x40(%rbp), %rdi
callq 0x8f10
cmpq $0x0, -0x80(%rbp)
je 0x8d42
movq -0x80(%rbp), %rdi
callq 0x82f0
movl $0x0, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x92e0
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x92e0
movq -0x48(%rbp), %rdi
callq 0x8490
movq %rax, %rdi
callq 0x8e80
nopw %cs:(%rax,%rax)
nopl (%rax)