From 5ecea5a8d1f7f559911d4e9ebe4b691298a01693 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Sat, 25 Aug 2007 13:20:00 +1000 Subject: [PATCH] More Octave module hacking. --- Octave/Makefile.am | 2 +- Octave/format.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++++ Octave/format.h | 2 ++ Octave/sfread.cc | 20 +++++++++---- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/Octave/Makefile.am b/Octave/Makefile.am index 2d390d7f..16544930 100644 --- a/Octave/Makefile.am +++ b/Octave/Makefile.am @@ -9,7 +9,7 @@ octconfig_DATA = sndfile_load.m sndfile_save.m sndfile_play.m all : sfread.oct sfwrite.oct -sfread.oct : sfread.cc +sfread.oct : format.o sfread.cc $(MKOCTFILE) $+ -lsndfile -o $@ sfwrite.oct : format.o sfwrite.cc diff --git a/Octave/format.cc b/Octave/format.cc index 83dfcf12..9e3a48ce 100644 --- a/Octave/format.cc +++ b/Octave/format.cc @@ -142,3 +142,73 @@ format_of_str (const std::string & fmt) return major_fmt | minor_fmt ; } /* format_of_str */ + +static const char * +string_of_major_format (int format) +{ + switch (format & SF_FORMAT_TYPEMASK) + { + case SF_FORMAT_WAV : return "wav" ; + case SF_FORMAT_AIFF : return "aiff" ; + case SF_FORMAT_AU : return "au" ; + case SF_FORMAT_PAF : return "paf" ; + case SF_FORMAT_SVX : return "svx" ; + case SF_FORMAT_NIST : return "nist" ; + case SF_FORMAT_VOC : return "voc" ; + case SF_FORMAT_IRCAM : return "ircam" ; + case SF_FORMAT_W64 : return "w64" ; + case SF_FORMAT_MAT4 : return "mat4" ; + case SF_FORMAT_MAT5 : return "mat5" ; + case SF_FORMAT_PVF : return "pvf" ; + case SF_FORMAT_XI : return "xi" ; + case SF_FORMAT_HTK : return "htk" ; + case SF_FORMAT_SDS : return "sds" ; + case SF_FORMAT_AVR : return "avr" ; + case SF_FORMAT_WAVEX : return "wavx" ; + case SF_FORMAT_SD2 : return "sd2" ; + case SF_FORMAT_FLAC : return "flac" ; + case SF_FORMAT_CAF : return "caf" ; + case SF_FORMAT_WVE : return "wfe" ; + default : break ; + } ; + + return "unknown" ; +} /* string_of_major_format */ + +static const char * +string_of_minor_format (int format) +{ + switch (format & SF_FORMAT_SUBMASK) + { + case SF_FORMAT_PCM_S8 : return "int8" ; + case SF_FORMAT_PCM_U8 : return "uint8" ; + case SF_FORMAT_PCM_16 : return "int16" ; + case SF_FORMAT_PCM_24 : return "int24" ; + case SF_FORMAT_PCM_32 : return "int32" ; + case SF_FORMAT_FLOAT : return "float" ; + case SF_FORMAT_DOUBLE : return "double" ; + case SF_FORMAT_ULAW : return "ulaw" ; + case SF_FORMAT_ALAW : return "alaw" ; + case SF_FORMAT_IMA_ADPCM : return "ima_adpcm" ; + case SF_FORMAT_MS_ADPCM : return "ms_adpcm" ; + case SF_FORMAT_GSM610 : return "gsm610" ; + case SF_FORMAT_G721_32 : return "g721_32" ; + case SF_FORMAT_G723_24 : return "g723_24" ; + case SF_FORMAT_G723_40 : return "g723_40" ; + default : break ; + } ; + + return "unknown" ; +} /* string_of_minor_format */ + +void +string_of_format (std::string & fmt, int format) +{ + char buffer [64] ; + + snprintf (buffer, sizeof (buffer), "%s-%s", string_of_major_format (format), string_of_minor_format (format)) ; + + fmt = buffer ; + + return ; +} /* string_of_format */ diff --git a/Octave/format.h b/Octave/format.h index 81686d0c..18843cee 100644 --- a/Octave/format.h +++ b/Octave/format.h @@ -17,3 +17,5 @@ */ int format_of_str (const std::string & fmt) ; + +void string_of_format (std::string & fmt, int format) ; diff --git a/Octave/sfread.cc b/Octave/sfread.cc index c3880373..3100f14b 100644 --- a/Octave/sfread.cc +++ b/Octave/sfread.cc @@ -20,12 +20,14 @@ #include "sndfile.h" +#include "format.h" + #define FOUR_GIG (0x100000000LL) #define BUFFER_FRAMES 8192 DEFUN_DLD (sfread, args, nargout , "\ -*- texinfo -*-\n\ -@deftypefn {Function File} {@var{I} =} sfread (@var{filename})\n\ +@deftypefn {Function File} {@var{I},@var{srate},@var{format} =} sfread (@var{filename})\n\ Read a sound file from disk using libsndfile.\n\ \n\ @seealso{wavread}\n\ @@ -39,7 +41,7 @@ Read a sound file from disk using libsndfile.\n\ int nargin = args.length () ; /* Bail out if the input parameters are bad. */ - if ((nargin != 1) || !args (0) .is_string () || (nargout < 1)) + if ((nargin != 1) || !args (0) .is_string () || nargout < 1 || nargout > 3) { print_usage () ; return retval ; } ; @@ -61,9 +63,8 @@ Read a sound file from disk using libsndfile.\n\ dim (0) = sfinfo.frames ; dim (1) = sfinfo.channels ; - puts ("Should probably be using Matrix instead.") ; - - NDArray out = NDArray (dim, 0.0) ; + /* Should I be using Matrix instead? */ + NDArray out (dim, 0.0) ; float buffer [BUFFER_FRAMES * sfinfo.channels] ; int readcount ; @@ -86,6 +87,15 @@ Read a sound file from disk using libsndfile.\n\ retval.append (out.squeeze ()) ; + if (nargout >= 2) + retval.append ((octave_uint32) sfinfo.samplerate) ; + + if (nargout >= 3) + { std::string fmt ("") ; + string_of_format (fmt, sfinfo.format) ; + retval.append (fmt) ; + } ; + /* Clean up. */ sf_close (file) ;