wav/text convertors now working

This commit is contained in:
tth 2022-03-08 11:33:01 +01:00
parent 307b590796
commit 082fb8e671
4 changed files with 259 additions and 0 deletions

View File

@ -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 --------

View File

@ -0,0 +1,32 @@
/*
* C SUPPORT FUNCTIONS
*/
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
#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__);
}
/* --------------------------------------------------------------- */

View File

@ -0,0 +1,102 @@
/*
* TEXT TO WAV
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#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;
}
/* --------------------------------------------------------------- */

View File

@ -0,0 +1,98 @@
/*
* WAV TO TEXT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#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; foo<lu; foo+=2) {
printf("%d %d\n", samples[foo], samples[foo+1]);
}
}
/*
* job done, some cleanup
*/
free(samples);
sf_close(sndf);
return 0;
}
/* --------------------------------------------------------------- */
void usage(void)
{
printf("usage:\n\twav2txt 1337.wav\n");
exit(0);
}
/* --------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo;
int format = 0;
print_version(argv[0]);
if (2 != argc) usage();
foo = convert_wav_to_text(argv[1], format);
fprintf(stderr, "got a %d from wav->text converter\n", foo);
return 0;
}
/* --------------------------------------------------------------- */