// File: /workspace/github_cpp_makefile_star2_O1/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 0x2880
cmpw $0x2f, (%r12)
jne 0x4fbf
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x2880
cmpl $0x2, %eax
jne 0x5206
leaq 0x30(%rsp), %rdi
callq 0x22c0
movl %eax, %ebp
cmpl $0x10, %eax
jl 0x4fb2
movl 0x2c(%rbx), %edx
leaq 0x25d8(%rip), %rdi      # 0x757c
movl %ebp, %esi
xorl %eax, %eax
callq 0x2050
movl $0x9, %ebp
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x2880
cmpw $0x3a, 0x30(%rsp)
jne 0x51ea
movl %ebp, 0x24(%rsp)
leaq -0x4(%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rbp
leaq 0x2904(%rip), %r12      # 0x78e8
leaq 0x2635(%rip), %r13      # 0x7620
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2880
movzwl 0x30(%rsp), %ecx
cmpl $0x3b, %ecx
je 0x5202
cmpl $0x3a, %ecx
je 0x503c
cmpl $0x2, %eax
je 0x510a
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x4ffb
cmpl $0x0, 0xc(%rsp)
je 0x523f
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2880
cmpw $0x25, 0x30(%rsp)
movl %r14d, 0x20(%rsp)
jne 0x509d
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 0x2880
cmpl $0x2, %eax
jne 0x525b
movq %rbp, %rdi
callq 0x2230
cvttsd2si %xmm0, %r14d
cmpl $0x80, %r14d
jl 0x50cd
movl 0x2c(%rbx), %esi
leaq 0x252a(%rip), %rdi      # 0x75f0
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 0x4ffb
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
shlq $0x4, %rax
movss 0x21c4(%rip), %xmm0     # 0x7310
ucomiss (%r15,%rax), %xmm0
jbe 0x517c
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
leaq 0x24cd(%rip), %rdi      # 0x762e
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rsi
movslq (%rsi), %rax
shlq $0x4, %rax
movl $0x0, (%r15,%rax)
movslq (%rsi), %rax
movq 0x18(%rsp), %rcx
movl 0x2c(%rcx), %ecx
incl %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
shlq $0x4, %rax
movss (%r15,%rax), %xmm1
ucomiss %xmm0, %xmm1
jb 0x51c8
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
leaq 0x24b2(%rip), %rdi      # 0x765f
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rsi
movslq (%rsi), %rax
shlq $0x4, %rax
movl $0x0, (%r15,%rax)
movq 0x18(%rsp), %rax
movb 0x24(%rax), %al
movslq (%rsi), %rcx
leal 0x1(%rcx), %edx
shlq $0x4, %rcx
movb %al, 0xc(%r15,%rcx)
movl %edx, (%rsi)
incl 0xc(%rsp)
jmp 0x4ffb
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x26ef(%rip), %rdi      # 0x78e8
leaq 0x2746(%rip), %rsi      # 0x7946
jmp 0x521c
xorl %eax, %eax
jmp 0x522d
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x26d3(%rip), %rdi      # 0x78e8
leaq 0x2351(%rip), %rsi      # 0x756d
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 0x236c(%rip), %rdi      # 0x75b9
leaq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x5228
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x267e(%rip), %rdi      # 0x78e8
leaq 0x2370(%rip), %rsi      # 0x75e1
jmp 0x521c
nop