// File: 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
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x458(%rbp)
movl $0x0, -0x448(%rbp)
movl $0x0, -0x44c(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2b10
movl %eax, -0x34(%rbp)
leaq -0x440(%rbp), %rdi
leaq 0x8567(%rip), %rsi      # 0x104da
callq 0x2320
cmpl $0x0, %eax
jne 0x801b
movq -0x18(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2b10
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
je 0x7fc4
movq -0x458(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x440(%rbp), %rcx
leaq 0x865e(%rip), %rdx      # 0x10611
callq 0x7e10
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x83f3
leaq -0x440(%rbp), %rdi
callq 0x2380
movl %eax, -0x30(%rbp)
cmpl $0xf, -0x30(%rbp)
jle 0x8008
movl -0x30(%rbp), %eax
movl %eax, -0x45c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x89f0
movl -0x45c(%rbp), %esi
movl %eax, %edx
leaq 0x8626(%rip), %rdi      # 0x10620
movb $0x0, %al
callq 0x2060
movl $0x9, -0x30(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2b10
movl %eax, -0x34(%rbp)
leaq -0x440(%rbp), %rdi
leaq 0x8634(%rip), %rsi      # 0x1065d
callq 0x2320
cmpl $0x0, %eax
je 0x805d
movq -0x458(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x440(%rbp), %rcx
leaq 0x8611(%rip), %rdx      # 0x1065d
callq 0x7e10
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x83f3
jmp 0x805f
movq -0x18(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2b10
movl %eax, -0x34(%rbp)
leaq -0x440(%rbp), %rdi
leaq 0x8548(%rip), %rsi      # 0x105c8
callq 0x2320
cmpl $0x0, %eax
jne 0x8096
movl $0x0, -0x4(%rbp)
jmp 0x83f3
leaq -0x440(%rbp), %rdi
leaq 0x85b9(%rip), %rsi      # 0x1065d
callq 0x2320
cmpl $0x0, %eax
jne 0x8257
cmpl $0x0, -0x44c(%rbp)
jne 0x810c
leaq -0x440(%rbp), %rax
movq %rax, -0x470(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2f40
movq %rax, -0x468(%rbp)
movq -0x18(%rbp), %rdi
callq 0x89f0
movq -0x470(%rbp), %rsi
movq -0x468(%rbp), %rdx
movl %eax, %ecx
leaq 0x8566(%rip), %rdi      # 0x1065f
movb $0x0, %al
callq 0x2060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x83f3
movq -0x18(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2b10
movl %eax, -0x34(%rbp)
leaq -0x440(%rbp), %rdi
leaq 0x855a(%rip), %rsi      # 0x10687
callq 0x2320
cmpl $0x0, %eax
jne 0x8190
xorl %eax, %eax
movl %eax, %edi
callq 0x21e0
movl %eax, %edi
callq 0x2200
callq 0x2090
movq -0x458(%rbp), %rcx
cltd
idivl 0x28(%rcx)
movl %edx, -0x448(%rbp)
callq 0x2090
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x817d
xorl %eax, %eax
subl -0x448(%rbp), %eax
movl %eax, -0x448(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2b10
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
je 0x81c0
movq -0x458(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x440(%rbp), %rcx
leaq 0x84da(%rip), %rdx      # 0x10689
callq 0x7e10
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x83f3
leaq -0x440(%rbp), %rdi
callq 0x22f0
cvttsd2si %xmm0, %eax
movl %eax, -0x444(%rbp)
cmpl $0x7f, -0x444(%rbp)
jle 0x81f8
movq -0x18(%rbp), %rdi
callq 0x89f0
movl %eax, %esi
leaq 0x84a7(%rip), %rdi      # 0x10698
movb $0x0, %al
callq 0x2060
movl -0x444(%rbp), %eax
addl -0x448(%rbp), %eax
movl %eax, -0x444(%rbp)
cmpl $0x0, -0x444(%rbp)
jge 0x821d
movl $0x0, -0x444(%rbp)
cmpl $0x7f, -0x444(%rbp)
jle 0x8230
movl $0x7f, -0x444(%rbp)
movl -0x444(%rbp), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdx
movl (%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movb %cl, 0xc(%rax)
jmp 0x805f
cmpl $0x2, -0x34(%rbp)
je 0x8280
movq -0x458(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x440(%rbp), %rcx
leaq 0x8452(%rip), %rdx      # 0x106c8
callq 0x7e10
jmp 0x805f
leaq -0x440(%rbp), %rdi
callq 0x22f0
cvtsd2ss %xmm0, %xmm0
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss 0x808c(%rip), %xmm0     # 0x1037c
ucomiss (%rax), %xmm0
jbe 0x833e
movq -0x18(%rbp), %rdi
callq 0x2f40
movq %rax, -0x478(%rbp)
movq -0x18(%rbp), %rdi
callq 0x89f0
movq -0x478(%rbp), %rsi
movl %eax, %edx
leaq 0x83b8(%rip), %rdi      # 0x106d6
movb $0x0, %al
callq 0x2060
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movq -0x458(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movss (%rcx), %xmm0
movl 0x2c(%rax), %eax
addl $0x1, %eax
cvtsi2ss %eax, %xmm1
ucomiss %xmm1, %xmm0
jb 0x83b3
movq -0x18(%rbp), %rdi
callq 0x2f40
movq %rax, -0x480(%rbp)
movq -0x18(%rbp), %rdi
callq 0x89f0
movq -0x480(%rbp), %rsi
movl %eax, %edx
leaq 0x8374(%rip), %rdi      # 0x10707
movb $0x0, %al
callq 0x2060
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movq -0x458(%rbp), %rax
movl 0x24(%rax), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movb %cl, 0xc(%rax)
movq -0x28(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x44c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44c(%rbp)
jmp 0x805f
movl -0x4(%rbp), %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nop