From 6873c47633e0b6870c3421a42a3357c818ed6cd5 Mon Sep 17 00:00:00 2001 From: Josef Miegl Date: Mon, 14 Jun 2021 22:53:26 +0200 Subject: [PATCH] add ask2ft --- .gitignore | 1 + Makefile | 3 +++ ask2ft.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 ask2ft.c diff --git a/.gitignore b/.gitignore index 6abce07..86dfdd8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ garaz.sh pitxft +ask2ft *.o diff --git a/Makefile b/Makefile index 8080c52..63a7f12 100644 --- a/Makefile +++ b/Makefile @@ -35,5 +35,8 @@ mailbox.o: mailbox.c mailbox.h pitxft.o: pitxft.c mailbox.h $(CC) $(CFLAGS) pitxft.c +ask2ft: ask2ft.c + $(CC) -O2 -o ask2ft ask2ft.c + clean: rm -f *.o diff --git a/ask2ft.c b/ask2ft.c new file mode 100644 index 0000000..180abe7 --- /dev/null +++ b/ask2ft.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + +#pragma pack(1) + +struct __attribute__((__packed__)) ft { + double frequency; + uint32_t duration; + uint32_t padding; +}; + +int main(int argc, char **argv) { + // arguments: freq samplerate samples/bit + + if(argc < 4) { + fprintf(stderr, "Error: set arguments: freq samplerate samples/bit\n"); + exit(1); + } + + int frequency = atoi(argv[1]); + int samplerate = atoi(argv[2]); + int samples = atoi(argv[3]); + + fprintf(stderr, "Frequency: %d, Samplerate: %d, Samples/bit: %d\n", frequency, samplerate, samples); + + freopen(NULL, "wb", stdout); // ft is a binary format + + size_t ilen = 64; + char* input = calloc(ilen, sizeof(char)); + for(size_t ch = 0; (input[ch] = getc(stdin)) != EOF && input[ch] != '\n'; ch++) { + if(ch == ilen -1) { + ilen *= 2; + input = realloc(input, sizeof(char)*ilen); + } + } + + const char binary[16][5] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; + const char digits[] = "0123456789abcdef"; + char* bininput = calloc(strlen(input)*4+1, sizeof(char)); + + size_t bi = 0; + while(input[bi]) { + char *v = strchr(digits, tolower(input[bi++])); + if(v) strcat(bininput, binary[v - digits]); + } + + size_t i = 0; + char prevchar = 0; + size_t prevcharcount = 0; + while(bininput[i]) { + if((prevchar != bininput[i]) && (i > 0)) { + struct ft* ftchunk = calloc(1, sizeof(struct ft)); + if(prevchar == '1') ftchunk->frequency = (double)frequency; + ftchunk->duration = (uint64_t)prevcharcount*samples*1000000000/samplerate; + fwrite(ftchunk, sizeof(struct ft), 1, stdout); + fprintf(stderr, "Writing chunk: %f %u\n", ftchunk->frequency, ftchunk->duration); + free(ftchunk); + prevcharcount = 0; + } + prevchar = bininput[i++]; + prevcharcount++; + } + + return 0; +}