// File: /workspace/github_cpp_makefile_star2_O3/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 $0x438, %rsp            # imm = 0x438
movl %r9d, %ebp
movl %r8d, %r14d
movq %rcx, 0x10(%rsp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
leaq 0x30(%rsp), %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x2884
movzwl (%r12), %eax
cmpw $0x2f, %ax
jne 0x502a
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x2884
cmpl $0x2, %eax
jne 0x5275
leaq 0x30(%rsp), %rdi
callq 0x22c0
movl %eax, %ebp
cmpl $0x10, %eax
jl 0x5015
movl 0x2c(%rbx), %edx
leaq 0x256d(%rip), %rdi      # 0x7574
movl %ebp, %esi
xorl %eax, %eax
callq 0x2050
movl $0x9, %ebp
leaq 0x30(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2884
movzwl (%r12), %eax
cmpw $0x3a, %ax
jne 0x5259
movl %ebp, 0x24(%rsp)
leaq -0x4(%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rbp
leaq 0x2893(%rip), %r12      # 0x78e0
leaq 0x25c4(%rip), %r13      # 0x7618
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2884
movzwl 0x30(%rsp), %ecx
cmpl $0x3b, %ecx
je 0x5271
cmpl $0x3a, %ecx
je 0x50a5
cmpl $0x2, %eax
je 0x5173
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x5064
cmpl $0x0, 0xc(%rsp)
je 0x52ae
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2884
cmpw $0x25, 0x30(%rsp)
movl %r14d, 0x20(%rsp)
jne 0x5106
xorl %edi, %edi
callq 0x2160
movl %eax, %edi
callq 0x2180
callq 0x2070
cltd
movq 0x18(%rsp), %rcx
idivl 0x28(%rcx)
movl %edx, %r14d
callq 0x2070
movl %r14d, %ecx
negl %ecx
testb $0x1, %al
cmovnel %r14d, %ecx
movl %ecx, 0x8(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2884
cmpl $0x2, %eax
jne 0x52ca
movq %rbp, %rdi
callq 0x2230
cvttsd2si %xmm0, %r14d
cmpl $0x80, %r14d
jl 0x5136
movl 0x2c(%rbx), %esi
leaq 0x24b9(%rip), %rdi      # 0x75e8
xorl %eax, %eax
callq 0x2050
addl 0x8(%rsp), %r14d
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
cmpl $0x7f, %r14d
movl $0x7f, %eax
cmovgel %eax, %r14d
movq 0x10(%rsp), %rax
movslq (%rax), %rax
shlq $0x4, %rax
movq 0x28(%rsp), %rcx
movb %r14b, (%rcx,%rax)
movl 0x20(%rsp), %r14d
jmp 0x5064
movq %rbp, %rdi
callq 0x2230
cvtsd2ss %xmm0, %xmm0
movq 0x10(%rsp), %rsi
movslq (%rsi), %rax
shlq $0x4, %rax
movss %xmm0, (%r15,%rax)
movl %r14d, 0x4(%r15,%rax)
movslq (%rsi), %rax
shlq $0x4, %rax
movl 0x24(%rsp), %ecx
movl %ecx, 0x8(%r15,%rax)
movslq (%rsi), %rax
movq %rax, %rcx
shlq $0x4, %rcx
movss (%r15,%rcx), %xmm0
movss 0x214a(%rip), %xmm1     # 0x7308
ucomiss %xmm0, %xmm1
jbe 0x51f2
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
leaq 0x2455(%rip), %rdi      # 0x7626
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rsi
movslq (%rsi), %rax
movq %rax, %rcx
shlq $0x4, %rcx
movl $0x0, (%r15,%rcx)
xorps %xmm0, %xmm0
movq 0x18(%rsp), %rcx
movl 0x2c(%rcx), %ecx
incl %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
ucomiss %xmm1, %xmm0
jae 0x520d
movslq %eax, %rcx
jmp 0x523b
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
leaq 0x243c(%rip), %rdi      # 0x7657
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rsi
movl (%rsi), %eax
movslq %eax, %rcx
movq %rcx, %rdx
shlq $0x4, %rdx
movl $0x0, (%r15,%rdx)
movq 0x18(%rsp), %rdx
movb 0x24(%rdx), %dl
shlq $0x4, %rcx
movb %dl, 0xc(%r15,%rcx)
incl %eax
movl %eax, (%rsi)
incl 0xc(%rsp)
jmp 0x5064
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x2678(%rip), %rdi      # 0x78e0
leaq 0x26cf(%rip), %rsi      # 0x793e
jmp 0x528b
xorl %eax, %eax
jmp 0x529c
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x265c(%rip), %rdi      # 0x78e0
leaq 0x22da(%rip), %rsi      # 0x7565
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdx
movl 0x2c(%rbx), %ecx
leaq 0x22f5(%rip), %rdi      # 0x75b1
leaq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x5297
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x2607(%rip), %rdi      # 0x78e0
leaq 0x22f9(%rip), %rsi      # 0x75d9
jmp 0x528b