annotate libinterp/dldfcn/audioread.cc @ 19872:d575cd1e0da7

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