Differenze tra le versioni di "Decodifica dei dati precedentemente codificati mediante le funzioni esterne del API di Speex"
Riga 15: | Riga 15: | ||
void *speex_decoder_init(const SpeexMode *mode) | void *speex_decoder_init(const SpeexMode *mode) | ||
la quale, diversamente, determina la sollevazione di un errore. | la quale, diversamente, determina la sollevazione di un errore. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<FONT color=gray size=1>' ''Speex License'' | <FONT color=gray size=1>' ''Speex License'' | ||
Riga 88: | Riga 73: | ||
− | Library "/tmp/libspeexdec" <FONT color=gray>' ''Richiama la libreria da noi realizzata per la gestione sicura della funzione "speex_decoder_init()" di libspeex''</font> | + | Library "/tmp/libspeexdec" <FONT color=gray>' ''Richiama la libreria da noi realizzata per la gestione sicura della funzione "speex_decoder_init()" di libspeex'' |
− | + | ' ''char * encoder_init()''</font> | |
Private Extern decoder_init() As Pointer | Private Extern decoder_init() As Pointer | ||
'''Public''' Sub Main() | '''Public''' Sub Main() | ||
− | + | ||
Dim state As Pointer | Dim state As Pointer | ||
Dim inFile, exFile As String | Dim inFile, exFile As String | ||
Riga 104: | Riga 89: | ||
Dim bits As New SpeexBits | Dim bits As New SpeexBits | ||
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, | 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] <FONT color=gray>' ''blocco d'intestazione del file wav futuro: 2 canali, 16 bit, hz 44100''</font> | |
− | + | ||
− | |||
<FONT color=gray>' ''Verifica se già è presente la libreria ad hoc per la gestione sicura della funzione "speex_decoder_init()" di libspeex:''</font> | <FONT color=gray>' ''Verifica se già è presente la libreria ad hoc per la gestione sicura della funzione "speex_decoder_init()" di libspeex:''</font> | ||
− | + | If Exist("/tmp/libspeexdec.so") = False Then creaSpeexDec() | |
− | + | ||
− | + | inFile = "<FONT color=gray>''/tmp/codificato.sw''</font>" | |
− | + | exFile = "<FONT color=gray>''/tmp/decodificato.wav''</font>" | |
− | + | fin = Open inFile For Read | |
− | + | fex = Open exFile For Create | |
− | + | ||
<FONT color=gray>' ''Invoca la funzione esterna della libreria ad hoc:''</font> | <FONT color=gray>' ''Invoca la funzione esterna della libreria ad hoc:''</font> | ||
− | + | state = decoder_init() | |
− | + | ||
<FONT color=gray>' ''Imposta il miglioramento percettivo:''</font> | <FONT color=gray>' ''Imposta il miglioramento percettivo:''</font> | ||
− | + | tmp = 1 | |
− | + | speex_decoder_ctl(state, SPEEX_SET_ENH, VarPtr(tmp)) | |
<FONT color=gray>' ''Inizializza la Struttura contenente i bit:''</font> | <FONT color=gray>' ''Inizializza la Struttura contenente i bit:''</font> | ||
− | + | speex_bits_init(bits) | |
− | + | ||
<FONT color=gray>' ''Inizia la scrittura del file wav:''</font> | <FONT color=gray>' ''Inizia la scrittura del file wav:''</font> | ||
− | + | bh.Write(fex, 0, bh.Count) | |
− | + | ||
− | + | While True | |
− | + | If Eof(fin) Then Break | |
− | |||
<FONT color=gray>' ''Legge la dimensione codificata di un frame:''</font> | <FONT color=gray>' ''Legge la dimensione codificata di un frame:''</font> | ||
− | + | Read #fin, nbBytes | |
− | + | ||
<FONT color=gray>' ''Legge il pacchetto codifificato:''</font> | <FONT color=gray>' ''Legge il pacchetto codifificato:''</font> | ||
− | + | cbits.Read(fin, 0, nbBytes) | |
− | + | ||
<FONT color=gray>' ''Copia i dati dalla Struttura bit-stream:''</font> | <FONT color=gray>' ''Copia i dati dalla Struttura bit-stream:''</font> | ||
− | + | speex_bits_read_from(bits, cbits, nbBytes) | |
<FONT color=gray>' ''Decodifica i dati:''</font> | <FONT color=gray>' ''Decodifica i dati:''</font> | ||
− | + | speex_decode(state, bits, exSi) | |
− | + | ||
<FONT color=gray>' ''Copia i dati dala variabile di tipo "Single" nella variabile di tipo "Short" (16 bits) per l'uscita e scrittura nel file:''</font> | <FONT color=gray>' ''Copia i dati dala variabile di tipo "Single" nella variabile di tipo "Short" (16 bits) per l'uscita e scrittura nel file:''</font> | ||
− | + | For i = 0 To FRAME_SIZE - 1 | |
− | + | exSh[i] = exSi[i] | |
− | + | Next | |
− | + | ||
<FONT color=gray>' ''scrive i dati audio decodificati nel file:''</font> | <FONT color=gray>' ''scrive i dati audio decodificati nel file:''</font> | ||
− | + | exSh.Write(fex, 0, FRAME_SIZE) | |
− | + | ||
− | + | Wend | |
− | + | Print "Fine decodificazione" | |
− | + | ||
− | |||
<FONT color=gray>' ''Va in chiusura.''</font> | <FONT color=gray>' ''Va in chiusura.''</font> | ||
− | + | ||
<FONT color=gray>' ''Distrugge lo stato di codificatore:''</font> | <FONT color=gray>' ''Distrugge lo stato di codificatore:''</font> | ||
− | + | speex_decoder_destroy(state) | |
<FONT color=gray>' ''Elimina la Struttura contenente i bit:''</font> | <FONT color=gray>' ''Elimina la Struttura contenente i bit:''</font> | ||
− | + | speex_bits_destroy(bits) | |
<FONT color=gray>' ''Chiude i file:''</font> | <FONT color=gray>' ''Chiude i file:''</font> | ||
− | + | fex.Close | |
− | + | fin.Close | |
− | + | ||
'''End''' | '''End''' | ||
<FONT color=gray>' ''Crea l'apposita libreria per la gestione sicura della funzione "speex_decoder_init()" di libspeex:''</font> | <FONT color=gray>' ''Crea l'apposita libreria per la gestione sicura della funzione "speex_decoder_init()" di libspeex:''</font> | ||
− | '''Private''' Procedure | + | '''Private''' Procedure creaSpeexDec() |
− | |||
− | |||
+ | File.Save("/tmp/libspeexcod.c", "#include <speex/speex.h>\n\n" & | ||
+ | " const SpeexMode speex_nb_mode;\n\n" & | ||
+ | "// Crea un nuovo stato del codificatore in modalità \"narrowband\":\n" & | ||
+ | "char * encoder_init() {\n\n" & | ||
+ | " return speex_encoder_init(&speex_nb_mode);\n\n}") | ||
+ | |||
+ | Shell "gcc -o /tmp/libspeexcod.so /tmp/libspeexcod.c -shared -fPIC -lspeex" Wait | ||
+ | |||
'''End''' | '''End''' | ||
Versione delle 10:48, 26 ago 2018
Speex consente di codificare i dati audio del "parlato", comprimendoli.
Speex pone a disposizione risorse e funzioni per la decodifica dei dati audio, precedentemente codificati e compressi, al fine così di consentire l'ascolto dell'audio.
Uso delle risorse di Speex mediante la libreria libspeex
La decodifica dei dati precedentemente codificati del "parlato" mediante le risorse e le funzioni di Speex in Gambas richiedono di richiamare la libreria (nella sua attuale versione): libspeex.so.1.5.0
La decodifica dei dati audio del "parlato" precedentemente codificati
Il seguente codice provvede a decodificare dati audio precedentemente codificati da Speex ed a salvarli nuovamente in un file di dati audio grezzi PCM a 16 bit, 44100 Hz, 2 canali. Dai dati grezzi così ottenuti, il codice ricostruirà un file wav.
Sarà necessario scrivere, ed utilizzare successivamente nell'applicazione Gambas, una libreria esterna condivisa .so, da noi appositamente sritta in C, per poter gestire con sicurezza la funzione
void *speex_decoder_init(const SpeexMode *mode)
la quale, diversamente, determina la sollevazione di un errore.
' Speex License ' Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ' Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. ' Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. ' Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ' AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ' (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, ' STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Public Struct SpeexBits chars As Pointer ' "raw" data nbBits As Integer ' Total number Of bits stored In the stream charPtr As Integer ' Position Of the byte "cursor" bitPtr As Integer ' Position Of the bit "cursor" within the current char owner As Integer ' Does the struct "own" the "raw" buffer(member "chars") overflow As Integer ' Set To one If we Try To Read past the valid data buf_size As Integer ' Allocated size For buffer reserved1 As Integer ' Reserved For future use reserved2 As Integer ' Reserved For future use End Struct Private Const FRAME_SIZE As Byte = 160 Library "libspeex:1.5.0" Private Const SPEEX_SET_ENH As Byte = 0 ' Set enhancement on/off ' void *speex_decoder_init(const SpeexMode *mode) ' Returns a handle to a newly created decoder state structure. Private Extern speex_decoder_init(mode As Pointer) ' int speex_decoder_ctl(void *state, int request, void *ptr) ' Used like the ioctl function to control the encoder parameters Private Extern speex_decoder_ctl(stP As Pointer, request As Integer, ptr As Pointer) As Integer ' void speex_bits_read_from(SpeexBits * bits, char * bytes, Int Len) ' Initializes the bit-stream from the data in an area of memory Private Extern speex_bits_read_from(sbSt As SpeexBits, bytes As Byte[], lenI As Integer) ' int speex_decode(void *state, SpeexBits *bits, float *out) ' Uses an existing decoder state to decode one frame of speech from bit-stream bits. The output speech is saved written to out. Private Extern speex_decode(stP As Pointer, sbSt As SpeexBits, outfl As Single[]) ' void speex_decoder_destroy(void *state) ' Frees all resources associated to an existing Speex decoder state. Private Extern speex_decoder_destroy(stP As Pointer) ' void speex_bits_destroy(SpeexBits *bits) ' Frees all resources associated to a SpeexBits struct. Private Extern speex_bits_destroy(sbSt As SpeexBits) Library "/tmp/libspeexdec" ' Richiama la libreria da noi realizzata per la gestione sicura della funzione "speex_decoder_init()" di libspeex ' char * encoder_init() Private Extern decoder_init() As Pointer Public Sub Main() Dim state As Pointer Dim inFile, exFile As String Dim fin, fex As File Dim exSh As New Short[FRAME_SIZE] Dim exSi As New Single[FRAME_SIZE] Dim cbits As New Byte[200] Dim nbBytes, i, tmp As Integer Dim bits As New SpeexBits 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 ' Verifica se già è presente la libreria ad hoc per la gestione sicura della funzione "speex_decoder_init()" di libspeex: If Exist("/tmp/libspeexdec.so") = False Then creaSpeexDec() inFile = "/tmp/codificato.sw" exFile = "/tmp/decodificato.wav" fin = Open inFile For Read fex = Open exFile For Create ' Invoca la funzione esterna della libreria ad hoc: state = decoder_init() ' Imposta il miglioramento percettivo: tmp = 1 speex_decoder_ctl(state, SPEEX_SET_ENH, VarPtr(tmp)) ' Inizializza la Struttura contenente i bit: speex_bits_init(bits) ' Inizia la scrittura del file wav: bh.Write(fex, 0, bh.Count) While True If Eof(fin) Then Break ' Legge la dimensione codificata di un frame: Read #fin, nbBytes ' Legge il pacchetto codifificato: cbits.Read(fin, 0, nbBytes) ' Copia i dati dalla Struttura bit-stream: speex_bits_read_from(bits, cbits, nbBytes) ' Decodifica i dati: speex_decode(state, bits, exSi) ' Copia i dati dala variabile di tipo "Single" nella variabile di tipo "Short" (16 bits) per l'uscita e scrittura nel file: For i = 0 To FRAME_SIZE - 1 exSh[i] = exSi[i] Next ' scrive i dati audio decodificati nel file: exSh.Write(fex, 0, FRAME_SIZE) Wend Print "Fine decodificazione" ' Va in chiusura. ' Distrugge lo stato di codificatore: speex_decoder_destroy(state) ' Elimina la Struttura contenente i bit: speex_bits_destroy(bits) ' Chiude i file: fex.Close fin.Close End ' Crea l'apposita libreria per la gestione sicura della funzione "speex_decoder_init()" di libspeex: Private Procedure creaSpeexDec() File.Save("/tmp/libspeexcod.c", "#include <speex/speex.h>\n\n" & " const SpeexMode speex_nb_mode;\n\n" & "// Crea un nuovo stato del codificatore in modalità \"narrowband\":\n" & "char * encoder_init() {\n\n" & " return speex_encoder_init(&speex_nb_mode);\n\n}") Shell "gcc -o /tmp/libspeexcod.so /tmp/libspeexcod.c -shared -fPIC -lspeex" Wait End
Riferimenti
[1] Il sito di Speex