@@ -30,15 +30,17 @@ MicroBitAudioProcessor::MicroBitAudioProcessor(DataSource& source) : audiostream
30
30
audiostream.connect (*this );
31
31
zeroOffset = 0 ;
32
32
divisor = 1 ;
33
- // arm_rfft_fast_init_f32(&fft_instance, AUDIO_SAMPLES_NUMBER);
33
+ arm_rfft_fast_init_f32 (&fft_instance, AUDIO_SAMPLES_NUMBER);
34
34
35
35
/* Double Buffering: We allocate twice the number of samples*/
36
36
buf = (float *)malloc (sizeof (float ) * AUDIO_SAMPLES_NUMBER * 2 );
37
37
output = (float *)malloc (sizeof (float ) * AUDIO_SAMPLES_NUMBER);
38
+ mag = (float *)malloc (sizeof (float ) * AUDIO_SAMPLES_NUMBER / 2 );
38
39
39
40
position = 0 ;
41
+ recording = false ;
40
42
41
- if (buf == NULL || output == NULL ) {
43
+ if (buf == NULL || output == NULL || mag == NULL ) {
42
44
DMESG (" DEVICE_NO_RESOURCES" );
43
45
target_panic (DEVICE_OOM);
44
46
}
@@ -48,6 +50,7 @@ MicroBitAudioProcessor::~MicroBitAudioProcessor()
48
50
{
49
51
free (buf);
50
52
free (output);
53
+ free (mag);
51
54
}
52
55
53
56
int MicroBitAudioProcessor::pullRequest ()
@@ -56,40 +59,59 @@ int MicroBitAudioProcessor::pullRequest()
56
59
int minimum = 0 ;
57
60
int maximum = 0 ;
58
61
int s;
59
- int8_t result;
62
+ int result;
60
63
61
- auto mic_samples = audiostream.pull ().getBytes ();
64
+ auto mic_samples = audiostream.pull ();
65
+
66
+ if (!recording)
67
+ return DEVICE_OK;
62
68
63
69
int16_t *data = (int16_t *) &mic_samples[0 ];
64
- int samples = AUDIO_SAMPLES_NUMBER / 2 ;
70
+ int samples = mic_samples. length () / 2 ;
65
71
66
72
for (int i=0 ; i < samples; i++)
67
73
{
68
74
z += *data;
69
75
70
76
s = (int ) *data;
71
- s = s - zeroOffset;
72
- s = s / divisor;
73
- result = ( int8_t ) s;
77
+ // s = s - zeroOffset;
78
+ // s = s / divisor;
79
+ result = s;
74
80
75
81
if (s < minimum)
76
82
minimum = s;
77
83
78
84
if (s > maximum)
79
85
maximum = s;
80
86
87
+ // if (recording)
88
+ // rec[position] = (float)result;
89
+
81
90
data++;
82
91
buf[position++] = (float )result;
83
92
93
+
84
94
if (!(position % AUDIO_SAMPLES_NUMBER))
85
95
{
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;
96
+ float maxValue = 0 ;
97
+ uint32_t index = 0 ;
98
+
99
+ /* We have AUDIO_SAMPLES_NUMBER samples, we can run the FFT on them */
100
+ uint16_t offset = position <= AUDIO_SAMPLES_NUMBER ? 0 : AUDIO_SAMPLES_NUMBER;
88
101
if (offset != 0 )
89
102
position = 0 ;
90
103
91
- // arm_rfft_fast_f32(&fft_instance, buf + offset, output, 0);
92
- DMESG (" o[0]: %d" , output[0 ]);
104
+ DMESG (" Run FFT, %d" , offset);
105
+ arm_rfft_fast_f32 (&fft_instance, buf + offset, output, 0 );
106
+ arm_cmplx_mag_f32 (output, mag, AUDIO_SAMPLES_NUMBER / 2 );
107
+ arm_max_f32 (mag + 1 , AUDIO_SAMPLES_NUMBER / 2 - 1 , &maxValue, &index);
108
+
109
+ uint32_t freq = ((uint32_t )MIC_SAMPLE_RATE / AUDIO_SAMPLES_NUMBER) * (index + 1 );
110
+ DMESG (" Freq: %d (max: %d.%d, Index: %d)" ,
111
+ freq,
112
+ (int )maxValue,
113
+ ((int )(maxValue * 100 ) % 100 ),
114
+ index);
93
115
}
94
116
}
95
117
@@ -103,3 +125,16 @@ int MicroBitAudioProcessor::setDivisor(int d)
103
125
divisor = d;
104
126
return DEVICE_OK;
105
127
}
128
+
129
+
130
+ void MicroBitAudioProcessor::startRecording ()
131
+ {
132
+ this ->recording = true ;
133
+ DMESG (" START RECORDING" );
134
+ }
135
+
136
+ void MicroBitAudioProcessor::stopRecording (MicroBit& uBit)
137
+ {
138
+ this ->recording = false ;
139
+ DMESG (" STOP RECORDING" );
140
+ }
0 commit comments