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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19534
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19532
diff changeset
1 /*
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19532
diff changeset
2
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
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
27 #include <string>
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
28 #include <map>
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
29
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
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
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
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
137
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
138 #ifdef HAVE_SNDFILE
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
139 static void
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
140 fill_extension_table (std::map<std::string, int> &table)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
141 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
142 table["wav"] = SF_FORMAT_WAV;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
143 table["aiff"] = SF_FORMAT_AIFF;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
144 table["au"] = SF_FORMAT_AU;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
145 table["raw"] = SF_FORMAT_RAW;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
146 table["paf"] = SF_FORMAT_PAF;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
147 table["svx"] = SF_FORMAT_SVX;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
148 table["nist"] = SF_FORMAT_NIST;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
149 table["voc"] = SF_FORMAT_VOC;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
150 table["ircam"] = SF_FORMAT_IRCAM;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
151 table["w64"] = SF_FORMAT_W64;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
152 table["mat4"] = SF_FORMAT_MAT4;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
153 table["mat5"] = SF_FORMAT_MAT5;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
154 table["pvf"] = SF_FORMAT_PVF;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
155 table["xi"] = SF_FORMAT_XI;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
156 table["htk"] = SF_FORMAT_HTK;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
157 table["sds"] = SF_FORMAT_SDS;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
158 table["avr"] = SF_FORMAT_AVR;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
159 table["wavex"] = SF_FORMAT_WAVEX;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
160 table["sd2"] = SF_FORMAT_SD2;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
161 table["flac"] = SF_FORMAT_FLAC;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
162 table["caf"] = SF_FORMAT_CAF;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
163 table["wve"] = SF_FORMAT_WVE;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
164 table["ogg"] = SF_FORMAT_OGG;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
165 table["mpc2k"] = SF_FORMAT_MPC2K;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
166 table["rf64"] = SF_FORMAT_RF64;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
167 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
168 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
169
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
170 DEFUN_DLD (audiowrite, args, ,
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
171 "-*- texinfo -*-\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
172 @deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs})\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
173 \n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
174 Write audio data from the matrix @var{y} to a file specified by @var{filename},\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
175 file format will be determined by the file extension.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
176 \n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
177 @end deftypefn\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
178 @deftypefn {Loadable Function} {} audiowrite (@var{filename}, @var{y}, @var{fs}, @var{name}, @var{value})\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
179 \n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
180 Lets you specify additional parameters when writing the file. Those parameters\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
181 are given in the table below:\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
182 \n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
183 @table @samp\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
184 @item BitsPerSample\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
185 Number of bits per sample, valid values are 8, 16, 24 and 32. Default is 16.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
186 @item BitRate\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
187 Valid argument name, but ignored. Left for compatibility with MATLAB.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
188 @item Quality\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
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\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
190 @item Title\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
191 Title for the audio file.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
192 @item Artist\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
193 Artist name.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
194 @item Comment\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
195 Comment.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
196 @end table\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
197 @end deftypefn")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
198 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
199 octave_scalar_map retval;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
200 #ifdef HAVE_SNDFILE
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
201 std::map<std::string, int> extension_to_format;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
202 fill_extension_table (extension_to_format);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
203 std::string filename = args(0).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
204 std::string extension = filename.substr (filename.find_last_of (".") + 1);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
205 std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
206 Matrix audio = args(1).matrix_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
207 SNDFILE *file;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
208 SF_INFO info;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
209 float *data = (float *)malloc (audio.rows () * audio.cols () * sizeof (float));
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
210 for (int i = 0; i < audio.cols (); i++)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
211 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
212 for (int j = 0; j < audio.rows (); j++)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
213 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
214 data[j * audio.cols () + i] = audio(j, i);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
215 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
216 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
217
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
218 if (extension == "ogg")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
219 info.format = SF_FORMAT_VORBIS;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
220 else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
221 info.format = SF_FORMAT_PCM_16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
222
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
223 std::string title = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
224 std::string artist = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
225 std::string comment = "";
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
226 float quality = 0.75;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
227 for (int i = 3; i < args.length (); i += 2)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
228 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
229 if (args(i).string_value () == "BitsPerSample")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
230 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
231 int bits = args(i + 1).int_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
232 if (bits == 8)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
233 info.format |= SF_FORMAT_PCM_S8;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
234 else if (bits == 16)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
235 info.format |= SF_FORMAT_PCM_16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
236 else if (bits == 24)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
237 info.format |= SF_FORMAT_PCM_24;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
238 else if (bits == 32)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
239 info.format |= SF_FORMAT_PCM_32;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
240 else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
241 error ("audiowrite: wrong number of bits specified");
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
242 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
243 else if (args(i).string_value () == "BitRate")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
244 ;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
245 else if (args(i).string_value () == "Quality")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
246 quality = args(i + 1).int_value () * 0.01;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
247 else if (args(i).string_value () == "Title")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
248 title = args(i + 1).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
249 else if (args(i).string_value () == "Artist")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
250 artist = args(i + 1).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
251 else if (args(i).string_value () == "Comment")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
252 comment = args(i + 1).string_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
253 else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
254 error ("audiowrite: wrong argument name");
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
255 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
256 info.samplerate = args(2).int_value ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
257 info.channels = audio.cols ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
258 info.format |= extension_to_format[extension];
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
259 file = sf_open (filename.c_str (), SFM_WRITE, &info);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
260 if (title != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
261 sf_set_string (file, SF_STR_TITLE, title.c_str ());
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
262 if (artist != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
263 sf_set_string (file, SF_STR_ARTIST, artist.c_str ());
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
264 if (comment != "")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
265 sf_set_string (file, SF_STR_COMMENT, comment.c_str ());
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
266 sf_write_float (file, data, audio.rows () * audio.cols ());
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
267 sf_close (file);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
268 free (data);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
269 #else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
270 error ("sndfile not found on your system and thus audiowrite is not functional");
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
271 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
272 return octave_value (retval);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
273 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
274
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
275 DEFUN_DLD (audioinfo, args, ,
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
276 "-*- texinfo -*-\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
277 @deftypefn {Loadable Function} {@var{info} =} audioinfo (@var{filename})\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
278 Return information about an audio file specified by @var{filename}.\n\
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
279 @end deftypefn")
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
280 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
281 octave_scalar_map retval;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
282 if (args.length () != 1 || not args(0).is_string ())
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
283 {
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
284 print_usage ();
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
285 return octave_value (retval);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
286 }
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
287 #ifdef HAVE_SNDFILE
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
288 Matrix audio;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
289 SNDFILE *file;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
290 SF_INFO info;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
291 info.format = 0;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
292 int start, end;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
293 file = sf_open (args(0).string_value ().c_str (), SFM_READ, &info);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
294 retval.assign ("Filename", args(0).string_value ());
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
295 retval.assign ("CompressionMethod", "");
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
296 retval.assign ("NumChannels", info.channels);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
297 retval.assign ("SampleRate", info.samplerate);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
298 retval.assign ("TotalSamples", info.frames);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
299 retval.assign ("Duration", (float)info.frames / (float)info.samplerate);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
300
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
301 int bits;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
302 if (info.format & SF_FORMAT_PCM_S8)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
303 bits = 8;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
304 else if (info.format & SF_FORMAT_PCM_U8)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
305 bits = 8;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
306 else if (info.format & SF_FORMAT_PCM_16)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
307 bits = 16;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
308 else if (info.format & SF_FORMAT_PCM_24)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
309 bits = 24;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
310 else if (info.format & SF_FORMAT_PCM_32)
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
311 bits = 32;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
312 else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
313 bits = -1;
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
314
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
315 retval.assign ("BitsPerSample", bits);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
316 retval.assign ("BitRate", -1);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
317 retval.assign ("Title", sf_get_string (file, SF_STR_TITLE));
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
318 retval.assign ("Artist", sf_get_string (file, SF_STR_ARTIST));
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
319 retval.assign ("Comment", sf_get_string (file, SF_STR_COMMENT));
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
320 #else
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
321 error ("sndfile not found on your system and thus audioinfo is not functional");
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
322 #endif
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
323 return octave_value (retval);
99522db5b911 merge audio source files
John W. Eaton <jwe@octave.org>
parents: 19539
diff changeset
324 }