Mercurial > audlegacy-plugins
annotate src/filewriter/vorbis.c @ 2775:5df83337516f
branch merge
author | Andrew O. Shadoura <bugzilla@tut.by> |
---|---|
date | Fri, 13 Jun 2008 05:57:25 +0300 |
parents | f1f7ee810de8 769e17da93dd |
children | f1b6f1b2cdb3 |
rev | line source |
---|---|
986 | 1 /* FileWriter Vorbis Plugin |
2 * Copyright (c) 2007 William Pitcock <nenolod@sacredspiral.co.uk> | |
3 * | |
4 * Partially derived from Og(g)re - Ogg-Output-Plugin: | |
5 * Copyright (c) 2002 Lars Siebold <khandha5@gmx.net> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 */ | |
21 | |
22 #include "plugins.h" | |
1911 | 23 |
24 #ifdef FILEWRITER_VORBIS | |
25 | |
986 | 26 #include <vorbis/vorbisenc.h> |
27 #include <stdlib.h> | |
28 | |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
29 static void vorbis_init(write_output_callback write_output_func); |
988 | 30 static void vorbis_configure(void); |
986 | 31 static gint vorbis_open(void); |
32 static void vorbis_write(gpointer data, gint length); | |
2774
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
33 static void vorbis_flush(void); |
986 | 34 static void vorbis_close(void); |
35 static gint vorbis_free(void); | |
36 static gint vorbis_playing(void); | |
37 static gint vorbis_get_written_time(void); | |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
38 static gint (*write_output)(void *ptr, gint length); |
986 | 39 |
40 FileWriter vorbis_plugin = | |
41 { | |
42 vorbis_init, | |
988 | 43 vorbis_configure, |
986 | 44 vorbis_open, |
45 vorbis_write, | |
2774
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
46 vorbis_flush, |
986 | 47 vorbis_close, |
48 vorbis_free, | |
49 vorbis_playing, | |
50 vorbis_get_written_time | |
51 }; | |
52 | |
988 | 53 static float v_base_quality = 0.5; |
986 | 54 |
55 static ogg_stream_state os; | |
56 static ogg_page og; | |
57 static ogg_packet op; | |
58 | |
59 static vorbis_dsp_state vd; | |
60 static vorbis_block vb; | |
61 static vorbis_info vi; | |
62 static vorbis_comment vc; | |
63 | |
64 static float **encbuffer; | |
65 static guint64 olen = 0; | |
66 | |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
67 static void vorbis_init(write_output_callback write_output_func) |
986 | 68 { |
2124 | 69 ConfigDb *db = aud_cfg_db_open(); |
986 | 70 |
2124 | 71 aud_cfg_db_get_float(db, "filewriter_vorbis", "base_quality", &v_base_quality); |
986 | 72 |
2124 | 73 aud_cfg_db_close(db); |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
74 |
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
75 if (write_output_func) |
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
76 write_output=write_output_func; |
986 | 77 } |
78 | |
79 static gint vorbis_open(void) | |
80 { | |
81 gint result; | |
82 ogg_packet header; | |
83 ogg_packet header_comm; | |
84 ogg_packet header_code; | |
85 | |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
86 vorbis_init(NULL); |
986 | 87 |
88 written = 0; | |
89 olen = 0; | |
90 | |
91 vorbis_info_init(&vi); | |
92 vorbis_comment_init(&vc); | |
93 | |
94 if (tuple) | |
95 { | |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
96 const gchar *scratch; |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
97 gchar tmpstr[32]; |
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
98 gint scrint; |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
99 |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
100 if ((scratch = aud_tuple_get_string(tuple, FIELD_TITLE, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
101 vorbis_comment_add_tag(&vc, "title", (gchar *) scratch); |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
102 if ((scratch = aud_tuple_get_string(tuple, FIELD_ARTIST, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
103 vorbis_comment_add_tag(&vc, "artist", (gchar *) scratch); |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
104 if ((scratch = aud_tuple_get_string(tuple, FIELD_ALBUM, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
105 vorbis_comment_add_tag(&vc, "album", (gchar *) scratch); |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
106 if ((scratch = aud_tuple_get_string(tuple, FIELD_GENRE, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
107 vorbis_comment_add_tag(&vc, "genre", (gchar *) scratch); |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
108 if ((scratch = aud_tuple_get_string(tuple, FIELD_DATE, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
109 vorbis_comment_add_tag(&vc, "date", (gchar *) scratch); |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
110 if ((scratch = aud_tuple_get_string(tuple, FIELD_COMMENT, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
111 vorbis_comment_add_tag(&vc, "comment", (gchar *) scratch); |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
112 |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
113 if ((scrint = aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL))) |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
114 { |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
115 g_snprintf(tmpstr, sizeof(tmpstr), "%d", scrint); |
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
116 vorbis_comment_add_tag(&vc, "tracknumber", tmpstr); |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
117 } |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
118 |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1911
diff
changeset
|
119 if ((scrint = aud_tuple_get_int(tuple, FIELD_YEAR, NULL))) |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
120 { |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
121 g_snprintf(tmpstr, sizeof(tmpstr), "%d", scrint); |
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
122 vorbis_comment_add_tag(&vc, "year", tmpstr); |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
123 } |
986 | 124 } |
125 | |
126 if ((result = vorbis_encode_init_vbr(&vi, (long)input.channels, (long)input.frequency, v_base_quality)) != 0) | |
127 { | |
128 vorbis_info_clear(&vi); | |
129 return 0; | |
130 } | |
131 | |
132 vorbis_analysis_init(&vd, &vi); | |
133 vorbis_block_init(&vd, &vb); | |
134 | |
135 srand(time(NULL)); | |
136 ogg_stream_init(&os, rand()); | |
137 | |
138 vorbis_analysis_headerout(&vd, &vc, &header, &header_comm, &header_code); | |
139 | |
140 ogg_stream_packetin(&os, &header); | |
141 ogg_stream_packetin(&os, &header_comm); | |
142 ogg_stream_packetin(&os, &header_code); | |
143 | |
144 while((result = ogg_stream_flush(&os, &og))) | |
145 { | |
146 if (result == 0) | |
147 break; | |
148 | |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
149 written += write_output(og.header, og.header_len); |
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
150 written += write_output(og.body, og.body_len); |
986 | 151 } |
152 | |
153 return 1; | |
154 } | |
155 | |
156 static void vorbis_write(gpointer data, gint length) | |
157 { | |
158 int i; | |
159 int result; | |
160 short int *tmpdata; | |
161 | |
162 /* ask vorbisenc for a buffer to fill with pcm data */ | |
163 encbuffer = vorbis_analysis_buffer(&vd, length); | |
164 tmpdata = data; | |
165 | |
166 /* | |
167 * deinterleave the audio signal, 32768.0 is the highest peak level allowed | |
168 * in a 16-bit PCM signal. | |
169 */ | |
170 if (input.channels == 1) | |
171 { | |
172 for (i = 0; i < (length / 2); i++) | |
173 { | |
174 encbuffer[0][i] = tmpdata[i] / 32768.0; | |
175 encbuffer[1][i] = tmpdata[i] / 32768.0; | |
176 } | |
177 } | |
178 else | |
179 { | |
180 for (i = 0; i < (length / 4); i++) | |
181 { | |
182 encbuffer[0][i] = tmpdata[2 * i] / 32768.0; | |
183 encbuffer[1][i] = tmpdata[2 * i + 1] / 32768.0; | |
184 } | |
185 } | |
186 | |
187 vorbis_analysis_wrote(&vd, i); | |
188 | |
189 while(vorbis_analysis_blockout(&vd, &vb) == 1) | |
190 { | |
191 vorbis_analysis(&vb, &op); | |
192 vorbis_bitrate_addblock(&vb); | |
193 | |
194 while (vorbis_bitrate_flushpacket(&vd, &op)) | |
195 { | |
196 ogg_stream_packetin(&os, &op); | |
197 | |
198 while ((result = ogg_stream_pageout(&os, &og))) | |
199 { | |
200 if (result == 0) | |
201 break; | |
202 | |
2766
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
203 written += write_output(og.header, og.header_len); |
6d08e3120615
make some file filewriter backends use callbacks instead of calling VFS directly
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2124
diff
changeset
|
204 written += write_output(og.body, og.body_len); |
986 | 205 } |
206 } | |
207 } | |
208 | |
209 olen += length; | |
210 } | |
211 | |
2774
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
212 static void vorbis_flush(void) |
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
213 { |
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
214 //nothing to do here yet. --AOS |
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
215 } |
f1f7ee810de8
add metadata to stream + flush() should do a real flush at least with mp3
Andrew O. Shadoura <bugzilla@tut.by>
parents:
2766
diff
changeset
|
216 |
986 | 217 static void vorbis_close(void) |
218 { | |
219 ogg_stream_clear(&os); | |
220 | |
221 vorbis_block_clear(&vb); | |
222 vorbis_dsp_clear(&vd); | |
223 vorbis_info_clear(&vi); | |
224 } | |
225 | |
226 static gint vorbis_free(void) | |
227 { | |
228 return 1000000; | |
229 } | |
230 | |
231 static gint vorbis_playing(void) | |
232 { | |
233 return 0; | |
234 } | |
235 | |
236 static gint vorbis_get_written_time(void) | |
237 { | |
238 if (input.frequency && input.channels) | |
1269
0e160bafce1c
- adapt filewriter for file:// scheme.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1004
diff
changeset
|
239 return (gint) ((olen * 1000) / (input.frequency * 2 * input.channels) + offset); |
986 | 240 |
241 return 0; | |
242 } | |
988 | 243 |
244 /* configuration stuff */ | |
245 static GtkWidget *configure_win = NULL; | |
246 static GtkWidget *quality_frame, *quality_vbox, *quality_hbox1, *quality_spin, *quality_label; | |
247 static GtkObject *quality_adj; | |
248 | |
249 static void quality_change(GtkAdjustment *adjustment, gpointer user_data) | |
250 { | |
251 if (gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(quality_spin))) | |
252 v_base_quality = gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(quality_spin)) / 10; | |
253 else | |
254 v_base_quality = 0.0; | |
255 } | |
256 | |
257 static void configure_ok_cb(gpointer data) | |
258 { | |
2124 | 259 ConfigDb *db = aud_cfg_db_open(); |
988 | 260 |
2124 | 261 aud_cfg_db_set_float(db, "filewrite_vorbis", "base_quality", v_base_quality); |
988 | 262 |
2124 | 263 aud_cfg_db_close(db); |
988 | 264 |
265 gtk_widget_hide(configure_win); | |
266 } | |
267 | |
268 static void vorbis_configure(void) | |
269 { | |
270 GtkWidget *vbox, *bbox; | |
271 GtkWidget *button; | |
272 | |
273 if (configure_win == NULL) | |
274 { | |
275 configure_win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
276 g_signal_connect(G_OBJECT(configure_win), "destroy", G_CALLBACK(gtk_widget_destroyed), NULL); | |
277 | |
278 gtk_window_set_title(GTK_WINDOW(configure_win), _("Vorbis Encoder Configuration")); | |
279 gtk_container_set_border_width(GTK_CONTAINER(configure_win), 5); | |
280 | |
281 vbox = gtk_vbox_new(FALSE, 5); | |
282 gtk_container_add(GTK_CONTAINER(configure_win), vbox); | |
283 | |
284 /* quality options */ | |
285 quality_frame = gtk_frame_new(_("Quality")); | |
286 gtk_container_set_border_width(GTK_CONTAINER(quality_frame), 5); | |
287 gtk_box_pack_start(GTK_BOX(vbox), quality_frame, FALSE, FALSE, 2); | |
288 | |
289 quality_vbox = gtk_vbox_new(FALSE, 5); | |
290 gtk_container_set_border_width(GTK_CONTAINER(quality_vbox), 10); | |
291 gtk_container_add(GTK_CONTAINER(quality_frame), quality_vbox); | |
292 | |
293 /* quality option: vbr level */ | |
294 quality_hbox1 = gtk_hbox_new(FALSE, 5); | |
295 gtk_container_set_border_width(GTK_CONTAINER(quality_hbox1), 10); | |
296 gtk_container_add(GTK_CONTAINER(quality_vbox), quality_hbox1); | |
297 | |
298 quality_label = gtk_label_new(_("Quality level (0 - 10):")); | |
299 gtk_misc_set_alignment(GTK_MISC(quality_label), 0, 0.5); | |
300 gtk_box_pack_start(GTK_BOX(quality_hbox1), quality_label, TRUE, TRUE, 0); | |
301 | |
302 quality_adj = gtk_adjustment_new(5, 0, 10, 0.1, 1, 1); | |
303 quality_spin = gtk_spin_button_new(GTK_ADJUSTMENT(quality_adj), 1, 2); | |
304 gtk_box_pack_start(GTK_BOX(quality_hbox1), quality_spin, TRUE, TRUE, 0); | |
305 g_signal_connect(G_OBJECT(quality_adj), "value-changed", G_CALLBACK(quality_change), NULL); | |
306 | |
307 gtk_spin_button_set_value(GTK_SPIN_BUTTON(quality_spin), (v_base_quality * 10)); | |
308 | |
309 /* buttons */ | |
310 bbox = gtk_hbutton_box_new(); | |
311 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
312 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5); | |
313 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0); | |
314 | |
315 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
316 g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(gtk_widget_hide), GTK_OBJECT(configure_win)); | |
317 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0); | |
318 | |
319 button = gtk_button_new_from_stock(GTK_STOCK_OK); | |
320 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(configure_ok_cb), NULL); | |
321 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0); | |
322 gtk_widget_grab_default(button); | |
323 } | |
324 | |
325 gtk_widget_show_all(configure_win); | |
326 } | |
1911 | 327 |
328 #endif |