aboutsummaryrefslogtreecommitdiff
path: root/tuner.c
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2017-04-09 01:20:56 +0200
committerReiner Herrmann <reiner@reiner-h.de>2017-04-09 01:25:41 +0200
commit4b3082a34d7200d4b8ac980949bae355fc982c6b (patch)
tree3f3edd2e0dbfc217220d89d7eff5b4dc7dd9231e /tuner.c
parent7af91e2bc4a51670343ba3d5852c8e6b688a5a13 (diff)
Minor cleanup and pretty printing
Diffstat (limited to 'tuner.c')
-rw-r--r--tuner.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/tuner.c b/tuner.c
index c4e6221..4fcae2d 100644
--- a/tuner.c
+++ b/tuner.c
@@ -8,6 +8,8 @@
#include <fftw3.h>
#include <alsa/asoundlib.h>
+#include "common.h"
+
#define SAMPLE_RATE 8000
#define FFT_SIZE (1<<13)
@@ -91,7 +93,6 @@ static void find_note(double freq)
if (freq > notes[i+1]) {
continue;
}
- printf("%f %f\n", freq, (notes[i] + notes[i+1])/2);
if (freq > (notes[i] + notes[i+1])/2) {
note = note_str[i+1];
dir = -1;
@@ -108,10 +109,8 @@ static void find_note(double freq)
dir = 0;
}
- if (dir < 0) printf(">");
- printf("%c", note);
- if (dir > 0) printf("<");
- printf("\n");
+ printf("\rNote: %c%c%c Frequency: %.2f Hz ", dir<0?'>':' ', note, dir>0?'<':' ', freq);
+ fflush(stdout);
}
static void process_frames()
@@ -131,7 +130,6 @@ static void process_frames()
}
}
freq = (float)index * SAMPLE_RATE / FFT_SIZE;
- printf("freq: %f Hz\n", freq);
find_note(freq);
}
@@ -147,12 +145,17 @@ static void capture(snd_pcm_t *pcm_handle)
process_frames();
}
}
+ printf("\n");
}
int main()
{
snd_pcm_t *pcm_handle;
+ if (toggle_nonblocking_input() < 0) {
+ return 1;
+ }
+
if (snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_CAPTURE, 0) < 0) {
fprintf(stderr, "Failed opening device for capturing\n");
return 1;
@@ -166,11 +169,14 @@ int main()
signal(SIGTERM, handle_signals);
fft_init();
-
capture(pcm_handle);
snd_pcm_close(pcm_handle);
fft_cleanup();
+ if (toggle_nonblocking_input() < 0) {
+ return 1;
+ }
+
return 0;
}