annotate libinterp/dldfcn/audioread.cc @ 19507:2e174b5e7703

Fix audiodevinfo, audioinfo, audioread and audiowrite build process * audiodevinfo.cc: added bits_to_format utility function * audioinfo.cc, audioread.cc, audiowrite.cc: make sure to not attempt to include sndfile.h if it is not present
author Vytautas Jančauskas <unaudio@gmail.com>
date Wed, 11 Sep 2013 22:05:47 +0300
parents 9161b7653392
children 93f26237c30b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19497
157eeaa53277 Fix includes in audioread, audiowrite and audioinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19496
diff changeset
1 #include "oct.h"
157eeaa53277 Fix includes in audioread, audiowrite and audioinfo
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19496
diff changeset
2 #include "ov-struct.h"
19507
2e174b5e7703 Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19502
diff changeset
3 #ifdef HAVE_SNDFILE
2e174b5e7703 Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19502
diff changeset
4 #include <sndfile.h>
2e174b5e7703 Fix audiodevinfo, audioinfo, audioread and audiowrite build process
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19502
diff changeset
5 #endif
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
6
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
7 DEFUN_DLD(audioread, args, ,
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
8 "-*- texinfo -*-\n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
9 @deftypefn{Loadable Function} [@var{y}, @var{Fs}] = audioread(@var{filename})\n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
10 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
11 Load an audio file that is specified by @var{filename}. It will be loaded in to \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
12 a column matrix with as many rows as there are audio frames and as many columns \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
13 as there are channels in the file. Sampling rate will be stored in @var{Fs}. \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
14 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
15 @deftypefnx{Loadable Function} [@var{y}, @var{Fs}] = audioread(@var{filename}, @var{samples})\n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
16 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
17 Read a specified range of samples from a file specified by @var{filename}. \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
18 Argument @var{samples} is a vector with two values specifying starting frame \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
19 and ending frame. \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
20 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
21 @deftypefnx{Loadable Function} [@var{y}, @var{Fs}] = audioread(@var{filename}, @var{dataType})\n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
22 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
23 Read a file and return an array of specified type. If @var{dataType} is \"native\" then \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
24 an array of fixed width integer type will be returned depending on how data is stored \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
25 in the audio file. If @var{dataType} is \"double\" a double matrix will be returned. \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
26 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
27 @deftypefnx{Loadable Function} [@var{y}, @var{Fs}] = audioread(@var{filename}, @var{samples}, @var{dataType})\n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
28 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
29 Read a file and return a specified range of frames in an array of specified type. \
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
30 \n\
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
31 @end deftypefn"
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
32 )
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
33 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
34 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
35 #ifdef HAVE_SNDFILE
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
36 Matrix audio;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
37 octave_value ret_audio;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
38 SNDFILE *file;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
39 SF_INFO info;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
40 info.format = 0;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
41 int start, end;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
42 file = sf_open(args(0).string_value ().c_str (), SFM_READ, &info);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
43 start = 0;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
44 end = info.frames;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
45 float *data = (float *)malloc (sizeof(float) * info.frames * info.channels);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
46 sf_read_float(file, data, info.frames * info.channels);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
47 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
48 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
49 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
50 start = range(0);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
51 end = range(1);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
52 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
53 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
54 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
55 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
56 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
57 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
58 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
59 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
60 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
61 free (data);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
62 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
63 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
64 std::string type;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
65 if (args.length () == 3)
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
66 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
67 type = args(2).string_value ();
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
68 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
69 else
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
70 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
71 type = args(1).string_value ();
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 if (type == "native")
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
74 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
75 if (info.format & SF_FORMAT_PCM_S8)
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
76 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
77 ret_audio = octave_value ((audio * 127)).int8_array_value ();
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
78 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
79 else if (info.format & SF_FORMAT_PCM_U8)
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
80 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
81 ret_audio = octave_value ((audio * 127 + 127)).uint8_array_value ();
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
82 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
83 else if (info.format & SF_FORMAT_PCM_16)
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
84 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
85 ret_audio = octave_value ((audio * 32767)).int16_array_value ();
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
86 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
87 else if (info.format & SF_FORMAT_PCM_24)
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
88 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
89 ret_audio = octave_value ((audio * 8388608)).int32_array_value ();
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
90 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
91 else if (info.format & SF_FORMAT_PCM_32)
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
92 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
93 ret_audio = octave_value ((audio * 2147483648)).int32_array_value ();
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 else
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 ret_audio = octave_value (audio);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
98 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
99 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
100 else
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
101 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
102 ret_audio = octave_value (audio);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
103 }
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 else
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
106 {
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
107 ret_audio = octave_value (audio);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
108 }
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
109 retval(0) = ret_audio;
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
110 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
111 #else
9161b7653392 compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19497
diff changeset
112 error("sndfile not found on your system and thus audioread is not functional")
9161b7653392 compile audio file utilities even when sndfile is not present
Vytautas Jančauskas <unaudio@gmail.com>
parents: 19497
diff changeset
113 #endif
19496
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
114 return octave_value(retval);
0243b86b3cee Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
115 }