-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisten.c
257 lines (226 loc) · 7.08 KB
/
listen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* main.c - chromatic guitar tuner
*
* Copyright (C) 2012 by Bjorn Roche
* Tweaked by Libby Miller for use on a Raspberry PI, June 2014
*
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. This software is provided "as is" without express or
* implied warranty.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include "libfft.h"
#include "libfft.c"
#include "portaudio.h"
#include <wiringPi.h>
/*#include <bits/sigaction.h>*/
/* -- some basic parameters -- */
#define SAMPLE_RATE (44100)
#define FFT_SIZE (1024)
#define FFT_EXP_SIZE (10)
//#define FFT_SIZE (4096)
//#define FFT_EXP_SIZE (12)
#ifndef M_PI
#define M_PI (3.1415926536)
#endif
/* -- functions declared and used here -- */
void buildHammingWindow( float *window, int size );
void buildHanWindow( float *window, int size );
void applyWindow( float *window, float *data, int size );
//a must be of length 2, and b must be of length 3
void computeSecondOrderLowPassParameters( float srate, float f, float *a, float *b );
//mem must be of length 4.
float processSecondOrderFilter( float x, float *mem, float *a, float *b );
void signalHandler( int signum ) ;
static bool running = true;
/* -- main function -- */
int main( int argc, char **argv ) {
/* wiring pi setup */
wiringPiSetup () ;
pinMode (0, OUTPUT) ;
pinMode (1, OUTPUT) ;
pinMode (2, OUTPUT) ;
PaStreamParameters inputParameters;
float a[2], b[3], mem1[4], mem2[4];
float data[FFT_SIZE];
float datai[FFT_SIZE];
float window[FFT_SIZE];
float freqTable[FFT_SIZE];
char * noteNameTable[FFT_SIZE];
float notePitchTable[FFT_SIZE];
void * fft = NULL;
PaStream *stream = NULL;
PaError err = 0;
struct sigaction action;
// add signal listen so we know when to exit:
action.sa_handler = signalHandler;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
sigaction (SIGINT, &action, NULL);
sigaction (SIGHUP, &action, NULL);
sigaction (SIGTERM, &action, NULL);
// build the window, fft, etc
buildHanWindow( window, FFT_SIZE );
fft = initfft( FFT_EXP_SIZE );
// computeSecondOrderLowPassParameters( SAMPLE_RATE, 3500, a, b );
computeSecondOrderLowPassParameters( SAMPLE_RATE, 20000, a, b );
mem1[0] = 0; mem1[1] = 0; mem1[2] = 0; mem1[3] = 0;
mem2[0] = 0; mem2[1] = 0; mem2[2] = 0; mem2[3] = 0;
//freq/note tables
int i = 0;
for(i=0; i<FFT_SIZE; ++i ) {
freqTable[i] = ( SAMPLE_RATE * i ) / (float) ( FFT_SIZE );
}
// initialize portaudio
err = Pa_Initialize();
printf( "\n\nversion of portaudio %s ",Pa_GetVersionText());
int numDevices;
numDevices = Pa_GetDeviceCount();
if( numDevices < 0 ){
printf( "ERROR: Pa_CountDevices returned 0x%x\n", numDevices );
err = numDevices;
goto error;
}
if( err != paNoError ) goto error;
inputParameters.device = Pa_GetDefaultInputDevice();
inputParameters.channelCount = 1;
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ;
inputParameters.hostApiSpecificStreamInfo = NULL;
printf( "Opening %s\n",
Pa_GetDeviceInfo( inputParameters.device )->name );
err = Pa_OpenStream( &stream,
&inputParameters,
NULL, //no output
SAMPLE_RATE,
FFT_SIZE,
paClipOff,
NULL,
NULL );
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
// this is the main loop where we listen to and
// process audio.
while( running )
{
// read some data
err = Pa_ReadStream( stream, data, FFT_SIZE );
//if( err ) goto error; //FIXME: we don't want to err on xrun
// not sure what to do with this - get overflow errors
if(err) printf(".");
if(!err){
// low-pass
int j = 0;
for( j=0; j<FFT_SIZE; ++j ) {
data[j] = processSecondOrderFilter( data[j], mem1, a, b );
data[j] = processSecondOrderFilter( data[j], mem2, a, b );
}
// window
applyWindow( window, data, FFT_SIZE );
// do the fft
for( j=0; j<FFT_SIZE; ++j )
datai[j] = 0;
applyfft( fft, data, datai, false );
//find the peak
float maxVal = -1;
int maxIndex = -1;
for( j=0; j<FFT_SIZE/2; ++j ) {
float v = data[j] * data[j] + datai[j] * datai[j] ;
if( v > maxVal ) {
maxVal = v;
maxIndex = j;
}
}
float freq = freqTable[maxIndex];
// if(freq > 350.0 && freq < 3400 ){
if(freq > 0.0 && freq < 10000.0){
printf("freq %f \n",freq);
/* turn off all RGB */
digitalWrite (0, LOW);
digitalWrite (1, LOW);
digitalWrite (2, LOW);
/* pick an RGB value depending on the audio level */
/* super simple for now! */
if(freq > 0 && freq < 1000){
digitalWrite (0, HIGH);
}
if(freq > 1000 && freq < 3000){
digitalWrite (1, HIGH);
}
if(freq > 3000 && freq < 10000){
digitalWrite (2, HIGH);
}
}
}
}
err = Pa_StopStream( stream );
if( err != paNoError ) goto error;
// cleanup
destroyfft( fft );
Pa_Terminate();
return 0;
error:
if( stream ) {
Pa_AbortStream( stream );
Pa_CloseStream( stream );
}
destroyfft( fft );
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return 1;
}
void buildHammingWindow( float *window, int size )
{
int i = 0;
for( i=0; i<size; ++i )
window[i] = .54 - .46 * cos( 2 * M_PI * i / (float) size );
}
void buildHanWindow( float *window, int size )
{
int i = 0;
for( i=0; i<size; ++i )
window[i] = .5 * ( 1 - cos( 2 * M_PI * i / (size-1.0) ) );
}
void applyWindow( float *window, float *data, int size )
{
int i=0;
for( i=0; i<size; ++i )
data[i] *= window[i] ;
}
void computeSecondOrderLowPassParameters( float srate, float f, float *a, float *b )
{
float a0;
float w0 = 2 * M_PI * f/srate;
float cosw0 = cos(w0);
float sinw0 = sin(w0);
//float alpha = sinw0/2;
float alpha = sinw0/2 * sqrt(2);
a0 = 1 + alpha;
a[0] = (-2*cosw0) / a0;
a[1] = (1 - alpha) / a0;
b[0] = ((1-cosw0)/2) / a0;
b[1] = ( 1-cosw0) / a0;
b[2] = b[0];
}
float processSecondOrderFilter( float x, float *mem, float *a, float *b )
{
float ret = b[0] * x + b[1] * mem[0] + b[2] * mem[1]
- a[0] * mem[2] - a[1] * mem[3] ;
mem[1] = mem[0];
mem[0] = x;
mem[3] = mem[2];
mem[2] = ret;
return ret;
}
void signalHandler( int signum ) { running = false; }