drums.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdint.h>
  2. #include "system.h"
  3. #include "display.h"
  4. #include "systick.h"
  5. #include "button.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include "ui.h"
  10. #include "timer.h"
  11. #include "led.h"
  12. #include "drums.h"
  13. #include "drone.h"
  14. #include "settings.h"
  15. #define TRACKS 8
  16. static struct drums_pattern *pattern = NULL;
  17. static uint32_t pattern_len = 8;
  18. static uint32_t pattern_pos = 0;
  19. static unsigned char *dsample[TRACKS] = {};
  20. static unsigned int dsample_len[TRACKS] = {};
  21. static void beat_cb(uint32_t b)
  22. {
  23. int i;
  24. uint32_t key;
  25. pattern = &Settings->drums[Settings->current_pattern];
  26. //drone_beat(pattern_pos);
  27. key = pattern->data[pattern_pos];
  28. for (i = 0; i < TRACKS; i++) {
  29. if ((key & (1 << i)) == (1 << i)) {
  30. if (dsample[i]) {
  31. while(dac_is_busy())
  32. dac_stop();
  33. dac_play_direct(dsample[i], dsample_len[i]);
  34. break;
  35. }
  36. }
  37. }
  38. pattern_pos++;
  39. if (pattern_pos >= pattern->len) {
  40. pattern_pos = 0;
  41. }
  42. led_beat((pattern_pos % 8) + 1);
  43. }
  44. void drums_oneshot(int track)
  45. {
  46. while(dac_is_busy())
  47. dac_stop();
  48. dac_play_direct(dsample[track], dsample_len[track]);
  49. }
  50. void drums_init(void)
  51. {
  52. dsample[0] = drumkit_0_au;
  53. dsample[1] = drumkit_1_au;
  54. dsample[2] = drumkit_2_au;
  55. dsample[3] = drumkit_3_au;
  56. dsample[4] = drumkit_4_au;
  57. dsample[5] = drumkit_5_au;
  58. dsample[6] = drumkit_6_au;
  59. dsample_len[0] = drumkit_0_au_len;
  60. dsample_len[1] = drumkit_1_au_len;
  61. dsample_len[2] = drumkit_2_au_len;
  62. dsample_len[3] = drumkit_3_au_len;
  63. dsample_len[4] = drumkit_4_au_len;
  64. dsample_len[5] = drumkit_5_au_len;
  65. dsample_len[6] = drumkit_6_au_len;
  66. }
  67. void drums_start(void)
  68. {
  69. timer_set_beat_callback(beat_cb);
  70. }
  71. void drums_stop(void)
  72. {
  73. timer_clear_beat_callback();
  74. }
  75. void drums_set(uint32_t track, int pos)
  76. {
  77. pattern = Settings->drums[Settings->current_pattern].data;
  78. pattern->data[pos] |= (1 << track);
  79. }
  80. void drums_clear(uint32_t track, int pos)
  81. {
  82. pattern = Settings->drums[Settings->current_pattern].data;
  83. pattern->data[pos] &= ~(1 << track);
  84. }
  85. int drums_get(uint32_t track, int pos)
  86. {
  87. pattern = Settings->drums[Settings->current_pattern].data;
  88. return (!!(pattern->data[pos] & (1 << track)));
  89. }