Mercurial > audlegacy-plugins
annotate src/filewriter/vorbis.c @ 989:84bef123b22e trunk
[svn] - provide full metadata from the file's tuple when performing the transcode
author | nenolod |
---|---|
date | Mon, 30 Apr 2007 22:03:19 -0700 |
parents | 1d25b3513a17 |
children | be2d04b2bd28 |
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 { | |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
87 gchar *scratch; |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
88 |
986 | 89 vorbis_comment_add_tag(&vc, "title", tuple->track_name); |
90 vorbis_comment_add_tag(&vc, "artist", tuple->performer); | |
91 vorbis_comment_add_tag(&vc, "album", tuple->album_name); | |
92 vorbis_comment_add_tag(&vc, "genre", tuple->genre); | |
989
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
93 vorbis_comment_add_tag(&vc, "date", tuple->date); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
94 vorbis_comment_add_tag(&vc, "comment", tuple->comment); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
95 |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
96 if (tuple->track_number) |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
97 { |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
98 scratch = g_strdup_printf("%d", tuple->track_number); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
99 vorbis_comment_add_tag(&vc, "tracknumber", scratch); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
100 g_free(scratch); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
101 } |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
102 |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
103 if (tuple->year) |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
104 { |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
105 scratch = g_strdup_printf("%d", tuple->year); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
106 vorbis_comment_add_tag(&vc, "year", scratch); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
107 g_free(scratch); |
84bef123b22e
[svn] - provide full metadata from the file's tuple when performing the transcode
nenolod
parents:
988
diff
changeset
|
108 } |
986 | 109 } |
110 | |
111 if ((result = vorbis_encode_init_vbr(&vi, (long)input.channels, (long)input.frequency, v_base_quality)) != 0) | |
112 { | |
113 vorbis_info_clear(&vi); | |
114 return 0; | |
115 } | |
116 | |
117 vorbis_analysis_init(&vd, &vi); | |
118 vorbis_block_init(&vd, &vb); | |
119 | |
120 srand(time(NULL)); | |
121 ogg_stream_init(&os, rand()); | |
122 | |
123 vorbis_analysis_headerout(&vd, &vc, &header, &header_comm, &header_code); | |
124 | |
125 ogg_stream_packetin(&os, &header); | |
126 ogg_stream_packetin(&os, &header_comm); | |
127 ogg_stream_packetin(&os, &header_code); | |
128 | |
129 while((result = ogg_stream_flush(&os, &og))) | |
130 { | |
131 if (result == 0) | |
132 break; | |
133 | |
134 written += vfs_fwrite(og.header, 1, og.header_len, output_file); | |
135 written += vfs_fwrite(og.body, 1, og.body_len, output_file); | |
136 } | |
137 | |
138 return 1; | |
139 } | |
140 | |
141 static void vorbis_write(gpointer data, gint length) | |
142 { | |
143 int i; | |
144 int result; | |
145 short int *tmpdata; | |
146 | |
147 /* ask vorbisenc for a buffer to fill with pcm data */ | |
148 encbuffer = vorbis_analysis_buffer(&vd, length); | |
149 tmpdata = data; | |
150 | |
151 /* | |
152 * deinterleave the audio signal, 32768.0 is the highest peak level allowed | |
153 * in a 16-bit PCM signal. | |
154 */ | |
155 if (input.channels == 1) | |
156 { | |
157 for (i = 0; i < (length / 2); i++) | |
158 { | |
159 encbuffer[0][i] = tmpdata[i] / 32768.0; | |
160 encbuffer[1][i] = tmpdata[i] / 32768.0; | |
161 } | |
162 } | |
163 else | |
164 { | |
165 for (i = 0; i < (length / 4); i++) | |
166 { | |
167 encbuffer[0][i] = tmpdata[2 * i] / 32768.0; | |
168 encbuffer[1][i] = tmpdata[2 * i + 1] / 32768.0; | |
169 } | |
170 } | |
171 | |
172 vorbis_analysis_wrote(&vd, i); | |
173 | |
174 while(vorbis_analysis_blockout(&vd, &vb) == 1) | |
175 { | |
176 vorbis_analysis(&vb, &op); | |
177 vorbis_bitrate_addblock(&vb); | |
178 | |
179 while (vorbis_bitrate_flushpacket(&vd, &op)) | |
180 { | |
181 ogg_stream_packetin(&os, &op); | |
182 | |
183 while ((result = ogg_stream_pageout(&os, &og))) | |
184 { | |
185 if (result == 0) | |
186 break; | |
187 | |
188 written += vfs_fwrite(og.header, 1, og.header_len, output_file); | |
189 written += vfs_fwrite(og.body, 1, og.body_len, output_file); | |
190 } | |
191 } | |
192 } | |
193 | |
194 olen += length; | |
195 } | |
196 | |
197 static void vorbis_close(void) | |
198 { | |
199 ogg_stream_clear(&os); | |
200 | |
201 vorbis_block_clear(&vb); | |
202 vorbis_dsp_clear(&vd); | |
203 vorbis_info_clear(&vi); | |
204 } | |
205 | |
206 static gint vorbis_free(void) | |
207 { | |
208 return 1000000; | |
209 } | |
210 | |
211 static gint vorbis_playing(void) | |
212 { | |
213 return 0; | |
214 } | |
215 | |
216 static gint vorbis_get_written_time(void) | |
217 { | |
218 if (input.frequency && input.channels) | |
219 return (gint) ((olen * 1000) / (input.frequency * 2 * input.channels)); | |
220 | |
221 return 0; | |
222 } | |
988 | 223 |
224 /* configuration stuff */ | |
225 static GtkWidget *configure_win = NULL; | |
226 static GtkWidget *quality_frame, *quality_vbox, *quality_hbox1, *quality_spin, *quality_label; | |
227 static GtkObject *quality_adj; | |
228 | |
229 static void quality_change(GtkAdjustment *adjustment, gpointer user_data) | |
230 { | |
231 if (gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(quality_spin))) | |
232 v_base_quality = gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(quality_spin)) / 10; | |
233 else | |
234 v_base_quality = 0.0; | |
235 } | |
236 | |
237 static void configure_ok_cb(gpointer data) | |
238 { | |
239 ConfigDb *db = bmp_cfg_db_open(); | |
240 | |
241 bmp_cfg_db_set_float(db, "filewrite_vorbis", "base_quality", v_base_quality); | |
242 | |
243 bmp_cfg_db_close(db); | |
244 | |
245 gtk_widget_hide(configure_win); | |
246 } | |
247 | |
248 static void vorbis_configure(void) | |
249 { | |
250 GtkWidget *vbox, *bbox; | |
251 GtkWidget *button; | |
252 | |
253 if (configure_win == NULL) | |
254 { | |
255 configure_win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
256 g_signal_connect(G_OBJECT(configure_win), "destroy", G_CALLBACK(gtk_widget_destroyed), NULL); | |
257 | |
258 gtk_window_set_title(GTK_WINDOW(configure_win), _("Vorbis Encoder Configuration")); | |
259 gtk_container_set_border_width(GTK_CONTAINER(configure_win), 5); | |
260 | |
261 vbox = gtk_vbox_new(FALSE, 5); | |
262 gtk_container_add(GTK_CONTAINER(configure_win), vbox); | |
263 | |
264 /* quality options */ | |
265 quality_frame = gtk_frame_new(_("Quality")); | |
266 gtk_container_set_border_width(GTK_CONTAINER(quality_frame), 5); | |
267 gtk_box_pack_start(GTK_BOX(vbox), quality_frame, FALSE, FALSE, 2); | |
268 | |
269 quality_vbox = gtk_vbox_new(FALSE, 5); | |
270 gtk_container_set_border_width(GTK_CONTAINER(quality_vbox), 10); | |
271 gtk_container_add(GTK_CONTAINER(quality_frame), quality_vbox); | |
272 | |
273 /* quality option: vbr level */ | |
274 quality_hbox1 = gtk_hbox_new(FALSE, 5); | |
275 gtk_container_set_border_width(GTK_CONTAINER(quality_hbox1), 10); | |
276 gtk_container_add(GTK_CONTAINER(quality_vbox), quality_hbox1); | |
277 | |
278 quality_label = gtk_label_new(_("Quality level (0 - 10):")); | |
279 gtk_misc_set_alignment(GTK_MISC(quality_label), 0, 0.5); | |
280 gtk_box_pack_start(GTK_BOX(quality_hbox1), quality_label, TRUE, TRUE, 0); | |
281 | |
282 quality_adj = gtk_adjustment_new(5, 0, 10, 0.1, 1, 1); | |
283 quality_spin = gtk_spin_button_new(GTK_ADJUSTMENT(quality_adj), 1, 2); | |
284 gtk_box_pack_start(GTK_BOX(quality_hbox1), quality_spin, TRUE, TRUE, 0); | |
285 g_signal_connect(G_OBJECT(quality_adj), "value-changed", G_CALLBACK(quality_change), NULL); | |
286 | |
287 gtk_spin_button_set_value(GTK_SPIN_BUTTON(quality_spin), (v_base_quality * 10)); | |
288 | |
289 /* buttons */ | |
290 bbox = gtk_hbutton_box_new(); | |
291 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
292 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5); | |
293 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0); | |
294 | |
295 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
296 g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(gtk_widget_hide), GTK_OBJECT(configure_win)); | |
297 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0); | |
298 | |
299 button = gtk_button_new_from_stock(GTK_STOCK_OK); | |
300 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(configure_ok_cb), NULL); | |
301 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0); | |
302 gtk_widget_grab_default(button); | |
303 } | |
304 | |
305 gtk_widget_show_all(configure_win); | |
306 } |