Convertire un file OggVorbis in WAV con le funzioni esterne del API di Libvorbisidec
La libreria Libvorbisidec (Tremor) intende essere il più possibile simile alla libreria Vorbisfile distribuita gratuitamente da Xiph.org . Essa fornisce una libreria di solo numeri interi destinati alla decodifica di tutti i formati di file Vorbis attuali e futuri.
Per poter fruire delle risorse del API di Libvorbisidec si dovrà utilizzare la libreria (nella sua attuale versione): "libvorbisidec.so.1.0.3"
Di seguito mostreremo un semplice codice per convertire un file OggVorbis in file WAV. Poiché l'uso della libreria esterna Libvorbisidec prevede il richiamo di una complessa Struttura esterna, ed al fine di poter gestire detta Struttura esterna in modo assolutamente sicuro, ci serviremo di una libreria esterna scritta in C, che realizzeremo ad hoc e che richiameremo all'interno del codice Gambas:
#include <stdio.h> #include "/usr/include/tremor/ivorbisfile.h" int Dim_OggVorbis_File() { return sizeof(OggVorbis_File); }
Tale codice in linguaggio C in questo esempio sarà posto nella cartella Dati dell'applicativo Gambas, e andrà poi trasformato in una libreria condivisa .so , che - come già accennato - sarà richiamata nel codice Gambas.
Il codice Gambas potrà essere il seguente:
Library "libvorbisidec:1.0.3" ' int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes) ' Open and initialize an OggVorbis_File structure. Private Extern ov_open(f As Pointer, vfP As Pointer, initial As String, ibytes As Long) As Integer ' long ov_read(OggVorbis_File *vf,char *buffer,int length, int *bitstream) ' Decode a Vorbis file within a loop. Private Extern ov_read(vfP As Pointer, buffer As Byte[], length As Integer, bitstream As Pointer) As Long Library "libc:6" ' FILE *fopen(const char *path, const char *mode) ' Apre il file path associandolo ad uno stream. Private Extern fopen(path As String, mode As String) As Pointer ' int fclose(FILE *stream) ' Chiude il file associato a stream. Private Extern fclose(stmP As Pointer) As Integer Library "/tmp/libadhoc" Private Extern Dim_OggVorbis_File() As Integer Public Sub Main() Dim vf, fin As Pointer Dim eo, current_section, err As Integer Dim ret As Long Dim filefinaleWAV As String = "/tmp/datiPCMgrezzi" Dim pcmout As New Byte[](4096) Dim f As File ' Creiamo la libreria ad hoc per la gestione "sicura" della Struttura "OggVorbis_File": Shell "gcc -o /tmp/libadhoc.so " & Application.Path &/ "libadhoc.c -shared" Wait fin = fopen("/home/ploppo/Scrivania/LINUX/Hydrate-Kenny_Beltrey.ogg", "rb") f = Open filefinaleWAV For Create vf = Alloc(Dim_OggVorbis_File()) err = ov_open(fin, vf, Null, 0) If err < 0 Then Error.Raise("Il file caricato potrebbe non essere del formato Ogg !") ' Ciclo per la decodifica dei dati Ogg e per la scrittura del file contenente i dati grezzi PCM: While Not eo ret = ov_read(vf, pcmout, pcmout.Count, VarPtr(current_section)) If ret = 0 Then eo = 1 Else ' Scrive il file contenente i dati grezzi PCM: pcmout.Write(f, 0, ret) Endif Wend ' Va a creare il file WAV finale: crea_file_wav(filefinaleWAV) ' Va in chiusura: Free(vf) f.Close fclose(fin) End Private Procedure crea_file_wav(audioWAV As String) Dim fl, fwav As File Dim dati As New Byte[](Stat(audioWAV).Size) Dim bh As Byte[] = [&52, &49, &46, &46, &00, &00, &00, &00, &57, &41, &56, &45, &66, &6D, &74, &20, &10, &00, &00, &00, &01, &00, &02, &00, &44, &AC, &00, &00, &10, &B1, &02, &00, &04, &00, &10, &00, &64, &61, &74, &61, &00, &00, &00, &00] ' blocco d'intestazione del file wav futuro: 2 canali, 16 bit, hz 44100 fl = Open audioWAV For Read Write dati.Read(fl) fl.Close bh.Insert(dati, 44) fwav = Open "/tmp/fileAudio.wav" For Create ' Inizia la scrittura del file wav: bh.Write(fwav, 0, bh.Count) dati.Clear bh.Clear fl.Close fwav.Close End