Mercurial > audlegacy-plugins
annotate src/filewriter/flac.c @ 2693:8d10c920d26d
Use g_free().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 06 Jun 2008 14:52:25 +0300 |
parents | fa9f85cebade |
children | f1f7ee810de8 |
rev | line source |
---|---|
991 | 1 /* FileWriter FLAC 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" | |
1002
ad29227009f6
[svn] ifdef filewriter/flac.c out completely if FILEWRITER_FLAC isn't defined
js
parents:
996
diff
changeset
|
23 |
ad29227009f6
[svn] ifdef filewriter/flac.c out completely if FILEWRITER_FLAC isn't defined
js
parents:
996
diff
changeset
|
24 #ifdef FILEWRITER_FLAC |
ad29227009f6
[svn] ifdef filewriter/flac.c out completely if FILEWRITER_FLAC isn't defined
js
parents:
996
diff
changeset
|
25 |
995
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
26 #include <FLAC/all.h> |
991 | 27 #include <stdlib.h> |
28 | |
29 static gint flac_open(void); | |
30 static void flac_write(gpointer data, gint length); | |
31 static void flac_close(void); | |
32 static gint flac_free(void); | |
33 static gint flac_playing(void); | |
34 static gint flac_get_written_time(void); | |
35 | |
36 FileWriter flac_plugin = | |
37 { | |
38 NULL, | |
39 NULL, | |
40 flac_open, | |
41 flac_write, | |
42 flac_close, | |
43 flac_free, | |
44 flac_playing, | |
45 flac_get_written_time | |
46 }; | |
47 | |
48 static FLAC__StreamEncoder *flac_encoder; | |
49 static guint64 olen = 0; | |
50 | |
51 static FLAC__StreamEncoderWriteStatus flac_write_cb(const FLAC__StreamEncoder *encoder, | |
52 const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, gpointer data) | |
53 { | |
1978 | 54 written += aud_vfs_fwrite(buffer, bytes, 1, (VFSFile *) data); |
991 | 55 |
56 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; | |
57 } | |
58 | |
59 static FLAC__StreamEncoderSeekStatus flac_seek_cb(const FLAC__StreamEncoder *encoder, | |
60 FLAC__uint64 absolute_byte_offset, gpointer data) | |
61 { | |
62 VFSFile *file = (VFSFile *) data; | |
63 | |
1978 | 64 if (aud_vfs_fseek(file, absolute_byte_offset, SEEK_SET) < 0) |
991 | 65 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; |
66 | |
67 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; | |
68 } | |
69 | |
70 static FLAC__StreamEncoderTellStatus flac_tell_cb(const FLAC__StreamEncoder *encoder, | |
71 FLAC__uint64 *absolute_byte_offset, gpointer data) | |
72 { | |
73 VFSFile *file = (VFSFile *) data; | |
74 | |
1978 | 75 *absolute_byte_offset = aud_vfs_ftell(file); |
991 | 76 |
77 return FLAC__STREAM_ENCODER_TELL_STATUS_OK; | |
78 } | |
79 | |
995
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
80 #define INSERT_VORBIS_COMMENT(t, keyword) \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
81 if (t) \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
82 { \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
83 gchar *scratch = g_strdup_printf(keyword, t); \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
84 comment_entry.length = strlen(scratch); \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
85 comment_entry.entry = (guchar *) scratch; \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
86 FLAC__metadata_object_vorbiscomment_insert_comment(meta, \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
87 meta->data.vorbis_comment.num_comments, comment_entry, TRUE); \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
88 g_free(scratch); \ |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
89 } |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
90 |
991 | 91 static gint flac_open(void) |
92 { | |
93 written = 0; | |
94 olen = 0; | |
95 | |
96 flac_encoder = FLAC__stream_encoder_new(); | |
97 | |
98 FLAC__stream_encoder_set_channels(flac_encoder, input.channels); | |
99 FLAC__stream_encoder_set_sample_rate(flac_encoder, input.frequency); | |
100 FLAC__stream_encoder_init_stream(flac_encoder, flac_write_cb, flac_seek_cb, flac_tell_cb, | |
101 NULL, output_file); | |
102 | |
995
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
103 if (tuple) |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
104 { |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
105 FLAC__StreamMetadata *meta; |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
106 FLAC__StreamMetadata_VorbisComment_Entry comment_entry; |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
107 |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
108 meta = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
109 |
1976
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
110 INSERT_VORBIS_COMMENT(aud_tuple_get_string(tuple, FIELD_TITLE, NULL), "title=%s"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
111 INSERT_VORBIS_COMMENT(aud_tuple_get_string(tuple, FIELD_ARTIST, NULL), "artist=%s"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
112 INSERT_VORBIS_COMMENT(aud_tuple_get_string(tuple, FIELD_ALBUM, NULL), "album=%s"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
113 INSERT_VORBIS_COMMENT(aud_tuple_get_string(tuple, FIELD_GENRE, NULL), "genre=%s"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
114 INSERT_VORBIS_COMMENT(aud_tuple_get_string(tuple, FIELD_COMMENT, NULL), "comment=%s"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
115 INSERT_VORBIS_COMMENT(aud_tuple_get_string(tuple, FIELD_DATE, NULL), "date=%s"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
116 INSERT_VORBIS_COMMENT(aud_tuple_get_int(tuple, FIELD_YEAR, NULL), "year=%d"); |
5fa26178eaef
s/tuple_/aud_tuple_/g
William Pitcock <nenolod@atheme.org>
parents:
1687
diff
changeset
|
117 INSERT_VORBIS_COMMENT(aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL), "tracknumber=%d"); |
995
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
118 |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
119 FLAC__stream_encoder_set_metadata(flac_encoder, &meta, 1); |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
120 } |
73f5e44d20d2
[svn] - add tuple handling, but i don't think i'm doing it right...
nenolod
parents:
994
diff
changeset
|
121 |
991 | 122 return 1; |
123 } | |
124 | |
125 static void flac_write(gpointer data, gint length) | |
126 { | |
994
91f9925d88f1
[svn] - use my version for now as r2126 does not work
nenolod
parents:
993
diff
changeset
|
127 #if 1 |
991 | 128 FLAC__int32 *encbuffer[2]; |
129 short int *tmpdata = data; | |
130 int i; | |
131 | |
132 encbuffer[0] = g_new0(FLAC__int32, length / input.channels); | |
133 encbuffer[1] = g_new0(FLAC__int32, length / input.channels); | |
134 | |
135 if (input.channels == 1) | |
136 { | |
137 for (i = 0; i < (length / 2); i++) | |
138 { | |
992 | 139 encbuffer[0][i] = tmpdata[i]; |
140 encbuffer[1][i] = tmpdata[i]; | |
991 | 141 } |
142 } | |
143 else | |
144 { | |
145 for (i = 0; i < (length / 4); i++) | |
146 { | |
992 | 147 encbuffer[0][i] = tmpdata[2 * i]; |
148 encbuffer[1][i] = tmpdata[2 * i + 1]; | |
991 | 149 } |
150 } | |
151 | |
1044
b1128efde471
[svn] - get rid of all warnings gcc 4.2.0 emits with my build configuration.
yaz
parents:
1002
diff
changeset
|
152 FLAC__stream_encoder_process(flac_encoder, (const FLAC__int32 **)encbuffer, length / (input.channels * 2)); |
991 | 153 olen += length; |
154 | |
155 g_free(encbuffer[0]); | |
156 g_free(encbuffer[1]); | |
994
91f9925d88f1
[svn] - use my version for now as r2126 does not work
nenolod
parents:
993
diff
changeset
|
157 #else |
993 | 158 FLAC__int32 *encbuffer; |
994
91f9925d88f1
[svn] - use my version for now as r2126 does not work
nenolod
parents:
993
diff
changeset
|
159 gint16 *tmpdata = data; |
993 | 160 int i; |
161 | |
162 encbuffer = g_new0(FLAC__int32, length); | |
163 | |
996 | 164 for (i=0; i < length; i++) { |
993 | 165 encbuffer[i] = tmpdata[i]; |
166 } | |
167 | |
996 | 168 FLAC__stream_encoder_process_interleaved(flac_encoder, encbuffer, length); |
993 | 169 olen += length; |
170 | |
171 g_free(encbuffer); | |
994
91f9925d88f1
[svn] - use my version for now as r2126 does not work
nenolod
parents:
993
diff
changeset
|
172 #endif |
991 | 173 } |
174 | |
175 static void flac_close(void) | |
176 { | |
177 FLAC__stream_encoder_finish(flac_encoder); | |
178 FLAC__stream_encoder_delete(flac_encoder); | |
179 } | |
180 | |
181 static gint flac_free(void) | |
182 { | |
183 return 1000000; | |
184 } | |
185 | |
186 static gint flac_playing(void) | |
187 { | |
188 return 0; | |
189 } | |
190 | |
191 static gint flac_get_written_time(void) | |
192 { | |
193 if (input.frequency && input.channels) | |
1269
0e160bafce1c
- adapt filewriter for file:// scheme.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1044
diff
changeset
|
194 return (gint) ((olen * 1000) / (input.frequency * 2 * input.channels) + offset); |
991 | 195 |
196 return 0; | |
197 } | |
1002
ad29227009f6
[svn] ifdef filewriter/flac.c out completely if FILEWRITER_FLAC isn't defined
js
parents:
996
diff
changeset
|
198 |
ad29227009f6
[svn] ifdef filewriter/flac.c out completely if FILEWRITER_FLAC isn't defined
js
parents:
996
diff
changeset
|
199 #endif |