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