Mercurial > octave-nkf
annotate libinterp/dldfcn/audioread.cc @ 19553:e616c2830413
various style fixes for audio code
* audiodevinfo.cc, audioread.cc: Style fixes.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 02 Jan 2015 13:09:27 -0500 |
parents | fc85d9026bb6 |
children | 7f271cef5850 |
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 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
3 Copyright (C) 2013 Vytautas JanĨauskas |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
4 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
5 This file is part of Octave. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
6 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
10 option) any later version. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
11 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
15 for more details. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
16 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
20 |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
21 */ |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19532
diff
changeset
|
22 |
19535
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
24 #include <config.h> |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
25 #endif |
1f551d169db2
Include config.h in audio project source files
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
26 |
19544 | 27 #include <string> |
28 #include <map> | |
29 | |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
30 #include "oct-locbuf.h" |
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
31 |
19545
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
32 #include "defun-dld.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
33 #include "error.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
34 #include "gripes.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
35 #include "oct-obj.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
36 #include "ov.h" |
19497
157eeaa53277
Fix includes in audioread, audiowrite and audioinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19496
diff
changeset
|
37 #include "ov-struct.h" |
19544 | 38 |
19507
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19502
diff
changeset
|
39 #ifdef HAVE_SNDFILE |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
40 #include <sndfile.h> |
19507
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19502
diff
changeset
|
41 #endif |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
42 |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
43 DEFUN_DLD (audioread, args, , |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
44 "-*- texinfo -*-\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
45 @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
|
46 @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
|
47 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
48 @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
|
49 @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:
19547
diff
changeset
|
50 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:
19547
diff
changeset
|
51 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:
19547
diff
changeset
|
52 to audio frames 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
|
53 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
54 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
|
55 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
|
56 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
57 The optional argument @var{datatype} specifies the datatype to return.\n\ |
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
58 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:
19547
diff
changeset
|
59 data is stored in the audio file.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
60 \n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
61 Read a file and return a specified range of frames in an array of specified type.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
62 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
63 @end deftypefn") |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
64 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
65 octave_value_list retval; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
66 |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
67 #ifdef HAVE_SNDFILE |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
68 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
69 int nargin = args.length (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
70 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
71 if (nargin < 1 || nargin > 3) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
72 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
73 print_usage (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
74 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
75 } |
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 std::string filename = args(0).string_value (); |
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 (error_state) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
80 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
81 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
82 SF_INFO info; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
83 info.format = 0; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
84 SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
85 int start = 0; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
86 int end = info.frames; |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
87 OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
88 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
|
89 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
90 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
|
91 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
92 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
|
93 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
94 if (error_state) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
95 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
96 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
97 start = range(0); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
98 end = range(1); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
99 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
100 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
101 Matrix audio (end - start, info.channels); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
102 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
103 for (int i = start; i < end; i++) |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
104 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
105 for (int channel = 0; channel < info.channels; channel++) |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
106 audio(i - start, channel) = data[i * info.channels + channel]; |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
107 } |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
108 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
109 octave_value ret_audio; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
110 |
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 std::string type; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
114 if (nargin == 3) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
115 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
|
116 else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
117 type = args(1).string_value (); |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
118 |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
119 if (error_state) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
120 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
121 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
122 if (type == "native") |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
123 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
124 if (info.format & SF_FORMAT_PCM_S8) |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
125 ret_audio = int8NDArray (audio * 127); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
126 else if (info.format & SF_FORMAT_PCM_U8) |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
127 ret_audio = uint8NDArray (audio * 127 + 127); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
128 else if (info.format & SF_FORMAT_PCM_16) |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
129 ret_audio = int16NDArray (audio * 32767); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
130 else if (info.format & SF_FORMAT_PCM_24) |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
131 ret_audio = int32NDArray (audio * 8388608); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
132 else if (info.format & SF_FORMAT_PCM_32) |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
133 ret_audio = int32NDArray (audio * 2147483648); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
134 else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
135 ret_audio = audio; |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
136 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
137 else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
138 ret_audio = audio; |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
139 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
140 else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
141 ret_audio = audio; |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
142 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
143 retval(1) = info.samplerate; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
144 retval(0) = ret_audio; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
145 |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
146 #else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
147 |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
148 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
|
149 |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
150 #endif |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
151 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
152 return retval; |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
153 } |
19544 | 154 |
155 #ifdef HAVE_SNDFILE | |
156 static void | |
157 fill_extension_table (std::map<std::string, int> &table) | |
158 { | |
159 table["wav"] = SF_FORMAT_WAV; | |
160 table["aiff"] = SF_FORMAT_AIFF; | |
161 table["au"] = SF_FORMAT_AU; | |
162 table["raw"] = SF_FORMAT_RAW; | |
163 table["paf"] = SF_FORMAT_PAF; | |
164 table["svx"] = SF_FORMAT_SVX; | |
165 table["nist"] = SF_FORMAT_NIST; | |
166 table["voc"] = SF_FORMAT_VOC; | |
167 table["ircam"] = SF_FORMAT_IRCAM; | |
168 table["w64"] = SF_FORMAT_W64; | |
169 table["mat4"] = SF_FORMAT_MAT4; | |
170 table["mat5"] = SF_FORMAT_MAT5; | |
171 table["pvf"] = SF_FORMAT_PVF; | |
172 table["xi"] = SF_FORMAT_XI; | |
173 table["htk"] = SF_FORMAT_HTK; | |
174 table["sds"] = SF_FORMAT_SDS; | |
175 table["avr"] = SF_FORMAT_AVR; | |
176 table["wavex"] = SF_FORMAT_WAVEX; | |
177 table["sd2"] = SF_FORMAT_SD2; | |
178 table["flac"] = SF_FORMAT_FLAC; | |
179 table["caf"] = SF_FORMAT_CAF; | |
180 table["wve"] = SF_FORMAT_WVE; | |
181 table["ogg"] = SF_FORMAT_OGG; | |
182 table["mpc2k"] = SF_FORMAT_MPC2K; | |
183 table["rf64"] = SF_FORMAT_RF64; | |
184 } | |
185 #endif | |
186 | |
187 DEFUN_DLD (audiowrite, args, , | |
188 "-*- texinfo -*-\n\ | |
189 @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
|
190 @deftypefnx {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value}, @dots{})\n\ |
19544 | 191 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
192 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:
19547
diff
changeset
|
193 format determined by the file extension.\n\ |
19544 | 194 \n\ |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
195 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:
19547
diff
changeset
|
196 following options:\n\ |
19544 | 197 \n\ |
198 @table @samp\n\ | |
199 @item BitsPerSample\n\ | |
200 Number of bits per sample, valid values are 8, 16, 24 and 32. Default is 16.\n\ | |
201 @item BitRate\n\ | |
19550
fc85d9026bb6
doc fixes for audio functions
John W. Eaton <jwe@octave.org>
parents:
19547
diff
changeset
|
202 Valid argument name, but ignored. Left for compatibility with @sc{matlab}.\n\ |
19544 | 203 @item Quality\n\ |
204 Quality setting for the Ogg Vorbis compressor. Values can range between 0 and 100 with 100 being the highest quality setting. Default is 75.\n\ | |
205 @item Title\n\ | |
206 Title for the audio file.\n\ | |
207 @item Artist\n\ | |
208 Artist name.\n\ | |
209 @item Comment\n\ | |
210 Comment.\n\ | |
211 @end table\n\ | |
212 @end deftypefn") | |
213 { | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
214 // 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
|
215 // was written successfully? |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
216 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
217 octave_value retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
218 |
19544 | 219 #ifdef HAVE_SNDFILE |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
220 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
221 int nargin = args.length (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
222 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
223 if (nargin < 3) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
224 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
225 print_usage (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
226 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
227 } |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
228 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
229 std::string filename = args(0).string_value (); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
230 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
231 if (error_state) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
232 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
233 |
19544 | 234 std::map<std::string, int> extension_to_format; |
235 fill_extension_table (extension_to_format); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
236 |
19544 | 237 std::string extension = filename.substr (filename.find_last_of (".") + 1); |
238 std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
239 |
19544 | 240 Matrix audio = args(1).matrix_value (); |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
241 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
242 if (error_state) |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
243 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
244 |
19544 | 245 SNDFILE *file; |
246 SF_INFO info; | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
247 |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
248 OCTAVE_LOCAL_BUFFER (float, data, audio.rows () * audio.cols ()); |
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
249 |
19544 | 250 for (int i = 0; i < audio.cols (); i++) |
251 { | |
252 for (int j = 0; j < audio.rows (); j++) | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
253 data[j * audio.cols () + i] = audio(j, i); |
19544 | 254 } |
255 | |
256 if (extension == "ogg") | |
257 info.format = SF_FORMAT_VORBIS; | |
258 else | |
259 info.format = SF_FORMAT_PCM_16; | |
260 | |
261 std::string title = ""; | |
262 std::string artist = ""; | |
263 std::string comment = ""; | |
19546
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
264 // Quality is currently unused? |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
265 // |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
266 // float quality = 0.75; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
267 for (int i = 3; i < nargin; i += 2) |
19544 | 268 { |
269 if (args(i).string_value () == "BitsPerSample") | |
270 { | |
271 int bits = args(i + 1).int_value (); | |
272 if (bits == 8) | |
273 info.format |= SF_FORMAT_PCM_S8; | |
274 else if (bits == 16) | |
275 info.format |= SF_FORMAT_PCM_16; | |
276 else if (bits == 24) | |
277 info.format |= SF_FORMAT_PCM_24; | |
278 else if (bits == 32) | |
279 info.format |= SF_FORMAT_PCM_32; | |
280 else | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
281 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
282 error ("audiowrite: wrong number of bits specified"); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
283 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
284 } |
19544 | 285 } |
286 else if (args(i).string_value () == "BitRate") | |
287 ; | |
19546
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
288 // Quality is currently unused? |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
289 // |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
290 // else if (args(i).string_value () == "Quality") |
a5eb03a7e2a5
eliminate unused variable and parameter warnings
John W. Eaton <jwe@octave.org>
parents:
19545
diff
changeset
|
291 // quality = args(i + 1).int_value () * 0.01; |
19544 | 292 else if (args(i).string_value () == "Title") |
293 title = args(i + 1).string_value (); | |
294 else if (args(i).string_value () == "Artist") | |
295 artist = args(i + 1).string_value (); | |
296 else if (args(i).string_value () == "Comment") | |
297 comment = args(i + 1).string_value (); | |
298 else | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
299 { |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
300 error ("audiowrite: wrong argument name"); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
301 return retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
302 } |
19544 | 303 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
304 |
19544 | 305 info.samplerate = args(2).int_value (); |
306 info.channels = audio.cols (); | |
307 info.format |= extension_to_format[extension]; | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
308 |
19544 | 309 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
|
310 |
19544 | 311 if (title != "") |
312 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
|
313 |
19544 | 314 if (artist != "") |
315 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
|
316 |
19544 | 317 if (comment != "") |
318 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
|
319 |
19544 | 320 sf_write_float (file, data, audio.rows () * audio.cols ()); |
321 sf_close (file); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
322 |
19544 | 323 #else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
324 |
19544 | 325 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
|
326 |
19544 | 327 #endif |
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 return retval; |
19544 | 330 } |
331 | |
332 DEFUN_DLD (audioinfo, args, , | |
333 "-*- texinfo -*-\n\ | |
334 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\ | |
335 Return information about an audio file specified by @var{filename}.\n\ | |
336 @end deftypefn") | |
337 { | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
338 octave_value retval; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
339 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
340 if (args.length () != 1) |
19544 | 341 { |
342 print_usage (); | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
343 return retval; |
19544 | 344 } |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
345 |
19544 | 346 #ifdef HAVE_SNDFILE |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
347 |
19544 | 348 SF_INFO info; |
349 info.format = 0; | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
350 SNDFILE *file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
351 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
352 octave_scalar_map result; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
353 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
354 result.assign ("Filename", args(0).string_value ()); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
355 result.assign ("CompressionMethod", ""); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
356 result.assign ("NumChannels", info.channels); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
357 result.assign ("SampleRate", info.samplerate); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
358 result.assign ("TotalSamples", info.frames); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
359 |
19547
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
360 double dframes = info.frames; |
4cb4210bd392
use C++ style casts in audio code
John W. Eaton <jwe@octave.org>
parents:
19546
diff
changeset
|
361 double drate = info.samplerate; |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
362 result.assign ("Duration", dframes / drate); |
19544 | 363 |
364 int bits; | |
365 if (info.format & SF_FORMAT_PCM_S8) | |
366 bits = 8; | |
367 else if (info.format & SF_FORMAT_PCM_U8) | |
368 bits = 8; | |
369 else if (info.format & SF_FORMAT_PCM_16) | |
370 bits = 16; | |
371 else if (info.format & SF_FORMAT_PCM_24) | |
372 bits = 24; | |
373 else if (info.format & SF_FORMAT_PCM_32) | |
374 bits = 32; | |
375 else | |
376 bits = -1; | |
377 | |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
378 result.assign ("BitsPerSample", bits); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
379 result.assign ("BitRate", -1); |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
380 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
|
381 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
|
382 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
|
383 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
384 retval = result; |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
385 |
19544 | 386 #else |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
387 |
19544 | 388 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
|
389 |
19544 | 390 #endif |
19553
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
391 |
e616c2830413
various style fixes for audio code
John W. Eaton <jwe@octave.org>
parents:
19550
diff
changeset
|
392 return retval; |
19544 | 393 } |