// File: /workspace/github_cpp_makefile_star2_O2/mikeakohn[P]drumsplusplus/build/../src/Song.cxx
// Function: Song::add_beats(Tokens*, Song::Beat*, int&, int, int)
int Song::add_beats(
  Tokens *tokens,
  Beat *beats,
  int &ptr,
  int i,
  int midi_channel)
{
  int token_type;
  char token[1024];
  int m, modifier, c;

  // This will read an entire line of beats from a pattern for
  // a single instrument in that pattern.

  modifier = 0;
  c = 0;

  token_type = tokens->get(token);

  // Allows a different channel number to be used (another instrument
  // other than drums.
  if (strcmp(token, "/") == 0)
  {
    token_type = tokens->get(token);

    if (token_type != TOKEN_NUMBER)
    {
      print_error(tokens, "Channel number", token);
      return -1;
    }

    midi_channel = atoi(token);

    if (midi_channel > 15)
    {
      printf("Warning: MIDI channel %d is higher than the max 15; line %d\n",
        midi_channel, tokens->get_line());
      midi_channel = 9;
    }

    token_type = tokens->get(token);
  }

  if (strcmp(token, ":") != 0)
  {
    print_error(tokens, ":", token);
    return -1;
  }

  while(1)
  {
    token_type = tokens->get(token);

    if (strcmp(token, ";") == 0) return 0;

    if (strcmp(token, ":") == 0)
    {
      if (c == 0)
      {
        printf("Error: Unexpected token '%s' at %s:%d.\n",
          token, tokens->get_filename(), tokens->get_line());
        return -1;
      }

      token_type = tokens->get(token);

      if (strcmp(token, "%") == 0)
      {
        // Allow the volume of the drum to drift.
        srand(time(NULL));
        modifier = rand() % song_info.drift;
        if ((rand() % 2) == 0) { modifier = -modifier; }
        token_type = tokens->get(token);
      }

      if (token_type != TOKEN_NUMBER)
      {
        print_error(tokens, "volume integer", token);
        return -1;
      }

      m = (int)atof(token);

      if (m > 127)
      {
        printf("Warning: Volume is higher than 127 on line %d.\n",
          tokens->get_line());
      }

      m = m + modifier;

      if (m < 0) { m = 0; }
      if (m > 127) { m = 127; }

      // FIXME: This is a bad way to do this.  Would be better to check
      // if a : exist after reading the beat values.
      beats[ptr - 1].volume = m;
      continue;
    }

    if (token_type != TOKEN_NUMBER)
    {
      print_error(tokens, "a beat number", token);
      continue;
    }

    beats[ptr].value = atof(token);
    beats[ptr].instrument = i;
    beats[ptr].channel = midi_channel;

    if (beats[ptr].value < 1)
    {
      printf("Error: Beat is less than 1 at %s:%d.  Ignoring.\n",
        tokens->get_filename(), tokens->get_line());
      beats[ptr].value = 0;
    }

    if (beats[ptr].value >= song_info.time_signature_beats + 1)
    {
      printf("Error: Beat exceeds beats per measure at %s:%d.  Ignoring.\n",
        tokens->get_filename(), tokens->get_line());
      beats[ptr].value = 0;
    }

    beats[ptr].volume = song_info.default_volume;

    ptr++;
    c++;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %r9d, %ebp
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
leaq 0x20(%rsp), %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x2828
movzwl (%r12), %eax
cmpw $0x2f, %ax
jne 0x4acc
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x2828
cmpl $0x2, %eax
jne 0x4cd7
leaq 0x20(%rsp), %rdi
callq 0x22f0
movl %eax, %ebp
cmpl $0x10, %eax
jl 0x4ab7
movl 0x2c(%rbx), %edx
leaq 0x2ae1(%rip), %rdi      # 0x758c
movl %ebp, %esi
xorl %eax, %eax
callq 0x2050
pushq $0x9
popq %rbp
leaq 0x20(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2828
movzwl (%r12), %eax
cmpw $0x3a, %ax
jne 0x4cca
movl %ebp, 0x14(%rsp)
leaq -0x4(%r15), %rax
movq %rax, 0x18(%rsp)
leaq 0x20(%rsp), %rbp
leaq 0x2b41(%rip), %r12      # 0x7630
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2828
movzwl 0x20(%rsp), %ecx
cmpl $0x3b, %ecx
je 0x4cd3
cmpl $0x3a, %ecx
je 0x4b35
cmpl $0x2, %eax
je 0x4bfa
movq %rbx, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x49e6
jmp 0x4afe
cmpl $0x0, 0x4(%rsp)
je 0x4d00
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2828
cmpw $0x25, 0x20(%rsp)
movl %r13d, 0x10(%rsp)
jne 0x4b95
xorl %edi, %edi
callq 0x2170
movl %eax, %edi
callq 0x2190
callq 0x2070
cltd
movq 0x8(%rsp), %rcx
idivl 0x28(%rcx)
movl %edx, %r13d
callq 0x2070
movl %r13d, %ecx
negl %ecx
testb $0x1, %al
cmovnel %r13d, %ecx
movl %ecx, (%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2828
cmpl $0x2, %eax
jne 0x4d1c
movq %rbp, %rdi
callq 0x2250
cvttsd2si %xmm0, %r13d
cmpl $0x80, %r13d
jl 0x4bc5
movl 0x2c(%rbx), %esi
leaq 0x2a42(%rip), %rdi      # 0x7600
xorl %eax, %eax
callq 0x2050
addl (%rsp), %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
cmpl $0x7f, %r13d
pushq $0x7f
popq %rax
cmovgel %eax, %r13d
movslq (%r14), %rax
shlq $0x4, %rax
movq 0x18(%rsp), %rcx
movb %r13b, (%rcx,%rax)
movl 0x10(%rsp), %r13d
jmp 0x4afe
movq %rbp, %rdi
callq 0x2250
cvtsd2ss %xmm0, %xmm0
movslq (%r14), %rax
shlq $0x4, %rax
movss %xmm0, (%r15,%rax)
movl %r13d, 0x4(%r15,%rax)
movslq (%r14), %rax
shlq $0x4, %rax
movl 0x14(%rsp), %ecx
movl %ecx, 0x8(%r15,%rax)
movslq (%r14), %rax
movq %rax, %rcx
shlq $0x4, %rcx
movss (%r15,%rcx), %xmm0
movss 0x26e0(%rip), %xmm1     # 0x7320
ucomiss %xmm0, %xmm1
jbe 0x4c6c
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
leaq 0x29eb(%rip), %rdi      # 0x763e
xorl %eax, %eax
callq 0x2050
movslq (%r14), %rax
movq %rax, %rcx
shlq $0x4, %rcx
andl $0x0, (%r15,%rcx)
xorps %xmm0, %xmm0
movq 0x8(%rsp), %rcx
movl 0x2c(%rcx), %ecx
incl %ecx
cvtsi2ss %ecx, %xmm1
ucomiss %xmm1, %xmm0
jae 0x4c84
movslq %eax, %rcx
jmp 0x4cab
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
leaq 0x29dd(%rip), %rdi      # 0x766f
xorl %eax, %eax
callq 0x2050
movl (%r14), %eax
movslq %eax, %rcx
movq %rcx, %rdx
shlq $0x4, %rdx
andl $0x0, (%r15,%rdx)
movq 0x8(%rsp), %rdx
movb 0x24(%rdx), %dl
shlq $0x4, %rcx
movb %dl, 0xc(%r15,%rcx)
incl %eax
movl %eax, (%r14)
incl 0x4(%rsp)
jmp 0x4afe
leaq 0x2c85(%rip), %rdx      # 0x7956
jmp 0x4cde
xorl %eax, %eax
jmp 0x4cee
leaq 0x289f(%rip), %rdx      # 0x757d
leaq 0x20(%rsp), %rcx
movq %rbx, %rsi
callq 0x49e6
pushq $-0x1
popq %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdx
movl 0x2c(%rbx), %ecx
leaq 0x28bb(%rip), %rdi      # 0x75c9
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x4ceb
leaq 0x28ce(%rip), %rdx      # 0x75f1
jmp 0x4cde
nop