Mercurial > octave
annotate libinterp/dldfcn/audioread.cc @ 21200:fcac5dbbf9ed
maint: Indent #ifdef blocks in libinterp.
* builtins.h, Cell.cc, __contourc__.cc, __dispatch__.cc, __dsearchn__.cc,
__ichol__.cc, __ilu__.cc, __lin_interpn__.cc, __pchip_deriv__.cc, __qp__.cc,
balance.cc, besselj.cc, betainc.cc, bitfcns.cc, bsxfun.cc,
c-file-ptr-stream.cc, c-file-ptr-stream.h, cellfun.cc, colloc.cc,
comment-list.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, debug.cc,
defaults.cc, defaults.in.h, defun-dld.h, defun.cc, defun.h, det.cc, dirfns.cc,
display.cc, dlmread.cc, dot.cc, dynamic-ld.cc, eig.cc, ellipj.cc, error.cc,
errwarn.cc, event-queue.cc, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc,
find.cc, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc,
givens.cc, gl-render.cc, gl2ps-print.cc, graphics.cc, graphics.in.h, gripes.cc,
hash.cc, help.cc, hess.cc, hex2num.cc, input.cc, inv.cc, jit-ir.cc,
jit-typeinfo.cc, jit-util.cc, jit-util.h, kron.cc, load-path.cc, load-save.cc,
lookup.cc, ls-ascii-helper.cc, ls-hdf5.cc, ls-mat-ascii.cc, ls-mat4.cc,
ls-mat5.cc, ls-oct-binary.cc, ls-oct-text.cc, ls-oct-text.h, ls-utils.cc,
ls-utils.h, lsode.cc, lu.cc, luinc.cc, mappers.cc, matrix_type.cc, max.cc,
mex.h, mexproto.h, mgorth.cc, nproc.cc, oct-errno.in.cc, oct-fstrm.cc,
oct-hdf5-types.cc, oct-hdf5.h, oct-hist.cc, oct-iostrm.cc, oct-lvalue.cc,
oct-map.cc, oct-prcstrm.cc, oct-procbuf.cc, oct-stream.cc, oct-strstrm.cc,
octave-link.cc, ordschur.cc, pager.cc, pinv.cc, pr-output.cc, procstream.cc,
profiler.cc, psi.cc, pt-jit.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc,
regexp.cc, schur.cc, sighandlers.cc, sparse-xdiv.cc, sparse-xpow.cc, sparse.cc,
spparms.cc, sqrtm.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc,
sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, sysdep.h, time.cc, toplev.cc,
tril.cc, tsearch.cc, txt-eng-ft.cc, txt-eng.cc, typecast.cc, urlwrite.cc,
utils.cc, variables.cc, xdiv.cc, xnorm.cc, xpow.cc, zfstream.cc,
__delaunayn__.cc, __eigs__.cc, __fltk_uigetfile__.cc, __glpk__.cc,
__init_fltk__.cc, __init_gnuplot__.cc, __magick_read__.cc, __osmesa_print__.cc,
__voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, ccolamd.cc, chol.cc,
colamd.cc, convhulln.cc, dmperm.cc, fftw.cc, oct-qhull.h, qr.cc, symbfact.cc,
symrcm.cc, oct-conf.in.cc, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc,
ov-base-scalar.cc, ov-base-sparse.cc, ov-base.cc, ov-bool-mat.cc,
ov-bool-sparse.cc, ov-bool.cc, ov-builtin.cc, ov-cell.cc, ov-ch-mat.cc,
ov-class.cc, ov-classdef.cc, ov-colon.cc, ov-complex.cc, ov-cs-list.cc,
ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc, ov-fcn-handle.cc,
ov-fcn-inline.cc, ov-fcn.cc, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc,
ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-int16.cc,
ov-int32.cc, ov-int64.cc, ov-int8.cc, ov-java.cc, ov-lazy-idx.cc,
ov-mex-fcn.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-perm.cc, ov-range.cc,
ov-re-diag.cc, ov-re-mat.cc, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc,
ov-struct.cc, ov-typeinfo.cc, ov-uint16.cc, ov-uint32.cc, ov-uint64.cc,
ov-uint8.cc, ov-usr-fcn.cc, ov.cc, ovl.cc, octave.cc, op-b-b.cc, op-b-bm.cc,
op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc,
op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-double-conv.cc, op-fcdm-fcdm.cc,
op-fcdm-fdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc,
op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc,
op-fdm-fdm.cc, op-float-conv.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc,
op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc,
op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc,
op-int-conv.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc,
op-m-sm.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc,
op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc,
op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc,
op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc,
op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc,
op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc, op-ui16-ui16.cc,
op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, pt-arg-list.cc,
pt-array-list.cc, pt-assign.cc, pt-binop.cc, pt-bp.cc, pt-cbinop.cc,
pt-cell.cc, pt-check.cc, pt-classdef.cc, pt-cmd.cc, pt-colon.cc, pt-colon.h,
pt-const.cc, pt-decl.cc, pt-eval.cc, pt-except.cc, pt-exp.cc, pt-fcn-handle.cc,
pt-funcall.cc, pt-id.cc, pt-idx.cc, pt-jump.cc, pt-loop.cc, pt-mat.cc,
pt-misc.cc, pt-pr-code.cc, pt-select.cc, pt-stmt.cc, pt-unop.cc, pt.cc,
token.cc, Array-jit.cc, Array-os.cc, Array-sym.cc, Array-tc.cc, version.cc:
Indent #ifdef blocks in libinterp.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 05 Feb 2016 16:29:08 -0800 |
parents | 228b65504557 |
children | 40de9f8f23a6 |
rev | line source |
---|---|
19500
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
1 /* |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
2 |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19675
diff
changeset
|
3 Copyright (C) 2013-2015 Vytautas JanĨauskas |
19500
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
4 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
5 This file is part of Octave. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
6 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
10 option) any later version. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
11 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
15 for more details. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
16 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
20 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
21 */ |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19498
diff
changeset
|
22 |
19501
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19500
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
21200
fcac5dbbf9ed
maint: Indent #ifdef blocks in libinterp.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
24 # include <config.h> |
19501
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19500
diff
changeset
|
25 #endif |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19500
diff
changeset
|
26 |
19510 | 27 #include <string> |
28 #include <map> | |
29 | |
19513
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
30 #include "oct-locbuf.h" |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
31 #include "unwind-prot.h" |
19513
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
32 |
19511
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19510
diff
changeset
|
33 #include "defun-dld.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19510
diff
changeset
|
34 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21096
diff
changeset
|
35 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
36 #include "ovl.h" |
19511
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19510
diff
changeset
|
37 #include "ov.h" |
19463
157eeaa53277
Fix includes in audioread, audiowrite and audioinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19462
diff
changeset
|
38 #include "ov-struct.h" |
19510 | 39 |
19473
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19468
diff
changeset
|
40 #ifdef HAVE_SNDFILE |
21200
fcac5dbbf9ed
maint: Indent #ifdef blocks in libinterp.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
41 # include <sndfile.h> |
19473
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19468
diff
changeset
|
42 #endif |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
43 |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
44 #ifdef HAVE_SNDFILE |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
45 static void |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
46 safe_close (SNDFILE *file) |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
47 { |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
48 sf_close (file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
49 } |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
50 #endif |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
51 |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
52 DEFUN_DLD (audioread, args, , |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
53 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
54 @deftypefn {} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
55 @deftypefnx {} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples})\n\ |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
56 \n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
57 @deftypefnx {} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{datatype})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
58 @deftypefnx {} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples}, @var{datatype})\n\ |
20163
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
59 Read the audio file @var{filename} and return the audio data @var{y} and\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
60 sampling rate @var{fs}.\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
61 \n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
62 The audio data is stored as matrix with rows corresponding to audio frames\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
63 and columns corresponding to channels.\n\ |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
64 \n\ |
19516
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19513
diff
changeset
|
65 The optional two-element vector argument @var{samples} specifies starting\n\ |
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19513
diff
changeset
|
66 and ending frames.\n\ |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
67 \n\ |
19516
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19513
diff
changeset
|
68 The optional argument @var{datatype} specifies the datatype to return.\n\ |
20163
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
69 If it is @qcode{\"native\"}, then the type of data depends on how the data\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
70 is stored in the audio file.\n\ |
19516
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19513
diff
changeset
|
71 @end deftypefn") |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
72 { |
19468
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19463
diff
changeset
|
73 #ifdef HAVE_SNDFILE |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
74 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
75 int nargin = args.length (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
76 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
77 if (nargin < 1 || nargin > 3) |
20790
c2d9556d51d0
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20578
diff
changeset
|
78 print_usage (); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
79 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
80 std::string filename = args(0).xstring_value ("audioread: FILENAME must be a string"); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
81 |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
82 SF_INFO info; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
83 info.format = 0; |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
84 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
85 |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
86 if (! file) |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
87 error ("audioread: failed to open input file %s", filename.c_str ()); |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
88 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
89 unwind_protect frame; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
90 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
91 frame.add_fcn (safe_close, file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
92 |
19513
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
93 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
94 |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
95 sf_read_float (file, data, info.frames * info.channels); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
96 |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
97 sf_count_t start = 0; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
98 sf_count_t end = info.frames; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
99 |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
100 if ((nargin == 2 && ! args(1).is_string ()) || nargin == 3) |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
101 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
102 RowVector range = args(1).row_vector_value (); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
103 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20163
diff
changeset
|
104 if (range.numel () != 2) |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
105 error ("audioread: invalid specification for range of frames"); |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
106 |
19811
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19697
diff
changeset
|
107 double dstart = xisinf (range(0)) ? info.frames : range(0); |
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19697
diff
changeset
|
108 double dend = xisinf (range(1)) ? info.frames : range(1); |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
109 |
19811
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19697
diff
changeset
|
110 if (dstart < 1 || dstart > dend || dend > info.frames |
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19697
diff
changeset
|
111 || D_NINT (dstart) != dstart || D_NINT (dend) != dend) |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
112 error ("audioread: invalid specification for range of frames"); |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
113 |
19811
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19697
diff
changeset
|
114 start = dstart - 1; |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
115 end = dend; |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
116 } |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
117 |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
118 sf_count_t items = end - start; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
119 |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
120 Matrix audio (items, info.channels); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
121 |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
122 double *paudio = audio.fortran_vec (); |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
123 |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
124 data += start * info.channels; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
125 |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
126 for (int i = 0; i < items; i++) |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
127 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
128 for (int channel = 0; channel < info.channels; channel++) |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
129 paudio[items*channel+i] = *data++; |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
130 } |
19513
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
131 |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
132 octave_value ret_audio; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
133 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
134 if ((nargin == 2 && args(1).is_string ()) || nargin == 3) |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
135 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
136 std::string type; |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
137 if (nargin == 3) |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
138 type = args(2).string_value (); |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
139 else |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
140 type = args(1).string_value (); |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
141 |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
142 if (type == "native") |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
143 { |
19675
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
144 switch (info.format & SF_FORMAT_SUBMASK) |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
145 { |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
146 case SF_FORMAT_PCM_S8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
147 ret_audio = int8NDArray (audio * 127); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
148 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
149 case SF_FORMAT_PCM_U8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
150 ret_audio = uint8NDArray (audio * 127 + 127); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
151 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
152 case SF_FORMAT_PCM_16: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
153 ret_audio = int16NDArray (audio * 32767); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
154 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
155 case SF_FORMAT_PCM_24: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
156 ret_audio = int32NDArray (audio * 8388608); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
157 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
158 case SF_FORMAT_PCM_32: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
159 ret_audio = int32NDArray (audio * 2147483648); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
160 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
161 default: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
162 ret_audio = audio; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
163 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
164 } |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
165 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
166 else |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
167 ret_audio = audio; |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
168 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
169 else |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
170 ret_audio = audio; |
19503
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19501
diff
changeset
|
171 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
172 return ovl (ret_audio, info.samplerate); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
173 |
19468
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19463
diff
changeset
|
174 #else |
21103
5a83a422d8a7
Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
175 err_disabled_feature ("audioread", |
5a83a422d8a7
Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
176 "reading and writing sound files through libsndfile"); |
19468
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19463
diff
changeset
|
177 #endif |
19462
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
178 } |
19510 | 179 |
180 #ifdef HAVE_SNDFILE | |
19526
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19519
diff
changeset
|
181 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
182 static int |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
183 extension_to_format (const std::string& ext) |
19510 | 184 { |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
185 static bool initialized = false; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
186 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
187 static std::map<std::string, int> table; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
188 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
189 if (! initialized) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
190 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
191 table["wav"] = SF_FORMAT_WAV; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
192 table["aiff"] = SF_FORMAT_AIFF; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
193 table["au"] = SF_FORMAT_AU; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
194 table["raw"] = SF_FORMAT_RAW; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
195 table["paf"] = SF_FORMAT_PAF; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
196 table["svx"] = SF_FORMAT_SVX; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
197 table["nist"] = SF_FORMAT_NIST; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
198 table["voc"] = SF_FORMAT_VOC; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
199 table["ircam"] = SF_FORMAT_IRCAM; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
200 table["w64"] = SF_FORMAT_W64; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
201 table["mat4"] = SF_FORMAT_MAT4; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
202 table["mat5"] = SF_FORMAT_MAT5; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
203 table["pvf"] = SF_FORMAT_PVF; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
204 table["xi"] = SF_FORMAT_XI; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
205 table["htk"] = SF_FORMAT_HTK; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
206 table["sds"] = SF_FORMAT_SDS; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
207 table["avr"] = SF_FORMAT_AVR; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
208 table["wavex"] = SF_FORMAT_WAVEX; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
209 table["sd2"] = SF_FORMAT_SD2; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
210 table["flac"] = SF_FORMAT_FLAC; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
211 table["caf"] = SF_FORMAT_CAF; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
212 table["wve"] = SF_FORMAT_WVE; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
213 table["ogg"] = SF_FORMAT_OGG; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
214 table["mpc2k"] = SF_FORMAT_MPC2K; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
215 table["rf64"] = SF_FORMAT_RF64; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
216 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
217 initialized = true; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
218 } |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
219 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
220 std::map<std::string, int>::const_iterator it = table.find (ext); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
221 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
222 return (it != table.end ()) ? it->second : 0; |
19510 | 223 } |
19526
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19519
diff
changeset
|
224 |
19510 | 225 #endif |
226 | |
227 DEFUN_DLD (audiowrite, args, , | |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
228 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
229 @deftypefn {} {} audiowrite (@var{filename}, @var{y}, @var{fs})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
230 @deftypefnx {} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value}, @dots{})\n\ |
19510 | 231 \n\ |
20163
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
232 Write audio data from the matrix @var{y} to @var{filename} at sampling rate\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
233 @var{fs} with the file format determined by the file extension.\n\ |
19510 | 234 \n\ |
20163
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19872
diff
changeset
|
235 Additional name/value argument pairs may be used to specify the\n\ |
19516
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19513
diff
changeset
|
236 following options:\n\ |
19510 | 237 \n\ |
238 @table @samp\n\ | |
239 @item BitsPerSample\n\ | |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
240 Number of bits per sample, valid values are 8, 16, 24 and 32. Default is 16.\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
241 \n\ |
19510 | 242 @item BitRate\n\ |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
243 Valid argument name, but ignored. Left for compatibility with @sc{matlab}.\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
244 \n\ |
19510 | 245 @item Quality\n\ |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
246 Quality setting for the Ogg Vorbis compressor. Values can range between 0\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
247 and 100 with 100 being the highest quality setting. Default is 75.\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
248 \n\ |
19510 | 249 @item Title\n\ |
250 Title for the audio file.\n\ | |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
251 \n\ |
19510 | 252 @item Artist\n\ |
253 Artist name.\n\ | |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
254 \n\ |
19510 | 255 @item Comment\n\ |
256 Comment.\n\ | |
257 @end table\n\ | |
258 @end deftypefn") | |
259 { | |
260 #ifdef HAVE_SNDFILE | |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
261 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
262 int nargin = args.length (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
263 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
264 if (nargin < 3) |
20790
c2d9556d51d0
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20578
diff
changeset
|
265 print_usage (); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
266 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
267 std::string filename = args(0).xstring_value ("audiowrite: FILENAME must be a string"); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
268 |
19812
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
269 double bias = 0.0; |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
270 double scale = 1.0; |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
271 |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
272 if (args(1).is_uint8_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
273 bias = scale = std::pow (2.0, 7); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
274 else if (args(1).is_int16_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
275 scale = std::pow (2.0, 15); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
276 else if (args(1).is_int32_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
277 scale = std::pow (2.0, 31); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
278 else if (args(1).is_integer_type ()) |
21129
228b65504557
maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents:
21103
diff
changeset
|
279 err_wrong_type_arg ("audiowrite", args(1)); |
19812
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
280 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
281 Matrix audio = args(1).matrix_value (); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
282 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
283 int samplerate = args(2).int_value (); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
284 |
19871
ed5ee3f610db
Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents:
19813
diff
changeset
|
285 std::string ext; |
19872
d575cd1e0da7
Replace std::size_t with bare size_t for conformity to rest of code.
Rik <rik@octave.org>
parents:
19871
diff
changeset
|
286 size_t dotpos = filename.find_last_of ("."); |
19871
ed5ee3f610db
Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents:
19813
diff
changeset
|
287 if (dotpos != std::string::npos) |
ed5ee3f610db
Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents:
19813
diff
changeset
|
288 ext = filename.substr (dotpos + 1); |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
289 std::transform (ext.begin (), ext.end (), ext.begin (), ::tolower); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
290 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
291 sf_count_t items_to_write = audio.rows () * audio.columns (); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
292 |
19813
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19812
diff
changeset
|
293 if (audio.rows () == 1) |
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19812
diff
changeset
|
294 audio = audio.transpose (); |
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19812
diff
changeset
|
295 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
296 OCTAVE_LOCAL_BUFFER (float, data, items_to_write); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
297 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
298 sf_count_t idx = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
299 for (int i = 0; i < audio.rows (); i++) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
300 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
301 for (int j = 0; j < audio.columns (); j++) |
19812
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
302 { |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
303 double elem = (audio.xelem (i, j) - bias) / scale; |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
304 data[idx++] = std::min (std::max (elem, -1.0), 1.0); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19811
diff
changeset
|
305 } |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
306 } |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
307 |
19510 | 308 SF_INFO info; |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
309 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
310 memset (&info, 0, sizeof (info)) ; |
19513
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
311 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
312 sf_count_t chunk_size = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
313 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
314 if (ext == "ogg") |
19510 | 315 { |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
316 info.format = SF_FORMAT_VORBIS; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
317 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
318 // FIXME: there seems to be a bug writing ogg files in one shot |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
319 // that causes a segfault. Breaking it up into a series of |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
320 // smaller chunks seems to avoid the problem and produce valid |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
321 // files. |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
322 chunk_size = 0x1FFFFE; |
19510 | 323 } |
324 else | |
325 info.format = SF_FORMAT_PCM_16; | |
326 | |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
327 info.channels = audio.columns (); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
328 info.samplerate = samplerate; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
329 info.channels = audio.cols (); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
330 info.format |= extension_to_format (ext); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
331 |
19510 | 332 std::string title = ""; |
333 std::string artist = ""; | |
334 std::string comment = ""; | |
19512
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
335 // Quality is currently unused? |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
336 // |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
337 // float quality = 0.75; |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
338 for (int i = 3; i < nargin; i += 2) |
19510 | 339 { |
340 if (args(i).string_value () == "BitsPerSample") | |
341 { | |
19675
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
342 info.format &= ~SF_FORMAT_SUBMASK; |
19510 | 343 int bits = args(i + 1).int_value (); |
344 if (bits == 8) | |
19675
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
345 { |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
346 if ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
347 info.format |= SF_FORMAT_PCM_U8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
348 else |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
349 info.format |= SF_FORMAT_PCM_S8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
350 } |
19510 | 351 else if (bits == 16) |
352 info.format |= SF_FORMAT_PCM_16; | |
353 else if (bits == 24) | |
354 info.format |= SF_FORMAT_PCM_24; | |
355 else if (bits == 32) | |
356 info.format |= SF_FORMAT_PCM_32; | |
357 else | |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
358 error ("audiowrite: wrong number of bits specified"); |
19510 | 359 } |
360 else if (args(i).string_value () == "BitRate") | |
361 ; | |
19512
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
362 // Quality is currently unused? |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
363 // |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
364 // else if (args(i).string_value () == "Quality") |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19511
diff
changeset
|
365 // quality = args(i + 1).int_value () * 0.01; |
19510 | 366 else if (args(i).string_value () == "Title") |
367 title = args(i + 1).string_value (); | |
368 else if (args(i).string_value () == "Artist") | |
369 artist = args(i + 1).string_value (); | |
370 else if (args(i).string_value () == "Comment") | |
371 comment = args(i + 1).string_value (); | |
372 else | |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
373 error ("audiowrite: wrong argument name"); |
19510 | 374 } |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
375 |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
376 SNDFILE *file = sf_open (filename.c_str (), SFM_WRITE, &info); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
377 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
378 if (! file) |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
379 error ("audiowrite: failed to open output file %s", filename.c_str ()); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
380 |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
381 unwind_protect frame; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
382 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
383 frame.add_fcn (safe_close, file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
384 |
19510 | 385 if (title != "") |
386 sf_set_string (file, SF_STR_TITLE, title.c_str ()); | |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
387 |
19510 | 388 if (artist != "") |
389 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); | |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
390 |
19510 | 391 if (comment != "") |
392 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); | |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
393 |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
394 sf_count_t total_items_written = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
395 sf_count_t offset = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
396 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
397 if (chunk_size == 0) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
398 chunk_size = items_to_write; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
399 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
400 while (total_items_written < items_to_write) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
401 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
402 if (items_to_write - offset < chunk_size) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
403 chunk_size = items_to_write - offset; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
404 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
405 sf_count_t items_written = sf_write_float (file, data+offset, chunk_size); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
406 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
407 if (items_written != chunk_size) |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
408 error ("audiowrite: write failed, wrote %ld of %ld items\n", |
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
409 items_written, chunk_size); |
19553
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
410 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
411 total_items_written += items_written; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
412 offset += chunk_size; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
413 } |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
414 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
415 // FIXME: shouldn't we return something to indicate whether the file |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
416 // was written successfully? |
20941
a4f5da7c5463
maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
417 return ovl (); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
418 |
19510 | 419 #else |
21103
5a83a422d8a7
Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
420 err_disabled_feature ("audiowrite", |
5a83a422d8a7
Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
421 "reading and writing sound files through libsndfile"); |
19510 | 422 #endif |
423 } | |
424 | |
425 DEFUN_DLD (audioinfo, args, , | |
19571
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19559
diff
changeset
|
426 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20825
diff
changeset
|
427 @deftypefn {} {@var{info} =} audioinfo (@var{filename})\n\ |
19510 | 428 Return information about an audio file specified by @var{filename}.\n\ |
429 @end deftypefn") | |
430 { | |
19526
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19519
diff
changeset
|
431 #ifdef HAVE_SNDFILE |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
432 if (args.length () != 1) |
20790
c2d9556d51d0
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20578
diff
changeset
|
433 print_usage (); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
434 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
435 std::string filename = args(0).xstring_value ("audioinfo: FILENAME must be a string"); |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
436 |
19510 | 437 SF_INFO info; |
438 info.format = 0; | |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
439 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
440 |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
441 if (! file) |
20825
66cd994d1b79
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20790
diff
changeset
|
442 error ("audioinfo: failed to open file %s", filename.c_str ()); |
19559
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
443 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
444 unwind_protect frame; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
445 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
446 frame.add_fcn (safe_close, file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19554
diff
changeset
|
447 |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
448 octave_scalar_map result; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
449 |
19554
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
450 result.assign ("Filename", filename); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
451 result.assign ("CompressionMethod", ""); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
452 result.assign ("NumChannels", info.channels); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
453 result.assign ("SampleRate", info.samplerate); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
454 result.assign ("TotalSamples", info.frames); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
455 |
19513
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
456 double dframes = info.frames; |
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19512
diff
changeset
|
457 double drate = info.samplerate; |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
458 result.assign ("Duration", dframes / drate); |
19510 | 459 |
460 int bits; | |
19675
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
461 switch (info.format & SF_FORMAT_SUBMASK) |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
462 { |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
463 case SF_FORMAT_PCM_S8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
464 bits = 8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
465 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
466 case SF_FORMAT_PCM_U8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
467 bits = 8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
468 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
469 case SF_FORMAT_PCM_16: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
470 bits = 16; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
471 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
472 case SF_FORMAT_PCM_24: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
473 bits = 24; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
474 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
475 case SF_FORMAT_PCM_32: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
476 bits = 32; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
477 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
478 default: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
479 bits = -1; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
480 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19571
diff
changeset
|
481 } |
19510 | 482 |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
483 result.assign ("BitsPerSample", bits); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
484 result.assign ("BitRate", -1); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
485 result.assign ("Title", sf_get_string (file, SF_STR_TITLE)); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
486 result.assign ("Artist", sf_get_string (file, SF_STR_ARTIST)); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
487 result.assign ("Comment", sf_get_string (file, SF_STR_COMMENT)); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
488 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20865
diff
changeset
|
489 return ovl (result); |
19519
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19516
diff
changeset
|
490 |
19510 | 491 #else |
21103
5a83a422d8a7
Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
492 err_disabled_feature ("audioinfo", |
5a83a422d8a7
Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
493 "reading and writing sound files through libsndfile"); |
19510 | 494 #endif |
495 } |