24 #if defined (HAVE_CONFIG_H)
42 #if defined (HAVE_SNDFILE)
46 #if defined (HAVE_SNDFILE)
48 safe_close (SNDFILE *
file)
75 #if defined (HAVE_SNDFILE)
79 if (nargin < 1 || nargin > 3)
86 SNDFILE *
file = sf_open (filename.c_str (), SFM_READ, &info);
89 error (
"audioread: failed to open input file %s", filename.c_str ());
93 frame.
add_fcn (safe_close, file);
97 sf_read_float (file, data, info.frames * info.channels);
100 sf_count_t end = info.frames;
102 if ((nargin == 2 && !
args(1).is_string ()) || nargin == 3)
106 if (range.
numel () != 2)
107 error (
"audioread: invalid specification for range of frames");
112 if (dstart < 1 || dstart > dend || dend > info.frames
115 error (
"audioread: invalid specification for range of frames");
121 sf_count_t items = end -
start;
123 Matrix audio (items, info.channels);
127 data += start * info.channels;
129 for (
int i = 0;
i < items;
i++)
131 for (
int channel = 0; channel < info.channels; channel++)
132 paudio[items*channel+
i] = *data++;
137 if ((nargin == 2 &&
args(1).is_string ()) || nargin == 3)
141 type =
args(2).string_value ();
143 type =
args(1).string_value ();
145 if (type ==
"native")
147 switch (info.format & SF_FORMAT_SUBMASK)
149 case SF_FORMAT_PCM_S8:
152 case SF_FORMAT_PCM_U8:
155 case SF_FORMAT_PCM_16:
158 case SF_FORMAT_PCM_24:
161 case SF_FORMAT_PCM_32:
175 return ovl (ret_audio, info.samplerate);
179 octave_unused_parameter (
args);
182 "reading and writing sound files through libsndfile");
187 #if defined (HAVE_SNDFILE)
192 static bool initialized =
false;
194 static std::map<std::string, int> table;
198 table[
"wav"] = SF_FORMAT_WAV;
199 table[
"aiff"] = SF_FORMAT_AIFF;
200 table[
"au"] = SF_FORMAT_AU;
201 table[
"raw"] = SF_FORMAT_RAW;
202 table[
"paf"] = SF_FORMAT_PAF;
203 table[
"svx"] = SF_FORMAT_SVX;
204 table[
"nist"] = SF_FORMAT_NIST;
205 table[
"voc"] = SF_FORMAT_VOC;
206 table[
"ircam"] = SF_FORMAT_IRCAM;
207 table[
"w64"] = SF_FORMAT_W64;
208 table[
"mat4"] = SF_FORMAT_MAT4;
209 table[
"mat5"] = SF_FORMAT_MAT5;
210 table[
"pvf"] = SF_FORMAT_PVF;
211 table[
"xi"] = SF_FORMAT_XI;
212 table[
"htk"] = SF_FORMAT_HTK;
213 table[
"sds"] = SF_FORMAT_SDS;
214 table[
"avr"] = SF_FORMAT_AVR;
215 table[
"wavex"] = SF_FORMAT_WAVEX;
216 table[
"sd2"] = SF_FORMAT_SD2;
217 table[
"flac"] = SF_FORMAT_FLAC;
218 table[
"caf"] = SF_FORMAT_CAF;
219 table[
"wve"] = SF_FORMAT_WVE;
220 table[
"ogg"] = SF_FORMAT_OGG;
221 table[
"mpc2k"] = SF_FORMAT_MPC2K;
222 table[
"rf64"] = SF_FORMAT_RF64;
227 std::map<std::string, int>::const_iterator it = table.find (ext);
229 return (it != table.end ()) ? it->second : 0;
268 #if defined (HAVE_SNDFILE)
280 if (
args(1).is_uint8_type ())
282 else if (
args(1).is_int16_type ())
284 else if (
args(1).is_int32_type ())
286 else if (
args(1).is_integer_type ())
291 int samplerate =
args(2).int_value ();
294 size_t dotpos = filename.find_last_of (
".");
295 if (dotpos != std::string::npos)
296 ext = filename.substr (dotpos + 1);
297 std::transform (ext.begin (), ext.end (), ext.begin (), ::tolower);
299 sf_count_t items_to_write = audio.
rows () * audio.
columns ();
301 if (audio.
rows () == 1)
307 for (
int i = 0;
i < audio.
rows ();
i++)
309 for (
int j = 0; j < audio.
columns (); j++)
311 double elem = (audio.
xelem (
i, j) - bias) / scale;
318 memset (&info, 0,
sizeof (info));
320 sf_count_t chunk_size = 0;
324 info.format = SF_FORMAT_VORBIS;
330 chunk_size = 0x1FFFFE;
333 info.format = SF_FORMAT_PCM_16;
335 info.channels = audio.
columns ();
336 info.samplerate = samplerate;
337 info.channels = audio.
cols ();
338 info.format |= extension_to_format (ext);
349 error (
"audiowrite: invalid number of arguments");
354 if (keyword ==
"BitsPerSample")
356 info.format &= ~SF_FORMAT_SUBMASK;
360 if ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
361 info.format |= SF_FORMAT_PCM_U8;
363 info.format |= SF_FORMAT_PCM_S8;
366 info.format |= SF_FORMAT_PCM_16;
368 info.format |= SF_FORMAT_PCM_24;
370 info.format |= SF_FORMAT_PCM_32;
372 error (
"audiowrite: wrong number of bits specified");
374 else if (keyword ==
"BitRate")
380 else if (keyword ==
"Title")
382 else if (keyword ==
"Artist")
384 else if (keyword ==
"Comment")
387 error (
"audiowrite: wrong argument name");
390 SNDFILE *
file = sf_open (filename.c_str (), SFM_WRITE, &info);
393 error (
"audiowrite: failed to open output file %s", filename.c_str ());
397 frame.
add_fcn (safe_close, file);
400 sf_set_string (file, SF_STR_TITLE, title.c_str ());
403 sf_set_string (file, SF_STR_ARTIST, artist.c_str ());
406 sf_set_string (file, SF_STR_COMMENT, comment.c_str ());
408 sf_count_t total_items_written = 0;
409 sf_count_t offset = 0;
412 chunk_size = items_to_write;
414 while (total_items_written < items_to_write)
416 if (items_to_write - offset < chunk_size)
417 chunk_size = items_to_write - offset;
419 sf_count_t items_written = sf_write_float (file, data+offset, chunk_size);
421 if (items_written != chunk_size)
422 error (
"audiowrite: write failed, wrote %ld of %ld items\n",
423 items_written, chunk_size);
425 total_items_written += items_written;
426 offset += chunk_size;
435 octave_unused_parameter (
args);
438 "reading and writing sound files through libsndfile");
449 #if defined (HAVE_SNDFILE)
451 if (
args.length () != 1)
454 std::string filename =
args(0).xstring_value (
"audioinfo: FILENAME must be a string");
458 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info);
461 error (
"audioinfo: failed to open file %s", filename.c_str ());
465 frame.
add_fcn (safe_close, file);
469 result.
assign (
"Filename", filename);
470 result.
assign (
"CompressionMethod",
"");
471 result.
assign (
"NumChannels", info.channels);
472 result.
assign (
"SampleRate", info.samplerate);
473 result.
assign (
"TotalSamples", info.frames);
475 double dframes = info.frames;
476 double drate = info.samplerate;
477 result.
assign (
"Duration", dframes / drate);
480 switch (info.format & SF_FORMAT_SUBMASK)
482 case SF_FORMAT_PCM_S8:
485 case SF_FORMAT_PCM_U8:
488 case SF_FORMAT_PCM_16:
491 case SF_FORMAT_PCM_24:
494 case SF_FORMAT_PCM_32:
502 result.
assign (
"BitsPerSample", bits);
503 result.
assign (
"BitRate", -1);
504 result.
assign (
"Title", sf_get_string (file, SF_STR_TITLE));
505 result.
assign (
"Artist", sf_get_string (file, SF_STR_ARTIST));
506 result.
assign (
"Comment", sf_get_string (file, SF_STR_COMMENT));
512 octave_unused_parameter (
args);
515 "reading and writing sound files through libsndfile");
520 #if defined (HAVE_SNDFILE)
523 audio_sub_formats (
int format)
526 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count,
sizeof (
int));
528 for (
int i = 0;
i < count;
i++)
532 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &info,
sizeof (info));
535 memset (&sfinfo, 0,
sizeof (sfinfo));
537 sfinfo.format = (format & SF_FORMAT_TYPEMASK) | info.format;
539 if (sf_format_check (&sfinfo))
556 #if defined (HAVE_SNDFILE)
558 if (
args.length () > 1)
562 if (
args.length () > 0)
564 search =
args(0).string_value ();
565 std::transform (search.begin (), search.end (), search.begin (), tolower);
569 sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count,
sizeof (
int));
571 for (
int i = 0;
i < count;
i++)
575 sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info,
sizeof (info));
578 if (! search.empty ())
582 match = nm.compare (0, search.length (),
search) == 0;
588 octave_stdout <<
"extension: " << info.extension << std::endl;
592 audio_sub_formats (info.format);
598 octave_unused_parameter (
args);
601 "getting sound formats through libsndfile");
For example cd octave end example noindent changes the current working directory to file
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
intNDArray< octave_uint8 > uint8NDArray
int int_value(bool req_int=false, bool frc_str_conv=false) const
void error(const char *fmt,...)
intNDArray< octave_int16 > int16NDArray
octave_idx_type rows(void) const
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
void add_fcn(void(*fcn)(void))
intNDArray< octave_int8 > int8NDArray
std::string string_value(bool force=false) const
if(nargin< 2) print_usage()
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
Matrix transpose(void) const
static std::list< std::string > search(const std::string &path, const std::string &original_name, bool all)
void err_wrong_type_arg(const char *name, const char *s)
With real return the complex result
T & xelem(octave_idx_type n)
octave::unwind_protect frame
charNDArray max(char d, const charNDArray &m)
intNDArray< octave_int32 > int32NDArray
=val(i)}if ode{val(i)}occurs in table i
void assign(const std::string &k, const octave_value &val)
void scale(Matrix &m, double x, double y, double z)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
ColumnVector transform(const Matrix &m, double x, double y, double z)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
const T * fortran_vec(void) const
octave_idx_type cols(void) const
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
octave_idx_type columns(void) const
OCTAVE_EXPORT octave_value_list or cell arrays Arguments are concatenated vertically The returned values are padded with blanks as needed to make each row of the string array have the same length Empty input strings are significant and will concatenated in the output For numerical each element is converted to the corresponding ASCII character A range error results if an input is outside the ASCII range(0-255).For cell arrays
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
charNDArray min(char d, const charNDArray &m)