Mercurial > octave-nkf
annotate libinterp/dldfcn/audioread.cc @ 19545:19f75d156ffe
don't include oct.h in Octave source files
* audiodevinfo.cc, audioread.cc: Don't include oct.h.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 02 Jan 2015 00:47:25 -0500 |
parents | 99522db5b911 |
children | a5eb03a7e2a5 |
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 | |
19545
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
30 #include "defun-dld.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
31 #include "error.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
32 #include "gripes.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
33 #include "oct-obj.h" |
19f75d156ffe
don't include oct.h in Octave source files
John W. Eaton <jwe@octave.org>
parents:
19544
diff
changeset
|
34 #include "ov.h" |
19497
157eeaa53277
Fix includes in audioread, audiowrite and audioinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19496
diff
changeset
|
35 #include "ov-struct.h" |
19544 | 36 |
19507
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19502
diff
changeset
|
37 #ifdef HAVE_SNDFILE |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
38 #include <sndfile.h> |
19507
2e174b5e7703
Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19502
diff
changeset
|
39 #endif |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
40 |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
41 DEFUN_DLD (audioread, args, , |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
42 "-*- texinfo -*-\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
43 @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
44 \n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
45 Load an audio file that is specified by @var{filename}. It will be loaded\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
46 in to a column matrix with as many rows as there are audio frames and as many\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
47 columns as there are channels in the file. Sampling rate will be stored in\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
48 @var{fs}.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
49 \n\ |
19532
0ac6a4e7369b
change docstrings in audio defundld cc files to work with new texinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19525
diff
changeset
|
50 @end deftypefn\n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
51 @deftypefn {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
|
52 \n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
53 Read a specified range of samples from a file specified by @var{filename}.\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
54 Argument @var{samples} is a vector with two values specifying starting frame\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
55 and ending frame.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
56 \n\ |
19532
0ac6a4e7369b
change docstrings in audio defundld cc files to work with new texinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19525
diff
changeset
|
57 @end deftypefn\n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
58 @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{datatype})\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
59 \n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
60 Read a file and return an array of specified type. If @var{datatype} is\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
61 @qcode{\"native\"} then an array of fixed width integer type will be returned\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
62 depending on how data is stored in the audio file. If @var{datatype} is\n\ |
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
63 @qcode{\"double\"} a double matrix will be returned.\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
64 \n\ |
19532
0ac6a4e7369b
change docstrings in audio defundld cc files to work with new texinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19525
diff
changeset
|
65 @end deftypefn\n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
66 @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename}, @var{samples}, @var{datatype})\n\ |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
67 \n\ |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
68 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
|
69 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
70 @end deftypefn" |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
71 ) |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
72 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
73 octave_value_list retval; |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
74 #ifdef HAVE_SNDFILE |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
75 Matrix audio; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
76 octave_value ret_audio; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
77 SNDFILE *file; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
78 SF_INFO info; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
79 info.format = 0; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
80 int start, end; |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
81 file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
82 start = 0; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
83 end = info.frames; |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
84 float *data = (float *)malloc (sizeof (float) * info.frames * info.channels); |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
85 sf_read_float (file, data, info.frames * info.channels); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
86 if (args.length () == 2 && !args(1).is_string () || args.length () == 3) |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
87 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
88 RowVector range = args(1).row_vector_value (); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
89 start = range(0); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
90 end = range(1); |
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 audio.resize (end - start, info.channels); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
93 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
|
94 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
95 for (int channel = 0; channel < info.channels; channel++) |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
96 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
97 audio(i - start, channel) = data[i * info.channels + channel]; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
98 } |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
99 } |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
100 free (data); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
101 if (args.length () == 2 && args(1).is_string () || args.length () == 3) |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
102 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
103 std::string type; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
104 if (args.length () == 3) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
105 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
|
106 else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
107 type = args(1).string_value (); |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
108 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
109 if (type == "native") |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
110 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
111 if (info.format & SF_FORMAT_PCM_S8) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
112 ret_audio = octave_value ((audio * 127)).int8_array_value (); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
113 else if (info.format & SF_FORMAT_PCM_U8) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
114 ret_audio = octave_value ((audio * 127 + 127)).uint8_array_value (); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
115 else if (info.format & SF_FORMAT_PCM_16) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
116 ret_audio = octave_value ((audio * 32767)).int16_array_value (); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
117 else if (info.format & SF_FORMAT_PCM_24) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
118 ret_audio = octave_value ((audio * 8388608)).int32_array_value (); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
119 else if (info.format & SF_FORMAT_PCM_32) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
120 ret_audio = octave_value ((audio * 2147483648)).int32_array_value (); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
121 else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
122 ret_audio = octave_value (audio); |
19496
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 else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
125 ret_audio = octave_value (audio); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
126 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
127 else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
128 ret_audio = octave_value (audio); |
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
129 |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
130 retval(0) = ret_audio; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
131 retval(1) = info.samplerate; |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
132 #else |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
133 error ("sndfile not found on your system and thus audioread is not functional"); |
19502
9161b7653392
compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents:
19497
diff
changeset
|
134 #endif |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19535
diff
changeset
|
135 return octave_value (retval); |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
136 } |
19544 | 137 |
138 #ifdef HAVE_SNDFILE | |
139 static void | |
140 fill_extension_table (std::map<std::string, int> &table) | |
141 { | |
142 table["wav"] = SF_FORMAT_WAV; | |
143 table["aiff"] = SF_FORMAT_AIFF; | |
144 table["au"] = SF_FORMAT_AU; | |
145 table["raw"] = SF_FORMAT_RAW; | |
146 table["paf"] = SF_FORMAT_PAF; | |
147 table["svx"] = SF_FORMAT_SVX; | |
148 table["nist"] = SF_FORMAT_NIST; | |
149 table["voc"] = SF_FORMAT_VOC; | |
150 table["ircam"] = SF_FORMAT_IRCAM; | |
151 table["w64"] = SF_FORMAT_W64; | |
152 table["mat4"] = SF_FORMAT_MAT4; | |
153 table["mat5"] = SF_FORMAT_MAT5; | |
154 table["pvf"] = SF_FORMAT_PVF; | |
155 table["xi"] = SF_FORMAT_XI; | |
156 table["htk"] = SF_FORMAT_HTK; | |
157 table["sds"] = SF_FORMAT_SDS; | |
158 table["avr"] = SF_FORMAT_AVR; | |
159 table["wavex"] = SF_FORMAT_WAVEX; | |
160 table["sd2"] = SF_FORMAT_SD2; | |
161 table["flac"] = SF_FORMAT_FLAC; | |
162 table["caf"] = SF_FORMAT_CAF; | |
163 table["wve"] = SF_FORMAT_WVE; | |
164 table["ogg"] = SF_FORMAT_OGG; | |
165 table["mpc2k"] = SF_FORMAT_MPC2K; | |
166 table["rf64"] = SF_FORMAT_RF64; | |
167 } | |
168 #endif | |
169 | |
170 DEFUN_DLD (audiowrite, args, , | |
171 "-*- texinfo -*-\n\ | |
172 @deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs})\n\ | |
173 \n\ | |
174 Write audio data from the matrix @var{y} to a file specified by @var{filename},\n\ | |
175 file format will be determined by the file extension.\n\ | |
176 \n\ | |
177 @end deftypefn\n\ | |
178 @deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value})\n\ | |
179 \n\ | |
180 Lets you specify additional parameters when writing the file. Those parameters\n\ | |
181 are given in the table below:\n\ | |
182 \n\ | |
183 @table @samp\n\ | |
184 @item BitsPerSample\n\ | |
185 Number of bits per sample, valid values are 8, 16, 24 and 32. Default is 16.\n\ | |
186 @item BitRate\n\ | |
187 Valid argument name, but ignored. Left for compatibility with MATLAB.\n\ | |
188 @item Quality\n\ | |
189 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\ | |
190 @item Title\n\ | |
191 Title for the audio file.\n\ | |
192 @item Artist\n\ | |
193 Artist name.\n\ | |
194 @item Comment\n\ | |
195 Comment.\n\ | |
196 @end table\n\ | |
197 @end deftypefn") | |
198 { | |
199 octave_scalar_map retval; | |
200 #ifdef HAVE_SNDFILE | |
201 std::map<std::string, int> extension_to_format; | |
202 fill_extension_table (extension_to_format); | |
203 std::string filename = args(0).string_value (); | |
204 std::string extension = filename.substr (filename.find_last_of (".") + 1); | |
205 std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower); | |
206 Matrix audio = args(1).matrix_value (); | |
207 SNDFILE *file; | |
208 SF_INFO info; | |
209 float *data = (float *)malloc (audio.rows () * audio.cols () * sizeof (float)); | |
210 for (int i = 0; i < audio.cols (); i++) | |
211 { | |
212 for (int j = 0; j < audio.rows (); j++) | |
213 { | |
214 data[j * audio.cols () + i] = audio(j, i); | |
215 } | |
216 } | |
217 | |
218 if (extension == "ogg") | |
219 info.format = SF_FORMAT_VORBIS; | |
220 else | |
221 info.format = SF_FORMAT_PCM_16; | |
222 | |
223 std::string title = ""; | |
224 std::string artist = ""; | |
225 std::string comment = ""; | |
226 float quality = 0.75; | |
227 for (int i = 3; i < args.length (); i += 2) | |
228 { | |
229 if (args(i).string_value () == "BitsPerSample") | |
230 { | |
231 int bits = args(i + 1).int_value (); | |
232 if (bits == 8) | |
233 info.format |= SF_FORMAT_PCM_S8; | |
234 else if (bits == 16) | |
235 info.format |= SF_FORMAT_PCM_16; | |
236 else if (bits == 24) | |
237 info.format |= SF_FORMAT_PCM_24; | |
238 else if (bits == 32) | |
239 info.format |= SF_FORMAT_PCM_32; | |
240 else | |
241 error ("audiowrite: wrong number of bits specified"); | |
242 } | |
243 else if (args(i).string_value () == "BitRate") | |
244 ; | |
245 else if (args(i).string_value () == "Quality") | |
246 quality = args(i + 1).int_value () * 0.01; | |
247 else if (args(i).string_value () == "Title") | |
248 title = args(i + 1).string_value (); | |
249 else if (args(i).string_value () == "Artist") | |
250 artist = args(i + 1).string_value (); | |
251 else if (args(i).string_value () == "Comment") | |
252 comment = args(i + 1).string_value (); | |
253 else | |
254 error ("audiowrite: wrong argument name"); | |
255 } | |
256 info.samplerate = args(2).int_value (); | |
257 info.channels = audio.cols (); | |
258 info.format |= extension_to_format[extension]; | |
259 file = sf_open (filename.c_str (), SFM_WRITE, &info); | |
260 if (title != "") | |
261 sf_set_string (file, SF_STR_TITLE, title.c_str ()); | |
262 if (artist != "") | |
263 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); | |
264 if (comment != "") | |
265 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); | |
266 sf_write_float (file, data, audio.rows () * audio.cols ()); | |
267 sf_close (file); | |
268 free (data); | |
269 #else | |
270 error ("sndfile not found on your system and thus audiowrite is not functional"); | |
271 #endif | |
272 return octave_value (retval); | |
273 } | |
274 | |
275 DEFUN_DLD (audioinfo, args, , | |
276 "-*- texinfo -*-\n\ | |
277 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\ | |
278 Return information about an audio file specified by @var{filename}.\n\ | |
279 @end deftypefn") | |
280 { | |
281 octave_scalar_map retval; | |
282 if (args.length () != 1 || not args(0).is_string ()) | |
283 { | |
284 print_usage (); | |
285 return octave_value (retval); | |
286 } | |
287 #ifdef HAVE_SNDFILE | |
288 Matrix audio; | |
289 SNDFILE *file; | |
290 SF_INFO info; | |
291 info.format = 0; | |
292 int start, end; | |
293 file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info); | |
294 retval.assign ("Filename", args(0).string_value ()); | |
295 retval.assign ("CompressionMethod", ""); | |
296 retval.assign ("NumChannels", info.channels); | |
297 retval.assign ("SampleRate", info.samplerate); | |
298 retval.assign ("TotalSamples", info.frames); | |
299 retval.assign ("Duration", (float)info.frames / (float)info.samplerate); | |
300 | |
301 int bits; | |
302 if (info.format & SF_FORMAT_PCM_S8) | |
303 bits = 8; | |
304 else if (info.format & SF_FORMAT_PCM_U8) | |
305 bits = 8; | |
306 else if (info.format & SF_FORMAT_PCM_16) | |
307 bits = 16; | |
308 else if (info.format & SF_FORMAT_PCM_24) | |
309 bits = 24; | |
310 else if (info.format & SF_FORMAT_PCM_32) | |
311 bits = 32; | |
312 else | |
313 bits = -1; | |
314 | |
315 retval.assign ("BitsPerSample", bits); | |
316 retval.assign ("BitRate", -1); | |
317 retval.assign ("Title", sf_get_string (file, SF_STR_TITLE)); | |
318 retval.assign ("Artist", sf_get_string (file, SF_STR_ARTIST)); | |
319 retval.assign ("Comment", sf_get_string (file, SF_STR_COMMENT)); | |
320 #else | |
321 error ("sndfile not found on your system and thus audioinfo is not functional"); | |
322 #endif | |
323 return octave_value (retval); | |
324 } |