From 3e1826a33286381874bf3d7c69ddce63c56a576c Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sat, 1 Jun 2019 01:37:54 -0400 Subject: [PATCH] fixed blip (rounding error), other waves, added key selection (left/right) --- quantum/audio/audio_arm.c | 106 ++++++++++++++++-------- quantum/process_keycode/process_music.c | 10 +++ 2 files changed, 82 insertions(+), 34 deletions(-) diff --git a/quantum/audio/audio_arm.c b/quantum/audio/audio_arm.c index 05920df702c..98adb71142b 100644 --- a/quantum/audio/audio_arm.c +++ b/quantum/audio/audio_arm.c @@ -78,11 +78,22 @@ bool glissando = true; float startup_song[][2] = STARTUP_SONG; #define DAC_BUFFER_SIZE 256U + #ifndef DAC_SAMPLE_MAX -#define DAC_SAMPLE_MAX 4095U + #define DAC_SAMPLE_MAX 4095U #endif -#define DAC_SAMPLE_RATE 44100U +#ifndef DAC_SAMPLE_RATE + #define DAC_SAMPLE_RATE 44100U +#endif + +#ifndef DAC_VOICES_MAX + #define DAC_VOICES_MAX 5 +#endif + +#ifndef DAC_OFF_VALUE + #define DAC_OFF_VALUE DAC_SAMPLE_MAX / 2 +#endif GPTConfig gpt7cfg1 = { .frequency = DAC_SAMPLE_RATE, @@ -92,25 +103,6 @@ GPTConfig gpt7cfg1 = { }; static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = { - // First half is max, second half is 0 - // [0 ... DAC_BUFFER_SIZE/2-1] = DAC_SAMPLE_MAX, - // [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE -1] = 0, - - // 100 values, max 4095 - // 0x800,0x880,0x900,0x97f,0x9fd,0xa78,0xaf1,0xb67, - // 0xbda,0xc49,0xcb3,0xd19,0xd79,0xdd4,0xe29,0xe78, - // 0xec0,0xf02,0xf3c,0xf6f,0xf9b,0xfbf,0xfdb,0xfef, - // 0xffb,0xfff,0xffb,0xfef,0xfdb,0xfbf,0xf9b,0xf6f, - // 0xf3c,0xf02,0xec0,0xe78,0xe29,0xdd4,0xd79,0xd19, - // 0xcb3,0xc49,0xbda,0xb67,0xaf1,0xa78,0x9fd,0x97f, - // 0x900,0x880,0x800,0x77f,0x6ff,0x680,0x602,0x587, - // 0x50e,0x498,0x425,0x3b6,0x34c,0x2e6,0x286,0x22b, - // 0x1d6,0x187,0x13f,0xfd,0xc3,0x90,0x64,0x40, - // 0x24,0x10,0x4,0x0,0x4,0x10,0x24,0x40, - // 0x64,0x90,0xc3,0xfd,0x13f,0x187,0x1d6,0x22b, - // 0x286,0x2e6,0x34c,0x3b6,0x425,0x498,0x50e,0x587, - // 0x602,0x680,0x6ff,0x77f,0x800 - // 256 values, max 4095 0x800,0x832,0x864,0x896,0x8c8,0x8fa,0x92c,0x95e, 0x98f,0x9c0,0x9f1,0xa22,0xa52,0xa82,0xab1,0xae0, @@ -144,12 +136,51 @@ static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = { 0x38e,0x3b8,0x3e3,0x40e,0x43a,0x467,0x494,0x4c2, 0x4f0,0x51f,0x54e,0x57d,0x5ad,0x5dd,0x60e,0x63f, 0x670,0x6a1,0x6d3,0x705,0x737,0x769,0x79b,0x7cd - }; -#define DAC_VOICES_MAX 5 +static const dacsample_t dac_buffer_triangle[DAC_BUFFER_SIZE] = { + // 256 values, max 4095 + 0x20,0x40,0x60,0x80,0xa0,0xc0,0xe0,0x100, + 0x120,0x140,0x160,0x180,0x1a0,0x1c0,0x1e0,0x200, + 0x220,0x240,0x260,0x280,0x2a0,0x2c0,0x2e0,0x300, + 0x320,0x340,0x360,0x380,0x3a0,0x3c0,0x3e0,0x400, + 0x420,0x440,0x460,0x480,0x4a0,0x4c0,0x4e0,0x500, + 0x520,0x540,0x560,0x580,0x5a0,0x5c0,0x5e0,0x600, + 0x620,0x640,0x660,0x680,0x6a0,0x6c0,0x6e0,0x700, + 0x720,0x740,0x760,0x780,0x7a0,0x7c0,0x7e0,0x800, + 0x81f,0x83f,0x85f,0x87f,0x89f,0x8bf,0x8df,0x8ff, + 0x91f,0x93f,0x95f,0x97f,0x99f,0x9bf,0x9df,0x9ff, + 0xa1f,0xa3f,0xa5f,0xa7f,0xa9f,0xabf,0xadf,0xaff, + 0xb1f,0xb3f,0xb5f,0xb7f,0xb9f,0xbbf,0xbdf,0xbff, + 0xc1f,0xc3f,0xc5f,0xc7f,0xc9f,0xcbf,0xcdf,0xcff, + 0xd1f,0xd3f,0xd5f,0xd7f,0xd9f,0xdbf,0xddf,0xdff, + 0xe1f,0xe3f,0xe5f,0xe7f,0xe9f,0xebf,0xedf,0xeff, + 0xf1f,0xf3f,0xf5f,0xf7f,0xf9f,0xfbf,0xfdf,0xfff, + 0xfdf,0xfbf,0xf9f,0xf7f,0xf5f,0xf3f,0xf1f,0xeff, + 0xedf,0xebf,0xe9f,0xe7f,0xe5f,0xe3f,0xe1f,0xdff, + 0xddf,0xdbf,0xd9f,0xd7f,0xd5f,0xd3f,0xd1f,0xcff, + 0xcdf,0xcbf,0xc9f,0xc7f,0xc5f,0xc3f,0xc1f,0xbff, + 0xbdf,0xbbf,0xb9f,0xb7f,0xb5f,0xb3f,0xb1f,0xaff, + 0xadf,0xabf,0xa9f,0xa7f,0xa5f,0xa3f,0xa1f,0x9ff, + 0x9df,0x9bf,0x99f,0x97f,0x95f,0x93f,0x91f,0x8ff, + 0x8df,0x8bf,0x89f,0x87f,0x85f,0x83f,0x81f,0x800, + 0x7e0,0x7c0,0x7a0,0x780,0x760,0x740,0x720,0x700, + 0x6e0,0x6c0,0x6a0,0x680,0x660,0x640,0x620,0x600, + 0x5e0,0x5c0,0x5a0,0x580,0x560,0x540,0x520,0x500, + 0x4e0,0x4c0,0x4a0,0x480,0x460,0x440,0x420,0x400, + 0x3e0,0x3c0,0x3a0,0x380,0x360,0x340,0x320,0x300, + 0x2e0,0x2c0,0x2a0,0x280,0x260,0x240,0x220,0x200, + 0x1e0,0x1c0,0x1a0,0x180,0x160,0x140,0x120,0x100, + 0xe0,0xc0,0xa0,0x80,0x60,0x40,0x20,0x0 +}; -dacsample_t dac_buffer_lr[DAC_BUFFER_SIZE] = { DAC_SAMPLE_MAX / 2 }; +// static const dacsample_t dac_buffer_square[DAC_BUFFER_SIZE] = { +// // First half is max, second half is 0 +// [0 ... DAC_BUFFER_SIZE/2-1] = DAC_SAMPLE_MAX, +// [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE -1] = 0, +// }; + +dacsample_t dac_buffer_lr[DAC_BUFFER_SIZE] = { DAC_OFF_VALUE }; float dac_if[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; @@ -159,29 +190,37 @@ float dac_if[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; static void end_cb1(DACDriver * dacp, dacsample_t * samples, size_t rows) { (void)dacp; - //(void)dac_buffer; + (void)dac_buffer; - int working_voices = voices; + uint8_t working_voices = voices; if (working_voices > DAC_VOICES_MAX) working_voices = DAC_VOICES_MAX; - for (int s = 0; s < rows; s++) { + for (uint8_t s = 0; s < rows; s++) { if (working_voices > 0) { uint16_t sample_sum = 0; - for (int i = 0; i < working_voices; i++) { + for (uint8_t i = 0; i < working_voices; i++) { dac_if[i] = dac_if[i] + ((frequencies[i]*(float)DAC_BUFFER_SIZE)/(float)DAC_SAMPLE_RATE*1.5); - while(dac_if[i] >= DAC_BUFFER_SIZE) + + // Needed because % doesn't work with floats + // 0.5 less than the size because we use round() later + while(dac_if[i] >= (DAC_BUFFER_SIZE - 0.5)) dac_if[i] = dac_if[i] - DAC_BUFFER_SIZE; + + // Wavetable generation/lookup // sine - sample_sum += dac_buffer[(uint16_t)round(dac_if[i])] / working_voices; - // rising triangle + // sample_sum += dac_buffer[(uint16_t)round(dac_if[i])] / working_voices; + // triangle wave (5 voices) + sample_sum += dac_buffer_triangle[(uint16_t)round(dac_if[i])] / working_voices; + // rising triangle (4 voices) // sample_sum += (uint16_t)round((dac_if[i] * DAC_SAMPLE_MAX) / DAC_BUFFER_SIZE / working_voices ); // square (max 5 voices) // sample_sum += ((dac_if[i] > (DAC_BUFFER_SIZE / 2)) ? DAC_SAMPLE_MAX / working_voices: 0); + } samples[s] = sample_sum; } else { - samples[s] = DAC_SAMPLE_MAX / 2; + samples[s] = DAC_OFF_VALUE; } } } @@ -349,9 +388,8 @@ void play_note(float freq, int vol) { playing_note = true; - envelope_index = 0; - if (freq > 0) { + envelope_index = 0; frequencies[voices] = freq; volumes[voices] = vol; voices++; diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c index 697aa237fac..c85bc598ccd 100644 --- a/quantum/process_keycode/process_music.c +++ b/quantum/process_keycode/process_music.c @@ -195,6 +195,16 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { music_sequence_interval+=10; return false; } + + if (keycode == KC_LEFT) { + music_offset--; + return false; + } + + if (keycode == KC_RIGHT) { + music_offset++; + return false; + } } uint8_t note = 36;