2022-03-08 21:33:01 +11:00
|
|
|
/*
|
|
|
|
* TEXT TO WAV
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sndfile.h>
|
|
|
|
|
|
|
|
#include "support.h"
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define SMPL_COUNT 8192
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WARNING !
|
2022-05-04 22:51:44 +11:00
|
|
|
* this function write only 16bits stereo input
|
2022-03-08 21:33:01 +11:00
|
|
|
*/
|
2022-05-04 22:51:44 +11:00
|
|
|
int convert_text_to_wav(FILE *input, char *outf, int sr, int format)
|
2022-03-08 21:33:01 +11:00
|
|
|
{
|
|
|
|
SNDFILE * sndf;
|
|
|
|
SF_INFO sfinfo;
|
|
|
|
short *buffer;
|
|
|
|
int left, right;
|
|
|
|
int nb_lus, idx;
|
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
2022-05-04 22:51:44 +11:00
|
|
|
fprintf(stderr, ">>> %s ( '%s' )\n", __func__, outf);
|
2022-03-08 21:33:01 +11: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 22:51:44 +11:00
|
|
|
sfinfo.samplerate = sr;
|
2022-03-08 21:33:01 +11:00
|
|
|
sfinfo.channels = 2;
|
|
|
|
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
|
|
|
|
|
2022-05-04 22:51:44 +11:00
|
|
|
if ( ! (sndf=sf_open(outf, SFM_WRITE, &sfinfo)) ) {
|
2022-03-08 21:33:01 +11:00
|
|
|
fprintf(stderr, "write to %s : err %s\n",
|
2022-05-04 22:51:44 +11:00
|
|
|
outf, sf_strerror (NULL));
|
2022-03-08 21:33:01 +11: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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-04 22:51:44 +11:00
|
|
|
foo = convert_text_to_wav(stdin, argv[1], 44100, 0);
|
2022-03-08 21:33:01 +11:00
|
|
|
fprintf(stderr, "got a %d from text->wav converter\n", foo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|