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