From 50864fd556ca61183ef6e41b66d42fd633182b28 Mon Sep 17 00:00:00 2001 From: Kamal Mostafa Date: Thu, 23 Jun 2011 23:46:18 -0700 Subject: [PATCH] simpleaudio: tx file extension sets audio format --- src/minimodem.1 | 4 +-- src/simpleaudio-sndfile.c | 57 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/minimodem.1 b/src/minimodem.1 index cfd33e4..fa42c17 100644 --- a/src/minimodem.1 +++ b/src/minimodem.1 @@ -51,8 +51,8 @@ ASCII 8\-N\-1 .B \-5, \-\-baudot Baudot 5\-N\-1.5 .TP -.B \-f, \-\-file {filename.flac} -encode or decode a FLAC audio file +.B \-f, \-\-file filename.wav +encode or decode an audio file (extension sets audio format) .TP .B \-b, \-\-bandwidth {rx_bandwidth} .TP diff --git a/src/simpleaudio-sndfile.c b/src/simpleaudio-sndfile.c index 436e987..7316ebf 100644 --- a/src/simpleaudio-sndfile.c +++ b/src/simpleaudio-sndfile.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -76,6 +77,56 @@ static const struct simpleaudio_backend simpleaudio_backend_sndfile = { sa_sndfile_close, }; + +/* (Why) doesn't libsndfile provide an API for this?... */ +static const struct sndfile_format { + unsigned int major_format; + char *str; +} sndfile_formats[] = { + { SF_FORMAT_WAV, "WAV" }, + { SF_FORMAT_AIFF, "AIFF" }, + { SF_FORMAT_AU, "AU" }, + { SF_FORMAT_RAW, "RAW" }, + { SF_FORMAT_PAF, "PAF" }, + { SF_FORMAT_SVX, "SVX" }, + { SF_FORMAT_NIST, "NIST" }, + { SF_FORMAT_VOC, "VOC" }, + { SF_FORMAT_IRCAM, "IRCAM" }, + { SF_FORMAT_W64, "W64" }, + { SF_FORMAT_MAT4, "MAT4" }, + { SF_FORMAT_MAT5, "MAT5" }, + { SF_FORMAT_PVF, "PVF" }, + { SF_FORMAT_XI, "XI" }, + { SF_FORMAT_HTK, "HTK" }, + { SF_FORMAT_SDS, "SDS" }, + { SF_FORMAT_AVR, "AVR" }, + { SF_FORMAT_WAVEX, "WAVEX" }, + { SF_FORMAT_SD2, "SD2" }, + { SF_FORMAT_FLAC, "FLAC" }, + { SF_FORMAT_CAF, "CAF" }, + { SF_FORMAT_WVE, "WVE" }, + { SF_FORMAT_OGG, "OGG" }, + { SF_FORMAT_MPC2K, "MPC2K" }, + { SF_FORMAT_RF64, "RF64" }, + { 0, 0 } +}; + +static unsigned int +sndfile_format_from_path( const char *path ) +{ + const char *p = strrchr(path, '.'); + if ( p ) + p++; + else + p = path; + + const struct sndfile_format *sfmt; + for ( sfmt=sndfile_formats; sfmt->str; sfmt++ ) + if ( strcasecmp(sfmt->str,p) == 0 ) + return sfmt->major_format; + return SF_FORMAT_WAV; +} + simpleaudio * simpleaudio_open_stream_sndfile( int sa_stream_direction, @@ -83,13 +134,13 @@ simpleaudio_open_stream_sndfile( { /* setting for SA_STREAM_PLAYBACK (file write) */ SF_INFO sfinfo = { - .format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16, // FIXME - hardcoded + .format = 0, .samplerate = 48000, .channels = 1, }; - if ( sa_stream_direction == SA_STREAM_RECORD ) - sfinfo.format = 0; + if ( sa_stream_direction == SA_STREAM_PLAYBACK ) + sfinfo.format = sndfile_format_from_path(path) | SF_FORMAT_PCM_16; /* Create the recording stream */ SNDFILE *s;