waveblender/drums.c
2020-04-11 15:28:44 +02:00

102 lines
2.3 KiB
C

#include <stdint.h>
#include "system.h"
#include "display.h"
#include "systick.h"
#include "button.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ui.h"
#include "timer.h"
#include "led.h"
#define MAX_PATTERN_LEN 64
#define DRUMS_KICK (1 << 0)
#define DRUMS_HIHAT (1 << 1)
#define DRUMS_SNARE (1 << 2)
#define DRUMS_TOM1 (1 << 3)
#define DRUMS_TOM2 (1 << 4)
#define DRUMS_CYM (1 << 6)
#define DRUMS_CLAP (1 << 7)
#define TRACKS 8
static uint32_t pattern[MAX_PATTERN_LEN] = { DRUMS_KICK, DRUMS_HIHAT, DRUMS_SNARE, DRUMS_HIHAT, DRUMS_KICK, DRUMS_KICK, DRUMS_SNARE, DRUMS_HIHAT };
//static uint32_t pattern[MAX_PATTERN_LEN] = { DRUMS_KICK, DRUMS_KICK, DRUMS_KICK, DRUMS_KICK, DRUMS_KICK, DRUMS_KICK, DRUMS_SNARE, DRUMS_HIHAT };
static uint32_t pattern_len = 8;
static uint32_t pattern_pos = 0;
static unsigned char *dsample[TRACKS] = {};
static unsigned int dsample_len[TRACKS] = {};
void beat_cb(uint32_t b)
{
uint32_t key = pattern[pattern_pos];
int i;
for (i = 0; i < TRACKS; i++) {
if ((key & (1 << i)) == (1 << i)) {
if (dsample[i]) {
while(dac_is_busy())
dac_stop();
dac_play_direct(dsample[i], dsample_len[i]);
break;
}
}
}
pattern_pos++;
if (pattern_pos >= pattern_len) {
pattern_pos = 0;
}
led_beat((pattern_pos % 8) + 1);
}
void drums_init(void)
{
dsample[0] = drumkit_0_au;
dsample[1] = drumkit_1_au;
dsample[2] = drumkit_2_au;
dsample[3] = drumkit_3_au;
dsample[4] = drumkit_4_au;
dsample[5] = drumkit_5_au;
dsample[6] = drumkit_6_au;
dsample_len[0] = drumkit_0_au_len;
dsample_len[1] = drumkit_1_au_len;
dsample_len[2] = drumkit_2_au_len;
dsample_len[3] = drumkit_3_au_len;
dsample_len[4] = drumkit_4_au_len;
dsample_len[5] = drumkit_5_au_len;
dsample_len[6] = drumkit_6_au_len;
}
void drums_start(void)
{
timer_set_beat_callback(beat_cb);
}
void drums_stop(void)
{
timer_clear_beat_callback();
}
void drums_set_pattern_len(int l)
{
pattern_len = l;
timer_set_beat(1);
}
void drums_set(uint32_t track, int pos)
{
pattern[pos] |= track;
}
void drums_clear(uint32_t track, int pos)
{
pattern[pos] &= ~track;
}