Mercurial > octave-nkf
annotate libinterp/dldfcn/audioread.cc @ 19497:157eeaa53277
Fix includes in audioread, audiowrite and audioinfo
* audioinfo.cc, audiowrite.cc, audioinfo.cc: changed octave header includes
to use local headers
author | Vytautas Jančauskas <unaudio@gmail.com> |
---|---|
date | Tue, 10 Sep 2013 14:08:46 +0300 |
parents | 0243b86b3cee |
children | 9161b7653392 |
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" |
19496
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
3 #include <sndfile.h> |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
4 |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
5 DEFUN_DLD(audioread, args, , |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
6 "-*- texinfo -*-\n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
7 @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
|
8 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
13 @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
|
14 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
15 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
|
16 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
|
17 and ending frame. \ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
18 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
19 @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
|
20 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
25 @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
|
26 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
27 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
|
28 \n\ |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
29 @end deftypefn" |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
30 ) |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
31 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
32 octave_value_list retval; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
33 Matrix audio; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
34 octave_value ret_audio; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
35 SNDFILE *file; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
36 SF_INFO info; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
37 info.format = 0; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
38 int start, end; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
39 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
|
40 start = 0; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
41 end = info.frames; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
46 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
|
47 start = range(0); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
48 end = range(1); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
49 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
50 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
|
51 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
|
52 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
53 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
|
54 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
55 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
|
56 } |
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 free (data); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
59 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
|
60 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
61 std::string type; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
62 if (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 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
|
65 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
66 else |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
67 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
68 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
|
69 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
70 if (type == "native") |
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 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
|
73 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
74 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
|
75 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
76 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
|
77 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
78 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
|
79 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
80 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
|
81 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
82 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
|
83 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
84 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
|
85 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
86 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
|
87 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
88 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
|
89 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
90 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
|
91 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
92 else |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
93 { |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
94 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
|
95 } |
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 else |
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 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
|
100 } |
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 else |
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 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
|
105 } |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
106 retval(0) = ret_audio; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
107 retval(1) = info.samplerate; |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
108 return octave_value(retval); |
0243b86b3cee
Added libsndfile based audio file utilities to the source tree
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
109 } |