annotate libinterp/dldfcn/audioread.cc @ 20198:075a5e2e1ba5 stable

doc: Update more docstrings to have one sentence summary as first line. Reviewed build-aux, libinterp/dldfcn, libinterp/octave-value, libinterp/parse-tree directories. * build-aux/mk-opts.pl, libinterp/dldfcn/__magick_read__.cc, libinterp/dldfcn/amd.cc, libinterp/dldfcn/audiodevinfo.cc, libinterp/dldfcn/audioread.cc, libinterp/dldfcn/ccolamd.cc, libinterp/dldfcn/chol.cc, libinterp/dldfcn/colamd.cc, libinterp/dldfcn/convhulln.cc, libinterp/dldfcn/dmperm.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc, libinterp/dldfcn/symbfact.cc, libinterp/dldfcn/symrcm.cc, libinterp/octave-value/ov-base.cc, libinterp/octave-value/ov-bool-mat.cc, libinterp/octave-value/ov-cell.cc, libinterp/octave-value/ov-class.cc, libinterp/octave-value/ov-fcn-handle.cc, libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-null-mat.cc, libinterp/octave-value/ov-oncleanup.cc, libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-struct.cc, libinterp/octave-value/ov-typeinfo.cc, libinterp/octave-value/ov-usr-fcn.cc, libinterp/octave-value/ov.cc, libinterp/parse-tree/lex.ll, libinterp/parse-tree/oct-parse.in.yy, libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-eval.cc, libinterp/parse-tree/pt-mat.cc: doc: Update more docstrings to have one sentence summary as first line.
author Rik <rik@octave.org>
date Sun, 03 May 2015 21:52:42 -0700
parents d575cd1e0da7
children 00cf2847355d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
27 #include <string>
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
28 #include <map>
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
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
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
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
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
87 if (error_state)
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
88 return retval;
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
89
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
90 SF_INFO info;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
91 info.format = 0;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
92 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
93
19593
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
94 if (! file)
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 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
97 return retval;
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
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
100 unwind_protect frame;
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
101
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
102 frame.add_fcn (safe_close, file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
103
19547
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19546
diff changeset
104 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
105
19537
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
106 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
107
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
108 sf_count_t start = 0;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
109 sf_count_t end = info.frames;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
110
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
111 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
112 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
113 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
114
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
115 if (error_state)
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
116 return retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
117
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
118 if (range.nelem () != 2)
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
119 {
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
120 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
121 return retval;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
122 }
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
123
19845
f24d9486e66b audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents: 19731
diff changeset
124 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
125 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
126
19845
f24d9486e66b audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents: 19731
diff changeset
127 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
128 || 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
129 {
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
130 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
131 return retval;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
132 }
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
133
19845
f24d9486e66b audioread: Fix sample index argument to be Matlab compatible
Mike Miller <mtmiller@ieee.org>
parents: 19731
diff changeset
134 start = dstart - 1;
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
135 end = dend;
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
136 }
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
137
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
138 sf_count_t items = end - start;
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 Matrix audio (items, info.channels);
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
141
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
142 double *paudio = audio.fortran_vec ();
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
143
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
144 data += start * info.channels;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
145
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
146 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
147 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
148 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
149 paudio[items*channel+i] = *data++;
19537
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
150 }
19547
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19546
diff changeset
151
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
152 octave_value ret_audio;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
153
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
154 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
155 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
156 std::string type;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
157 if (nargin == 3)
19537
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
158 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
159 else
19537
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
160 type = args(1).string_value ();
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
161
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
162 if (error_state)
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
163 return retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
164
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
165 if (type == "native")
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
166 {
19709
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
167 switch (info.format & SF_FORMAT_SUBMASK)
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
168 {
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
169 case SF_FORMAT_PCM_S8:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
170 ret_audio = int8NDArray (audio * 127);
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_U8:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
173 ret_audio = uint8NDArray (audio * 127 + 127);
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 case SF_FORMAT_PCM_16:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
176 ret_audio = int16NDArray (audio * 32767);
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 case SF_FORMAT_PCM_24:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
179 ret_audio = int32NDArray (audio * 8388608);
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
180 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
181 case SF_FORMAT_PCM_32:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
182 ret_audio = int32NDArray (audio * 2147483648);
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
183 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
184 default:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
185 ret_audio = audio;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
186 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
187 }
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
188 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
189 else
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
190 ret_audio = audio;
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
191 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
192 else
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
193 ret_audio = audio;
19537
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
194
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
195 retval(1) = info.samplerate;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
196 retval(0) = ret_audio;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
197
19502
9161b7653392 compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19497
diff changeset
198 #else
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
199
19537
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19535
diff changeset
200 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
201
19502
9161b7653392 compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19497
diff changeset
202 #endif
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
203
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
204 return retval;
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
205 }
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
206
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
207 #ifdef HAVE_SNDFILE
19560
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
208
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
209 static int
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
210 extension_to_format (const std::string& ext)
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
211 {
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
212 static bool initialized = false;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
213
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
214 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
215
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
216 if (! initialized)
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
217 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
244 initialized = true;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
245 }
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
246
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
247 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
248
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
249 return (it != table.end ()) ? it->second : 0;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
250 }
19560
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
251
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
252 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
253
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
254 DEFUN_DLD (audiowrite, args, ,
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
255 "-*- texinfo -*-\n\
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
256 @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
257 @deftypefnx {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value}, @dots{})\n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
258 \n\
20198
075a5e2e1ba5 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19907
diff changeset
259 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
260 @var{fs} with the file format determined by the file extension.\n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
261 \n\
20198
075a5e2e1ba5 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19907
diff changeset
262 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
263 following options:\n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
264 \n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
265 @table @samp\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
266 @item BitsPerSample\n\
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
267 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
268 \n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
269 @item BitRate\n\
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
270 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
271 \n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
272 @item Quality\n\
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
273 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
274 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
275 \n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
276 @item Title\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
277 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
278 \n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
279 @item Artist\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
280 Artist name.\n\
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
281 \n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
282 @item Comment\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
283 Comment.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
284 @end table\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
285 @end deftypefn")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
286 {
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
287 // 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
288 // was written successfully?
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
289
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
290 octave_value retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
291
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
292 #ifdef HAVE_SNDFILE
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
293
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
294 int nargin = args.length ();
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
295
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
296 if (nargin < 3)
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
297 {
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
298 print_usage ();
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
299 return retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
300 }
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
301
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
302 std::string filename = args(0).string_value ();
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
303
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
304 if (error_state)
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
305 return retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
306
19847
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19846
diff changeset
307 Matrix audio = args(1).matrix_value ();
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
308
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
309 if (error_state)
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
310 return retval;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
311
19846
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
312 double bias = 0.0;
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
313 double scale = 1.0;
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
314
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 {
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
323 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
324 return retval;
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
325 }
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
326
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
327 int samplerate = args(2).int_value ();
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
328
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
329 if (error_state)
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
330 return retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
331
19906
ed5ee3f610db Correct tests using std::string::find_XXX which did not use std::string::npos.
Rik <rik@octave.org>
parents: 19847
diff changeset
332 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
333 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
334 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
335 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
336 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
337
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
338 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
339
19847
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19846
diff changeset
340 if (audio.rows () == 1)
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19846
diff changeset
341 audio = audio.transpose ();
879cff0c05dc audiowrite: Accept a row vector input as a single channel
Mike Miller <mtmiller@ieee.org>
parents: 19846
diff changeset
342
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
343 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
344
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
345 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
346 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
347 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
348 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
349 {
f9ca80482c41 audiowrite: Scale integer inputs before writing to an audio file
Mike Miller <mtmiller@ieee.org>
parents: 19845
diff changeset
350 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
351 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
352 }
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
353 }
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
354
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
355 SF_INFO info;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
356
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
357 memset (&info, 0, sizeof (info)) ;
19547
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19546
diff changeset
358
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
359 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
360
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
361 if (ext == "ogg")
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
362 {
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
363 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
364
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
365 // 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
366 // 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
367 // 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
368 // files.
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
369 chunk_size = 0x1FFFFE;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
370 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
371 else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
372 info.format = SF_FORMAT_PCM_16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
373
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
374 info.channels = audio.columns ();
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
375 info.samplerate = samplerate;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
376 info.channels = audio.cols ();
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
377 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
378
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
379 std::string title = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
380 std::string artist = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
381 std::string comment = "";
19546
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
382 // Quality is currently unused?
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
383 //
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
384 // float quality = 0.75;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
385 for (int i = 3; i < nargin; i += 2)
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
386 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
387 if (args(i).string_value () == "BitsPerSample")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
388 {
19709
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
389 info.format &= ~SF_FORMAT_SUBMASK;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
390 int bits = args(i + 1).int_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
391 if (bits == 8)
19709
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
392 {
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
393 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
394 info.format |= SF_FORMAT_PCM_U8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
395 else
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
396 info.format |= SF_FORMAT_PCM_S8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
397 }
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
398 else if (bits == 16)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
399 info.format |= SF_FORMAT_PCM_16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
400 else if (bits == 24)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
401 info.format |= SF_FORMAT_PCM_24;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
402 else if (bits == 32)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
403 info.format |= SF_FORMAT_PCM_32;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
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 number of bits specified");
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
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
409 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
410 else if (args(i).string_value () == "BitRate")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
411 ;
19546
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
412 // Quality is currently unused?
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
413 //
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
414 // else if (args(i).string_value () == "Quality")
a5eb03a7e2a5 eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents: 19545
diff changeset
415 // quality = args(i + 1).int_value () * 0.01;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
416 else if (args(i).string_value () == "Title")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
417 title = args(i + 1).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
418 else if (args(i).string_value () == "Artist")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
419 artist = args(i + 1).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
420 else if (args(i).string_value () == "Comment")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
421 comment = args(i + 1).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
422 else
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
423 {
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
424 error ("audiowrite: wrong argument name");
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
425 return retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
426 }
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
427 }
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
428
19593
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
429 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
430
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
431 if (! file)
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
432 {
19593
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
433 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
434 return retval;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
435 }
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
436
19593
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
437 unwind_protect frame;
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
438
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
439 frame.add_fcn (safe_close, file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
440
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
441 if (title != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
442 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
443
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
444 if (artist != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
445 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
446
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
447 if (comment != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
448 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
449
19587
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
450 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
451 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
452
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
453 if (chunk_size == 0)
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
454 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
455
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
456 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
457 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
458 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
459 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
460
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
461 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
462
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
463 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
464 {
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
465 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
466 items_written, chunk_size);
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
467 return retval;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
468 }
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
469
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
470 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
471 offset += chunk_size;
57efd0a020cf write ogg files in chunks to avoid apparent bug
John W. Eaton <jwe@octave.org>
parents: 19578
diff changeset
472 }
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
473
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
474 #else
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
475
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
476 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
477
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
478 #endif
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
479
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
480 return retval;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
481 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
482
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
483 DEFUN_DLD (audioinfo, args, ,
19605
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19593
diff changeset
484 "-*- texinfo -*-\n\
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
485 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
486 Return information about an audio file specified by @var{filename}.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
487 @end deftypefn")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
488 {
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
489 octave_value retval;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
490
19560
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
491 #ifdef HAVE_SNDFILE
7f271cef5850 protect all portaudio and sndfile dependent code with #ifdefs
John W. Eaton <jwe@octave.org>
parents: 19553
diff changeset
492
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
493 if (args.length () != 1)
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
494 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
495 print_usage ();
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
496 return retval;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
497 }
19553
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 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
500
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
501 if (error_state)
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
502 return retval;
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
503
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
504 SF_INFO info;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
505 info.format = 0;
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
506 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
507
19593
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
508 if (! file)
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
509 {
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
510 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
511 return retval;
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
512 }
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
513
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
514 unwind_protect frame;
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
515
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
516 frame.add_fcn (safe_close, file);
701b43ce4467 properly close audio files
John W. Eaton <jwe@octave.org>
parents: 19588
diff changeset
517
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
518 octave_scalar_map result;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
519
19588
398e42431dcc improve argument decoding in audioread.cc functions
John W. Eaton <jwe@octave.org>
parents: 19587
diff changeset
520 result.assign ("Filename", filename);
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
521 result.assign ("CompressionMethod", "");
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
522 result.assign ("NumChannels", info.channels);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
523 result.assign ("SampleRate", info.samplerate);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
524 result.assign ("TotalSamples", info.frames);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
525
19547
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19546
diff changeset
526 double dframes = info.frames;
4cb4210bd392 use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents: 19546
diff changeset
527 double drate = info.samplerate;
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
528 result.assign ("Duration", dframes / drate);
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
529
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
530 int bits;
19709
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
531 switch (info.format & SF_FORMAT_SUBMASK)
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
532 {
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
533 case SF_FORMAT_PCM_S8:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
534 bits = 8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
535 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
536 case SF_FORMAT_PCM_U8:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
537 bits = 8;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
538 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
539 case SF_FORMAT_PCM_16:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
540 bits = 16;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
541 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
542 case SF_FORMAT_PCM_24:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
543 bits = 24;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
544 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
545 case SF_FORMAT_PCM_32:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
546 bits = 32;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
547 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
548 default:
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
549 bits = -1;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
550 break;
793d295fed4d Fix sndfile bit width handling in audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19605
diff changeset
551 }
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
552
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
553 result.assign ("BitsPerSample", bits);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
554 result.assign ("BitRate", -1);
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
555 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
556 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
557 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
558
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
559 retval = result;
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
560
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
561 #else
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
562
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
563 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
564
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
565 #endif
19553
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
566
e616c2830413 various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents: 19550
diff changeset
567 return retval;
19544
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
568 }