Differenze tra le versioni di "Generare un file WAV da dati audio grezzi con le funzioni esterne del API di LibAo"
Da Gambas-it.org - Wikipedia.
(Creata pagina con 'Per l'uso delle risorse della libreria ''libao'' bisognerà richiamare nel proprio applicativo Gambas la libreria attualmente alla versione: "''libao.so.4.0.0''" . Di seguito...') |
|||
(11 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
− | + | Con la libreria esterna ''libao'' è possibile generare un file WAV da dati audio grezzi. | |
− | Di seguito un possibile codice per generare con le risorse della libreria ''libao'' un file wav utilizzando | + | Per l'uso delle risorse della libreria ''libao'' bisognerà avere installata nel proprio sistema e richiamare nell'applicativo Gambas la libreria condivisa attualmente alla versione: "''libao.so.4.1.1'' ". |
− | + | ||
+ | Di seguito un possibile codice per generare con le risorse della libreria ''libao'' un file wav, ad esempio, utilizzando un file contenente dati audio grezzi: | ||
+ | Library "libao:4.1.1" | ||
+ | |||
+ | Public Struct ao_sample_format | ||
bits As Integer <FONT color=gray>' ''bits per sample''</font> | bits As Integer <FONT color=gray>' ''bits per sample''</font> | ||
rate As Integer <FONT color=gray>' ''samples per second (in a single channel)''</font> | rate As Integer <FONT color=gray>' ''samples per second (in a single channel)''</font> | ||
Riga 8: | Riga 12: | ||
byte_format As Integer <FONT color=gray>' ''Byte ordering in sample, see constants below''</font> | byte_format As Integer <FONT color=gray>' ''Byte ordering in sample, see constants below''</font> | ||
matrix As Pointer <FONT color=gray>' ''input channel location/ordering''</font> | matrix As Pointer <FONT color=gray>' ''input channel location/ordering''</font> | ||
− | + | End Struct | |
− | + | ||
− | + | Private Const AO_FMT_LITTLE As Integer = 1 | |
− | |||
− | |||
− | |||
− | |||
− | |||
<FONT color=gray>' ''void ao_initialize(void)'' | <FONT color=gray>' ''void ao_initialize(void)'' | ||
' ''library setup''</font> | ' ''library setup''</font> | ||
− | + | Private Extern ao_initialize() | |
<FONT color=gray>' ''int ao_driver_id(void)'' | <FONT color=gray>' ''int ao_driver_id(void)'' | ||
' ''driver information''</font> | ' ''driver information''</font> | ||
− | + | Private Extern ao_driver_id(s As String) As Integer | |
<FONT color=gray>' ''int ao_default_driver_id(void)'' | <FONT color=gray>' ''int ao_default_driver_id(void)'' | ||
' ''driver information''</font> | ' ''driver information''</font> | ||
− | + | Private Extern ao_default_driver_id() As Integer | |
<FONT color=gray>' ''ao_device* ao_open_file(int driver_id, const char *filename, int overwrite, ao_sample_format *format, ao_option *options)'' | <FONT color=gray>' ''ao_device* ao_open_file(int driver_id, const char *filename, int overwrite, ao_sample_format *format, ao_option *options)'' | ||
' ''Open a file for audio output. The file format is determined by the audio driver used. | ' ''Open a file for audio output. The file format is determined by the audio driver used. | ||
' ''on-NULL pointer inicates success. This pointer must be passed in subsequent calls to ao_play() and ao_close().''</font> | ' ''on-NULL pointer inicates success. This pointer must be passed in subsequent calls to ao_play() and ao_close().''</font> | ||
− | + | Private Extern ao_open_file(driver_id As Integer, filename As String, overwrite As Integer, fmt As Ao_sample_format, options As Pointer) As Pointer | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<FONT color=gray>' ''int ao_close(ao_device *device)'' | <FONT color=gray>' ''int ao_close(ao_device *device)'' | ||
' ''Chiude il dispositivo audio''</font> | ' ''Chiude il dispositivo audio''</font> | ||
− | + | Private Extern ao_close(device As Pointer) As Integer | |
<FONT color=gray>' ''void ao_shutdown(void)''</font> | <FONT color=gray>' ''void ao_shutdown(void)''</font> | ||
<FONT color=gray>' ''library teardown''</font> | <FONT color=gray>' ''library teardown''</font> | ||
− | + | Private Extern ao_shutdown() | |
− | + | Public Sub Main() | |
− | + | Dim ao_sf As New Ao_sample_format | |
− | + | Dim device As Pointer | |
− | + | Dim driver, default_driver As Integer | |
− | + | Dim buffer As Byte[] | |
− | |||
<FONT color=gray>' ''Inizializza la libreria 'libao':''</font> | <FONT color=gray>' ''Inizializza la libreria 'libao':''</font> | ||
− | + | ao_initialize() | |
<FONT color=gray>' ''Imposta il driver audio come predefinito:''</font> | <FONT color=gray>' ''Imposta il driver audio come predefinito:''</font> | ||
− | + | default_driver = ao_default_driver_id() | |
<FONT color=gray>' ''Imposta le caratteristiche dell'audio in uscita:''</font> | <FONT color=gray>' ''Imposta le caratteristiche dell'audio in uscita:''</font> | ||
− | + | With ao_sf | |
− | + | .bits = 16 | |
− | + | .channels = 2 | |
− | + | .rate = 44100 | |
− | + | .byte_format = AO_FMT_LITTLE | |
− | + | End With | |
− | + | ||
− | |||
− | |||
− | |||
<FONT color=gray>' '''''Crea il file wav''''' | <FONT color=gray>' '''''Crea il file wav''''' | ||
' ''Imposta il driver audio per il wav:''</font> | ' ''Imposta il driver audio per il wav:''</font> | ||
− | + | driver = ao_driver_id("wav") | |
− | + | <FONT color=gray>' ''Imposta il percorso e le caratteristiche del file WAV che sarà infine creato:''</font> | |
− | + | device = ao_open_file(driver, "/tmp/intWAV", 1, ao_sf, 0) | |
− | + | If device == 0 Then Error.Raise("Errore nell'apertura del dispositivo audio !") | |
− | |||
+ | <FONT color=gray>' ''Carica ad esempio un file contenente sono dati audio grezzi:''</font> | ||
+ | buffer = Byte[].FromString(File.Load("<FONT color=darkgreen>''/percorso/file/dati/audio/grezzi''</font>")) | ||
− | + | Crea_File_Wav(buffer, device) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<FONT color=gray>' ''Va in chiusura:''</font> | <FONT color=gray>' ''Va in chiusura:''</font> | ||
− | + | buffer.Clear() | |
− | + | ao_shutdown() | |
− | |||
− | + | End | |
− | Private | + | Private Procedure Crea_File_Wav(buf As Byte[], dev as Pointer ) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | While len(File.Load("/tmp/intWAV")) = 0 | |
− | + | ao_close(dev) | |
− | + | wait 0.01 | |
+ | Wend | ||
− | <FONT color=gray>' '' | + | <FONT color=gray>' ''Crea il nuovo file WAV associando i dti di intestazione del WAV con i dati grezzi:''</font> |
− | + | File.Save("/tmp/file.wav", File.Load("/tmp/intWAV") & buf.ToString(0, buf.Count)) | |
− | + | End | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Riferimenti= | =Riferimenti= | ||
− | + | * http://www.xiph.org/ao/doc/ | |
+ | * http://manual.freeshell.org/libao-1.0.0 |
Versione attuale delle 18:48, 13 gen 2024
Con la libreria esterna libao è possibile generare un file WAV da dati audio grezzi.
Per l'uso delle risorse della libreria libao bisognerà avere installata nel proprio sistema e richiamare nell'applicativo Gambas la libreria condivisa attualmente alla versione: "libao.so.4.1.1 ".
Di seguito un possibile codice per generare con le risorse della libreria libao un file wav, ad esempio, utilizzando un file contenente dati audio grezzi:
Library "libao:4.1.1" Public Struct ao_sample_format bits As Integer ' bits per sample rate As Integer ' samples per second (in a single channel) channels As Integer ' number of audio channels byte_format As Integer ' Byte ordering in sample, see constants below matrix As Pointer ' input channel location/ordering End Struct Private Const AO_FMT_LITTLE As Integer = 1 ' void ao_initialize(void) ' library setup Private Extern ao_initialize() ' int ao_driver_id(void) ' driver information Private Extern ao_driver_id(s As String) As Integer ' int ao_default_driver_id(void) ' driver information Private Extern ao_default_driver_id() As Integer ' ao_device* ao_open_file(int driver_id, const char *filename, int overwrite, ao_sample_format *format, ao_option *options) ' Open a file for audio output. The file format is determined by the audio driver used. ' on-NULL pointer inicates success. This pointer must be passed in subsequent calls to ao_play() and ao_close(). Private Extern ao_open_file(driver_id As Integer, filename As String, overwrite As Integer, fmt As Ao_sample_format, options As Pointer) As Pointer ' int ao_close(ao_device *device) ' Chiude il dispositivo audio Private Extern ao_close(device As Pointer) As Integer ' void ao_shutdown(void) ' library teardown Private Extern ao_shutdown() Public Sub Main() Dim ao_sf As New Ao_sample_format Dim device As Pointer Dim driver, default_driver As Integer Dim buffer As Byte[] ' Inizializza la libreria 'libao': ao_initialize() ' Imposta il driver audio come predefinito: default_driver = ao_default_driver_id() ' Imposta le caratteristiche dell'audio in uscita: With ao_sf .bits = 16 .channels = 2 .rate = 44100 .byte_format = AO_FMT_LITTLE End With ' Crea il file wav ' Imposta il driver audio per il wav: driver = ao_driver_id("wav") ' Imposta il percorso e le caratteristiche del file WAV che sarà infine creato: device = ao_open_file(driver, "/tmp/intWAV", 1, ao_sf, 0) If device == 0 Then Error.Raise("Errore nell'apertura del dispositivo audio !") ' Carica ad esempio un file contenente sono dati audio grezzi: buffer = Byte[].FromString(File.Load("/percorso/file/dati/audio/grezzi")) Crea_File_Wav(buffer, device) ' Va in chiusura: buffer.Clear() ao_shutdown() End Private Procedure Crea_File_Wav(buf As Byte[], dev as Pointer ) While len(File.Load("/tmp/intWAV")) = 0 ao_close(dev) wait 0.01 Wend ' Crea il nuovo file WAV associando i dti di intestazione del WAV con i dati grezzi: File.Save("/tmp/file.wav", File.Load("/tmp/intWAV") & buf.ToString(0, buf.Count)) End