Skip to content

Commit ad41d36

Browse files
raphaelgaultmicrobit-carlos
authored andcommitted
fft: Define MicroBitAudioProcessor
Signed-off-by: Raphael Gault <[email protected]>
1 parent 539a8c7 commit ad41d36

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

inc/MicroBitAudioProcessor.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 "DataStream.h"
27+
#define ARM_MATH_CM4
28+
#include "arm_math.h"
29+
30+
#ifndef MICROBIT_AUDIO_PROCESSOR_H
31+
#define MICROBIT_AUDIO_PROCESSOR_H
32+
33+
#define AUDIO_SAMPLES_NUMBER 256
34+
35+
class MicroBitAudioProcessor : public DataSink
36+
{
37+
DataSource &audiostream;
38+
int zeroOffset; // unsigned value that is the best effort guess of the zero point of the data source
39+
int divisor; // Used for LINEAR modes
40+
arm_rfft_fast_instance_f32 fft_instance;
41+
float *buf;
42+
float *output;
43+
uint16_t position;
44+
45+
public:
46+
MicroBitAudioProcessor(DataSource& source);
47+
~MicroBitAudioProcessor();
48+
virtual int pullRequest();
49+
int setDivisor(int d);
50+
};
51+
52+
#endif

source/MicroBitAudioProcessor.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)