Mercurial > octave-nkf
comparison libinterp/dldfcn/audiowrite.cc @ 19537:36a26a131209
Apply Octave coding style to audio project additions
* libinterp/dldfcn/__player_audioplayer__.cc,
libinterp/dldfcn/__recorder_audiorecorder__.cc,
libinterp/dldfcn/audiodevinfo.cc, libinterp/dldfcn/audioinfo.cc,
libinterp/dldfcn/audioread.cc, libinterp/dldfcn/audiowrite.cc,
libinterp/dldfcn/player_class.cc, libinterp/dldfcn/player_class.h,
libinterp/dldfcn/recorder_class.cc, libinterp/dldfcn/recorder_class.h,
scripts/audio/@audioplayer/__get_properties__.m,
scripts/audio/@audioplayer/audioplayer.m,
scripts/audio/@audioplayer/display.m, scripts/audio/@audioplayer/get.m,
scripts/audio/@audioplayer/isplaying.m, scripts/audio/@audioplayer/pause.m,
scripts/audio/@audioplayer/play.m,
scripts/audio/@audioplayer/playblocking.m,
scripts/audio/@audioplayer/resume.m, scripts/audio/@audioplayer/set.m,
scripts/audio/@audioplayer/stop.m, scripts/audio/@audioplayer/subsasgn.m,
scripts/audio/@audioplayer/subsref.m,
scripts/audio/@audiorecorder/__get_properties__.m,
scripts/audio/@audiorecorder/audiorecorder.m,
scripts/audio/@audiorecorder/display.m, scripts/audio/@audiorecorder/get.m,
scripts/audio/@audiorecorder/getaudiodata.m,
scripts/audio/@audiorecorder/getplayer.m,
scripts/audio/@audiorecorder/isrecording.m,
scripts/audio/@audiorecorder/pause.m, scripts/audio/@audiorecorder/play.m,
scripts/audio/@audiorecorder/record.m,
scripts/audio/@audiorecorder/recordblocking.m,
scripts/audio/@audiorecorder/resume.m, scripts/audio/@audiorecorder/set.m,
scripts/audio/@audiorecorder/stop.m,
scripts/audio/@audiorecorder/subsasgn.m,
scripts/audio/@audiorecorder/subsref.m: Apply consistent Octave indentation,
spacing, and quoting styles. Strip trailing whitespace. Remove braces from
one-line if-else blocks. Simplify some variable declarations.
author | Mike Miller <mtmiller@ieee.org> |
---|---|
date | Thu, 03 Oct 2013 07:52:58 -0400 |
parents | 1f551d169db2 |
children | ce02743b6f2a |
comparison
equal
deleted
inserted
replaced
19536:97c9ba013ed1 | 19537:36a26a131209 |
---|---|
25 #endif | 25 #endif |
26 | 26 |
27 #include "oct.h" | 27 #include "oct.h" |
28 #include "ov-struct.h" | 28 #include "ov-struct.h" |
29 #ifdef HAVE_SNDFILE | 29 #ifdef HAVE_SNDFILE |
30 #include <sndfile.h> | 30 #include <sndfile.h> |
31 #endif | 31 #endif |
32 #include <string> | 32 #include <string> |
33 #include <map> | 33 #include <map> |
34 | 34 |
35 #ifdef HAVE_SNDFILE | 35 #ifdef HAVE_SNDFILE |
36 static void fill_extension_table(std::map<std::string, int> &table) | 36 static void |
37 fill_extension_table (std::map<std::string, int> &table) | |
37 { | 38 { |
38 table["wav"] = SF_FORMAT_WAV; | 39 table["wav"] = SF_FORMAT_WAV; |
39 table["aiff"] = SF_FORMAT_AIFF; | 40 table["aiff"] = SF_FORMAT_AIFF; |
40 table["au"] = SF_FORMAT_AU; | 41 table["au"] = SF_FORMAT_AU; |
41 table["raw"] = SF_FORMAT_RAW; | 42 table["raw"] = SF_FORMAT_RAW; |
60 table["ogg"] = SF_FORMAT_OGG; | 61 table["ogg"] = SF_FORMAT_OGG; |
61 table["mpc2k"] = SF_FORMAT_MPC2K; | 62 table["mpc2k"] = SF_FORMAT_MPC2K; |
62 table["rf64"] = SF_FORMAT_RF64; | 63 table["rf64"] = SF_FORMAT_RF64; |
63 } | 64 } |
64 #endif | 65 #endif |
65 | 66 |
66 DEFUN_DLD(audiowrite, args, , | 67 DEFUN_DLD (audiowrite, args, , |
67 "-*- texinfo -*-\n\ | 68 "-*- texinfo -*-\n\ |
68 @deftypefn{Loadable Function} audiowrite(@var{filename}, @var{y}, @var{Fs})\n\ | 69 @deftypefn{Loadable Function} audiowrite (@var{filename}, @var{y}, @var{Fs})\n\ |
69 \n\ | 70 \n\ |
70 Write audio data from the matrix @var{y} to a file specified by @var{filename}, \ | 71 Write audio data from the matrix @var{y} to a file specified by @var{filename}, \ |
71 file format will be determined by the file extension.\ | 72 file format will be determined by the file extension.\ |
72 \n\ | 73 \n\ |
73 @end deftypefn\n\ | 74 @end deftypefn\n\ |
74 @deftypefn{Loadable Function} audiowrite(@var{filename}, @var{y}, @var{Fs}, @var{Name}, @var{Value})\n\ | 75 @deftypefn{Loadable Function} audiowrite (@var{filename}, @var{y}, @var{Fs}, @var{Name}, @var{Value})\n\ |
75 \n\ | 76 \n\ |
76 Lets you specify additional parameters when writing the file. Those parameters are given in the table below:\n\ | 77 Lets you specify additional parameters when writing the file. Those parameters are given in the table below:\n\ |
77 \n\ | 78 \n\ |
78 @table @samp\n\ | 79 @table @samp\n\ |
79 @item BitsPerSample\n\ | 80 @item BitsPerSample\n\ |
87 @item Artist\n\ | 88 @item Artist\n\ |
88 Artist name.\n\ | 89 Artist name.\n\ |
89 @item Comment\n\ | 90 @item Comment\n\ |
90 Comment.\n\ | 91 Comment.\n\ |
91 @end table\n\ | 92 @end table\n\ |
92 @end deftypefn" | 93 @end deftypefn") |
93 ) | |
94 { | 94 { |
95 octave_scalar_map retval; | 95 octave_scalar_map retval; |
96 #ifdef HAVE_SNDFILE | 96 #ifdef HAVE_SNDFILE |
97 std::map<std::string, int> extension_to_format; | 97 std::map<std::string, int> extension_to_format; |
98 fill_extension_table(extension_to_format); | 98 fill_extension_table (extension_to_format); |
99 std::string filename = args(0).string_value (); | 99 std::string filename = args(0).string_value (); |
100 std::string extension = filename.substr(filename.find_last_of(".") + 1); | 100 std::string extension = filename.substr (filename.find_last_of (".") + 1); |
101 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); | 101 std::transform (extension.begin (), extension.end (), extension.begin (), ::tolower); |
102 Matrix audio = args(1).matrix_value (); | 102 Matrix audio = args(1).matrix_value (); |
103 SNDFILE *file; | 103 SNDFILE *file; |
104 SF_INFO info; | 104 SF_INFO info; |
105 float *data = (float *)malloc(audio.rows () * audio.cols () * sizeof(float)); | 105 float *data = (float *)malloc (audio.rows () * audio.cols () * sizeof (float)); |
106 for (int i = 0; i < audio.cols (); i++) | 106 for (int i = 0; i < audio.cols (); i++) |
107 { | 107 { |
108 for (int j = 0; j < audio.rows (); j++) | 108 for (int j = 0; j < audio.rows (); j++) |
109 { | 109 { |
110 data[j * audio.cols () + i] = audio(j, i); | 110 data[j * audio.cols () + i] = audio(j, i); |
111 } | 111 } |
112 } | 112 } |
113 | |
113 if (extension == "ogg") | 114 if (extension == "ogg") |
114 { | 115 info.format = SF_FORMAT_VORBIS; |
115 info.format = SF_FORMAT_VORBIS; | |
116 } | |
117 else | 116 else |
118 { | 117 info.format = SF_FORMAT_PCM_16; |
119 info.format = SF_FORMAT_PCM_16; | 118 |
120 } | |
121 std::string title = ""; | 119 std::string title = ""; |
122 std::string artist = ""; | 120 std::string artist = ""; |
123 std::string comment = ""; | 121 std::string comment = ""; |
124 float quality = 0.75; | 122 float quality = 0.75; |
125 for (int i = 3; i < args.length (); i += 2) | 123 for (int i = 3; i < args.length (); i += 2) |
126 { | 124 { |
127 if (args(i).string_value () == "BitsPerSample") | 125 if (args(i).string_value () == "BitsPerSample") |
128 { | 126 { |
129 int bits = args(i + 1).int_value (); | 127 int bits = args(i + 1).int_value (); |
130 if (bits == 8) | 128 if (bits == 8) |
131 { | 129 info.format |= SF_FORMAT_PCM_S8; |
132 info.format |= SF_FORMAT_PCM_S8; | |
133 } | |
134 else if (bits == 16) | 130 else if (bits == 16) |
135 { | 131 info.format |= SF_FORMAT_PCM_16; |
136 info.format |= SF_FORMAT_PCM_16; | |
137 } | |
138 else if (bits == 24) | 132 else if (bits == 24) |
139 { | 133 info.format |= SF_FORMAT_PCM_24; |
140 info.format |= SF_FORMAT_PCM_24; | |
141 } | |
142 else if (bits == 32) | 134 else if (bits == 32) |
143 { | 135 info.format |= SF_FORMAT_PCM_32; |
144 info.format |= SF_FORMAT_PCM_32; | |
145 } | |
146 else | 136 else |
147 { | 137 error ("audiowrite: wrong number of bits specified"); |
148 error("audiowrite: wrong number of bits specified"); | |
149 } | |
150 } | 138 } |
151 else if (args(i).string_value () == "BitRate") | 139 else if (args(i).string_value () == "BitRate") |
152 { | 140 ; |
153 | |
154 } | |
155 else if (args(i).string_value () == "Quality") | 141 else if (args(i).string_value () == "Quality") |
156 { | 142 quality = args(i + 1).int_value () * 0.01; |
157 quality = args(i + 1).int_value () * 0.01; | |
158 } | |
159 else if (args(i).string_value () == "Title") | 143 else if (args(i).string_value () == "Title") |
160 { | 144 title = args(i + 1).string_value (); |
161 title = args(i + 1).string_value (); | |
162 } | |
163 else if (args(i).string_value () == "Artist") | 145 else if (args(i).string_value () == "Artist") |
164 { | 146 artist = args(i + 1).string_value (); |
165 artist = args(i + 1).string_value (); | |
166 } | |
167 else if (args(i).string_value () == "Comment") | 147 else if (args(i).string_value () == "Comment") |
168 { | 148 comment = args(i + 1).string_value (); |
169 comment = args(i + 1).string_value (); | |
170 } | |
171 else | 149 else |
172 { | 150 error ("audiowrite: wrong argument name"); |
173 error("audiowrite: wrong argument name"); | |
174 } | |
175 } | 151 } |
176 info.samplerate = args(2).int_value (); | 152 info.samplerate = args(2).int_value (); |
177 info.channels = audio.cols (); | 153 info.channels = audio.cols (); |
178 info.format |= extension_to_format[extension]; | 154 info.format |= extension_to_format[extension]; |
179 file = sf_open (filename.c_str (), SFM_WRITE, &info); | 155 file = sf_open (filename.c_str (), SFM_WRITE, &info); |
180 if (title != "") | 156 if (title != "") |
181 { | 157 sf_set_string (file, SF_STR_TITLE, title.c_str ()); |
182 sf_set_string (file, SF_STR_TITLE, title.c_str ()); | |
183 } | |
184 if (artist != "") | 158 if (artist != "") |
185 { | 159 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); |
186 sf_set_string (file, SF_STR_ARTIST, artist.c_str ()); | |
187 } | |
188 if (comment != "") | 160 if (comment != "") |
189 { | 161 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); |
190 sf_set_string (file, SF_STR_COMMENT, comment.c_str ()); | |
191 } | |
192 sf_write_float (file, data, audio.rows () * audio.cols ()); | 162 sf_write_float (file, data, audio.rows () * audio.cols ()); |
193 sf_close (file); | 163 sf_close (file); |
194 free (data); | 164 free (data); |
195 #else | 165 #else |
196 error("sndfile not found on your system and thus audiowrite is not functional"); | 166 error ("sndfile not found on your system and thus audiowrite is not functional"); |
197 #endif | 167 #endif |
198 return octave_value(retval); | 168 return octave_value (retval); |
199 } | 169 } |