24 #if defined (HAVE_CONFIG_H) 46 #if defined (HAVE_SNDFILE) 50 #if defined (HAVE_SNDFILE) 52 safe_close (SNDFILE *
file)
80 #if defined (HAVE_SNDFILE) 82 int nargin = args.length ();
91 SNDFILE *
file = sf_open (
filename.c_str (), SFM_READ, &info);
94 error (
"audioread: failed to open input file '%s': %s",
103 sf_read_float (
file, data, info.frames * info.channels);
105 sf_count_t
start = 0;
106 sf_count_t end = info.frames;
108 if ((
nargin == 2 && ! args(1).is_string ()) ||
nargin == 3)
112 if (
range.numel () != 2)
113 error (
"audioread: invalid specification for range of frames");
118 if (dstart < 1 || dstart > dend || dend > info.frames
121 error (
"audioread: invalid specification for range of frames");
127 sf_count_t items = end -
start;
129 Matrix audio (items, info.channels);
133 data +=
start * info.channels;
135 for (
int i = 0;
i < items;
i++)
137 for (
int channel = 0; channel < info.channels; channel++)
138 paudio[items*channel+
i] = *data++;
143 if ((
nargin == 2 && args(1).is_string ()) ||
nargin == 3)
147 type = args(2).string_value ();
149 type = args(1).string_value ();
151 if (
type ==
"native")
153 switch (info.format & SF_FORMAT_SUBMASK)
155 case SF_FORMAT_PCM_S8:
158 case SF_FORMAT_PCM_U8:
161 case SF_FORMAT_PCM_16:
164 case SF_FORMAT_PCM_24:
167 case SF_FORMAT_PCM_32:
181 return ovl (ret_audio, info.samplerate);
185 octave_unused_parameter (args);
188 "reading and writing sound files through libsndfile");
193 #if defined (HAVE_SNDFILE) 200 static std::map<std::string, int> table;
204 table[
"wav"] = SF_FORMAT_WAV;
205 table[
"aiff"] = SF_FORMAT_AIFF;
206 table[
"au"] = SF_FORMAT_AU;
207 table[
"raw"] = SF_FORMAT_RAW;
208 table[
"paf"] = SF_FORMAT_PAF;
209 table[
"svx"] = SF_FORMAT_SVX;
210 table[
"nist"] = SF_FORMAT_NIST;
211 table[
"voc"] = SF_FORMAT_VOC;
212 table[
"ircam"] = SF_FORMAT_IRCAM;
213 table[
"w64"] = SF_FORMAT_W64;
214 table[
"mat4"] = SF_FORMAT_MAT4;
215 table[
"mat5"] = SF_FORMAT_MAT5;
216 table[
"pvf"] = SF_FORMAT_PVF;
217 table[
"xi"] = SF_FORMAT_XI;
218 table[
"htk"] = SF_FORMAT_HTK;
219 table[
"sds"] = SF_FORMAT_SDS;
220 table[
"avr"] = SF_FORMAT_AVR;
221 table[
"wavex"] = SF_FORMAT_WAVEX;
222 table[
"sd2"] = SF_FORMAT_SD2;
223 table[
"flac"] = SF_FORMAT_FLAC;
224 table[
"caf"] = SF_FORMAT_CAF;
225 table[
"wve"] = SF_FORMAT_WVE;
226 table[
"ogg"] = SF_FORMAT_OGG;
227 table[
"mpc2k"] = SF_FORMAT_MPC2K;
228 table[
"rf64"] = SF_FORMAT_RF64;
233 std::map<std::string, int>::const_iterator it = table.find (ext);
235 return (it != table.end ()) ? it->second : 0;
275 #if defined (HAVE_SNDFILE) 277 int nargin = args.length ();
287 if (args(1).is_uint8_type ())
289 else if (args(1).is_int16_type ())
291 else if (args(1).is_int32_type ())
296 Matrix audio = args(1).matrix_value ();
300 int samplerate = args(2).int_value ();
303 size_t dotpos =
filename.find_last_of (
'.');
304 if (dotpos != std::string::npos)
306 std::transform (ext.begin (), ext.end (), ext.begin (), ::tolower);
308 sf_count_t items_to_write = audio.
rows () * audio.
columns ();
310 if (audio.
rows () == 1)
316 for (
int i = 0;
i < audio.
rows ();
i++)
318 for (
int j = 0; j < audio.
columns (); j++)
327 memset (&info, 0,
sizeof (info));
329 sf_count_t chunk_size = 0;
333 info.format = SF_FORMAT_VORBIS;
339 chunk_size = 0x100000;
342 info.format = SF_FORMAT_PCM_16;
344 info.channels = audio.
columns ();
345 info.samplerate = samplerate;
346 info.channels = audio.
cols ();
347 info.format |= extension_to_format (ext);
352 double quality = 0.75;
357 error (
"audiowrite: invalid number of arguments");
360 std::string keyword = args(
i).xtolower ().string_value ();
363 if (keyword ==
"bitspersample")
365 info.format &= ~SF_FORMAT_SUBMASK;
369 if ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
370 info.format |= SF_FORMAT_PCM_U8;
372 info.format |= SF_FORMAT_PCM_S8;
375 info.format |= SF_FORMAT_PCM_16;
377 info.format |= SF_FORMAT_PCM_32;
380 if ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
382 info.format |= SF_FORMAT_FLOAT;
384 info.format |= SF_FORMAT_PCM_32;
387 info.format |= SF_FORMAT_DOUBLE;
389 error (
"audiowrite: wrong number of bits specified");
391 else if (keyword ==
"bitrate")
393 "audiowrite: 'BitRate' accepted for Matlab " 394 "compatibility, but is ignored");
395 else if (keyword ==
"quality")
398 error (
"audiowrite: Quality value must be a scalar");
400 double value = value_arg.
xdouble_value (
"audiowrite: Quality value must be a numeric scalar between 0 and 100");
403 error (
"audiowrite: Quality value must be a number between 0 and 100");
405 quality =
value / 100;
407 else if (keyword ==
"title")
409 else if (keyword ==
"artist")
411 else if (keyword ==
"comment")
414 error (
"audiowrite: unrecognized option: '%s'", keyword_orig.c_str ());
417 SNDFILE *
file = sf_open (
filename.c_str (), SFM_WRITE, &info);
420 error (
"audiowrite: failed to open output file '%s': %s",
427 sf_command (
file, SFC_SET_VBR_ENCODING_QUALITY, &quality,
sizeof (quality));
430 sf_set_string (
file, SF_STR_TITLE, title.c_str ());
433 sf_set_string (
file, SF_STR_ARTIST, artist.c_str ());
436 sf_set_string (
file, SF_STR_COMMENT, comment.c_str ());
438 sf_count_t total_items_written = 0;
439 sf_count_t offset = 0;
442 chunk_size = items_to_write;
444 while (total_items_written < items_to_write)
446 if (items_to_write - offset < chunk_size)
447 chunk_size = items_to_write - offset;
449 sf_count_t items_written = sf_write_float (
file, data+offset, chunk_size);
451 if (items_written != chunk_size)
452 error (
"audiowrite: write failed, wrote %ld of %ld items\n",
453 items_written, chunk_size);
455 total_items_written += items_written;
456 offset += chunk_size;
465 octave_unused_parameter (args);
468 "reading and writing sound files through libsndfile");
532 #if defined (HAVE_SNDFILE) 534 if (args.length () != 1)
541 error (
"audioinfo: FILENAME '%s' not found",
filename.c_str ());
545 SNDFILE *
file = sf_open (
filename.c_str (), SFM_READ, &info);
548 error (
"audioinfo: failed to open input file '%s': %s",
559 result.assign (
"Filename", full_name);
560 result.assign (
"CompressionMethod",
"");
561 result.assign (
"NumChannels", info.channels);
562 result.assign (
"SampleRate", info.samplerate);
563 result.assign (
"TotalSamples", info.frames);
565 double dframes = info.frames;
566 double drate = info.samplerate;
567 result.assign (
"Duration", dframes / drate);
570 switch (info.format & SF_FORMAT_SUBMASK)
572 case SF_FORMAT_PCM_S8:
575 case SF_FORMAT_PCM_U8:
578 case SF_FORMAT_PCM_16:
581 case SF_FORMAT_PCM_24:
584 case SF_FORMAT_PCM_32:
592 result.assign (
"BitsPerSample", bits);
593 result.assign (
"BitRate", -1);
594 result.assign (
"Title", sf_get_string (
file, SF_STR_TITLE));
595 result.assign (
"Artist", sf_get_string (
file, SF_STR_ARTIST));
596 result.assign (
"Comment", sf_get_string (
file, SF_STR_COMMENT));
602 octave_unused_parameter (args);
605 "reading and writing sound files through libsndfile");
610 #if defined (HAVE_SNDFILE) 613 audio_sub_formats (
int format)
616 sf_command (
nullptr, SFC_GET_FORMAT_SUBTYPE_COUNT, &count,
sizeof (
int));
618 for (
int i = 0;
i < count;
i++)
622 sf_command (
nullptr, SFC_GET_FORMAT_SUBTYPE, &info,
sizeof (info));
625 memset (&sfinfo, 0,
sizeof (sfinfo));
627 sfinfo.format = (
format & SF_FORMAT_TYPEMASK) | info.format;
629 if (sf_format_check (&sfinfo))
647 #if defined (HAVE_SNDFILE) 649 if (args.length () > 1)
653 if (args.length () > 0)
655 search = args(0).string_value ();
660 sf_command (
nullptr, SFC_GET_FORMAT_MAJOR_COUNT, &count,
sizeof (
int));
662 for (
int i = 0;
i < count;
i++)
666 sf_command (
nullptr, SFC_GET_FORMAT_MAJOR, &info,
sizeof (info));
679 octave_stdout <<
"extension: " << info.extension << std::endl;
683 audio_sub_formats (info.format);
689 octave_unused_parameter (args);
692 "getting sound formats through libsndfile");
octave_idx_type rows(void) const
void warning_with_id(const char *id, const char *fmt,...)
For example cd octave end example noindent changes the current working directory to file
int int_value(bool req_int=false, bool frc_str_conv=false) const
std::string canonicalize_file_name(const std::string &name)
std::string string_value(bool force=false) const
double xdouble_value(const char *fmt,...) const
OCTINTERP_API void print_usage(void)
intNDArray< octave_uint8 > uint8NDArray
const T * fortran_vec(void) const
void add_fcn(void(*fcn)(void))
void error(const char *fmt,...)
octave_idx_type columns(void) const
intNDArray< octave_int16 > int16NDArray
octave_idx_type cols(void) const
Matrix transpose(void) const
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
intNDArray< octave_int8 > int8NDArray
OCTAVE_EXPORT octave_value_list isfloat
if(nargin< 2) print_usage()
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
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
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
void scale(Matrix &m, double x, double y, double z)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
octave::sys::file_stat fs(filename)
ColumnVector transform(const Matrix &m, double x, double y, double z)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
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
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
nd group nd example For each display the value
bool is_scalar_type(void) const
charNDArray min(char d, const charNDArray &m)