Mercurial > audlegacy-plugins
annotate src/filewriter/vorbis.c @ 1975:00394f91e3c6
Updated plugins depending on old configdb to #include <audacious/plugin.h> so that bmp_cfg_db_* functions are still defined.
author | Ben Tucker <ben.tucker@gmail.com> |
---|---|
date | Sat, 06 Oct 2007 18:39:37 -0700 |
parents | c123420debd7 |
children | 5fa26178eaef |
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 | |
29 static void vorbis_init(void); | |
988 | 30 static void vorbis_configure(void); |
986 | 31 static gint vorbis_open(void); |
32 static void vorbis_write(gpointer data, gint length); | |
33 static void vorbis_close(void); | |
34 static gint vorbis_free(void); | |
35 static gint vorbis_playing(void); | |
36 static gint vorbis_get_written_time(void); | |
37 | |
38 FileWriter vorbis_plugin = | |
39 { | |
40 vorbis_init, | |
988 | 41 vorbis_configure, |
986 | 42 vorbis_open, |
43 vorbis_write, | |
44 vorbis_close, | |
45 vorbis_free, | |
46 vorbis_playing, | |
47 vorbis_get_written_time | |
48 }; | |
49 | |
988 | 50 static float v_base_quality = 0.5; |
986 | 51 |
52 static ogg_stream_state os; | |
53 static ogg_page og; | |
54 static ogg_packet op; | |
55 | |
56 static vorbis_dsp_state vd; | |
57 static vorbis_block vb; | |
58 static vorbis_info vi; | |
59 static vorbis_comment vc; | |
60 | |
61 static float **encbuffer; | |
62 static guint64 olen = 0; | |
63 | |
64 static void vorbis_init(void) | |
65 { | |
66 ConfigDb *db = bmp_cfg_db_open(); | |
67 | |
68 bmp_cfg_db_get_float(db, "filewriter_vorbis", "base_quality", &v_base_quality); | |
69 | |
70 bmp_cfg_db_close(db); | |
71 } | |
72 | |
73 static gint vorbis_open(void) | |
74 { | |
75 gint result; | |
76 ogg_packet header; | |
77 ogg_packet header_comm; | |
78 ogg_packet header_code; | |
79 | |
80 vorbis_init(); | |
81 | |
82 written = 0; | |
83 olen = 0; | |
84 | |
85 vorbis_info_init(&vi); | |
86 vorbis_comment_init(&vc); | |
87 | |
88 if (tuple) | |
89 { | |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
90 const gchar *scratch; |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
91 gchar tmpstr[32]; |
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
92 gint scrint; |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
93 |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
94 if ((scratch = tuple_get_string(tuple, FIELD_TITLE, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
95 vorbis_comment_add_tag(&vc, "title", (gchar *) scratch); |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
96 if ((scratch = tuple_get_string(tuple, FIELD_ARTIST, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
97 vorbis_comment_add_tag(&vc, "artist", (gchar *) scratch); |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
98 if ((scratch = tuple_get_string(tuple, FIELD_ALBUM, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
99 vorbis_comment_add_tag(&vc, "album", (gchar *) scratch); |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
100 if ((scratch = tuple_get_string(tuple, FIELD_GENRE, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
101 vorbis_comment_add_tag(&vc, "genre", (gchar *) scratch); |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
102 if ((scratch = tuple_get_string(tuple, FIELD_DATE, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
103 vorbis_comment_add_tag(&vc, "date", (gchar *) scratch); |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
104 if ((scratch = tuple_get_string(tuple, FIELD_COMMENT, NULL))) |
1441
1b52e7eacd4c
filewriter: new tuple API
William Pitcock <nenolod@atheme-project.org>
parents:
1269
diff
changeset
|
105 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
|
106 |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
107 if ((scrint = 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
|
108 { |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
109 g_snprintf(tmpstr, sizeof(tmpstr), "%d", scrint); |
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
110 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
|
111 } |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
112 |
1687
d158ce84fda7
Modified for Tuplez/plugin API changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1441
diff
changeset
|
113 if ((scrint = 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
|
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, "year", tmpstr); |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
117 } |
986 | 118 } |
119 | |
120 if ((result = vorbis_encode_init_vbr(&vi, (long)input.channels, (long)input.frequency, v_base_quality)) != 0) | |
121 { | |
122 vorbis_info_clear(&vi); | |
123 return 0; | |
124 } | |
125 | |
126 vorbis_analysis_init(&vd, &vi); | |
127 vorbis_block_init(&vd, &vb); | |
128 | |
129 srand(time(NULL)); | |
130 ogg_stream_init(&os, rand()); | |
131 | |
132 vorbis_analysis_headerout(&vd, &vc, &header, &header_comm, &header_code); | |
133 | |
134 ogg_stream_packetin(&os, &header); | |
135 ogg_stream_packetin(&os, &header_comm); | |
136 ogg_stream_packetin(&os, &header_code); | |
137 | |
138 while((result = ogg_stream_flush(&os, &og))) | |
139 { | |
140 if (result == 0) | |
141 break; | |
142 | |
143 written += vfs_fwrite(og.header, 1, og.header_len, output_file); | |
144 written += vfs_fwrite(og.body, 1, og.body_len, output_file); | |
145 } | |
146 | |
147 return 1; | |
148 } | |
149 | |
150 static void vorbis_write(gpointer data, gint length) | |
151 { | |
152 int i; | |
153 int result; | |
154 short int *tmpdata; | |
155 | |
156 /* ask vorbisenc for a buffer to fill with pcm data */ | |
157 encbuffer = vorbis_analysis_buffer(&vd, length); | |
158 tmpdata = data; | |
159 | |
160 /* | |
161 * deinterleave the audio signal, 32768.0 is the highest peak level allowed | |
162 * in a 16-bit PCM signal. | |
163 */ | |
164 if (input.channels == 1) | |
165 { | |
166 for (i = 0; i < (length / 2); i++) | |
167 { | |
168 encbuffer[0][i] = tmpdata[i] / 32768.0; | |
169 encbuffer[1][i] = tmpdata[i] / 32768.0; | |
170 } | |
171 } | |
172 else | |
173 { | |
174 for (i = 0; i < (length / 4); i++) | |
175 { | |
176 encbuffer[0][i] = tmpdata[2 * i] / 32768.0; | |
177 encbuffer[1][i] = tmpdata[2 * i + 1] / 32768.0; | |
178 } | |
179 } | |
180 | |
181 vorbis_analysis_wrote(&vd, i); | |
182 | |
183 while(vorbis_analysis_blockout(&vd, &vb) == 1) | |
184 { | |
185 vorbis_analysis(&vb, &op); | |
186 vorbis_bitrate_addblock(&vb); | |
187 | |
188 while (vorbis_bitrate_flushpacket(&vd, &op)) | |
189 { | |
190 ogg_stream_packetin(&os, &op); | |
191 | |
192 while ((result = ogg_stream_pageout(&os, &og))) | |
193 { | |
194 if (result == 0) | |
195 break; | |
196 | |
197 written += vfs_fwrite(og.header, 1, og.header_len, output_file); | |
198 written += vfs_fwrite(og.body, 1, og.body_len, output_file); | |
199 } | |
200 } | |
201 } | |
202 | |
203 olen += length; | |
204 } | |
205 | |
206 static void vorbis_close(void) | |
207 { | |
208 ogg_stream_clear(&os); | |
209 | |
210 vorbis_block_clear(&vb); | |
211 vorbis_dsp_clear(&vd); | |
212 vorbis_info_clear(&vi); | |
213 } | |
214 | |
215 static gint vorbis_free(void) | |
216 { | |
217 return 1000000; | |
218 } | |
219 | |
220 static gint vorbis_playing(void) | |
221 { | |
222 return 0; | |
223 } | |
224 | |
225 static gint vorbis_get_written_time(void) | |
226 { | |
227 if (input.frequency && input.channels) | |
1269
0e160bafce1c
- adapt filewriter for file:// scheme.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1004
diff
changeset
|
228 return (gint) ((olen * 1000) / (input.frequency * 2 * input.channels) + offset); |
986 | 229 |
230 return 0; | |
231 } | |
988 | 232 |
233 /* configuration stuff */ | |
234 static GtkWidget *configure_win = NULL; | |
235 static GtkWidget *quality_frame, *quality_vbox, *quality_hbox1, *quality_spin, *quality_label; | |
236 static GtkObject *quality_adj; | |
237 | |
238 static void quality_change(GtkAdjustment *adjustment, gpointer user_data) | |
239 { | |
240 if (gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(quality_spin))) | |
241 v_base_quality = gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(quality_spin)) / 10; | |
242 else | |
243 v_base_quality = 0.0; | |
244 } | |
245 | |
246 static void configure_ok_cb(gpointer data) | |
247 { | |
248 ConfigDb *db = bmp_cfg_db_open(); | |
249 | |
250 bmp_cfg_db_set_float(db, "filewrite_vorbis", "base_quality", v_base_quality); | |
251 | |
252 bmp_cfg_db_close(db); | |
253 | |
254 gtk_widget_hide(configure_win); | |
255 } | |
256 | |
257 static void vorbis_configure(void) | |
258 { | |
259 GtkWidget *vbox, *bbox; | |
260 GtkWidget *button; | |
261 | |
262 if (configure_win == NULL) | |
263 { | |
264 configure_win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
265 g_signal_connect(G_OBJECT(configure_win), "destroy", G_CALLBACK(gtk_widget_destroyed), NULL); | |
266 | |
267 gtk_window_set_title(GTK_WINDOW(configure_win), _("Vorbis Encoder Configuration")); | |
268 gtk_container_set_border_width(GTK_CONTAINER(configure_win), 5); | |
269 | |
270 vbox = gtk_vbox_new(FALSE, 5); | |
271 gtk_container_add(GTK_CONTAINER(configure_win), vbox); | |
272 | |
273 /* quality options */ | |
274 quality_frame = gtk_frame_new(_("Quality")); | |
275 gtk_container_set_border_width(GTK_CONTAINER(quality_frame), 5); | |
276 gtk_box_pack_start(GTK_BOX(vbox), quality_frame, FALSE, FALSE, 2); | |
277 | |
278 quality_vbox = gtk_vbox_new(FALSE, 5); | |
279 gtk_container_set_border_width(GTK_CONTAINER(quality_vbox), 10); | |
280 gtk_container_add(GTK_CONTAINER(quality_frame), quality_vbox); | |
281 | |
282 /* quality option: vbr level */ | |
283 quality_hbox1 = gtk_hbox_new(FALSE, 5); | |
284 gtk_container_set_border_width(GTK_CONTAINER(quality_hbox1), 10); | |
285 gtk_container_add(GTK_CONTAINER(quality_vbox), quality_hbox1); | |
286 | |
287 quality_label = gtk_label_new(_("Quality level (0 - 10):")); | |
288 gtk_misc_set_alignment(GTK_MISC(quality_label), 0, 0.5); | |
289 gtk_box_pack_start(GTK_BOX(quality_hbox1), quality_label, TRUE, TRUE, 0); | |
290 | |
291 quality_adj = gtk_adjustment_new(5, 0, 10, 0.1, 1, 1); | |
292 quality_spin = gtk_spin_button_new(GTK_ADJUSTMENT(quality_adj), 1, 2); | |
293 gtk_box_pack_start(GTK_BOX(quality_hbox1), quality_spin, TRUE, TRUE, 0); | |
294 g_signal_connect(G_OBJECT(quality_adj), "value-changed", G_CALLBACK(quality_change), NULL); | |
295 | |
296 gtk_spin_button_set_value(GTK_SPIN_BUTTON(quality_spin), (v_base_quality * 10)); | |
297 | |
298 /* buttons */ | |
299 bbox = gtk_hbutton_box_new(); | |
300 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
301 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5); | |
302 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0); | |
303 | |
304 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
305 g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(gtk_widget_hide), GTK_OBJECT(configure_win)); | |
306 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0); | |
307 | |
308 button = gtk_button_new_from_stock(GTK_STOCK_OK); | |
309 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(configure_ok_cb), NULL); | |
310 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0); | |
311 gtk_widget_grab_default(button); | |
312 } | |
313 | |
314 gtk_widget_show_all(configure_win); | |
315 } | |
1911 | 316 |
317 #endif |