Python c example2:pylame2

Source: Internet
Author: User

#include <Python.h>#include<lame.h>//Pyton Object Variablestypedefstruct{pyobject_head FILE*OUTFP; Lame_global_flags*GFP;} Pylame2_encoderobject;StaticPyobject *encoder_new (Pytypeobject *type, Pyobject *args, Pyobject *kw) {Pylame2_encoderobject*self = (Pylame2_encoderobject *) Type->tp_alloc (type,0); Self-&GT;OUTFP =NULL; Self-&GT;GFP =NULL; return(Pyobject *) self;}Static voidEncoder_dealloc (Pylame2_encoderobject *Self ) {    if(self->GFP) {Lame_close ( self-GFP); }    if(self->OUTFP) {fclose ( self-OUTFP); } Self->ob_type->tp_free (self);}Static intEncoder_init (Pylame2_encoderobject * Self, pyobject *args, Pyobject *kw) {    Char*Outpath; if(! Pyarg_parsetuple (args,"s", &Outpath)) {        return-1; }    if(SELF-&GT;OUTFP | | self->GFP) {pyerr_setstring (pyexc_exception,"__init__ already called"); return-1; } Self-&GT;OUTFP = fopen (Outpath,"WB"); Self-&GT;GFP =Lame_init (); Lame_init_params ( Self-GFP); return 0;}StaticPyobject *encoder_encode (Pylame2_encoderobject *self, Pyobject *args) {    Char*In_buffer; intin_length; intmp3_length; Char*Mp3_buffer; intmp3_bytes; if(! (SELF-&GT;OUTFP && self->GTP)) {pyerr_setstring (pyexc_exception,"Encoder not open"); returnNULL; }    if(! Pyarg_parsetuple (args,"s#", &in_buffer, &in_length)) {        returnNULL; } in_length/=2; Mp3_length= (int)(1.25* in_length) +7200; Mp3_buffer= (Char*)malloc(mp3_length); if(In_length >0) {mp3_bytes=lame_encode_buffer_interleaved ( self-GTP, ( Short*) In_buffer, In_length/2, Mp3_buffer, mp3_length); if(Mpe3_bytes >0) {fwrite (Mp3_buffer,1, Mp3_bytes, self->OUTFP); }    }     Free(Mp3_buffer); Py_return_none;}StaticPyobject *encoder_close (Pylame2_encoderobject *Self ) {    intmp3_length; Char*Mp3_buffer; intmpe3_bytes; if(! (SELF-&GT;OUTFP && self->GFP)) {pyerr_setstring (pyexc_exception,"Encoder not open"); returnNULL; } mp3_length=7200; Mp3_buffer= (Char*)malloc(mp3_length); Mp3_bytes= Lame_encode_flush (SELF-&GT;GFP, Mp3_buffer,sizeof(Mp3_buffer)); if(Mp3_bytes >0) {fwrite (Mp3_buffer,1, Mp3_bytes, self->OUTFP); }     Free(Mp3_buffer); Lame_close ( Self-GFP); Self-&GT;GFP =NULL; Fclose ( Self-OUTFP); Self-&GT;OUTFP =NULL; Py_return_none;}StaticPymethoddef encoder_methods[] ={    {"encode", (pycfunction) Encoder_encode, Meth_varargs,"encodes and writes data to the output file."},    {"Close", (pycfunction) Encoder_close, Meth_noargs,"closes the output file."}, {null, NULL,0, NULL}};StaticPytypeobject Pylame2_encodertype ={pyobject_head_init (NULL)0,/*ob_size*/    "pylame2. Encoder",/*Tp_name*/    sizeof(Pylame2_encoderobject),/*tp_basicsize*/    0,/*tp_itemsize*/(destructor) Encoder_dealloc,/*Tp_dealloc*/    0,/*Tp_print*/    0,/*tp_getattr*/    0,/*tp_setattr*/    0,/*Tp_compare*/    0,/*Tp_repr*/    0,/*Tp_as_number*/    0,/*tp_as_sequence*/    0,/*tp_as_mapping*/    0,/*Tp_hash*/    0,/*Tp_call*/    0,/*Tp_str*/    0,/*Tp_getattro*/    0,/*Tp_setattro*/Py_tpflags_default,/*Tp_flags*/    "My First Encoder object.",/*Tp_doc*/    0,/*Tp_traverse*/    0,/*Tp_clear*/    0,/*Tp_richcompare*/    0,/*Tp_weaklistoffset*/    0,/*Tp_iter*/    0,/*Tp_iternext*/Encoder_methods,/*Tp_methods*/    0,/*tp_members*/    0,/*Tp_getset*/    0,/*Tp_base*/    0,/*tp_dict*/    0,/*Tp_descr_get*/    0,/*Tp_descr_set*/    0,/*Tp_dictoffset*/(Initproc) Encoder_init,/*Tp_init*/    0,/*Tp_alloc*/Encoder_new,/*tp_new*/    0,/*Tp_free*/};StaticPymethoddef pylame2_methods[] ={{null, NULL,0, NULL}}; Pymodinit_func initpylame2 () {Pyobject*m; if(Pytype_ready (&pylame2_encodertype) <0)    {        return; } m= Py_initmodule3 ("pylame2", Pylame2_methods,"My Second LAME module."); Py_incref (&Pylame2_encodertype); Pymodule_addobject (M,"Encoder", (Pyobject *) &pylame2_encodertype);};/*gcc-shared-i/usr/include/pytyon3.1-i/usr/include/lame pylame2.c-lmp3lame-o pylame2.so*//*Python code import pylame2 inbufsize = 4096 encoder = pylame2. Encoder (' Test.mp3 ') input = file (' Test.raw ', ' RB ') data = Input.read (inbufsize) while data! = ': Encoder. Encode (data) data = Input.read (inbufsize) input.close () encoder.close ()*/

Python c example2:pylame2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.