annotate libinterp/dldfcn/audioread.cc @ 28223:45763d59cb4f stable

use nullptr instead of NULL or 0 in a few more places * QWinTerminalImpl.cpp, oct-procbuf.cc, audioread.cc, jit-typeinfo.cc, lo-sysdep.cc, url-transfer.cc, shared-fcns.h: Replace NULL and 0 with nullptr where appropriate.
author John W. Eaton <jwe@octave.org>
date Wed, 15 Apr 2020 15:55:32 -0400
parents bd51beb6205e
children 26cfccfee9a0 0a5b15007766
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
3 // Copyright (C) 2013-2020 The Octave Project Developers
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19498
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
19501
1f551d169db2 Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
28 #endif
1f551d169db2 Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
29
23024
a6a7b054e4ba Rationalize #includes in libinterp/dldfcn directory.
Rik <rik@octave.org>
parents: 22755
diff changeset
30 #include <algorithm>
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
31 #include <map>
23024
a6a7b054e4ba Rationalize #includes in libinterp/dldfcn directory.
Rik <rik@octave.org>
parents: 22755
diff changeset
32 #include <string>
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
33
23024
a6a7b054e4ba Rationalize #includes in libinterp/dldfcn directory.
Rik <rik@octave.org>
parents: 22755
diff changeset
34 #include "dMatrix.h"
a6a7b054e4ba Rationalize #includes in libinterp/dldfcn directory.
Rik <rik@octave.org>
parents: 22755
diff changeset
35 #include "dRowVector.h"
23873
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
36 #include "file-ops.h"
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
37 #include "file-stat.h"
19513
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19512
diff changeset
38 #include "oct-locbuf.h"
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
39 #include "unwind-prot.h"
19513
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19512
diff changeset
40
19511
19f75d156ffe don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents: 19510
diff changeset
41 #include "defun-dld.h"
19f75d156ffe don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents: 19510
diff changeset
42 #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
43 #include "errwarn.h"
23024
a6a7b054e4ba Rationalize #includes in libinterp/dldfcn directory.
Rik <rik@octave.org>
parents: 22755
diff changeset
44 #include "ov.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
45 #include "ovl.h"
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
46 #include "pager.h"
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
47
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
48 #if defined (HAVE_SNDFILE)
21200
fcac5dbbf9ed maint: Indent #ifdef blocks in libinterp.
Rik <rik@octave.org>
parents: 21129
diff changeset
49 # include <sndfile.h>
19473
2e174b5e7703 Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19468
diff changeset
50 #endif
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
51
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
52 #if defined (HAVE_SNDFILE)
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
53 static void
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
54 safe_close (SNDFILE *file)
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
55 {
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
56 sf_close (file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
57 }
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
58 #endif
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
59
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
60 DEFUN_DLD (audioread, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
61 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
62 @deftypefn {} {[@var{y}, @var{fs}] =} audioread (@var{filename})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
63 @deftypefnx {} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
64
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
65 @deftypefnx {} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{datatype})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
66 @deftypefnx {} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples}, @var{datatype})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
67 Read the audio file @var{filename} and return the audio data @var{y} and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
68 sampling rate @var{fs}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
69
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
70 The audio data is stored as matrix with rows corresponding to audio frames
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
71 and columns corresponding to channels.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
72
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
73 The optional two-element vector argument @var{samples} specifies starting
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
74 and ending frames.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
75
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
76 The optional argument @var{datatype} specifies the datatype to return.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
77 If it is @qcode{"native"}, then the type of data depends on how the data
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
78 is stored in the audio file.
23866
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
79 @seealso{audiowrite, audioformats, audioinfo}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
80 @end deftypefn */)
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
81 {
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
82 #if defined (HAVE_SNDFILE)
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
83
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
84 int nargin = args.length ();
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
85
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
86 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
87 print_usage ();
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
88
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
89 std::string filename = args(0).xstring_value ("audioread: FILENAME must be a string");
19571
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19559
diff changeset
90
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
91 SF_INFO info;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
92 info.format = 0;
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
93 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
94
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
95 if (! file)
23963
3dd89a7b92c8 display message from libsndfile if opening files fails (bug #51859)
Markus Mützel <markus.muetzel@gmx.de>
parents: 23873
diff changeset
96 error ("audioread: failed to open input file '%s': %s",
3dd89a7b92c8 display message from libsndfile if opening files fails (bug #51859)
Markus Mützel <markus.muetzel@gmx.de>
parents: 23873
diff changeset
97 filename.c_str (), sf_strerror (file));
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
98
21743
f4d7d0eb5b0c use namespace for unwind_protect class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
99 octave::unwind_protect frame;
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
100
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
101 frame.add_fcn (safe_close, file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
102
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
103 OCTAVE_LOCAL_BUFFER (double, data, info.frames * info.channels);
19554
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
104
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
105 sf_read_double (file, data, info.frames * info.channels);
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
106
19554
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
107 sf_count_t start = 0;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
108 sf_count_t end = info.frames;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
109
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
110 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
111 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
112 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
113
20228
00cf2847355d Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20163
diff changeset
114 if (range.numel () != 2)
20825
66cd994d1b79 eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20790
diff changeset
115 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
116
23450
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23220
diff changeset
117 double dstart = (octave::math::isinf (range(0)) ? info.frames : range(0));
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23220
diff changeset
118 double dend = (octave::math::isinf (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
119
19811
f24d9486e66b audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents: 19697
diff changeset
120 if (dstart < 1 || dstart > dend || dend > info.frames
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
121 || octave::math::x_nint (dstart) != dstart
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
122 || octave::math::x_nint (dend) != dend)
20825
66cd994d1b79 eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20790
diff changeset
123 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
124
19811
f24d9486e66b audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents: 19697
diff changeset
125 start = dstart - 1;
19554
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
126 end = dend;
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
127 }
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
128
19554
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
129 sf_count_t items = end - start;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
130
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
131 Matrix audio (items, info.channels);
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
132
19554
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
133 double *paudio = audio.fortran_vec ();
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
134
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
135 data += start * info.channels;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
136
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
137 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
138 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
139 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
140 paudio[items*channel+i] = *data++;
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
141 }
19513
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19512
diff changeset
142
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
143 octave_value ret_audio;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
144
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
145 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
146 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
147 std::string type;
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
148 if (nargin == 3)
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
149 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
150 else
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
151 type = args(1).string_value ();
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
152
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
153 if (type == "native")
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
154 {
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
155 switch (info.format & SF_FORMAT_SUBMASK)
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
156 {
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
157 case SF_FORMAT_PCM_S8:
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
158 ret_audio = int8NDArray (audio * 128);
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
159 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
160 case SF_FORMAT_PCM_U8:
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
161 ret_audio = uint8NDArray (audio * 128 + 128);
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
162 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
163 case SF_FORMAT_PCM_16:
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
164 ret_audio = int16NDArray (audio * 32768);
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
165 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
166 case SF_FORMAT_PCM_24:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
167 ret_audio = int32NDArray (audio * 8388608);
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
168 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
169 case SF_FORMAT_PCM_32:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
170 ret_audio = int32NDArray (audio * 2147483648);
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
171 break;
25409
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
172 case SF_FORMAT_FLOAT:
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
173 ret_audio = FloatNDArray (audio);
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
174 break;
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
175 default:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
176 ret_audio = audio;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
177 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
178 }
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
179 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
180 else
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
181 ret_audio = audio;
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
182 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
183 else
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
184 ret_audio = audio;
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19501
diff changeset
185
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
186 return ovl (ret_audio, info.samplerate);
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
187
19468
9161b7653392 compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19463
diff changeset
188 #else
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
189
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21602
diff changeset
190 octave_unused_parameter (args);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21602
diff changeset
191
21103
5a83a422d8a7 Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents: 21100
diff changeset
192 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
193 "reading and writing sound files through libsndfile");
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
194
19468
9161b7653392 compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19463
diff changeset
195 #endif
19462
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
196 }
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
197
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
198 #if defined (HAVE_SNDFILE)
19526
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19519
diff changeset
199
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
200 static int
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
201 extension_to_format (const std::string& ext)
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
202 {
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
203 static bool initialized = false;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
204
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
205 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
206
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
207 if (! initialized)
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
208 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
235 initialized = true;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
236 }
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
237
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
238 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
239
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
240 return (it != table.end ()) ? it->second : 0;
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
241 }
19526
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19519
diff changeset
242
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
243 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
244
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
245 DEFUN_DLD (audiowrite, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
246 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
247 @deftypefn {} {} audiowrite (@var{filename}, @var{y}, @var{fs})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
248 @deftypefnx {} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
249
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
250 Write audio data from the matrix @var{y} to @var{filename} at sampling rate
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
251 @var{fs} with the file format determined by the file extension.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
252
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
253 Additional name/value argument pairs may be used to specify the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
254 following options:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
255
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
256 @table @samp
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
257 @item BitsPerSample
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
258 Number of bits per sample. Valid values are 8, 16, 24, and 32. Default is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
259 16.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
260
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
261 @item BitRate
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
262 Valid argument name, but ignored. Left for compatibility with @sc{matlab}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
263
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
264 @item Quality
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
265 Quality setting for the Ogg Vorbis compressor. Values can range between 0
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
266 and 100 with 100 being the highest quality setting. Default is 75.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
267
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
268 @item Title
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
269 Title for the audio file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
270
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
271 @item Artist
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
272 Artist name.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
273
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
274 @item Comment
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
275 Comment.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
276 @end table
23866
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
277 @seealso{audioread, audioformats, audioinfo}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
278 @end deftypefn */)
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
279 {
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
280 #if defined (HAVE_SNDFILE)
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
281
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
282 int nargin = args.length ();
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
283
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
284 if (nargin < 3)
20790
c2d9556d51d0 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20578
diff changeset
285 print_usage ();
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
286
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
287 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
288
19812
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
289 double bias = 0.0;
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
290 double scale = 1.0;
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
291
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
292 if (args(1).is_uint8_type ())
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
293 bias = scale = 127.5;
19812
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
294 else if (args(1).is_int16_type ())
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
295 scale = 32768; // 2^15
19812
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
296 else if (args(1).is_int32_type ())
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
297 scale = 2147483648; // 2^31
23580
2230f9e10fb3 maint: Deprecate is_integer_type and replace with isinteger.
Rik <rik@octave.org>
parents: 23459
diff changeset
298 else if (args(1).isinteger ())
21129
228b65504557 maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents: 21103
diff changeset
299 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
300
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
301 Matrix audio = args(1).matrix_value ();
19812
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
302
27416
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
303 if (! args(2).is_scalar_type () || ! args(2).isnumeric ())
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
304 error ("audiowrite: sample rate FS must be a positive scalar integer");
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
305 int samplerate = args(2).int_value ();
27416
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
306 if (samplerate < 1)
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
307 error ("audiowrite: sample rate FS must be a positive scalar integer");
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
308
19871
ed5ee3f610db Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents: 19813
diff changeset
309 std::string ext;
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23580
diff changeset
310 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
311 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
312 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
313 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
314
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
315 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
316
19813
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19812
diff changeset
317 if (audio.rows () == 1)
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19812
diff changeset
318 audio = audio.transpose ();
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19812
diff changeset
319
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
320 OCTAVE_LOCAL_BUFFER (double, data, items_to_write);
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
321
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
322 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
323 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
324 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
325 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
326 {
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19811
diff changeset
327 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
328 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
329 }
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
330 }
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
331
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
332 SF_INFO info;
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
333
21602
47c76e56a1de maint: Remove extra space between end of statement and semicolon.
Rik <rik@octave.org>
parents: 21547
diff changeset
334 memset (&info, 0, sizeof (info));
19513
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19512
diff changeset
335
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
336 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
337
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
338 if (ext == "ogg")
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
339 {
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
340 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
341
25572
64bcc3ab2f6a Write audio OGG files in smaller chunk sizes (bug #54280).
Rik <rik@octave.org>
parents: 25054
diff changeset
342 // FIXME: There seems to be a bug writing ogg files in one shot that
64bcc3ab2f6a Write audio OGG files in smaller chunk sizes (bug #54280).
Rik <rik@octave.org>
parents: 25054
diff changeset
343 // causes a segfault: https://bugs.debian.org/760898.
64bcc3ab2f6a Write audio OGG files in smaller chunk sizes (bug #54280).
Rik <rik@octave.org>
parents: 25054
diff changeset
344 // Breaking it up into a series of smaller chunks appears to avoid the
64bcc3ab2f6a Write audio OGG files in smaller chunk sizes (bug #54280).
Rik <rik@octave.org>
parents: 25054
diff changeset
345 // problem and produces valid files.
64bcc3ab2f6a Write audio OGG files in smaller chunk sizes (bug #54280).
Rik <rik@octave.org>
parents: 25054
diff changeset
346 chunk_size = 0x100000;
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
347 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
348 else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
349 info.format = SF_FORMAT_PCM_16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
350
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
351 info.channels = audio.columns ();
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
352 info.samplerate = samplerate;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
353 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
354
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
355 std::string title = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
356 std::string artist = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
357 std::string comment = "";
23868
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
358 double quality = 0.75;
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
359
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
360 for (int i = 3; i < nargin; i += 2)
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
361 {
21693
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
362 if (i >= nargin - 1)
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
363 error ("audiowrite: invalid number of arguments");
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
364
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
365 std::string keyword_orig = args(i).string_value ();
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
366 std::string keyword = args(i).xtolower ().string_value ();
21693
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
367 octave_value value_arg = args(i+1);
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
368
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
369 if (keyword == "bitspersample")
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
370 {
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
371 info.format &= ~SF_FORMAT_SUBMASK;
21693
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
372 int bits = value_arg.int_value ();
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
373 if (bits == 8)
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
374 {
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
375 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
376 info.format |= SF_FORMAT_PCM_U8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
377 else
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
378 info.format |= SF_FORMAT_PCM_S8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
379 }
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
380 else if (bits == 16)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
381 info.format |= SF_FORMAT_PCM_16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
382 else if (bits == 24)
25409
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
383 {
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
384 if ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
385 info.format |= SF_FORMAT_PCM_32;
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
386 else
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
387 info.format |= SF_FORMAT_PCM_24;
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
388 }
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
389 else if (bits == 32)
24006
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
390 {
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
391 if ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
392 && args(1).isfloat ())
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
393 info.format |= SF_FORMAT_FLOAT;
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
394 else
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
395 info.format |= SF_FORMAT_PCM_32;
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
396 }
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
397 else if (bits == 64)
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
398 info.format |= SF_FORMAT_DOUBLE;
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
399 else
20825
66cd994d1b79 eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20790
diff changeset
400 error ("audiowrite: wrong number of bits specified");
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
401 }
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
402 else if (keyword == "bitrate")
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
403 warning_with_id ("Octave:audiowrite:unused-parameter",
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
404 "audiowrite: 'BitRate' accepted for Matlab "
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
405 "compatibility, but is ignored");
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
406 else if (keyword == "quality")
23868
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
407 {
24006
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
408 if (! value_arg.is_scalar_type ())
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
409 error ("audiowrite: Quality value must be a scalar");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
410
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
411 double value = value_arg.xdouble_value ("audiowrite: Quality value must be a numeric scalar between 0 and 100");
23868
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
412
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
413 if (octave::math::isnan (value) || value < 0 || value > 100)
24006
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
414 error ("audiowrite: Quality value must be a number between 0 and 100");
23868
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
415
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
416 quality = value / 100;
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
417 }
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
418 else if (keyword == "title")
21693
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
419 title = value_arg.string_value ();
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
420 else if (keyword == "artist")
21693
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
421 artist = value_arg.string_value ();
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
422 else if (keyword == "comment")
21693
2c6d1ef071d1 avoid crash in audiowrite argument processing (bug #47875)
John W. Eaton <jwe@octave.org>
parents: 20163
diff changeset
423 comment = value_arg.string_value ();
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
424 else
23853
f8660521678e audiowrite: improve keyword option validation and error reporting
Mike Miller <mtmiller@octave.org>
parents: 23807
diff changeset
425 error ("audiowrite: unrecognized option: '%s'", keyword_orig.c_str ());
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
426 }
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
427
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
428 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
429
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
430 if (! file)
23963
3dd89a7b92c8 display message from libsndfile if opening files fails (bug #51859)
Markus Mützel <markus.muetzel@gmx.de>
parents: 23873
diff changeset
431 error ("audiowrite: failed to open output file '%s': %s",
3dd89a7b92c8 display message from libsndfile if opening files fails (bug #51859)
Markus Mützel <markus.muetzel@gmx.de>
parents: 23873
diff changeset
432 filename.c_str (), sf_strerror (file));
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
433
21743
f4d7d0eb5b0c use namespace for unwind_protect class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
434 octave::unwind_protect frame;
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
435
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
436 frame.add_fcn (safe_close, file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
437
28223
45763d59cb4f use nullptr instead of NULL or 0 in a few more places
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
438 sf_command (file, SFC_SET_NORM_DOUBLE, nullptr, SF_TRUE);
45763d59cb4f use nullptr instead of NULL or 0 in a few more places
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
439 sf_command (file, SFC_SET_CLIPPING, nullptr, SF_TRUE) ;
23868
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
440 sf_command (file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality));
ff9758331d78 audiowrite: add support for Quality option on Ogg Vorbis files (bug #51704)
Mike Miller <mtmiller@octave.org>
parents: 23866
diff changeset
441
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
442 if (title != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
443 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
444
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
445 if (artist != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
446 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
447
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
448 if (comment != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
449 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
450
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
451 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
452 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
453
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
454 if (chunk_size == 0)
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
455 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
456
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
457 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
458 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
459 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
460 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
461
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
462 sf_count_t items_written = sf_write_double (file, data+offset, chunk_size);
19553
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
463
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
464 if (items_written != chunk_size)
26162
ef1ef6364a6a Silence compiler warnings about incorrect format specifiers cross-compiling (bug #55046).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25573
diff changeset
465 error ("audiowrite: write failed, wrote %" PRId64 " of %" PRId64
ef1ef6364a6a Silence compiler warnings about incorrect format specifiers cross-compiling (bug #55046).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25573
diff changeset
466 " items\n", 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
467
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
468 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
469 offset += chunk_size;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19544
diff changeset
470 }
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
471
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
472 // 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
473 // was written successfully?
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
474 return ovl ();
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
475
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
476 #else
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
477
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21602
diff changeset
478 octave_unused_parameter (args);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21602
diff changeset
479
21103
5a83a422d8a7 Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents: 21100
diff changeset
480 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
481 "reading and writing sound files through libsndfile");
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
482
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
483 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
484 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
485
24006
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
486 /*
27414
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
487 ## Joint audiowrite/audioread tests
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
488 ## 8-bit Unsigned PCM
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
489 %!testif HAVE_SNDFILE <*56889>
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
490 %! fname = [tempname() ".wav"];
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
491 %! unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
492 %! y1 = uint8 ([0, 1, 2, 253, 254, 255]);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
493 %! audiowrite (fname, y1, 8000, "BitsPerSample", 8);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
494 %! y2 = audioread (fname, "native");
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
495 %! unwind_protect_cleanup
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
496 %! unlink (fname);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
497 %! end_unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
498 %! assert (y1(:), y2);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
499
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
500 ## 8-bit Signed PCM
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
501 %!testif HAVE_SNDFILE <*56889>
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
502 %! fname = [tempname() ".au"];
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
503 %! unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
504 %! y1 = uint8 ([0, 1, 2, 253, 254, 255]);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
505 %! audiowrite (fname, y1, 8000, "BitsPerSample", 8);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
506 %! y2 = audioread (fname, "native");
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
507 %! unwind_protect_cleanup
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
508 %! unlink (fname);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
509 %! end_unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
510 %! assert (y2, int8 ([-128; -127; -126; 125; 126; 127]));
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
511
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
512 ## 16-bit Signed PCM
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
513 %!testif HAVE_SNDFILE <*56889>
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
514 %! fname = [tempname() ".wav"];
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
515 %! unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
516 %! y1 = int16 ([-32768, -32767, -32766, 32765, 32766, 32767]);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
517 %! audiowrite (fname, y1, 8000, "BitsPerSample", 16);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
518 %! y2 = audioread (fname, "native");
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
519 %! unwind_protect_cleanup
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
520 %! unlink (fname);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
521 %! end_unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
522 %! assert (y1(:), y2);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
523
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
524 ## 24-bit Signed PCM
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
525 %!testif HAVE_SNDFILE <*56889>
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
526 %! fname = [tempname() ".au"];
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
527 %! unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
528 %! y1 = [-8388608, -8388607, -8388606, 8388605, 8388606, 8388607] / 8388608;
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
529 %! audiowrite (fname, y1, 8000, "BitsPerSample", 24);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
530 %! y2 = audioread (fname, "native");
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
531 %! unwind_protect_cleanup
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
532 %! unlink (fname);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
533 %! end_unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
534 %! assert (int32 ([-8388608; -8388607; -8388606; 8388605; 8388606; 8388607]),
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
535 %! y2);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
536
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
537 ## 32-bit Signed PCM
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
538 %!testif HAVE_SNDFILE <*56889>
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
539 %! fname = [tempname() ".wav"];
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
540 %! unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
541 %! y1 = int32 ([-2147483648, -2147483647, -2147483646, 2147483645, 2147483646, 2147483647 ]);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
542 %! audiowrite (fname, y1, 8000, "BitsPerSample", 32);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
543 %! y2 = audioread (fname, "native");
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
544 %! unwind_protect_cleanup
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
545 %! unlink (fname);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
546 %! end_unwind_protect
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
547 %! assert (y1(:), y2);
5283c505f92b Fix numerous errors in audiowrite/audioread (bug #56889).
Rik <rik@octave.org>
parents: 26387
diff changeset
548
24006
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
549 ## Test input validation
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
550 %!testif HAVE_SNDFILE
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
551 %! fail ("audiowrite (1, 1, 8e3)", "FILENAME must be a string");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
552 %! fail ("audiowrite ('foo', int64 (1), 8e3)", "wrong type argument 'int64 scalar'");
27416
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
553 %! fail ("audiowrite ('foo', [0 1], [8e3, 8e3])", "FS must be a positive scalar");
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
554 %! fail ("audiowrite ('foo', 1, {8e3})", "FS must be a .* integer");
602298e76e91 Resolve audiowrite FIXME about input validation for sample rate input FS.
Rik <rik@octave.org>
parents: 27414
diff changeset
555 %! fail ("audiowrite ('foo', 1, -8e3)", "FS must be a positive");
24006
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
556 %! fail ("audiowrite ('foo', 1, 8e3, 'bitspersample')", "invalid number of arguments");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
557 %! fail ("audiowrite ('foo', 1, 8e3, 'bitspersample', 48)", "wrong number of bits specified");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
558 %! fail ("audiowrite ('foo', 1, 8e3, 'quality', [2 3 4])", "Quality value must be a scalar");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
559 %! fail ("audiowrite ('foo', 1, 8e3, 'quality', NaN)", "Quality value must be .* between 0 and 100");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
560 %! fail ("audiowrite ('foo', 1, 8e3, 'quality', -1)", "Quality value must be .* between 0 and 100");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
561 %! fail ("audiowrite ('foo', 1, 8e3, 'quality', 101)", "Quality value must be .* between 0 and 100");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
562 %! fail ("audiowrite ('foo', 1, 8e3, 'foo', 'bar')", "unrecognized option: 'foo'");
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
563 */
d21de523dbce Write WAV files of 32-bits in FLOAT format rather than PCM_32 (bug #45490).
Rik <rik@octave.org>
parents: 23963
diff changeset
564
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
565 DEFUN_DLD (audioinfo, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
566 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
567 @deftypefn {} {@var{info} =} audioinfo (@var{filename})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
568 Return information about an audio file specified by @var{filename}.
23866
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
569
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
570 The output @var{info} is a structure containing the following fields:
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
571
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
572 @table @samp
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
573 @item Filename
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
574 Name of the audio file.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
575
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
576 @item CompressionMethod
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
577 Audio compression method. Unused, only present for compatibility with
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
578 @sc{matlab}.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
579
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
580 @item NumChannels
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
581 Number of audio channels.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
582
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
583 @item SampleRate
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
584 Sample rate of the audio, in Hertz.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
585
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
586 @item TotalSamples
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
587 Number of samples in the file.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
588
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
589 @item Duration
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
590 Duration of the audio, in seconds.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
591
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
592 @item BitsPerSample
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
593 Number of bits per sample.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
594
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
595 @item BitRate
25003
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24534
diff changeset
596 Audio bit rate. Unused, only present for compatibility with @sc{matlab}.
23866
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
597
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
598 @item Title
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
599 @qcode{"Title"} audio metadata value as a string, or empty if not present.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
600
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
601 @item Artist
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
602 @qcode{"Artist"} audio metadata value as a string, or empty if not present.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
603
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
604 @item Comment
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
605 @qcode{"Comment"} audio metadata value as a string, or empty if not present.
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
606 @end table
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
607 @seealso{audioread, audiowrite}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
608 @end deftypefn */)
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
609 {
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
610 #if defined (HAVE_SNDFILE)
19526
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19519
diff changeset
611
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
612 if (args.length () != 1)
20790
c2d9556d51d0 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20578
diff changeset
613 print_usage ();
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
614
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
615 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
616
23873
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
617 octave::sys::file_stat fs (filename);
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
618 if (! fs.exists ())
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
619 error ("audioinfo: FILENAME '%s' not found", filename.c_str ());
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
620
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
621 SF_INFO info;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
622 info.format = 0;
19554
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
623 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
624
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
625 if (! file)
23963
3dd89a7b92c8 display message from libsndfile if opening files fails (bug #51859)
Markus Mützel <markus.muetzel@gmx.de>
parents: 23873
diff changeset
626 error ("audioinfo: failed to open input file '%s': %s",
3dd89a7b92c8 display message from libsndfile if opening files fails (bug #51859)
Markus Mützel <markus.muetzel@gmx.de>
parents: 23873
diff changeset
627 filename.c_str (), sf_strerror (file));
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
628
21743
f4d7d0eb5b0c use namespace for unwind_protect class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
629 octave::unwind_protect frame;
19559
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
630
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
631 frame.add_fcn (safe_close, file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19554
diff changeset
632
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
633 octave_scalar_map result;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
634
23873
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
635 std::string full_name = octave::sys::canonicalize_file_name (filename);
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
636
9ada3f4f4e28 audioinfo: return absolute file name in info structure
Mike Miller <mtmiller@octave.org>
parents: 23868
diff changeset
637 result.assign ("Filename", full_name);
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
638 result.assign ("CompressionMethod", "");
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
639 result.assign ("NumChannels", info.channels);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
640 result.assign ("SampleRate", info.samplerate);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
641 result.assign ("TotalSamples", info.frames);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
642
19513
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19512
diff changeset
643 double dframes = info.frames;
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19512
diff changeset
644 double drate = info.samplerate;
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
645 result.assign ("Duration", dframes / drate);
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
646
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
647 int bits;
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
648 switch (info.format & SF_FORMAT_SUBMASK)
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
649 {
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
650 case SF_FORMAT_PCM_S8:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
651 bits = 8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
652 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
653 case SF_FORMAT_PCM_U8:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
654 bits = 8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
655 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
656 case SF_FORMAT_PCM_16:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
657 bits = 16;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
658 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
659 case SF_FORMAT_PCM_24:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
660 bits = 24;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
661 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
662 case SF_FORMAT_PCM_32:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
663 bits = 32;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
664 break;
25409
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
665 case SF_FORMAT_FLOAT:
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
666 bits = 32;
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
667 break;
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
668 case SF_FORMAT_DOUBLE:
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
669 bits = 64;
4f6c985ca28a Fix various bugs with audioread, audiowrite, audioinfo (bug #53975).
Rik <rik@octave.org>
parents: 25054
diff changeset
670 break;
19675
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
671 default:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
672 bits = -1;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
673 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
674 }
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
675
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
676 result.assign ("BitsPerSample", bits);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
677 result.assign ("BitRate", -1);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
678 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
679 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
680 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
681
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20865
diff changeset
682 return ovl (result);
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
683
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
684 #else
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
685
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21602
diff changeset
686 octave_unused_parameter (args);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21602
diff changeset
687
21103
5a83a422d8a7 Don't set 3rd argument to err_disabled_feature within core Octave.
Rik <rik@octave.org>
parents: 21100
diff changeset
688 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
689 "reading and writing sound files through libsndfile");
19519
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19516
diff changeset
690
19510
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
691 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
692 }
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
693
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
694 #if defined (HAVE_SNDFILE)
21712
09e1bb792dd9 Fix compilation error when building without sndfile
Mike Miller <mtmiller@octave.org>
parents: 21710
diff changeset
695
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
696 static void
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
697 audio_sub_formats (int format)
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
698 {
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
699 int count;
23459
ddcbf10c8c39 maint: Use C++11 nullptr rather than 0 or NULL for remaining instances in code.
Rik <rik@octave.org>
parents: 23450
diff changeset
700 sf_command (nullptr, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int));
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
701
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
702 for (int i = 0; i < count; i++)
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
703 {
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
704 SF_FORMAT_INFO info;
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
705 info.format = i;
23459
ddcbf10c8c39 maint: Use C++11 nullptr rather than 0 or NULL for remaining instances in code.
Rik <rik@octave.org>
parents: 23450
diff changeset
706 sf_command (nullptr, SFC_GET_FORMAT_SUBTYPE, &info, sizeof (info));
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
707
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
708 SF_INFO sfinfo;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
709 memset (&sfinfo, 0, sizeof (sfinfo));
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
710 sfinfo.channels = 1;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
711 sfinfo.format = (format & SF_FORMAT_TYPEMASK) | info.format;
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
712
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
713 if (sf_format_check (&sfinfo))
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
714 octave_stdout << " " << info.name << std::endl;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
715 }
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
716 }
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
717
21712
09e1bb792dd9 Fix compilation error when building without sndfile
Mike Miller <mtmiller@octave.org>
parents: 21710
diff changeset
718 #endif
09e1bb792dd9 Fix compilation error when building without sndfile
Mike Miller <mtmiller@octave.org>
parents: 21710
diff changeset
719
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
720 DEFUN_DLD (audioformats, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
721 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
722 @deftypefn {} {} audioformats ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
723 @deftypefnx {} {} audioformats (@var{format})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
724 Display information about all supported audio formats.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
725
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
726 If the optional argument @var{format} is given, then display only formats
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
727 with names that start with @var{format}.
23866
4f68899f9e5b doc: improve docstrings for audioformats, audioinfo, audioread, and audiowrite
Mike Miller <mtmiller@octave.org>
parents: 23853
diff changeset
728 @seealso{audioread, audiowrite}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
729 @end deftypefn */)
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
730 {
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21712
diff changeset
731 #if defined (HAVE_SNDFILE)
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
732
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
733 if (args.length () > 1)
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
734 print_usage ();
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
735
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
736 std::string search = "";
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
737 if (args.length () > 0)
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
738 {
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
739 search = args(0).string_value ();
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
740 std::transform (search.begin (), search.end (), search.begin (), tolower);
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
741 }
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
742
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
743 int count;
23459
ddcbf10c8c39 maint: Use C++11 nullptr rather than 0 or NULL for remaining instances in code.
Rik <rik@octave.org>
parents: 23450
diff changeset
744 sf_command (nullptr, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
745
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
746 for (int i = 0; i < count; i++)
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
747 {
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
748 SF_FORMAT_INFO info;
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
749 info.format = i;
23459
ddcbf10c8c39 maint: Use C++11 nullptr rather than 0 or NULL for remaining instances in code.
Rik <rik@octave.org>
parents: 23450
diff changeset
750 sf_command (nullptr, SFC_GET_FORMAT_MAJOR, &info, sizeof (info));
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
751 bool match = true;
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
752
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
753 if (! search.empty ())
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
754 {
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
755 std::string nm = info.name;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
756 std::transform (nm.begin (), nm.end (), nm.begin (), tolower);
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
757 match = nm.compare (0, search.length (), search) == 0;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
758 }
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
759
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
760 if (match)
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
761 {
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
762 octave_stdout << "name: " << info.name << std::endl;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
763 octave_stdout << "extension: " << info.extension << std::endl;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
764 octave_stdout << "id: " << info.format << std::endl;
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
765 octave_stdout << "subformats:" << std::endl;
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
766
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
767 audio_sub_formats (info.format);
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
768 }
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
769 }
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
770
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
771 #else
22265
15b9d7cb3098 eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
772
15b9d7cb3098 eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
773 octave_unused_parameter (args);
15b9d7cb3098 eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
774
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
775 err_disabled_feature ("audioformats",
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
776 "getting sound formats through libsndfile");
22265
15b9d7cb3098 eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
777
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
778 #endif
21710
f7850bd66042 audioformats: Clean up coding style and docstring
Mike Miller <mtmiller@octave.org>
parents: 21709
diff changeset
779
23024
a6a7b054e4ba Rationalize #includes in libinterp/dldfcn directory.
Rik <rik@octave.org>
parents: 22755
diff changeset
780 return octave_value_list ();
21709
88657798b650 audioformats: New function to list available audio formats (bug #47464)
Damjan Angelovski <damjangelovski@gmail.com>
parents: 21694
diff changeset
781 }