From 082fb8e671a783ee5faeb1a0166d5842b878f2b4 Mon Sep 17 00:00:00 2001 From: tth Date: Tue, 8 Mar 2022 11:33:01 +0100 Subject: [PATCH] wav/text convertors now working --- SoundBrotching/c-tools/Makefile | 27 ++++++++ SoundBrotching/c-tools/support.c | 32 ++++++++++ SoundBrotching/c-tools/text2wav.c | 102 ++++++++++++++++++++++++++++++ SoundBrotching/c-tools/wav2text.c | 98 ++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 SoundBrotching/c-tools/Makefile create mode 100644 SoundBrotching/c-tools/support.c create mode 100644 SoundBrotching/c-tools/text2wav.c create mode 100644 SoundBrotching/c-tools/wav2text.c diff --git a/SoundBrotching/c-tools/Makefile b/SoundBrotching/c-tools/Makefile new file mode 100644 index 0000000..6cf37fd --- /dev/null +++ b/SoundBrotching/c-tools/Makefile @@ -0,0 +1,27 @@ +# +# tth@konrad:~/Devel/Fortraneries/SoundBrotching/c-tools$ +# + +COPT = -Wall -Wextra -g -DDEBUG_LEVEL=1 + +all: text2wav wav2text text2ao + +# ---------------------------------------------------------- +# +# C tools +# +CLIBS = -lsndfile support.o + +support.o: support.c Makefile support.h + gcc $(COPT) -c $< + +text2wav: text2wav.c Makefile support.h support.o + gcc $(COPT) $< $(CLIBS) -o $@ + +wav2text: wav2text.c Makefile support.h support.o + gcc $(COPT) $< $(CLIBS) -o $@ + +text2ao: text2ao.c Makefile support.h support.o + gcc $(COPT) $< $(CLIBS) -lao -o $@ + +# ------ eomf -------- diff --git a/SoundBrotching/c-tools/support.c b/SoundBrotching/c-tools/support.c new file mode 100644 index 0000000..874b6df --- /dev/null +++ b/SoundBrotching/c-tools/support.c @@ -0,0 +1,32 @@ +/* + * C SUPPORT FUNCTIONS + */ + +#include +#include + +#include + +#include "support.h" + +/* --------------------------------------------------------------- */ +int display_sf_info(SF_INFO *psf, char *text) +{ + +fprintf(stderr, " +-- sf info [%s] %p\n", text, psf); + +fprintf(stderr, " | samplerate %d\n", psf->samplerate); +fprintf(stderr, " | channels %d\n", psf->channels); +fprintf(stderr, " | frames %ld\n", psf->frames); +fprintf(stderr, " | format 0x%x\n", psf->format); + +return 0; +} +/* --------------------------------------------------------------- */ + +void print_version(char *msg) +{ +fprintf(stderr, "=== %s compiled %s, %s\n", \ + msg, __DATE__, __TIME__); +} +/* --------------------------------------------------------------- */ diff --git a/SoundBrotching/c-tools/text2wav.c b/SoundBrotching/c-tools/text2wav.c new file mode 100644 index 0000000..6dbd7c5 --- /dev/null +++ b/SoundBrotching/c-tools/text2wav.c @@ -0,0 +1,102 @@ +/* + * TEXT TO WAV + */ + +#include +#include +#include + +#include + +#include "support.h" + +/* --------------------------------------------------------------- */ + +#define SMPL_COUNT 8192 + +/* + * WARNING ! + * this function can read only 16bits stereo input + */ +int convert_text_to_wav(FILE *input, char *outfname, int format) +{ +SNDFILE * sndf; +SF_INFO sfinfo; +short *buffer; +int left, right; +int nb_lus, idx; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( '%s' )\n", __func__, outfname); +#endif + +if (format) { + fprintf(stderr, "in %s, format must be 0, was %d\n", + __func__, format); + exit(1); + } + +/* + * allocate memory for input garbage + */ +if (NULL==(buffer=malloc(SMPL_COUNT * sizeof(short) * 2))) { + fprintf(stderr, "%s: MEMORY FULL\n", __func__); + abort(); + } + +memset(&sfinfo, 0, sizeof(sfinfo)); /* be clean */ +sfinfo.samplerate = 44100; +sfinfo.channels = 2; +sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; + +if ( ! (sndf=sf_open(outfname, SFM_WRITE, &sfinfo)) ) { + fprintf(stderr, "write to %s : err %s\n", + outfname, sf_strerror (NULL)); + return -2; + } + +/* + * and now, ladies an gentelman, entering the big loop + */ +nb_lus = idx = 0; +memset(buffer, 0, SMPL_COUNT * sizeof(short) * 2); + +while (2==fscanf(input, "%d %d", &left, &right)) { + buffer[idx++] = left; + buffer[idx++] = right; + + if (idx >= SMPL_COUNT) { + /* flush buffer to file */ + sf_write_short(sndf, buffer, idx); + idx = 0; + memset(buffer, 0, SMPL_COUNT * sizeof(short) * 2); + } + + nb_lus += 1; + } + +sf_write_short(sndf, buffer, idx); +sf_close(sndf); + +fprintf(stderr, "%s: %d buffers written\n", __func__, nb_lus); + +return 0; +} +/* --------------------------------------------------------------- */ +int main(int argc, char *argv[]) +{ +int foo; + +print_version(argv[0]); + +if (2 != argc) { + fprintf(stderr, "fubarized\n"); + exit(1); + } + +foo = convert_text_to_wav(stdin, argv[1], 0); +fprintf(stderr, "got a %d from text->wav converter\n", foo); + +return 0; +} +/* --------------------------------------------------------------- */ diff --git a/SoundBrotching/c-tools/wav2text.c b/SoundBrotching/c-tools/wav2text.c new file mode 100644 index 0000000..3ba0204 --- /dev/null +++ b/SoundBrotching/c-tools/wav2text.c @@ -0,0 +1,98 @@ +/* + * WAV TO TEXT + */ + +#include +#include +#include + +#include + +#include "support.h" + +/* --------------------------------------------------------------- */ +/* + * at this time, 'format' must be 0 + */ +int convert_wav_to_text(char *infname, int format) +{ +SNDFILE * sndf; +SF_INFO sfinfo; +int foo, lu; +short * samples; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( '%s' )\n", __func__, infname); +#endif + +if (format) { + fprintf(stderr, "in %s, format must be 0 and was %d\n", + __func__, format); + exit(1); + } + +memset(&sfinfo, 0, sizeof(sfinfo)); /* be clean */ + +sndf = sf_open(infname, SFM_READ, &sfinfo); +if (sndf==NULL) + { + /*catch the snfile errmsg here XXX */ + fprintf(stderr, "error sf_opening %s\n", infname); + exit(1); + } + +foo = display_sf_info(&sfinfo, infname); +if (foo) { + fprintf(stderr, "%s: corrupted sf_info ?\n", __func__); + abort(); + } +if (2 != sfinfo.channels) { + fprintf(stderr, "/!\\ %s is not a stereo file, exiting...\n", + infname); + return -2; + } + +/* get memory for bufferins read from sound file */ +if ( NULL == (samples = malloc(BUFFER_SIZE*sizeof(short))) ) + { + perror("\n no memory in converter"); + abort(); + } + +while ( (lu=sf_read_short(sndf, samples, BUFFER_SIZE)) > 0 ) + { + // fprintf(stderr, " LU = %5u\n", lu); + + for (foo=0; footext converter\n", foo); + +return 0; +} +/* --------------------------------------------------------------- */