sndfile.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. ** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU Lesser General Public License as published by
  6. ** the Free Software Foundation; either version 2.1 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU Lesser General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU Lesser General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <octave/oct.h>
  19. #include "sndfile.h"
  20. #define FOUR_GIG (0x100000000LL)
  21. #define BUFFER_FRAMES 8192
  22. static int format_of_str (const std::string & fmt) ;
  23. static void string_of_format (std::string & fmt, int format) ;
  24. DEFUN_DLD (sfversion, args, nargout ,
  25. "-*- texinfo -*-\n\
  26. @deftypefn {Loadable Function} {@var{version} =} sfversion ()\n\
  27. @cindex Reading sound files\n\
  28. Return a string containing the libsndfile version.\n\
  29. @seealso{sfread, sfwrite}\n\
  30. @end deftypefn")
  31. { char buffer [256] ;
  32. octave_value_list retval ;
  33. /* Bail out if the input parameters are bad. */
  34. if (args.length () != 0 || nargout > 1)
  35. { print_usage () ;
  36. return retval ;
  37. } ;
  38. sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
  39. std::string version (buffer) ;
  40. retval.append (version) ;
  41. return retval ;
  42. } /* sfversion */
  43. DEFUN_DLD (sfread, args, nargout ,
  44. "-*- texinfo -*-\n\
  45. @deftypefn {Loadable Function} {@var{data},@var{srate},@var{format} =} sfread (@var{filename})\n\
  46. @cindex Reading sound files\n\
  47. Read a sound file from disk using libsndfile.\n\
  48. @seealso{sfversion, sfwrite}\n\
  49. @end deftypefn")
  50. { SNDFILE * file ;
  51. SF_INFO sfinfo ;
  52. octave_value_list retval ;
  53. int nargin = args.length () ;
  54. /* Bail out if the input parameters are bad. */
  55. if ((nargin != 1) || !args (0) .is_string () || nargout < 1 || nargout > 3)
  56. { print_usage () ;
  57. return retval ;
  58. } ;
  59. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  60. std::string filename = args (0).string_value () ;
  61. if ((file = sf_open (filename.c_str (), SFM_READ, &sfinfo)) == NULL)
  62. { error ("sfread: couldn't open file %s : %s", filename.c_str (), sf_strerror (NULL)) ;
  63. return retval ;
  64. } ;
  65. if (sfinfo.frames > FOUR_GIG)
  66. printf ("This is a really huge file (%lld frames).\nYou may run out of memory trying to load it.\n", (long long) sfinfo.frames) ;
  67. dim_vector dim = dim_vector () ;
  68. dim.resize (2) ;
  69. dim (0) = sfinfo.frames ;
  70. dim (1) = sfinfo.channels ;
  71. /* Should I be using Matrix instead? */
  72. NDArray out (dim, 0.0) ;
  73. float buffer [BUFFER_FRAMES * sfinfo.channels] ;
  74. int readcount ;
  75. sf_count_t total = 0 ;
  76. do
  77. { readcount = sf_readf_float (file, buffer, BUFFER_FRAMES) ;
  78. /* Make sure we don't read more frames than we allocated. */
  79. if (total + readcount > sfinfo.frames)
  80. readcount = sfinfo.frames - total ;
  81. for (int ch = 0 ; ch < sfinfo.channels ; ch++)
  82. { for (int k = 0 ; k < readcount ; k++)
  83. out (total + k, ch) = buffer [k * sfinfo.channels + ch] ;
  84. } ;
  85. total += readcount ;
  86. } while (readcount > 0 && total < sfinfo.frames) ;
  87. retval.append (out.squeeze ()) ;
  88. if (nargout >= 2)
  89. retval.append ((octave_uint32) sfinfo.samplerate) ;
  90. if (nargout >= 3)
  91. { std::string fmt ("") ;
  92. string_of_format (fmt, sfinfo.format) ;
  93. retval.append (fmt) ;
  94. } ;
  95. /* Clean up. */
  96. sf_close (file) ;
  97. return retval ;
  98. } /* sfread */
  99. DEFUN_DLD (sfwrite, args, nargout ,
  100. "-*- texinfo -*-\n\
  101. @deftypefn {Function File} sfwrite (@var{filename},@var{data},@var{srate},@var{format})\n\
  102. Write a sound file to disk using libsndfile.\n\
  103. @seealso{sfread, sfversion}\n\
  104. @end deftypefn\n\
  105. ")
  106. { SNDFILE * file ;
  107. SF_INFO sfinfo ;
  108. octave_value_list retval ;
  109. int nargin = args.length () ;
  110. /* Bail out if the input parameters are bad. */
  111. if (nargin != 4 || !args (0).is_string () || !args (1).is_real_matrix ()
  112. || !args (2).is_real_scalar () || !args (3).is_string ()
  113. || nargout != 0)
  114. { print_usage () ;
  115. return retval ;
  116. } ;
  117. std::string filename = args (0).string_value () ;
  118. std::string format = args (3).string_value () ;
  119. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  120. sfinfo.format = format_of_str (format) ;
  121. if (sfinfo.format == 0)
  122. { error ("Bad format '%s'", format.c_str ()) ;
  123. return retval ;
  124. } ;
  125. sfinfo.samplerate = lrint (args (2).scalar_value ()) ;
  126. if (sfinfo.samplerate < 1)
  127. { error ("Bad sample rate : %d.\n", sfinfo.samplerate) ;
  128. return retval ;
  129. } ;
  130. Matrix data = args (1).matrix_value () ;
  131. long rows = args (1).rows () ;
  132. long cols = args (1).columns () ;
  133. if (cols > rows)
  134. { error ("Audio data should have one column per channel, but supplied data "
  135. "has %ld rows and %ld columns.\n", rows, cols) ;
  136. return retval ;
  137. } ;
  138. sfinfo.channels = cols ;
  139. if ((file = sf_open (filename.c_str (), SFM_WRITE, &sfinfo)) == NULL)
  140. { error ("Couldn't open file %s : %s", filename.c_str (), sf_strerror (NULL)) ;
  141. return retval ;
  142. } ;
  143. float buffer [BUFFER_FRAMES * sfinfo.channels] ;
  144. int writecount ;
  145. long total = 0 ;
  146. do
  147. {
  148. writecount = BUFFER_FRAMES ;
  149. /* Make sure we don't read more frames than we allocated. */
  150. if (total + writecount > rows)
  151. writecount = rows - total ;
  152. for (int ch = 0 ; ch < sfinfo.channels ; ch++)
  153. { for (int k = 0 ; k < writecount ; k++)
  154. buffer [k * sfinfo.channels + ch] = data (total + k, ch) ;
  155. } ;
  156. if (writecount > 0)
  157. sf_writef_float (file, buffer, writecount) ;
  158. total += writecount ;
  159. } while (writecount > 0 && total < rows) ;
  160. /* Clean up. */
  161. sf_close (file) ;
  162. return retval ;
  163. } /* sfwrite */
  164. static void
  165. str_split (const std::string & str, const std::string & delim, std::vector <std::string> & output)
  166. {
  167. unsigned int offset = 0 ;
  168. size_t delim_index = 0 ;
  169. delim_index = str.find (delim, offset) ;
  170. while (delim_index != std::string::npos)
  171. {
  172. output.push_back (str.substr(offset, delim_index - offset)) ;
  173. offset += delim_index - offset + delim.length () ;
  174. delim_index = str.find (delim, offset) ;
  175. }
  176. output.push_back (str.substr (offset)) ;
  177. } /* str_split */
  178. static int
  179. hash_of_str (const std::string & str)
  180. {
  181. int hash = 0 ;
  182. for (unsigned k = 0 ; k < str.length () ; k++)
  183. hash = (hash * 3) + tolower (str [k]) ;
  184. return hash ;
  185. } /* hash_of_str */
  186. static int
  187. major_format_of_hash (const std::string & str)
  188. { int hash ;
  189. hash = hash_of_str (str) ;
  190. switch (hash)
  191. {
  192. case 0x5c8 : /* 'wav' */ return SF_FORMAT_WAV ;
  193. case 0xf84 : /* 'aiff' */ return SF_FORMAT_AIFF ;
  194. case 0x198 : /* 'au' */ return SF_FORMAT_AU ;
  195. case 0x579 : /* 'paf' */ return SF_FORMAT_PAF ;
  196. case 0x5e5 : /* 'svx' */ return SF_FORMAT_SVX ;
  197. case 0x1118 : /* 'nist' */ return SF_FORMAT_NIST ;
  198. case 0x5d6 : /* 'voc' */ return SF_FORMAT_VOC ;
  199. case 0x324a : /* 'ircam' */ return SF_FORMAT_IRCAM ;
  200. case 0x505 : /* 'w64' */ return SF_FORMAT_W64 ;
  201. case 0x1078 : /* 'mat4' */ return SF_FORMAT_MAT4 ;
  202. case 0x1079 : /* 'mat5' */ return SF_FORMAT_MAT5 ;
  203. case 0x5b8 : /* 'pvf' */ return SF_FORMAT_PVF ;
  204. case 0x1d1 : /* 'xi' */ return SF_FORMAT_XI ;
  205. case 0x56f : /* 'htk' */ return SF_FORMAT_HTK ;
  206. case 0x5aa : /* 'sds' */ return SF_FORMAT_SDS ;
  207. case 0x53d : /* 'avr' */ return SF_FORMAT_AVR ;
  208. case 0x11d0 : /* 'wavx' */ return SF_FORMAT_WAVEX ;
  209. case 0x569 : /* 'sd2' */ return SF_FORMAT_SD2 ;
  210. case 0x1014 : /* 'flac' */ return SF_FORMAT_FLAC ;
  211. case 0x504 : /* 'caf' */ return SF_FORMAT_CAF ;
  212. case 0x5f6 : /* 'wve' */ return SF_FORMAT_WVE ;
  213. default : break ;
  214. } ;
  215. printf ("%s : hash '%s' -> 0x%x\n", __func__, str.c_str (), hash) ;
  216. return 0 ;
  217. } /* major_format_of_hash */
  218. static int
  219. minor_format_of_hash (const std::string & str)
  220. { int hash ;
  221. hash = hash_of_str (str) ;
  222. switch (hash)
  223. {
  224. case 0x1085 : /* 'int8' */ return SF_FORMAT_PCM_S8 ;
  225. case 0x358a : /* 'uint8' */ return SF_FORMAT_PCM_U8 ;
  226. case 0x31b0 : /* 'int16' */ return SF_FORMAT_PCM_16 ;
  227. case 0x31b1 : /* 'int24' */ return SF_FORMAT_PCM_24 ;
  228. case 0x31b2 : /* 'int32' */ return SF_FORMAT_PCM_32 ;
  229. case 0x3128 : /* 'float' */ return SF_FORMAT_FLOAT ;
  230. case 0x937d : /* 'double' */ return SF_FORMAT_DOUBLE ;
  231. case 0x11bd : /* 'ulaw' */ return SF_FORMAT_ULAW ;
  232. case 0xfa1 : /* 'alaw' */ return SF_FORMAT_ALAW ;
  233. case 0xfc361 : /* 'ima_adpcm' */ return SF_FORMAT_IMA_ADPCM ;
  234. case 0x5739a : /* 'ms_adpcm' */ return SF_FORMAT_MS_ADPCM ;
  235. case 0x9450 : /* 'gsm610' */ return SF_FORMAT_GSM610 ;
  236. case 0x172a3 : /* 'g721_32' */ return SF_FORMAT_G721_32 ;
  237. case 0x172d8 : /* 'g723_24' */ return SF_FORMAT_G723_24 ;
  238. case 0x172da : /* 'g723_40' */ return SF_FORMAT_G723_40 ;
  239. default : break ;
  240. } ;
  241. printf ("%s : hash '%s' -> 0x%x\n", __func__, str.c_str (), hash) ;
  242. return 0 ;
  243. } /* minor_format_of_hash */
  244. static const char *
  245. string_of_major_format (int format)
  246. {
  247. switch (format & SF_FORMAT_TYPEMASK)
  248. {
  249. case SF_FORMAT_WAV : return "wav" ;
  250. case SF_FORMAT_AIFF : return "aiff" ;
  251. case SF_FORMAT_AU : return "au" ;
  252. case SF_FORMAT_PAF : return "paf" ;
  253. case SF_FORMAT_SVX : return "svx" ;
  254. case SF_FORMAT_NIST : return "nist" ;
  255. case SF_FORMAT_VOC : return "voc" ;
  256. case SF_FORMAT_IRCAM : return "ircam" ;
  257. case SF_FORMAT_W64 : return "w64" ;
  258. case SF_FORMAT_MAT4 : return "mat4" ;
  259. case SF_FORMAT_MAT5 : return "mat5" ;
  260. case SF_FORMAT_PVF : return "pvf" ;
  261. case SF_FORMAT_XI : return "xi" ;
  262. case SF_FORMAT_HTK : return "htk" ;
  263. case SF_FORMAT_SDS : return "sds" ;
  264. case SF_FORMAT_AVR : return "avr" ;
  265. case SF_FORMAT_WAVEX : return "wavx" ;
  266. case SF_FORMAT_SD2 : return "sd2" ;
  267. case SF_FORMAT_FLAC : return "flac" ;
  268. case SF_FORMAT_CAF : return "caf" ;
  269. case SF_FORMAT_WVE : return "wfe" ;
  270. default : break ;
  271. } ;
  272. return "unknown" ;
  273. } /* string_of_major_format */
  274. static const char *
  275. string_of_minor_format (int format)
  276. {
  277. switch (format & SF_FORMAT_SUBMASK)
  278. {
  279. case SF_FORMAT_PCM_S8 : return "int8" ;
  280. case SF_FORMAT_PCM_U8 : return "uint8" ;
  281. case SF_FORMAT_PCM_16 : return "int16" ;
  282. case SF_FORMAT_PCM_24 : return "int24" ;
  283. case SF_FORMAT_PCM_32 : return "int32" ;
  284. case SF_FORMAT_FLOAT : return "float" ;
  285. case SF_FORMAT_DOUBLE : return "double" ;
  286. case SF_FORMAT_ULAW : return "ulaw" ;
  287. case SF_FORMAT_ALAW : return "alaw" ;
  288. case SF_FORMAT_IMA_ADPCM : return "ima_adpcm" ;
  289. case SF_FORMAT_MS_ADPCM : return "ms_adpcm" ;
  290. case SF_FORMAT_GSM610 : return "gsm610" ;
  291. case SF_FORMAT_G721_32 : return "g721_32" ;
  292. case SF_FORMAT_G723_24 : return "g723_24" ;
  293. case SF_FORMAT_G723_40 : return "g723_40" ;
  294. default : break ;
  295. } ;
  296. return "unknown" ;
  297. } /* string_of_minor_format */
  298. static int
  299. format_of_str (const std::string & fmt)
  300. {
  301. std::vector <std::string> split ;
  302. str_split (fmt, "-", split) ;
  303. if (split.size () != 2)
  304. return 0 ;
  305. int major_fmt = major_format_of_hash (split.at (0)) ;
  306. if (major_fmt == 0)
  307. return 0 ;
  308. int minor_fmt = minor_format_of_hash (split.at (1)) ;
  309. if (minor_fmt == 0)
  310. return 0 ;
  311. return major_fmt | minor_fmt ;
  312. } /* format_of_str */
  313. static void
  314. string_of_format (std::string & fmt, int format)
  315. {
  316. char buffer [64] ;
  317. snprintf (buffer, sizeof (buffer), "%s-%s", string_of_major_format (format), string_of_minor_format (format)) ;
  318. fmt = buffer ;
  319. return ;
  320. } /* string_of_format */