diff libinterp/dldfcn/audioread.cc @ 19544:99522db5b911

merge audio source files * audiodevinfo.cc: Fold __player_audioplayer__.cc, __recorder_audiorecorder__.cc, player_class.cc, player_class.h, recorder_class.cc, and recorder_class.h into this source file. * audioread.cc: Fold audioinfo.cc and audiowrite.cc into this source file. * libinterp/dldfcn/module-files: Update.
author John W. Eaton <jwe@octave.org>
date Fri, 02 Jan 2015 00:40:35 -0500
parents ce02743b6f2a
children 19f75d156ffe
line wrap: on
line diff
--- a/libinterp/dldfcn/audioread.cc	Wed Dec 31 15:38:13 2014 -0500
+++ b/libinterp/dldfcn/audioread.cc	Fri Jan 02 00:40:35 2015 -0500
@@ -24,8 +24,12 @@
 #include <config.h>
 #endif
 
+#include <string>
+#include <map>
+
 #include "oct.h"
 #include "ov-struct.h"
+
 #ifdef HAVE_SNDFILE
 #include <sndfile.h>
 #endif
@@ -126,3 +130,191 @@
 #endif
   return octave_value (retval);
 }
+
+#ifdef HAVE_SNDFILE
+static void
+fill_extension_table (std::map<std::string, int> &table)
+{
+  table["wav"] = SF_FORMAT_WAV;
+  table["aiff"] = SF_FORMAT_AIFF;
+  table["au"] = SF_FORMAT_AU;
+  table["raw"] = SF_FORMAT_RAW;
+  table["paf"] = SF_FORMAT_PAF;
+  table["svx"] = SF_FORMAT_SVX;
+  table["nist"] = SF_FORMAT_NIST;
+  table["voc"] = SF_FORMAT_VOC;
+  table["ircam"] = SF_FORMAT_IRCAM;
+  table["w64"] = SF_FORMAT_W64;
+  table["mat4"] = SF_FORMAT_MAT4;
+  table["mat5"] = SF_FORMAT_MAT5;
+  table["pvf"] = SF_FORMAT_PVF;
+  table["xi"] = SF_FORMAT_XI;
+  table["htk"] = SF_FORMAT_HTK;
+  table["sds"] = SF_FORMAT_SDS;
+  table["avr"] = SF_FORMAT_AVR;
+  table["wavex"] = SF_FORMAT_WAVEX;
+  table["sd2"] = SF_FORMAT_SD2;
+  table["flac"] = SF_FORMAT_FLAC;
+  table["caf"] = SF_FORMAT_CAF;
+  table["wve"] = SF_FORMAT_WVE;
+  table["ogg"] = SF_FORMAT_OGG;
+  table["mpc2k"] = SF_FORMAT_MPC2K;
+  table["rf64"] = SF_FORMAT_RF64;
+}
+#endif
+
+DEFUN_DLD (audiowrite, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs})\n\
+\n\
+Write audio data from the matrix @var{y} to a file specified by @var{filename},\n\
+file format will be determined by the file extension.\n\
+\n\
+@end deftypefn\n\
+@deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value})\n\
+\n\
+Lets you specify additional parameters when writing the file. Those parameters\n\
+are given in the table below:\n\
+\n\
+@table @samp\n\
+@item BitsPerSample\n\
+Number of bits per sample, valid values are 8, 16, 24 and 32. Default is 16.\n\
+@item BitRate\n\
+Valid argument name, but ignored. Left for compatibility with MATLAB.\n\
+@item Quality\n\
+Quality setting for the Ogg Vorbis compressor. Values can range between 0 and 100 with 100 being the highest quality setting. Default is 75.\n\
+@item Title\n\
+Title for the audio file.\n\
+@item Artist\n\
+Artist name.\n\
+@item Comment\n\
+Comment.\n\
+@end table\n\
+@end deftypefn")
+{
+  octave_scalar_map retval;
+#ifdef HAVE_SNDFILE
+  std::map<std::string, int> extension_to_format;
+  fill_extension_table (extension_to_format);
+  std::string filename = args(0).string_value ();
+  std::string extension = filename.substr (filename.find_last_of (".") + 1);
+  std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower);
+  Matrix audio = args(1).matrix_value ();
+  SNDFILE *file;
+  SF_INFO info;
+  float *data = (float *)malloc (audio.rows () * audio.cols () * sizeof (float));
+  for (int i = 0; i < audio.cols (); i++)
+    {
+      for (int j = 0; j < audio.rows (); j++)
+        {
+          data[j * audio.cols () + i] = audio(j, i);
+        }
+    }
+
+  if (extension == "ogg")
+    info.format = SF_FORMAT_VORBIS;
+  else
+    info.format = SF_FORMAT_PCM_16;
+
+  std::string title = "";
+  std::string artist = "";
+  std::string comment = "";
+  float quality = 0.75;
+  for (int i = 3; i < args.length (); i += 2)
+    {
+      if (args(i).string_value () == "BitsPerSample")
+        {
+          int bits = args(i + 1).int_value ();
+          if (bits == 8)
+            info.format |= SF_FORMAT_PCM_S8;
+          else if (bits == 16)
+            info.format |= SF_FORMAT_PCM_16;
+          else if (bits == 24)
+            info.format |= SF_FORMAT_PCM_24;
+          else if (bits == 32)
+            info.format |= SF_FORMAT_PCM_32;
+          else
+            error ("audiowrite: wrong number of bits specified");
+        }
+      else if (args(i).string_value () == "BitRate")
+        ;
+      else if (args(i).string_value () == "Quality")
+        quality = args(i + 1).int_value () * 0.01;
+      else if (args(i).string_value () == "Title")
+        title = args(i + 1).string_value ();
+      else if (args(i).string_value () == "Artist")
+        artist = args(i + 1).string_value ();
+      else if (args(i).string_value () == "Comment")
+        comment = args(i + 1).string_value ();
+      else
+        error ("audiowrite: wrong argument name");
+    }
+  info.samplerate = args(2).int_value ();
+  info.channels = audio.cols ();
+  info.format |= extension_to_format[extension];
+  file = sf_open (filename.c_str (), SFM_WRITE, &info);
+  if (title != "")
+    sf_set_string (file, SF_STR_TITLE, title.c_str ());
+  if (artist != "")
+    sf_set_string (file, SF_STR_ARTIST, artist.c_str ());
+  if (comment != "")
+    sf_set_string (file, SF_STR_COMMENT, comment.c_str ());
+  sf_write_float (file, data, audio.rows () * audio.cols ());
+  sf_close (file);
+  free (data);
+#else
+  error ("sndfile not found on your system and thus audiowrite is not functional");
+#endif
+  return octave_value (retval);
+}
+
+DEFUN_DLD (audioinfo, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\
+Return information about an audio file specified by @var{filename}.\n\
+@end deftypefn")
+{
+  octave_scalar_map retval;
+  if (args.length () != 1 || not args(0).is_string ())
+    {
+      print_usage ();
+      return octave_value (retval);
+    }
+#ifdef HAVE_SNDFILE
+  Matrix audio;
+  SNDFILE *file;
+  SF_INFO info;
+  info.format = 0;
+  int start, end;
+  file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info);
+  retval.assign ("Filename", args(0).string_value ());
+  retval.assign ("CompressionMethod", "");
+  retval.assign ("NumChannels", info.channels);
+  retval.assign ("SampleRate", info.samplerate);
+  retval.assign ("TotalSamples", info.frames);
+  retval.assign ("Duration", (float)info.frames / (float)info.samplerate);
+
+  int bits;
+  if (info.format & SF_FORMAT_PCM_S8)
+    bits = 8;
+  else if (info.format & SF_FORMAT_PCM_U8)
+    bits = 8;
+  else if (info.format & SF_FORMAT_PCM_16)
+    bits = 16;
+  else if (info.format & SF_FORMAT_PCM_24)
+    bits = 24;
+  else if (info.format & SF_FORMAT_PCM_32)
+    bits = 32;
+  else
+    bits = -1;
+
+  retval.assign ("BitsPerSample", bits);
+  retval.assign ("BitRate", -1);
+  retval.assign ("Title", sf_get_string (file, SF_STR_TITLE));
+  retval.assign ("Artist", sf_get_string (file, SF_STR_ARTIST));
+  retval.assign ("Comment", sf_get_string (file, SF_STR_COMMENT));
+#else
+  error ("sndfile not found on your system and thus audioinfo is not functional");
+#endif
+  return octave_value (retval);
+}