Eseguire un file audio mediante le funzioni di Alure
Da Gambas-it.org - Wikipedia.
Versione del 24 lug 2013 alle 11:07 di Vuott (Discussione | contributi)
ALURE è una libreria di utilità per aiutare a gestire le operazioni più comuni con applicazioni OpenAL. Questo include l'enumerazione e l'inizializzazione del dispositivo, il caricamento del file, e l'esecuzione. Lo scopo di questa API è quello di fornire funzionalità pre-organizzate per facilitare e velocizzare la programmazione dello sviluppatore.
La libreria condivisa contenente le funzioni esterne dell'API di OpenAlure, e che dovrà essere richiamata in Gambas, è attualmente la seguente:
libalure.so.1.2.0
Mostriamo di seguito un semplice codice per ottenere l'esecuzione e l'ascolto di file audio: |1|
Private isdone As Integer
Private src As Integer
Library "libalure:1.2.0"
' ALboolean alureInitDevice(const ALCchar * name, Const ALCint * attribs)
' Opens the named device, creates a context with the given attributes, and sets that context as current.
' The name and attribute list would be the same as what’s passed to alcOpenDevice and alcCreateContext respectively.
' Returns: AL_FALSE On error.
Private Extern alureInitDevice(name As String, attribs As String) As Boolean
' void alGenSources(Alsizei n, ALuint *sources)
Private Extern alGenSources(n As Integer, sources As Pointer) In "libalut:0.1.0"
' ALboolean alureStreamSizeIsMicroSec(ALboolean useUS)
' Specifies if the chunk size value given to the alureCreateStream functions is in bytes (default) or microseconds.
' Returns: Previously set value.
Private Extern alureStreamSizeIsMicroSec(useUS As Boolean) As Boolean
' alureStream* alureCreateStreamFromFile(Const ALchar * fname, ALsizei chunkLength, ALsizei numBufs, ALuint * bufs)
' Opens a file and sets it up for streaming. The given chunkLength is the number of bytes, or microseconds worth of bytes if alureStreamSizeIsMicroSec was last called with AL_TRUE,
' each buffer will fill with. ALURE will optionally generate the specified number of buffer objects, fill them with the beginning of the data,
' then place the new IDs into the provided storage, before returning. Requires an active context.
' Returns: An opaque handle used To control the opened stream, Or Null On error.
Private Extern alureCreateStreamFromFile(fname As String, chunkLength As Integer, numBufs As Integer, bufs As Pointer) As Pointer
' ALboolean alurePlaySourceStream(ALuint source, alureStream * stream, ALsizei numBufs, ALsizei loopcount, void( * eos_callback)(void * userdata, ALuint source), void * userdata)
' Starts playing a stream, using the specified source ID. A stream can only be played if it is not already playing.
' You must call alureUpdate at regular intervals to keep the stream playing, or else the stream will underrun
' and cause a break in the playback until an update call can restart it.
' If an underrun occurs, the source will enter a stopped state until it is automatically restarted. Instead, set a flag using the callback to indicate the stream being stopped.
' "eos_callback": This callback will be called when the stream reaches the end, no more loops are pending, and the source reaches a stopped state.
' It will also be called if an error occured and playback terminated.
' Returns: AL_FALSE On error.
Private Extern alurePlaySourceStream(source As Integer, strePnt As Pointer, numBufs As Integer, loopcount As Integer, eos_callback As Pointer, userdata As Pointer) As Boolean
' void alureUpdate(void)
' Updates the running list of streams, and checks for stopped sources.
' This makes sure that sources played with alurePlaySourceStream are kept fed from their associated stream, and sources played with alurePlaySource are still playing.
' It will call their callbacks as needed.
Private Extern alureUpdate()
' ALboolean alureStopSource(ALuint source, ALboolean run_callback)
' Stops the specified source ID, and any associated stream.
' Returns: AL_FALSE On error.
Private Extern alureStopSource(source As Integer, run_callback As Boolean) As Boolean
' void alDeleteSources(ALsizei n, ALuint * sources)
' This function deletes one or more sources.
Private Extern alDeleteSources(n As Integer, sources As Pointer) In "libalut:0.1.0"
' ALboolean alureDestroyStream(alureStream * stream, ALsizei numBufs, ALuint * bufs)'
' Closes an opened stream.
' Returns: AL_FALSE On error.
Private Extern alureDestroyStream(strePnt As Pointer, numBufs As Integer, bufs As Pointer) As Boolean
' ALboolean alureShutdownDevice(void)
' Destroys the current context and closes its associated device.
' Returns: AL_FALSE On error.
'Private Extern alureShutdownDevice() As Boolean
' ALboolean alurePauseSource(ALuint source)
' Pauses the specified source ID, and any associated stream. Returns AL_FALSE On Error.
Private Extern alurePauseSource(source As Integer) As Boolean
' ALboolean alureResumeSource(ALuint source)
' Resumes the specified source ID after being paused. Returns AL_FALSE On Error.
Private Extern alureResumeSource(source As Integer) As Boolean
Public Sub Button1_Click()
Dim streamP As Pointer
Dim ver As Boolean
Dim percorsoFile As String = "percorso_del_file_audio"
Dim lungh As Integer
alureInitDevice(Null, Null)
alGenSources(1, VarPtr(src))
alureStreamSizeIsMicroSec(False)
lungh = Stat(percorsoFile).Size
' Se si preferisce impostare la durata in base alla dimensione del file audio da eseguire,
' è opportuno passare il valore del secondo parametro almeno pari alla dimensione del file:
streamP = alureCreateStreamFromFile(percorsoFile, lungh, 0, Null)
If IsNull(streamP) Then Error.Raise("Impossibile caricare il file Audio !")
alurePlaySourceStream(src, streamP, 512, 0, eos_callback, Null)
While isdone = 0
Wait 0.01
alureUpdate()
Wend
alureStopSource(src, False)
alDeleteSources(1, VarPtr(src))
alureDestroyStream(streamP, 0, Null)
alureShutdownDevice()
isdone = 0
End
Public Function eos_callback(unused As Pointer, unused2 As Integer)
isdone = 1
End
' Mette in pausa l'esecuzione:
Public Sub Button2_Click()
alurePauseSource(src)
End
' Riprende l'esecuzione:
Public Sub Button3_Click()
alureResumeSource(src)
End
' Arresta l'esecuzione:
Public Sub Button4_Click()
alureStopSource(src, False)
End
Note
[1] Si è constatato che i file audio eseguibili sono quelli di tipo: wav, mp3, ogg.
Riferimenti
- Il sito dell'API di Alure