From 73853d651a15ab4c0a800b36c91475ade6534b4e Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Fri, 31 May 2019 17:46:18 -0400 Subject: [PATCH] 5 voices at 44.1khz --- quantum/audio/audio_arm.c | 113 ++++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 36 deletions(-) diff --git a/quantum/audio/audio_arm.c b/quantum/audio/audio_arm.c index e349a0758ce..05920df702c 100644 --- a/quantum/audio/audio_arm.c +++ b/quantum/audio/audio_arm.c @@ -77,12 +77,12 @@ bool glissando = true; #endif float startup_song[][2] = STARTUP_SONG; -#define DAC_BUFFER_SIZE 100U +#define DAC_BUFFER_SIZE 256U #ifndef DAC_SAMPLE_MAX #define DAC_SAMPLE_MAX 4095U #endif -#define DAC_SAMPLE_RATE 30000U +#define DAC_SAMPLE_RATE 44100U GPTConfig gpt7cfg1 = { .frequency = DAC_SAMPLE_RATE, @@ -96,52 +96,93 @@ static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = { // [0 ... DAC_BUFFER_SIZE/2-1] = DAC_SAMPLE_MAX, // [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE -1] = 0, - // 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 + // 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, + 0xb0f,0xb3d,0xb6b,0xb98,0xbc5,0xbf1,0xc1c,0xc47, + 0xc71,0xc9a,0xcc3,0xceb,0xd12,0xd39,0xd5f,0xd83, + 0xda7,0xdca,0xded,0xe0e,0xe2e,0xe4e,0xe6c,0xe8a, + 0xea6,0xec1,0xedc,0xef5,0xf0d,0xf24,0xf3a,0xf4f, + 0xf63,0xf76,0xf87,0xf98,0xfa7,0xfb5,0xfc2,0xfcd, + 0xfd8,0xfe1,0xfe9,0xff0,0xff5,0xff9,0xffd,0xffe, + 0xfff,0xffe,0xffd,0xff9,0xff5,0xff0,0xfe9,0xfe1, + 0xfd8,0xfcd,0xfc2,0xfb5,0xfa7,0xf98,0xf87,0xf76, + 0xf63,0xf4f,0xf3a,0xf24,0xf0d,0xef5,0xedc,0xec1, + 0xea6,0xe8a,0xe6c,0xe4e,0xe2e,0xe0e,0xded,0xdca, + 0xda7,0xd83,0xd5f,0xd39,0xd12,0xceb,0xcc3,0xc9a, + 0xc71,0xc47,0xc1c,0xbf1,0xbc5,0xb98,0xb6b,0xb3d, + 0xb0f,0xae0,0xab1,0xa82,0xa52,0xa22,0x9f1,0x9c0, + 0x98f,0x95e,0x92c,0x8fa,0x8c8,0x896,0x864,0x832, + 0x800,0x7cd,0x79b,0x769,0x737,0x705,0x6d3,0x6a1, + 0x670,0x63f,0x60e,0x5dd,0x5ad,0x57d,0x54e,0x51f, + 0x4f0,0x4c2,0x494,0x467,0x43a,0x40e,0x3e3,0x3b8, + 0x38e,0x365,0x33c,0x314,0x2ed,0x2c6,0x2a0,0x27c, + 0x258,0x235,0x212,0x1f1,0x1d1,0x1b1,0x193,0x175, + 0x159,0x13e,0x123,0x10a,0xf2,0xdb,0xc5,0xb0, + 0x9c,0x89,0x78,0x67,0x58,0x4a,0x3d,0x32, + 0x27,0x1e,0x16,0xf,0xa,0x6,0x2,0x1, + 0x0,0x1,0x2,0x6,0xa,0xf,0x16,0x1e, + 0x27,0x32,0x3d,0x4a,0x58,0x67,0x78,0x89, + 0x9c,0xb0,0xc5,0xdb,0xf2,0x10a,0x123,0x13e, + 0x159,0x175,0x193,0x1b1,0x1d1,0x1f1,0x212,0x235, + 0x258,0x27c,0x2a0,0x2c6,0x2ed,0x314,0x33c,0x365, + 0x38e,0x3b8,0x3e3,0x40e,0x43a,0x467,0x494,0x4c2, + 0x4f0,0x51f,0x54e,0x57d,0x5ad,0x5dd,0x60e,0x63f, + 0x670,0x6a1,0x6d3,0x705,0x737,0x769,0x79b,0x7cd + }; -dacsample_t dac_buffer_lr[1] = { DAC_SAMPLE_MAX / 2 }; +#define DAC_VOICES_MAX 5 + +dacsample_t dac_buffer_lr[DAC_BUFFER_SIZE] = { DAC_SAMPLE_MAX / 2 }; float dac_if[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; /* * DAC streaming callback. */ -static void end_cb1(DACDriver * dacp, dacsample_t * samples, size_t pos) { +static void end_cb1(DACDriver * dacp, dacsample_t * samples, size_t rows) { (void)dacp; - (void)pos; - //for (uint8_t i = 0; i < DAC_BUFFER_SIZE; i++) { - //samples[0] = (dac_buffer[dac_i] + dac_buffer[dac_j]) / 2; - //} + //(void)dac_buffer; - uint16_t sample_sum = 0; int working_voices = voices; - if (working_voices > 3) - working_voices = 3; - for (int 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) - dac_if[i] = dac_if[i] - DAC_BUFFER_SIZE; - sample_sum += dac_buffer[(uint8_t)round(dac_if[i]) % DAC_BUFFER_SIZE] / working_voices; - } + if (working_voices > DAC_VOICES_MAX) + working_voices = DAC_VOICES_MAX; - if (working_voices > 0) { - samples[0] = sample_sum; - } else { - samples[0] = DAC_SAMPLE_MAX; + for (int s = 0; s < rows; s++) { + if (working_voices > 0) { + uint16_t sample_sum = 0; + for (int 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) + dac_if[i] = dac_if[i] - DAC_BUFFER_SIZE; + // sine + sample_sum += dac_buffer[(uint16_t)round(dac_if[i])] / working_voices; + // rising triangle + // 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; + } } } @@ -195,7 +236,7 @@ void audio_init() { // dacStart(&DACD1, &dac1cfg1); // dacStartConversion(&DACD1, &dacgrpcfg1, dac_buffer_lr, 1); dacStart(&DACD2, &dac1cfg1); - dacStartConversion(&DACD2, &dacgrpcfg1, dac_buffer_lr, 1); + dacStartConversion(&DACD2, &dacgrpcfg1, dac_buffer_lr, DAC_BUFFER_SIZE); gptStart(&GPTD6, &gpt7cfg1); gptStartContinuous(&GPTD6, 2U);