From 408d0ef26dee248feffcc6354a69d808f8feaa39 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Mon, 17 Feb 2014 14:41:33 +0100 Subject: [PATCH] Allow 110Hz tones --- radio/src/audio_arm.cpp | 186 +++++++++++++++++++++++++++++----------- radio/src/audio_arm.h | 10 +-- 2 files changed, 142 insertions(+), 54 deletions(-) diff --git a/radio/src/audio_arm.cpp b/radio/src/audio_arm.cpp index 5d2fb1be9..b656ead9c 100644 --- a/radio/src/audio_arm.cpp +++ b/radio/src/audio_arm.cpp @@ -41,23 +41,112 @@ extern OS_MutexID audioMutex; const int16_t sineValues[] = { - 0, 64, 128, 191, 254, 316, 376, 435, 493, 548, - 601, 652, 700, 746, 789, 828, 864, 897, 926, 952, - 973, 991, 1005, 1015, 1021, 1024, 1021, 1015, 1005, 991, - 973, 952, 926, 897, 864, 828, 789, 746, 700, 652, - 601, 548, 493, 435, 376, 316, 254, 191, 128, 64, - 0, -64, -128, -191, -254, -316, -376, -435, -493, -548, - -601, -652, -700, -746, -789, -828, -864, -897, -926, -952, - -973, -991, -1005, -1015, -1021, -1024, -1021, -1015, -1005, -991, - -973, -952, -926, -897, -864, -828, -789, -746, -700, -652, - -601, -548, -493, -435, -376, -316, -254, -191, -128, -64, + 0, 6, 12, 18, 25, 31, 37, 43, 50, 56, + 62, 69, 75, 81, 87, 94, 100, 106, 112, 119, + 125, 131, 137, 144, 150, 156, 162, 168, 175, 181, + 187, 193, 199, 205, 212, 218, 224, 230, 236, 242, + 248, 254, 260, 267, 273, 279, 285, 291, 297, 303, + 309, 315, 321, 327, 333, 339, 344, 350, 356, 362, + 368, 374, 380, 386, 391, 397, 403, 409, 414, 420, + 426, 432, 437, 443, 449, 454, 460, 466, 471, 477, + 482, 488, 493, 499, 504, 510, 515, 521, 526, 531, + 537, 542, 547, 553, 558, 563, 568, 574, 579, 584, + 589, 594, 599, 604, 609, 615, 620, 625, 629, 634, + 639, 644, 649, 654, 659, 664, 668, 673, 678, 683, + 687, 692, 696, 701, 706, 710, 715, 719, 724, 728, + 732, 737, 741, 745, 750, 754, 758, 762, 767, 771, + 775, 779, 783, 787, 791, 795, 799, 803, 807, 811, + 814, 818, 822, 826, 829, 833, 837, 840, 844, 847, + 851, 854, 858, 861, 865, 868, 871, 875, 878, 881, + 884, 887, 890, 894, 897, 900, 903, 906, 908, 911, + 914, 917, 920, 922, 925, 928, 930, 933, 936, 938, + 941, 943, 946, 948, 950, 953, 955, 957, 959, 962, + 964, 966, 968, 970, 972, 974, 976, 978, 979, 981, + 983, 985, 986, 988, 990, 991, 993, 994, 996, 997, + 999, 1000, 1001, 1003, 1004, 1005, 1006, 1007, 1008, 1009, + 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1017, 1018, + 1019, 1019, 1020, 1020, 1021, 1021, 1022, 1022, 1022, 1023, + 1023, 1023, 1023, 1023, 1023, 1023, 1024, 1023, 1023, 1023, + 1023, 1023, 1023, 1023, 1022, 1022, 1022, 1021, 1021, 1020, + 1020, 1019, 1019, 1018, 1017, 1017, 1016, 1015, 1014, 1013, + 1012, 1011, 1010, 1009, 1008, 1007, 1006, 1005, 1004, 1003, + 1001, 1000, 999, 997, 996, 994, 993, 991, 990, 988, + 986, 985, 983, 981, 979, 978, 976, 974, 972, 970, + 968, 966, 964, 962, 959, 957, 955, 953, 950, 948, + 946, 943, 941, 938, 936, 933, 930, 928, 925, 922, + 920, 917, 914, 911, 908, 906, 903, 900, 897, 894, + 890, 887, 884, 881, 878, 875, 871, 868, 865, 861, + 858, 854, 851, 847, 844, 840, 837, 833, 829, 826, + 822, 818, 814, 811, 807, 803, 799, 795, 791, 787, + 783, 779, 775, 771, 767, 762, 758, 754, 750, 745, + 741, 737, 732, 728, 724, 719, 715, 710, 706, 701, + 696, 692, 687, 683, 678, 673, 668, 664, 659, 654, + 649, 644, 639, 634, 629, 625, 620, 615, 609, 604, + 599, 594, 589, 584, 579, 574, 568, 563, 558, 553, + 547, 542, 537, 531, 526, 521, 515, 510, 504, 499, + 493, 488, 482, 477, 471, 466, 460, 454, 449, 443, + 437, 432, 426, 420, 414, 409, 403, 397, 391, 386, + 380, 374, 368, 362, 356, 350, 344, 339, 333, 327, + 321, 315, 309, 303, 297, 291, 285, 279, 273, 267, + 260, 254, 248, 242, 236, 230, 224, 218, 212, 205, + 199, 193, 187, 181, 175, 168, 162, 156, 150, 144, + 137, 131, 125, 119, 112, 106, 100, 94, 87, 81, + 75, 69, 62, 56, 50, 43, 37, 31, 25, 18, + 12, 6, 0, -6, -12, -18, -25, -31, -37, -43, + -50, -56, -62, -69, -75, -81, -87, -94, -100, -106, + -112, -119, -125, -131, -137, -144, -150, -156, -162, -168, + -175, -181, -187, -193, -199, -205, -212, -218, -224, -230, + -236, -242, -248, -254, -260, -267, -273, -279, -285, -291, + -297, -303, -309, -315, -321, -327, -333, -339, -344, -350, + -356, -362, -368, -374, -380, -386, -391, -397, -403, -409, + -414, -420, -426, -432, -437, -443, -449, -454, -460, -466, + -471, -477, -482, -488, -493, -499, -504, -510, -515, -521, + -526, -531, -537, -542, -547, -553, -558, -563, -568, -574, + -579, -584, -589, -594, -599, -604, -609, -615, -620, -625, + -629, -634, -639, -644, -649, -654, -659, -664, -668, -673, + -678, -683, -687, -692, -696, -701, -706, -710, -715, -719, + -724, -728, -732, -737, -741, -745, -750, -754, -758, -762, + -767, -771, -775, -779, -783, -787, -791, -795, -799, -803, + -807, -811, -814, -818, -822, -826, -829, -833, -837, -840, + -844, -847, -851, -854, -858, -861, -865, -868, -871, -875, + -878, -881, -884, -887, -890, -894, -897, -900, -903, -906, + -908, -911, -914, -917, -920, -922, -925, -928, -930, -933, + -936, -938, -941, -943, -946, -948, -950, -953, -955, -957, + -959, -962, -964, -966, -968, -970, -972, -974, -976, -978, + -979, -981, -983, -985, -986, -988, -990, -991, -993, -994, + -996, -997, -999, -1000, -1001, -1003, -1004, -1005, -1006, -1007, + -1008, -1009, -1010, -1011, -1012, -1013, -1014, -1015, -1016, -1017, + -1017, -1018, -1019, -1019, -1020, -1020, -1021, -1021, -1022, -1022, + -1022, -1023, -1023, -1023, -1023, -1023, -1023, -1023, -1024, -1023, + -1023, -1023, -1023, -1023, -1023, -1023, -1022, -1022, -1022, -1021, + -1021, -1020, -1020, -1019, -1019, -1018, -1017, -1017, -1016, -1015, + -1014, -1013, -1012, -1011, -1010, -1009, -1008, -1007, -1006, -1005, + -1004, -1003, -1001, -1000, -999, -997, -996, -994, -993, -991, + -990, -988, -986, -985, -983, -981, -979, -978, -976, -974, + -972, -970, -968, -966, -964, -962, -959, -957, -955, -953, + -950, -948, -946, -943, -941, -938, -936, -933, -930, -928, + -925, -922, -920, -917, -914, -911, -908, -906, -903, -900, + -897, -894, -890, -887, -884, -881, -878, -875, -871, -868, + -865, -861, -858, -854, -851, -847, -844, -840, -837, -833, + -829, -826, -822, -818, -814, -811, -807, -803, -799, -795, + -791, -787, -783, -779, -775, -771, -767, -762, -758, -754, + -750, -745, -741, -737, -732, -728, -724, -719, -715, -710, + -706, -701, -696, -692, -687, -683, -678, -673, -668, -664, + -659, -654, -649, -644, -639, -634, -629, -625, -620, -615, + -609, -604, -599, -594, -589, -584, -579, -574, -568, -563, + -558, -553, -547, -542, -537, -531, -526, -521, -515, -510, + -504, -499, -493, -488, -482, -477, -471, -466, -460, -454, + -449, -443, -437, -432, -426, -420, -414, -409, -403, -397, + -391, -386, -380, -374, -368, -362, -356, -350, -344, -339, + -333, -327, -321, -315, -309, -303, -297, -291, -285, -279, + -273, -267, -260, -254, -248, -242, -236, -230, -224, -218, + -212, -205, -199, -193, -187, -181, -175, -168, -162, -156, + -150, -144, -137, -131, -125, -119, -112, -106, -100, -94, + -87, -81, -75, -69, -62, -56, -50, -43, -37, -31, + -25, -18, -12, -6, }; -#if 1 - const unsigned int toneVolumes[] = { 2, 4, 8, 12, 16 }; -#else - const unsigned int toneVolumes[] = { 4000, 8000, 16000, 24000, 32000 }; -#endif +const unsigned int toneVolumes[] = { 2, 4, 8, 12, 16 }; #if defined(SDCARD) const char * audioFilenames[] = { @@ -459,48 +548,49 @@ int AudioQueue::mixBeep(AudioContext &context, AudioBuffer *buffer, int volume, int result = 0; if (fragment.tone.duration > 0) { - result = AUDIO_BUFFER_SIZE; - if (fragment.tone.freq && context.state.tone.freq!=fragment.tone.freq && (!fragment.tone.freqIncr || abs(context.state.tone.freq-fragment.tone.freq) > 100)) { - int periods = BEEP_POINTS_COUNT / ((AUDIO_SAMPLE_RATE / fragment.tone.freq) + 1); - context.state.tone.count = (periods * AUDIO_SAMPLE_RATE) / fragment.tone.freq; - if (context.state.tone.idx >= context.state.tone.count) context.state.tone.idx = 0; -#if 1 - for (unsigned int i=0; i DIM(sineValues)) + end -= (end % DIM(sineValues)); + else + end = DIM(sineValues); + points = (double(end) - toneIdx) / context.state.tone.step; + } + else { + duration = AUDIO_BUFFER_DURATION; + fragment.tone.duration -= AUDIO_BUFFER_DURATION; + points = AUDIO_BUFFER_SIZE; } for (int i=0; idata[i], context.state.tone.points[context.state.tone.idx], fade); - context.state.tone.idx = context.state.tone.idx + 1; - if (context.state.tone.idx >= context.state.tone.count) { - context.state.tone.idx = 0; - if (end && i+BEEP_POINTS_COUNT>points) break; - } + int16_t sample = sineValues[int(toneIdx)] * toneVolumes[2+volume]; + mix(&buffer->data[i], sample, fade); + toneIdx += context.state.tone.step; + if ((unsigned int)toneIdx >= DIM(sineValues)) + toneIdx -= DIM(sineValues); } - fragment.tone.duration -= duration; - if (fragment.tone.duration > 0) + if (fragment.tone.duration > 0) { + context.state.tone.idx = toneIdx; return AUDIO_BUFFER_SIZE; + } + else { + context.state.tone.idx = 0; + } } if (fragment.tone.pause > 0) { @@ -548,7 +638,7 @@ void AudioQueue::wakeup() // mix the foreground context result = mixAudioContext(foregroundContext, buffer, g_eeGeneral.beepVolume, g_eeGeneral.wavVolume, fade); if (result > 0) { - size = max(size, result); + size = result; fade += 1; } diff --git a/radio/src/audio_arm.h b/radio/src/audio_arm.h index 9b45187b0..fadceeb59 100644 --- a/radio/src/audio_arm.h +++ b/radio/src/audio_arm.h @@ -47,11 +47,10 @@ #define AUDIO_BUFFER_SIZE (AUDIO_SAMPLE_RATE*AUDIO_BUFFER_DURATION/1000) #define AUDIO_BUFFER_COUNT (3) -#define BEEP_MIN_FREQ (440) +#define BEEP_MIN_FREQ (110) #define BEEP_DEFAULT_FREQ (2250) #define BEEP_KEY_UP_FREQ (BEEP_DEFAULT_FREQ+150) #define BEEP_KEY_DOWN_FREQ (BEEP_DEFAULT_FREQ-150) -#define BEEP_POINTS_COUNT 100 #define AUDIO_BUFFER_FREE (0) #define AUDIO_BUFFER_FILLED (1) @@ -105,10 +104,9 @@ class AudioContext { #endif struct { - int16_t points[BEEP_POINTS_COUNT]; - uint8_t count; - uint8_t idx; - uint16_t freq; + double step; + double idx; + double volume; } tone; } state;