|
| 1 | +/* |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2020 Arm Limited. |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "MicroBit.h" |
| 26 | +#include "MicroBitAudioProcessor.h" |
| 27 | + |
| 28 | +MicroBitAudioProcessor::MicroBitAudioProcessor(DataSource& source) : audiostream(source) |
| 29 | +{ |
| 30 | + audiostream.connect(*this); |
| 31 | + zeroOffset = 0; |
| 32 | + divisor = 1; |
| 33 | + //arm_rfft_fast_init_f32(&fft_instance, AUDIO_SAMPLES_NUMBER); |
| 34 | + |
| 35 | + /* Double Buffering: We allocate twice the number of samples*/ |
| 36 | + buf = (float *)malloc(sizeof(float) * AUDIO_SAMPLES_NUMBER * 2); |
| 37 | + output = (float *)malloc(sizeof(float) * AUDIO_SAMPLES_NUMBER); |
| 38 | + |
| 39 | + position = 0; |
| 40 | + |
| 41 | + if (buf == NULL || output == NULL) { |
| 42 | + DMESG("DEVICE_NO_RESOURCES"); |
| 43 | + target_panic(DEVICE_OOM); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +MicroBitAudioProcessor::~MicroBitAudioProcessor() |
| 48 | +{ |
| 49 | + free(buf); |
| 50 | + free(output); |
| 51 | +} |
| 52 | + |
| 53 | +int MicroBitAudioProcessor::pullRequest() |
| 54 | +{ |
| 55 | + int z = 0; |
| 56 | + int minimum = 0; |
| 57 | + int maximum = 0; |
| 58 | + int s; |
| 59 | + int8_t result; |
| 60 | + |
| 61 | + auto mic_samples = audiostream.pull().getBytes(); |
| 62 | + |
| 63 | + int16_t *data = (int16_t *) &mic_samples[0]; |
| 64 | + int samples = AUDIO_SAMPLES_NUMBER / 2; |
| 65 | + |
| 66 | + for (int i=0; i < samples; i++) |
| 67 | + { |
| 68 | + z += *data; |
| 69 | + |
| 70 | + s = (int) *data; |
| 71 | + s = s - zeroOffset; |
| 72 | + s = s / divisor; |
| 73 | + result = (int8_t)s; |
| 74 | + |
| 75 | + if (s < minimum) |
| 76 | + minimum = s; |
| 77 | + |
| 78 | + if (s > maximum) |
| 79 | + maximum = s; |
| 80 | + |
| 81 | + data++; |
| 82 | + buf[position++] = (float)result; |
| 83 | + |
| 84 | + if (!(position % AUDIO_SAMPLES_NUMBER)) |
| 85 | + { |
| 86 | + /* We have AUDIO_SAMPLES_NUMBER samples, we can run the FFT on them */ |
| 87 | + uint8_t offset = position <= AUDIO_SAMPLES_NUMBER ? 0 : AUDIO_SAMPLES_NUMBER; |
| 88 | + if (offset != 0) |
| 89 | + position = 0; |
| 90 | + |
| 91 | + //arm_rfft_fast_f32(&fft_instance, buf + offset, output, 0); |
| 92 | + DMESG("o[0]: %d", output[0]); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + z = z / samples; |
| 97 | + zeroOffset = z; |
| 98 | + return DEVICE_OK; |
| 99 | +} |
| 100 | + |
| 101 | +int MicroBitAudioProcessor::setDivisor(int d) |
| 102 | +{ |
| 103 | + divisor = d; |
| 104 | + return DEVICE_OK; |
| 105 | +} |
0 commit comments