Fortraneries/SoundBrotching/c-tools/text2wav.c

114 lines
2.3 KiB
C
Raw Normal View History

2022-03-08 11:33:01 +01:00
/*
2023-01-03 21:59:38 +01:00
* TEXT TO WAV - a klugeware from tTh 2023
2022-03-08 11:33:01 +01:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include "support.h"
/* --------------------------------------------------------------- */
#define SMPL_COUNT 8192
/*
* WARNING !
2022-05-04 13:51:44 +02:00
* this function write only 16bits stereo input
2022-03-08 11:33:01 +01:00
*/
2022-05-04 13:51:44 +02:00
int convert_text_to_wav(FILE *input, char *outf, int sr, int format)
2022-03-08 11:33:01 +01:00
{
SNDFILE * sndf;
SF_INFO sfinfo;
short *buffer;
int left, right;
int nb_lus, idx;
#if DEBUG_LEVEL
2023-01-03 21:59:38 +01:00
fprintf(stderr, ">>> %s ( '%s' %d %d )\n", __func__, outf, sr, format);
2022-03-08 11:33:01 +01:00
#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 */
2022-05-04 13:51:44 +02:00
sfinfo.samplerate = sr;
2022-03-08 11:33:01 +01:00
sfinfo.channels = 2;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
2022-05-04 13:51:44 +02:00
if ( ! (sndf=sf_open(outf, SFM_WRITE, &sfinfo)) ) {
2022-03-08 11:33:01 +01:00
fprintf(stderr, "write to %s : err %s\n",
2022-05-04 13:51:44 +02:00
outf, sf_strerror (NULL));
2022-03-08 11:33:01 +01:00
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;
2023-02-11 19:40:52 +01:00
/* SANITY CONTROL */
if ( (abs(left) > 32767) || (abs(right) > 32767) ) {
fprintf(stderr, "OVERFLOW sample %9d values %7d %7d\n",
idx, left, right);
}
2022-03-08 11:33:01 +01:00
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);
2023-01-03 21:59:38 +01:00
free(buffer);
fprintf(stderr, "%s: %d samples written\n", __func__, nb_lus);
2022-03-08 11:33:01 +01:00
return 0;
}
/* --------------------------------------------------------------- */
2023-01-03 21:59:38 +01:00
/*
* this is a preview software, so no args parsiong yet...
*/
2022-03-08 11:33:01 +01:00
int main(int argc, char *argv[])
{
int foo;
print_version(argv[0]);
if (2 != argc) {
fprintf(stderr, "fubarized\n");
exit(1);
}
2022-05-04 13:51:44 +02:00
foo = convert_text_to_wav(stdin, argv[1], 44100, 0);
2022-03-08 11:33:01 +01:00
fprintf(stderr, "got a %d from text->wav converter\n", foo);
return 0;
}
/* --------------------------------------------------------------- */