Convertire un file WAV in formato OggVorbis con le funzioni esterne del API di libvorbisfile, libvorbisenc e libogg
Per convertire un file WAV in formato VorbisOgg, è possibile utilizzare le risorse delle funzioni esterne contenute nelle librerie libvorbisfile, libvorbisenc e libogg.
Per poter fuire in Gambas di tali librerie esterne, è necessario avere installate le seguenti librerie dinamiche condivise: "libvorbisfile.so.3.3.4", "libvorbisenc.so.2.0.8" e "libogg.so.0.8.1" .
Nel semplice esempio appresso mostrato, saremo costretti a creare, inoltre, un nostra apposita libreria dinamica condivisa esterna, nella quale porre una buona parte del codice necessario per la conversione, atteso che in Gambas attualmente non è dichiarabile un tipo di valore corrispondente al "signed char", come richiesto appunto dalla parte di codice C.
Il file WAV da convertire dovrà avere le seguenti caratteristiche fondamentali: 44100 hertz, 16 bit, 2 canali.
Public Struct ogg_stream_state body_data As Pointer body_storage As Long body_fill As Long body_returned As Long lacing_vals As Pointer granule_vals As Pointer lacing_storage As Long lacing_fill As Long lacing_packet As Long lacing_returned As Long header[282] As Byte header_fill As Integer e_o_s As Integer b_o_s As Integer serialno As Long pageno As Long packetno As Long granulepos As Long End Struct Public Struct ogg_page header As Pointer header_len As Long body As Pointer body_len As Long End Struct Public Struct ogg_packet packet As Pointer bytes As Long b_o_s As Long e_o_s As Long granulepos As Long packetno As Long End Struct Public Struct vorbis_info version As Integer channels As Integer rate As Long bitrate_upper As Long bitrate_nominal As Long bitrate_lower As Long bitrate_window As Long codec_setup As Pointer End Struct Public Struct vorbis_comment user_comments As Pointer comment_lengths As Pointer comments As Integer vendor As Pointer End Struct Public Struct vorbis_dsp_state analysisp As Integer vi As Pointer pcm As Pointer pcmret As Pointer pcm_storage As Integer pcm_current As Integer pcm_returned As Integer preextrapolate As Integer eofflag As Integer lW As Long W As Long nW As Long centerW As Long granulepos As Long sequence As Long glue_bits As Long time_bits As Long floor_bits As Long res_bits As Long End Struct Public Struct oggpack_buffer endbyte As Long endbit As Integer buffer As Pointer ptr As Pointer storage As Long End Struct Public Struct vorbis_block pcm As Pointer opb As Struct Oggpack_buffer lW As Long W As Long nW As Long pcmend As Integer mode As Integer eofflag As Integer granulepos As Long sequence As Long vd As Pointer localstore As Pointer localtop As Long localalloc As Long totaluse As Long reap As Pointer glue_bits As Long time_bits As Long floor_bits As Long res_bits As Long internal As Pointer End Struct Library "libvorbisfile:3.3.4" ' void vorbis_info_init(vorbis_info *vi) ' Initializes a vorbis_info structure and allocates its internal storage. Private Extern vorbis_info_init(vo_inf As Vorbis_info) ' void vorbis_comment_init(vorbis_comment *vc) ' Initializes a vorbis_comment structure for use. Private Extern vorbis_comment_init(vc As Vorbis_comment) ' void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents) ' Adds a tag-comment pair to a vorbis_comment structure. Private Extern vorbis_comment_add_tag(vc As Vorbis_comment, tag As String, contents As String) ' int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi) ' Allocates and initializes the encoder's analysis state inside a is vorbis_dsp_state. Private Extern vorbis_analysis_init(v As Vorbis_dsp_state, vi As Vorbis_info) As Integer ' int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb) ' Initializes a vorbis_block structure and allocates its internal storage. Private Extern vorbis_block_init(v As Vorbis_dsp_state, vb As Vorbis_block) As Integer ' int vorbis_analysis_headerout(vorbis_dsp_state *v, vorbis_comment *vc, ogg_packet *op, ogg_packet *op_comm, ogg_packet *op_code) ' Creates and returns the three header packets needed to configure a decoder to accept compressed data. Private Extern vorbis_analysis_headerout(v As Vorbis_dsp_state, vc As Vorbis_comment, op As Ogg_packet, op_comm As Ogg_packet, op_code As Ogg_packet) As Integer ' int vorbis_block_clear(vorbis_block *vb) ' Frees the internal storage for a vorbis_block structure. Private Extern vorbis_block_clear(vb As Vorbis_block) ' void vorbis_dsp_clear(vorbis_dsp_state *v) ' Frees the internal storage for a vorbis_dsp_state structure. Private Extern vorbis_dsp_clear(vd As Vorbis_dsp_state) ' void vorbis_comment_clear(vorbis_comment *vc) ' Frees the internal storage associated with a vorbis_comment structure. Private Extern vorbis_comment_clear(vc As Vorbis_comment) ' vorbis_info_clear(vorbis_info *vi) ' Frees the internal storage for a vorbis_info structure. Private Extern vorbis_info_clear(vi As Vorbis_info) Library "libvorbisenc:2.0.8" ' int vorbis_encode_init_vbr(vorbis_info *vi, long channels, long rate, float base_quality) ' For setting up variable bitrate ("quality" based) modes. Private Extern vorbis_encode_init_vbr(vi As Vorbis_info, channels As Long, rate As Long, base_quality As Single) As Integer Library "libogg:0.8.1" ' int ogg_stream_init(ogg_stream_state *os,int serialno) ' Initialize an ogg_stream_state struct and allocates appropriate memory in preparation for encoding or decoding. Private Extern ogg_stream_init(os As Ogg_stream_state, serialno As Integer) As Integer ' int ogg_stream_packetin(ogg_stream_state *os, ogg_packet *op) ' Submits a packet to the bitstream for page encapsulation. Private Extern ogg_stream_packetin(os As Ogg_stream_state, op As Ogg_packet) As Integer ' int ogg_stream_flush(ogg_stream_state *os, ogg_page *og) ' checks for remaining packets inside the stream and forces remaining packets into a page, regardless of the size of the page. Private Extern ogg_stream_flush(os As Ogg_stream_state, og As Ogg_page) As Integer ' ' ' Clears and frees the internal memory used by the ogg_stream_state struct, but does not free the structure itself. Private Extern ogg_stream_clear(os As Ogg_stream_state)
Pagina in costruzione !