Skip to content

Commit 6321645

Browse files
authored
Wave file handling 2 (#4)
* force libsndfile to flush wav data to file after write. converted all `printf`s to `fprint`s to stderr opening the possibility of streaming data via stdout * indentation * added option to enable flushing wave file; it is now disabled by default * added option to pipe raw pcm to stdout. removed redundant file open. updated help text.
1 parent 5d691dd commit 6321645

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

include/dsd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ typedef struct
104104
int audio_out;
105105
char wav_out_file[1024];
106106
SNDFILE *wav_out_f;
107-
//int wav_out_fd;
107+
int wav_out_major_type;
108108
int serial_baud;
109109
char serial_dev[1024];
110110
int serial_fd;
@@ -281,7 +281,7 @@ int readAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d);
281281
void openMbeInFile (dsd_opts * opts, dsd_state * state);
282282
void closeMbeOutFile (dsd_opts * opts, dsd_state * state);
283283
void openMbeOutFile (dsd_opts * opts, dsd_state * state);
284-
void openWavOutFile (dsd_opts * opts, dsd_state * state);
284+
void openWavOutFile (dsd_opts * opts, __attribute__((unused)) dsd_state * state);
285285
void closeWavOutFile (dsd_opts * opts, dsd_state * state);
286286
void printFrameInfo (dsd_opts * opts, dsd_state * state);
287287
void processFrame (dsd_opts * opts, dsd_state * state);

src/dsd_file.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,16 @@ openMbeOutFile (dsd_opts * opts, dsd_state * state)
316316
fflush (opts->mbe_out_f);
317317
}
318318

319-
void openWavOutFile (dsd_opts *opts, dsd_state *state) {
319+
void openWavOutFile(dsd_opts *opts, __attribute__((unused)) dsd_state *state) {
320+
320321
SF_INFO info;
321322
info.samplerate = 8000;
322323
info.channels = 1;
323-
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE;
324+
info.format = opts->wav_out_major_type | SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE;
324325
opts->wav_out_f = sf_open(opts->wav_out_file, SFM_WRITE, &info);
325326

326327
if (!opts->wav_out_f) {
328+
perror(NULL);
327329
fprintf(stderr, "Error - could not open wav output file %s\n", opts->wav_out_file);
328330
return;
329331
}

src/dsd_main.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ initOpts (dsd_opts * opts)
118118
opts->audio_out = 1;
119119
opts->wav_out_file[0] = 0;
120120
opts->wav_out_f = NULL;
121-
//opts->wav_out_fd = -1;
121+
opts->wav_out_major_type = SF_FORMAT_WAV;
122122
opts->serial_baud = 115200;
123123
sprintf (opts->serial_dev, "/dev/ttyUSB0");
124124
opts->resume = 0;
@@ -276,7 +276,7 @@ usage ()
276276
fprintf(stderr, " -r <files> Read/Play saved mbe data from file(s)\n");
277277
fprintf(stderr, " -g <num> Audio output gain (default = 0 = auto, disable = -1)\n");
278278
fprintf(stderr, " -n Do not send synthesized speech to audio output device\n");
279-
fprintf(stderr, " -w <file> Output synthesized speech to a .wav file\n");
279+
fprintf(stderr, " -w - | <file> Output synthesized speech to a .wav file, or pipe raw PCM to stdout; both are mono, 8kHz, 16-bit, signed-int\n");
280280
fprintf(stderr, " -a Display port audio devices\n");
281281
fprintf(stderr, "\n");
282282
fprintf(stderr, "Scanner control options:\n");
@@ -576,10 +576,17 @@ main (int argc, char **argv)
576576
fprintf(stderr, "Disabling audio output to soundcard.\n");
577577
break;
578578
case 'w':
579+
// TODO consider: should piping to stdout imply '-q' as well?
580+
if (!strcmp("-", optarg)) {
581+
checkFileError(freopen(NULL, "wb", stdout));
582+
opts.audio_out = 0;
583+
opts.wav_out_major_type = SF_FORMAT_RAW;
584+
// opts.errorbars = 0;
585+
// opts.verbose = 0;
586+
}
579587
strncpy(opts.wav_out_file, optarg, 1023);
580588
opts.wav_out_file[1023] = '\0';
581589
fprintf(stderr, "Writing audio to file %s\n", opts.wav_out_file);
582-
openWavOutFile (&opts, &state);
583590
break;
584591
case 'B':
585592
sscanf (optarg, "%d", &opts.serial_baud);

0 commit comments

Comments
 (0)