1 #include <FLAC/metadata.h>
5 static PyObject
*flac_error
;
8 flac_pictures(char *fn
)
10 FLAC__Metadata_Chain
*chain
;
11 FLAC__Metadata_Iterator
*iterator
;
12 FLAC__StreamMetadata
*block
;
13 PyObject
*tuple
, *list
;
15 chain
= FLAC__metadata_chain_new();
17 return PyErr_NoMemory();
20 if(!FLAC__metadata_chain_read(chain
, fn
)) {
21 PyObject
*s
= PyString_FromFormat("metadata_chain_read (%d)",
22 FLAC__metadata_chain_status(chain
));
24 PyErr_SetObject(flac_error
, s
);
29 iterator
= FLAC__metadata_iterator_new();
30 if (iterator
== NULL
) {
31 return PyErr_NoMemory();
34 FLAC__metadata_iterator_init(iterator
, chain
);
42 /* This can fail, but the documentation doesn't say how, why,
43 * or how to react. metaflac/operations.c just prints a
44 * useless error message and keeps going. */
45 block
= FLAC__metadata_iterator_get_block(iterator
);
47 PyErr_SetString(flac_error
,
48 "metadata_iterator_get_block returned NULL");
52 if (block
->type
!= FLAC__METADATA_TYPE_PICTURE
) {
56 tuple
= Py_BuildValue("(s#issiiii)",
57 block
->data
.picture
.data
,
58 block
->data
.picture
.data_length
,
59 block
->data
.picture
.type
,
60 block
->data
.picture
.mime_type
,
61 block
->data
.picture
.description
,
62 block
->data
.picture
.width
,
63 block
->data
.picture
.height
,
64 block
->data
.picture
.depth
,
65 block
->data
.picture
.colors
);
70 if (PyList_Append(list
, tuple
) == -1) {
73 } while(FLAC__metadata_iterator_next(iterator
));
75 FLAC__metadata_iterator_delete(iterator
);
76 FLAC__metadata_chain_delete(chain
);
82 get_pictures(PyObject
*self
, PyObject
*args
)
86 if (!PyArg_ParseTuple(args
, "s", &fn
)) {
90 return flac_pictures(fn
);
94 PyMethodDef methods
[] = {
95 {"get_pictures", get_pictures
, METH_VARARGS
,
102 export_constants(PyObject
*m
)
107 n = PyInt_FromLong(FLAC__STREAM_METADATA_ ## x); \
108 if (n == NULL || PyModule_AddObject(m, #x, n) == -1) return;
110 foo(PICTURE_TYPE_OTHER
);
111 foo(PICTURE_TYPE_FILE_ICON_STANDARD
);
112 foo(PICTURE_TYPE_FILE_ICON
);
113 foo(PICTURE_TYPE_FRONT_COVER
);
114 foo(PICTURE_TYPE_BACK_COVER
);
115 foo(PICTURE_TYPE_LEAFLET_PAGE
);
116 foo(PICTURE_TYPE_MEDIA
);
117 foo(PICTURE_TYPE_LEAD_ARTIST
);
118 foo(PICTURE_TYPE_ARTIST
);
119 foo(PICTURE_TYPE_CONDUCTOR
);
120 foo(PICTURE_TYPE_BAND
);
121 foo(PICTURE_TYPE_COMPOSER
);
122 foo(PICTURE_TYPE_LYRICIST
);
123 foo(PICTURE_TYPE_RECORDING_LOCATION
);
124 foo(PICTURE_TYPE_DURING_RECORDING
);
125 foo(PICTURE_TYPE_DURING_PERFORMANCE
);
126 foo(PICTURE_TYPE_VIDEO_SCREEN_CAPTURE
);
127 foo(PICTURE_TYPE_FISH
);
128 foo(PICTURE_TYPE_ILLUSTRATION
);
129 foo(PICTURE_TYPE_BAND_LOGOTYPE
);
130 foo(PICTURE_TYPE_PUBLISHER_LOGOTYPE
);
131 foo(PICTURE_TYPE_UNDEFINED
);
139 PyObject
*m
= Py_InitModule("flac", methods
);
144 flac_error
= PyErr_NewException("org.diplodocus.flac.error", NULL
, NULL
);
145 if (flac_error
== NULL
) {
148 PyModule_AddObject(m
, "error", flac_error
);