Mercurial > octave-nkf
annotate libinterp/dldfcn/audioread.cc @ 20613:647db46ad754
eliminate more simple uses of error_state
* audioread.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 08 Oct 2015 17:41:21 -0400 |
parents | 00cf2847355d |
children |
rev | line source |
---|---|
19534
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
1 /* |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
2 |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19709
diff
changeset
|
3 Copyright (C) 2013-2015 Vytautas JanĨauskas |
19534
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
4 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
5 This file is part of Octave. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
6 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
10 option) any later version. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
11 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
15 for more details. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
16 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
20 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
21 */ |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
22 |
19535
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
24 #include <config.h> |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
25 #endif |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
26 |
19544 | 27 #include <string> |
28 #include <map> | |
29 | |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
30 #include "oct-locbuf.h" |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
31 #include "unwind-prot.h" |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
32 |
19545
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
33 #include "defun-dld.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
34 #include "error.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
35 #include "gripes.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
36 #include "oct-obj.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
37 #include "ov.h" |
19497
157eeaa53277
Fix includes in audioread, audiowrite and audioinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19496
diff
changeset
|
38 #include "ov-struct.h" |
19544 | 39 |
19507
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19502
diff
changeset
|
40 #ifdef HAVE_SNDFILE |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
41 #include <sndfile.h> |
19507
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19502
diff
changeset
|
42 #endif |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
43 |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
44 #ifdef HAVE_SNDFILE |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
45 static void |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
46 safe_close (SNDFILE *file) |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
47 { |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
48 sf_close (file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
49 } |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
50 #endif |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
51 |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
52 DEFUN_DLD (audioread, args, , |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
53 "-*- texinfo -*-\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
54 @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
55 @deftypefnx {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples})\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
56 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
57 @deftypefnx {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{datatype})\n\ |
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
58 @deftypefnx {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples}, @var{datatype})\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
59 Read the audio file @var{filename} and return the audio data @var{y} and\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
60 sampling rate @var{fs}.\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
61 \n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
62 The audio data is stored as matrix with rows corresponding to audio frames\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
63 and columns corresponding to channels.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
64 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
65 The optional two-element vector argument @var{samples} specifies starting\n\ |
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
66 and ending frames.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
67 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
68 The optional argument @var{datatype} specifies the datatype to return.\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
69 If it is @qcode{\"native\"}, then the type of data depends on how the data\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
70 is stored in the audio file.\n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
71 @end deftypefn") |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
72 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
73 octave_value_list retval; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
74 |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
75 #ifdef HAVE_SNDFILE |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
76 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
77 int nargin = args.length (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
78 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
79 if (nargin < 1 || nargin > 3) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
80 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
81 print_usage (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
82 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
83 } |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
84 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
85 std::string filename = args(0).string_value (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
86 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
87 SF_INFO info; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
88 info.format = 0; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
89 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
90 |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
91 if (! file) |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
92 { |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
93 error ("audioread: failed to open input file %s", filename.c_str ()); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
94 return retval; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
95 } |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
96 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
97 unwind_protect frame; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
98 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
99 frame.add_fcn (safe_close, file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
100 |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
101 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
102 |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
103 sf_read_float (file, data, info.frames * info.channels); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
104 |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
105 sf_count_t start = 0; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
106 sf_count_t end = info.frames; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
107 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
108 if ((nargin == 2 && ! args(1).is_string ()) || nargin == 3) |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
109 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
110 RowVector range = args(1).row_vector_value (); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
111 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
112 if (range.numel () != 2) |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
113 { |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
114 error ("audioread: invalid specification for range of frames"); |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
115 return retval; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
116 } |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
117 |
19845
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
118 double dstart = xisinf (range(0)) ? info.frames : range(0); |
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
119 double dend = xisinf (range(1)) ? info.frames : range(1); |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
120 |
19845
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
121 if (dstart < 1 || dstart > dend || dend > info.frames |
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
122 || D_NINT (dstart) != dstart || D_NINT (dend) != dend) |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
123 { |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
124 error ("audioread: invalid specification for range of frames"); |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
125 return retval; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
126 } |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
127 |
19845
f24d9486e66b
audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
128 start = dstart - 1; |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
129 end = dend; |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
130 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
131 |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
132 sf_count_t items = end - start; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
133 |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
134 Matrix audio (items, info.channels); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
135 |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
136 double *paudio = audio.fortran_vec (); |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
137 |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
138 data += start * info.channels; |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
139 |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
140 for (int i = 0; i < items; i++) |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
141 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
142 for (int channel = 0; channel < info.channels; channel++) |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
143 paudio[items*channel+i] = *data++; |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
144 } |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
145 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
146 octave_value ret_audio; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
147 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
148 if ((nargin == 2 && args(1).is_string ()) || nargin == 3) |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
149 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
150 std::string type; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
151 if (nargin == 3) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
152 type = args(2).string_value (); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
153 else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
154 type = args(1).string_value (); |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
155 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
156 if (type == "native") |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
157 { |
19709
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
158 switch (info.format & SF_FORMAT_SUBMASK) |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
159 { |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
160 case SF_FORMAT_PCM_S8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
161 ret_audio = int8NDArray (audio * 127); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
162 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
163 case SF_FORMAT_PCM_U8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
164 ret_audio = uint8NDArray (audio * 127 + 127); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
165 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
166 case SF_FORMAT_PCM_16: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
167 ret_audio = int16NDArray (audio * 32767); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
168 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
169 case SF_FORMAT_PCM_24: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
170 ret_audio = int32NDArray (audio * 8388608); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
171 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
172 case SF_FORMAT_PCM_32: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
173 ret_audio = int32NDArray (audio * 2147483648); |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
174 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
175 default: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
176 ret_audio = audio; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
177 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
178 } |
19496
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 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
181 ret_audio = audio; |
19496
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 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
184 ret_audio = audio; |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
185 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
186 retval(1) = info.samplerate; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
187 retval(0) = ret_audio; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
188 |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
189 #else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
190 |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
191 error ("sndfile not found on your system and thus audioread is not functional"); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
192 |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
193 #endif |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
194 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
195 return retval; |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
196 } |
19544 | 197 |
198 #ifdef HAVE_SNDFILE | |
19560
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
199 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
200 static int |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
201 extension_to_format (const std::string& ext) |
19544 | 202 { |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
203 static bool initialized = false; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
204 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
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:
19578
diff
changeset
|
206 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
207 if (! initialized) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
208 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
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:
19578
diff
changeset
|
234 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
235 initialized = true; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
236 } |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
237 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
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:
19578
diff
changeset
|
239 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
240 return (it != table.end ()) ? it->second : 0; |
19544 | 241 } |
19560
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
242 |
19544 | 243 #endif |
244 | |
245 DEFUN_DLD (audiowrite, args, , | |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
246 "-*- texinfo -*-\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
247 @deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs})\n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
248 @deftypefnx {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value}, @dots{})\n\ |
19544 | 249 \n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
250 Write audio data from the matrix @var{y} to @var{filename} at sampling rate\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
251 @var{fs} with the file format determined by the file extension.\n\ |
19544 | 252 \n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19907
diff
changeset
|
253 Additional name/value argument pairs may be used to specify the\n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
254 following options:\n\ |
19544 | 255 \n\ |
256 @table @samp\n\ | |
257 @item BitsPerSample\n\ | |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
258 Number of bits per sample, valid values are 8, 16, 24 and 32. Default is 16.\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
259 \n\ |
19544 | 260 @item BitRate\n\ |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
261 Valid argument name, but ignored. Left for compatibility with @sc{matlab}.\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
262 \n\ |
19544 | 263 @item Quality\n\ |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
264 Quality setting for the Ogg Vorbis compressor. Values can range between 0\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
265 and 100 with 100 being the highest quality setting. Default is 75.\n\ |
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
266 \n\ |
19544 | 267 @item Title\n\ |
268 Title for the audio file.\n\ | |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
269 \n\ |
19544 | 270 @item Artist\n\ |
271 Artist name.\n\ | |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
272 \n\ |
19544 | 273 @item Comment\n\ |
274 Comment.\n\ | |
275 @end table\n\ | |
276 @end deftypefn") | |
277 { | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
278 // FIXME: shouldn't we return something to indicate whether the file |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
279 // was written successfully? |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
280 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
281 octave_value retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
282 |
19544 | 283 #ifdef HAVE_SNDFILE |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
284 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
285 int nargin = args.length (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
286 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
287 if (nargin < 3) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
288 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
289 print_usage (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
290 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
291 } |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
292 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
293 std::string filename = args(0).string_value (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
294 |
19847
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19846
diff
changeset
|
295 Matrix audio = args(1).matrix_value (); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
296 |
19846
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
297 double bias = 0.0; |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
298 double scale = 1.0; |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
299 |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
300 if (args(1).is_uint8_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
301 bias = scale = std::pow (2.0, 7); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
302 else if (args(1).is_int16_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
303 scale = std::pow (2.0, 15); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
304 else if (args(1).is_int32_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
305 scale = std::pow (2.0, 31); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
306 else if (args(1).is_integer_type ()) |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
307 { |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
308 gripe_wrong_type_arg ("audiowrite", args(1)); |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
309 return retval; |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
310 } |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
311 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
312 int samplerate = args(2).int_value (); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
313 |
19906
ed5ee3f610db
Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents:
19847
diff
changeset
|
314 std::string ext; |
19907
d575cd1e0da7
Replace std::size_t with bare size_t for conformity to rest of code.
Rik <rik@octave.org>
parents:
19906
diff
changeset
|
315 size_t dotpos = filename.find_last_of ("."); |
19906
ed5ee3f610db
Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents:
19847
diff
changeset
|
316 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:
19847
diff
changeset
|
317 ext = filename.substr (dotpos + 1); |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
318 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:
19578
diff
changeset
|
319 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
320 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:
19578
diff
changeset
|
321 |
19847
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19846
diff
changeset
|
322 if (audio.rows () == 1) |
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19846
diff
changeset
|
323 audio = audio.transpose (); |
879cff0c05dc
audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents:
19846
diff
changeset
|
324 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
325 OCTAVE_LOCAL_BUFFER (float, data, items_to_write); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
326 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
327 sf_count_t idx = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
328 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:
19578
diff
changeset
|
329 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
330 for (int j = 0; j < audio.columns (); j++) |
19846
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
331 { |
f9ca80482c41
audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents:
19845
diff
changeset
|
332 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:
19845
diff
changeset
|
333 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:
19845
diff
changeset
|
334 } |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
335 } |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
336 |
19544 | 337 SF_INFO info; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
338 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
339 memset (&info, 0, sizeof (info)) ; |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
340 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
341 sf_count_t chunk_size = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
342 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
343 if (ext == "ogg") |
19544 | 344 { |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
345 info.format = SF_FORMAT_VORBIS; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
346 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
347 // FIXME: there seems to be a bug writing ogg files in one shot |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
348 // that causes a segfault. Breaking it up into a series of |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
349 // smaller chunks seems to avoid the problem and produce valid |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
350 // files. |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
351 chunk_size = 0x1FFFFE; |
19544 | 352 } |
353 else | |
354 info.format = SF_FORMAT_PCM_16; | |
355 | |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
356 info.channels = audio.columns (); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
357 info.samplerate = samplerate; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
358 info.channels = audio.cols (); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
359 info.format |= extension_to_format (ext); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
360 |
19544 | 361 std::string title = ""; |
362 std::string artist = ""; | |
363 std::string comment = ""; | |
19546
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
364 // Quality is currently unused? |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
365 // |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
366 // float quality = 0.75; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
367 for (int i = 3; i < nargin; i += 2) |
19544 | 368 { |
369 if (args(i).string_value () == "BitsPerSample") | |
370 { | |
19709
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
371 info.format &= ~SF_FORMAT_SUBMASK; |
19544 | 372 int bits = args(i + 1).int_value (); |
373 if (bits == 8) | |
19709
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
374 { |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
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:
19605
diff
changeset
|
376 info.format |= SF_FORMAT_PCM_U8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
377 else |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
378 info.format |= SF_FORMAT_PCM_S8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
379 } |
19544 | 380 else if (bits == 16) |
381 info.format |= SF_FORMAT_PCM_16; | |
382 else if (bits == 24) | |
383 info.format |= SF_FORMAT_PCM_24; | |
384 else if (bits == 32) | |
385 info.format |= SF_FORMAT_PCM_32; | |
386 else | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
387 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
388 error ("audiowrite: wrong number of bits specified"); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
389 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
390 } |
19544 | 391 } |
392 else if (args(i).string_value () == "BitRate") | |
393 ; | |
19546
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
394 // Quality is currently unused? |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
395 // |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
396 // else if (args(i).string_value () == "Quality") |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
397 // quality = args(i + 1).int_value () * 0.01; |
19544 | 398 else if (args(i).string_value () == "Title") |
399 title = args(i + 1).string_value (); | |
400 else if (args(i).string_value () == "Artist") | |
401 artist = args(i + 1).string_value (); | |
402 else if (args(i).string_value () == "Comment") | |
403 comment = args(i + 1).string_value (); | |
404 else | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
405 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
406 error ("audiowrite: wrong argument name"); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
407 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
408 } |
19544 | 409 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
410 |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
411 SNDFILE *file = sf_open (filename.c_str (), SFM_WRITE, &info); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
412 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
413 if (! file) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
414 { |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
415 error ("audiowrite: failed to open output file %s", filename.c_str ()); |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
416 return retval; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
417 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
418 |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
419 unwind_protect frame; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
420 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
421 frame.add_fcn (safe_close, file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
422 |
19544 | 423 if (title != "") |
424 sf_set_string (file, SF_STR_TITLE, title.c_str ()); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
425 |
19544 | 426 if (artist != "") |
427 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
428 |
19544 | 429 if (comment != "") |
430 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
431 |
19587
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
432 sf_count_t total_items_written = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
433 sf_count_t offset = 0; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
434 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
435 if (chunk_size == 0) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
436 chunk_size = items_to_write; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
437 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
438 while (total_items_written < items_to_write) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
439 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
440 if (items_to_write - offset < chunk_size) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
441 chunk_size = items_to_write - offset; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
442 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
443 sf_count_t items_written = sf_write_float (file, data+offset, chunk_size); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
444 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
445 if (items_written != chunk_size) |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
446 { |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
447 error ("audiowrite: write failed, wrote %ld of %ld items\n", |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
448 items_written, chunk_size); |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
449 return retval; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
450 } |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
451 |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
452 total_items_written += items_written; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
453 offset += chunk_size; |
57efd0a020cf
write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents:
19578
diff
changeset
|
454 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
455 |
19544 | 456 #else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
457 |
19544 | 458 error ("sndfile not found on your system and thus audiowrite is not functional"); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
459 |
19544 | 460 #endif |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
461 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
462 return retval; |
19544 | 463 } |
464 | |
465 DEFUN_DLD (audioinfo, args, , | |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19593
diff
changeset
|
466 "-*- texinfo -*-\n\ |
19544 | 467 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\ |
468 Return information about an audio file specified by @var{filename}.\n\ | |
469 @end deftypefn") | |
470 { | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
471 octave_value retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
472 |
19560
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
473 #ifdef HAVE_SNDFILE |
7f271cef5850
protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents:
19553
diff
changeset
|
474 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
475 if (args.length () != 1) |
19544 | 476 { |
477 print_usage (); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
478 return retval; |
19544 | 479 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
480 |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
481 std::string filename = args(0).string_value (); |
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
482 |
19544 | 483 SF_INFO info; |
484 info.format = 0; | |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
485 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
486 |
19593
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
487 if (! file) |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
488 { |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
489 error ("audioinfo: failed to open file %s", filename.c_str ()); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
490 return retval; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
491 } |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
492 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
493 unwind_protect frame; |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
494 |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
495 frame.add_fcn (safe_close, file); |
701b43ce4467
properly close audio files
John W. Eaton <jwe@octave.org>
parents:
19588
diff
changeset
|
496 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
497 octave_scalar_map result; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
498 |
19588
398e42431dcc
improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents:
19587
diff
changeset
|
499 result.assign ("Filename", filename); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
500 result.assign ("CompressionMethod", ""); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
501 result.assign ("NumChannels", info.channels); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
502 result.assign ("SampleRate", info.samplerate); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
503 result.assign ("TotalSamples", info.frames); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
504 |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
505 double dframes = info.frames; |
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
506 double drate = info.samplerate; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
507 result.assign ("Duration", dframes / drate); |
19544 | 508 |
509 int bits; | |
19709
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
510 switch (info.format & SF_FORMAT_SUBMASK) |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
511 { |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
512 case SF_FORMAT_PCM_S8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
513 bits = 8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
514 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
515 case SF_FORMAT_PCM_U8: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
516 bits = 8; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
517 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
518 case SF_FORMAT_PCM_16: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
519 bits = 16; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
520 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
521 case SF_FORMAT_PCM_24: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
522 bits = 24; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
523 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
524 case SF_FORMAT_PCM_32: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
525 bits = 32; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
526 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
527 default: |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
528 bits = -1; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
529 break; |
793d295fed4d
Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19605
diff
changeset
|
530 } |
19544 | 531 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
532 result.assign ("BitsPerSample", bits); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
533 result.assign ("BitRate", -1); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
534 result.assign ("Title", sf_get_string (file, SF_STR_TITLE)); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
535 result.assign ("Artist", sf_get_string (file, SF_STR_ARTIST)); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
536 result.assign ("Comment", sf_get_string (file, SF_STR_COMMENT)); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
537 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
538 retval = result; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
539 |
19544 | 540 #else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
541 |
19544 | 542 error ("sndfile not found on your system and thus audioinfo is not functional"); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
543 |
19544 | 544 #endif |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
545 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
546 return retval; |
19544 | 547 } |