diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2017-04-09 01:20:56 +0200 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2017-04-09 01:25:41 +0200 |
| commit | 4b3082a34d7200d4b8ac980949bae355fc982c6b (patch) | |
| tree | 3f3edd2e0dbfc217220d89d7eff5b4dc7dd9231e | |
| parent | 7af91e2bc4a51670343ba3d5852c8e6b688a5a13 (diff) | |
Minor cleanup and pretty printing
| -rw-r--r-- | Makefile | 18 | ||||
| -rw-r--r-- | common.c | 48 | ||||
| -rw-r--r-- | common.h | 6 | ||||
| -rw-r--r-- | metronome.c | 48 | ||||
| -rw-r--r-- | tuner.c | 20 |
5 files changed, 77 insertions, 63 deletions
@@ -1,18 +1,14 @@ CFLAGS = -Wall -Wextra -Werror -ggdb3 -O3 -LDFLAGS = -lasound -lfftw3 -lm -lrt +LDFLAGS = -lasound -lm -lrt -METRONOME_OBJS = metronome.o -METRONOME_BIN = metronome -TUNER_OBJS = tuner.o -TUNER_BIN = tuner - -OBJS = $(METRONOME_OBJS) $(TUNER_OBJS) -BINS = $(METRONOME_BIN) $(TUNER_BIN) +BINS = metronome tuner all: $(BINS) -$(METRONOME_BIN): $(METRONOME_OBJS) -$(TUNER_BIN): $(TUNER_OBJS) +metronome: metronome.o common.o + +tuner: LDFLAGS += -lfftw3 +tuner: tuner.o common.o clean: - rm -f $(OBJS) $(BINS) + rm -f *.o $(BINS) diff --git a/common.c b/common.c new file mode 100644 index 0000000..467a92c --- /dev/null +++ b/common.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> + +int toggle_nonblocking_input() +{ + static struct termios *saved_termios = NULL; + int ret = 0; + + if (saved_termios) { + /* restore old settings */ + if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, saved_termios)) < 0) { + fprintf(stderr, "Failed restoring termios settings: %s\n", strerror(errno)); + } + free(saved_termios); + saved_termios = NULL; + + /* restore cursor */ + fprintf(stderr, "\033[?25h"); + } else { + struct termios new_termios; + + /* get and backup current settings */ + saved_termios = malloc(sizeof(struct termios)); + if (tcgetattr(STDIN_FILENO, saved_termios) < 0) { + fprintf(stderr, "Failed retrieving current termios setings: %s\n", strerror(errno)); + free(saved_termios); + saved_termios = NULL; + return -1; + } + memcpy(&new_termios, saved_termios, sizeof(struct termios)); + + /* disable echo and canonical mode (line by line input; line editing) */ + new_termios.c_lflag &= ~(ICANON | ECHO); + + if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, &new_termios)) < 0) { + fprintf(stderr, "Failed setting to changed termios: %s\n", strerror(errno)); + } + + /* disable cursor */ + fprintf(stderr, "\033[?25l"); + } + + return ret; +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..80c552f --- /dev/null +++ b/common.h @@ -0,0 +1,6 @@ +#ifndef __COMMON_H__ +#define __COMMON_H__ + +int toggle_nonblocking_input(); + +#endif // __COMMON_H__ diff --git a/metronome.c b/metronome.c index adbae9d..6860dae 100644 --- a/metronome.c +++ b/metronome.c @@ -11,10 +11,10 @@ #include <signal.h> #include <sys/time.h> #include <sys/select.h> -#include <termios.h> -#include <unistd.h> #include <alsa/asoundlib.h> +#include "common.h" + #define SAMPLE_RATE 8000 #define BUFFER_SIZE 512 @@ -84,48 +84,6 @@ static int set_alsa_params(snd_pcm_t *pcm_handle) return 0; } -static int toggle_nonblocking_input() -{ - static struct termios *saved_termios = NULL; - int ret = 0; - - if (saved_termios) { - /* restore old settings */ - if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, saved_termios)) < 0) { - fprintf(stderr, "Failed restoring termios settings: %s\n", strerror(errno)); - } - free(saved_termios); - saved_termios = NULL; - - /* restore cursor */ - fprintf(stderr, "\033[?25h"); - } else { - struct termios new_termios; - - /* get and backup current settings */ - saved_termios = malloc(sizeof(struct termios)); - if (tcgetattr(STDIN_FILENO, saved_termios) < 0) { - fprintf(stderr, "Failed retrieving current termios setings: %s\n", strerror(errno)); - free(saved_termios); - saved_termios = NULL; - return -1; - } - memcpy(&new_termios, saved_termios, sizeof(struct termios)); - - /* disable echo and canonical mode (line by line input; line editing) */ - new_termios.c_lflag &= ~(ICANON | ECHO); - - if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, &new_termios)) < 0) { - fprintf(stderr, "Failed setting to changed termios: %s\n", strerror(errno)); - } - - /* disable cursor */ - fprintf(stderr, "\033[?25l"); - } - - return ret; -} - static int input_available() { struct timeval timeout = {0, 0}; @@ -243,6 +201,7 @@ static void play(snd_pcm_t *pcm_handle, const char *pattern) play_tone(pcm_handle, 's'); } } + printf("\n"); } static void usage(const char *name) @@ -310,7 +269,6 @@ int main(int argc, char *argv[]) instructions(); prepare_tones(); play(pcm_handle, pattern); - printf("\n"); snd_pcm_close(pcm_handle); @@ -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; } |
