#include "SerialMulti.hpp"
#include "RAWAudioPlayer.hpp"
#include "incbin.h"

CSerialMulti SerialM;
CRAWAudioPlayer RAWAudioPlayer;

// **** For Arduino IDE, it must be specified by absolute path ****
INCBIN("C:/src/demo2/1.raw", pcm_kick);
INCBIN("C:/src/demo2/2.raw", pcm_sn);
INCBIN("C:/src/demo2/3.raw", pcm_hh);
INCBIN("C:/src/demo2/4.raw", pcm_cc);
INCBIN("C:/src/demo2/5.raw", pcm_tom);
INCBIN("C:/src/demo2/6.raw", pcm_hit);
INCBIN("C:/src/demo2/7.raw", pcm_btn);
INCBIN("C:/src/demo2/8.raw", pcm_alarm);
INCBIN("C:/src/demo2/9.raw", pcm_jingle);

std::vector<CRAWAudioPlayer::TPCMInfo> PCMInfo{
  { (int8_t *)&pcm_kick, (int)&_size_pcm_kick, -1, 256, 127 },
  { (int8_t *)&pcm_sn, (int)&_size_pcm_sn, -1, 256, 127 },
  { (int8_t *)&pcm_hh, (int)&_size_pcm_hh, -1, 256, 127 },
  { (int8_t *)&pcm_cc, (int)&_size_pcm_cc, -1, 256, 127 },
  { (int8_t *)&pcm_tom, (int)&_size_pcm_tom, -1, 256, 127 },
  { (int8_t *)&pcm_hit, (int)&_size_pcm_hit, -1, 256, 127 },
  { (int8_t *)&pcm_btn, (int)&_size_pcm_btn, -1, 256, 127 },
  { (int8_t *)&pcm_alarm, (int)&_size_pcm_alarm, -1, 256, 127 },
  { (int8_t *)&pcm_jingle, (int)&_size_pcm_jingle, -1, 256, 127 },
};

void setup() {
  SerialM.begin(115200);
  RAWAudioPlayer.begin(&PCMInfo);
}

void loop() {
  SerialM.flush();
  while (SerialM.available()) {
    char c = SerialM.read();
    SerialM.printf("%c", c);
    switch (c) {
      case '#': SerialM.restart(); break;
      case '!': SerialM.usbboot(); break;

      case '<': SerialM.printf("\rvol=%d\n", RAWAudioPlayer.set_volume(RAWAudioPlayer.get_volume() + 1)); break;
      case '>': SerialM.printf("\rvol=%d\n", RAWAudioPlayer.set_volume(RAWAudioPlayer.get_volume() - 1)); break;

      case '1': RAWAudioPlayer.play(0, 24, 64); break;
      case '2': RAWAudioPlayer.play(1, 24, 64); break;
      case '3': RAWAudioPlayer.play(2, 24, 64); break;
      case '4': RAWAudioPlayer.play(3, 24, 64); break;
      case '5': RAWAudioPlayer.play(4, 24, 64); break;
      case '6': RAWAudioPlayer.play(5, 24, 64); break;
      case '7': RAWAudioPlayer.play(6, 48, 64); break;
      case '8': RAWAudioPlayer.play(7, 42, 64); break;
      case '9': RAWAudioPlayer.play(8, 29, 64); break;

      case 'z': RAWAudioPlayer.play(5, 24, 64); break;
      case 's': RAWAudioPlayer.play(5, 25, 64); break;
      case 'x': RAWAudioPlayer.play(5, 26, 64); break;
      case 'd': RAWAudioPlayer.play(5, 27, 64); break;
      case 'c': RAWAudioPlayer.play(5, 28, 64); break;
      case 'v': RAWAudioPlayer.play(5, 29, 64); break;
      case 'g': RAWAudioPlayer.play(5, 30, 64); break;
      case 'b': RAWAudioPlayer.play(5, 31, 64); break;
      case 'h': RAWAudioPlayer.play(5, 32, 64); break;
      case 'n': RAWAudioPlayer.play(5, 33, 64); break;
      case 'j': RAWAudioPlayer.play(5, 34, 64); break;
      case 'm': RAWAudioPlayer.play(5, 35, 64); break;
      case ',': RAWAudioPlayer.play(5, 36, 64); break;
    }
  }
  delay(10);
}
