diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-03-02 17:49:33 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-03-02 17:49:33 +0100 |
commit | 2e7c6404ad67bdd0659efdf1d9f2d9a52b2005df (patch) | |
tree | 4e6bd2c96c618e90eeeca717fffba872ab942d19 /src/common | |
parent | a8107b3b360449d2edb6f9dc122ed47de88ed3bc (diff) | |
parent | 021632411d14c4f87e16560ed56a5681da55170b (diff) | |
download | glutte-o-matic-2e7c6404ad67bdd0659efdf1d9f2d9a52b2005df.tar.gz glutte-o-matic-2e7c6404ad67bdd0659efdf1d9f2d9a52b2005df.tar.bz2 glutte-o-matic-2e7c6404ad67bdd0659efdf1d9f2d9a52b2005df.zip |
Merge branch '1750_soft'
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/includes/Audio/audio.h | 2 | ||||
-rw-r--r-- | src/common/includes/Audio/audio_in.h | 40 | ||||
-rw-r--r-- | src/common/includes/Audio/tone.h | 47 | ||||
-rw-r--r-- | src/common/sourcelist.txt | 2 | ||||
-rw-r--r-- | src/common/src/Audio/audio_in.c | 48 | ||||
-rw-r--r-- | src/common/src/Audio/tone.c | 95 | ||||
-rw-r--r-- | src/common/src/Core/main.c | 10 |
7 files changed, 242 insertions, 2 deletions
diff --git a/src/common/includes/Audio/audio.h b/src/common/includes/Audio/audio.h index 7aeb6e9..d48cbe5 100644 --- a/src/common/includes/Audio/audio.h +++ b/src/common/includes/Audio/audio.h @@ -32,7 +32,7 @@ void audio_stop_dma(void); // Initialize and power up audio hardware. Use the above defines for the parameters. // Can probably only be called once. -void audio_initialize(int plln,int pllr,int i2sdiv,int i2sodd, int rate); +void audio_initialize(int plln, int pllr, int i2sdiv, int i2sodd, int rate); // Power up and down the audio hardware. void audio_put_codec_in_reset(void); diff --git a/src/common/includes/Audio/audio_in.h b/src/common/includes/Audio/audio_in.h new file mode 100644 index 0000000..86c93b2 --- /dev/null +++ b/src/common/includes/Audio/audio_in.h @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#ifndef __AUDIO_IN_H__ +#define __AUDIO_IN_H__ + +#include <stdio.h> +#include "FreeRTOS.h" + +#define AUDIO_IN_BUF_LEN 2048 +#define AUDIO_IN_RATE 8000 + +int16_t * audio_in_buffer; + +void audio_in_initialize(int rate); +void audio_in_initialize_plateform(int rate); +void audio_in_buffer_ready(void); + +#endif diff --git a/src/common/includes/Audio/tone.h b/src/common/includes/Audio/tone.h new file mode 100644 index 0000000..0e80228 --- /dev/null +++ b/src/common/includes/Audio/tone.h @@ -0,0 +1,47 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Matthias P. Braendli, Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#ifndef __TONE_H_ +#define __TONE_H_ + +#include <stdio.h> +#include "Audio/audio_in.h" + +#define TONE_BUFFER_LEN AUDIO_IN_BUF_LEN + +#define TONE_N 100 + +struct tone_detector { + float coef; + float Q1; + float Q2; + float threshold; + + int num_samples_analysed; +}; + +void tone_init(int threshold); +int tone_detect_1750(int16_t sample); + +#endif diff --git a/src/common/sourcelist.txt b/src/common/sourcelist.txt index eada47f..9d6ec78 100644 --- a/src/common/sourcelist.txt +++ b/src/common/sourcelist.txt @@ -8,3 +8,5 @@ src/Core/fsm.c src/Core/main.c src/Audio/cw.c src/Audio/audio.c +src/Audio/audio_in.c +src/Audio/tone.c diff --git a/src/common/src/Audio/audio_in.c b/src/common/src/Audio/audio_in.c new file mode 100644 index 0000000..b046a62 --- /dev/null +++ b/src/common/src/Audio/audio_in.c @@ -0,0 +1,48 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#include "Audio/tone.h" +#include "Audio/audio_in.h" + +int16_t audio_in_buffer_0[AUDIO_IN_BUF_LEN]; +int16_t audio_in_buffer_1[AUDIO_IN_BUF_LEN]; + +void audio_in_initialize(int rate) { + audio_in_buffer = audio_in_buffer_0; + audio_in_initialize_plateform(rate); +} + +void audio_in_buffer_ready() { + + for (int i = 0; i < AUDIO_IN_BUF_LEN; i++) { + tone_detect_1750(audio_in_buffer[i]); + } + + if (audio_in_buffer == audio_in_buffer_0) { + audio_in_buffer = audio_in_buffer_1; + } else { + audio_in_buffer = audio_in_buffer_0; + } + +} diff --git a/src/common/src/Audio/tone.c b/src/common/src/Audio/tone.c new file mode 100644 index 0000000..60226ce --- /dev/null +++ b/src/common/src/Audio/tone.c @@ -0,0 +1,95 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony, Matthias P. Braendli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#include "Audio/tone.h" +#include "Core/common.h" + +#include <stdlib.h> + +#ifdef SIMULATOR +#include <math.h> +#define arm_cos_f32 cosf +#define arm_sin_f32 sinf +#else +#include "arm_math.h" +#endif + +static struct tone_detector detector_1750; + +int TONE_1750_DETECTED = 0; + +static void init_tone(struct tone_detector* detector, int freq, int threshold) { + detector->coef = 2.0f * arm_cos_f32(2.0f * FLOAT_PI * freq / AUDIO_IN_RATE); + detector->Q1 = 0; + detector->Q2 = 0; + detector->threshold = threshold; + detector->num_samples_analysed = 0; +} + +void tone_init(int threshold) { + init_tone(&detector_1750, 1750, threshold); +} + +/* Analyse a sample. Returns -1 if more samples needed, 0 if no tone detected, + * 1 if a tone was detected. + */ +static inline int analyse_sample(int16_t sample, struct tone_detector *detector) +{ + float Q0 = detector->coef * detector->Q1 - detector->Q2 + sample; + detector->Q2 = detector->Q1; + detector->Q1 = Q0; + + detector->num_samples_analysed++; + + if (detector->num_samples_analysed == TONE_N) { + const float m = sqrtf( + detector->Q1 * detector->Q1 + + detector->Q2 * detector->Q2 - + detector->coef * detector->Q1 * detector->Q2); + + detector->Q1 = 0; + detector->Q2 = 0; + detector->num_samples_analysed = 0; + + if (m > detector->threshold) { + return 1; + } + else { + return 0; + } + } + else { + return -1; + } +} + +int tone_detect_1750(int16_t sample) +{ + int r = analyse_sample(sample, &detector_1750); + if (r == 0 || r == 1) { + TONE_1750_DETECTED = r; + } + return r; +} + diff --git a/src/common/src/Core/main.c b/src/common/src/Core/main.c index 5ccc768..18c25b2 100644 --- a/src/common/src/Core/main.c +++ b/src/common/src/Core/main.c @@ -35,6 +35,8 @@ /* Includes */ #include "Audio/audio.h" +#include "Audio/audio_in.h" +#include "Audio/tone.h" #include "Audio/cw.h" #include "GPIO/pio.h" #include "GPIO/i2c.h" @@ -194,8 +196,11 @@ static void launcher_task(void __attribute__ ((unused))*pvParameters) trigger_fault(FAULT_SOURCE_MAIN); } - usart_debug_puts("Audio init\r\n"); + usart_debug_puts("Tone init\r\n"); +#warning TODO + tone_init(20000); + usart_debug_puts("Audio init\r\n"); audio_initialize(Audio16000HzSettings); usart_debug_puts("Audio set volume\r\n"); @@ -204,6 +209,9 @@ static void launcher_task(void __attribute__ ((unused))*pvParameters) usart_debug_puts("Audio set callback\r\n"); audio_play_with_callback(audio_callback, NULL); + usart_debug_puts("Audio in init\r\n"); + audio_in_initialize(AUDIO_IN_RATE); + usart_debug_puts("Init done.\r\n"); int last_qrp_from_supply = 0; |