~andreafeletto/dsp-dtmf-device

5017f71f50692ff59366e20cbf2aa41bbb7eacff — Andrea Feletto 5 months ago main
argument parsing and basic signal generation

MIT license
error utils functions
sender.c component
6 files changed, 176 insertions(+), 0 deletions(-)

A LICENSE
A Makefile
A config.mk
A sender.c
A utils.c
A utils.h
A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2020 Andrea Feletto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => Makefile +19 -0
@@ 1,19 @@

.POSIX:

include config.mk

SRC = sender.c utils.c
OBJ = ${SRC:.c=.o}

all: dtmf-sender

dtmf-sender: ${OBJ}
	${CC} -o $@ ${OBJ} ${LDFLAGS}

${OBJ}: utils.h

clean:
	rm -f ${OBJ} sender.o dtmf-sender

.PHONY: all clean
\ No newline at end of file

A  => config.mk +11 -0
@@ 1,11 @@

DESTDIR ?= .
PREFIX ?=

CC = cc

INCS = -I/usr/include/dtmf
LIBS = -lm -ldtmf

CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os ${INCS}
LDFLAGS = ${LIBS}

A  => sender.c +86 -0
@@ 1,86 @@

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dtmf/generator.h>

#include "utils.h"

static void
usage(void) {
	fputs("usage: dtmf-sender key [-f file] [-s fs] [-a amp] [-t sec]\n",
		stderr);
}

int
main(int argc, char **argv) {
	char key;
	FILE *ofp;
	double fs, amp, time;
	size_t i, len;
	struct dtmf_signal signal;
	struct dtmf_builder_sin_args args[2];

	if (argc < 2) {
		usage();
		return EXIT_FAILURE;
	}

	if (strlen(argv[1]) == 1) {
		if (dtmf_gen_key_exist(argv[1][0]) != 0) {
			key = argv[1][0];
		} else {
			warn("invalid key '%c'", argv[1][0]);
			return EXIT_FAILURE;
		}
	} else {
		usage();
		return EXIT_FAILURE;
	}

	ofp = stdout;
	fs = 8000.0;
	amp = 5.0;
	time = 0.5;

	for (i = 2; i < argc; i++) {
		if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
			usage();
			return EXIT_FAILURE;
		} else if (!strcmp(argv[i], "-f")) {
			if ((ofp = fopen(argv[++i], "w")) == NULL) {
				warn("cannot open output file '%s'", argv[i]);
				return EXIT_FAILURE;
			}
		} else if (!strcmp(argv[i], "-s")) {
			if ((fs = strtod(argv[++i], NULL)) <= 0) {
				warn("invalid sampling frequency");
				return EXIT_FAILURE;
			}
		} else if (!strcmp(argv[i], "-a")) {
			if ((amp = strtod(argv[++i], NULL)) == 0) {
				warn("invalid amplitude");
				return EXIT_FAILURE;
			}
		} else if (!strcmp(argv[i], "-t")) {
			if ((time = strtod(argv[++i], NULL)) <= 0) {
				warn("invalid time duration");
				return EXIT_FAILURE;
			}
		}
	}
	
	len = (size_t)round(time * fs);
	dtmf_signal_init(&signal, len, fs);

	args[0].a = args[1].a = amp;
	args[0].p = args[1].p = 0.0;
	dtmf_gen_generate_noisy(&signal, key, &args[0]);

	for (i = 0; i < signal.size; i++) {
		fprintf(ofp, "%f\n", signal.data[i]);
	}
	return EXIT_SUCCESS;
}

A  => utils.c +28 -0
@@ 1,28 @@

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "utils.h"

void
verr(const char *fmt, va_list ap) {
	fprintf(stderr, "error: ");
	vfprintf(stderr, fmt, ap);

	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}
}

void
warn(const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	verr(fmt, ap);
	va_end(ap);
}

A  => utils.h +11 -0
@@ 1,11 @@

#ifndef DSP_DTMF_DEVICE_UTILS
#define DSP_DTMF_DEVICE_UTILS

#include <stdarg.h>

void verr(const char *fmt, va_list ap);
void warn(const char *fmt, ...);
void die(const char *fmt, ...);

#endif