Eseguire un file mp3 usando le API di mpg123 e di Alsa

Da Gambas-it.org - Wikipedia.

E' possibile utilizzare in un medesimo applicativo le funzioni esterne dell'API di mpg123 unitamente a quelle di Alsa.
Le funzioni di mpg123 effettueranno la decodifica del file mp3 ed l'assegnazione dei dati ad un buffer. Tale buffer sarà, poi, passato all'apposita funzione esterna di Alsa per la scrittura dei dati nel suo sub-sistema PCM.


Vediamo, di seguito, un semplice esempio di quanto detto. Distingueremo la libreria di mpg123 colorandola di bluda quella di Alsa colorata di rosso:

Library "libmpg123:0.38.4"
 
Private Const MPG123_OK As Byte = 0
Private Const MPG123_ADD_FLAGS As Byte = 2
Private Const MPG123_FORCE_FLOAT As Short = 1024
Private Const MPG123_ENC_SIGNED_16 As Byte = 208

' int mpg123_init (void)
' Function to initialise the mpg123 library.
Private Extern mpg123_init() As Integer

' const char* mpg123_plain_strerror (int errcode)
' Return a string describing that error errcode means.
Private Extern mpg123_plain_strerror(errcode As Integer) As String

' mpg123_handle * mpg123_new (const char *decoder, int *error)
' Create a handle with optional choice of decoder (named by a string)
Private Extern mpg123_new(decoder As String, errorI As Integer) As Pointer

' int mpg123_param (mpg123_handle * mh, Enum mpg123_parms type, long value, double fvalue)
' Set a specific parameter, For a specific mpg123_handle, using a parameter type key chosen From the mpg123_parms enumeration, To the specified value.
Private Extern mpg123_param(mhP As Pointer, type As Integer, valueL As Long, fvalue As Float) As Integer

' int mpg123_open (mpg123_handle * mh, const char * path)
' Open and prepare to decode the specified file by filesystem path. This does not open HTTP urls.
Private Extern mpg123_open(mhP As Pointer, pathFile As String) As Integer

' const char* mpg123_strerror (mpg123_handle * mh)
' Give string describing what error has occured in the context of handle mh.
Private Extern mpg123_strerror(mhP As Pointer) As String

' int mpg123_getformat (mpg123_handle * mh, long * rate, int * channels, int * encoding)
' Get the current output format written to the addresses given.
Private Extern mpg123_getformat(mhP As Pointer, ratP As Pointer, channP As Pointer, encodP As Pointer) As Integer

' int mpg123_format_none (mpg123_handle * mh)
' Configure a mpg123 handle to accept no output format at all, use before specifying supported formats with mpg123_format.
Private Extern mpg123_format_none(mhP As Pointer) As Integer

' size_t mpg123_outblock (mpg123_handle * mh)
' The max size of one frame's decoded output with current settings.
' Use that to determine an appropriate minimum buffer size for decoding one frame.
Private Extern mpg123_outblock(mhP As Pointer) As Integer

' int mpg123_read (mpg123_handle * mh, unsigned char * outmemory, size_t outmemsize, size_t * done)
' Read from stream and decode up to outmemsize bytes.
Private Extern mpg123_read(mhP As Pointer, outmemory As Pointer, outmemsize As Integer, done As Pointer) As Integer

' int mpg123_close (mpg123_handle * mh)
' Closes the source, if libmpg123 opened it
Private Extern mpg123_close(mhP As Pointer) As Integer

' void mpg123_delete (mpg123_handle * mh)
' Delete handle, mh is either a valid mpg123 handle or NULL
Private Extern mpg123_delete(mhP As Pointer)

' void mpg123_exit (void)
' Function to close down the mpg123 library.
Private Extern mpg123_exit()


Library "libasound:2"

Private Const SND_PCM_STREAM_PLAYBACK As Byte = 0
Private Const SND_PCM_FORMAT_U8 As Byte = 1
Private Const SND_PCM_ACCESS_RW_INTERLEAVED As Byte = 3
  
' int snd_pcm_open(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode)
' Opens a PBM.
Private Extern snd_pcm_open(pcmP As Pointer, nome As String, stream As Integer, mode As Integer) As Integer

' const char * snd_strerror (int errnum)
' Returns the message For an Error code.
Private Extern snd_strerror(errnum As Integer) As String

' int snd_pcm_set_params (snd_pcm_t *pcm, snd_pcm_format_t format, snd_pcm_access_t access, unsigned int channels, unsigned int rate, int soft_resample, unsigned int latency)
' Set the hardware And software parameters in a simple way.
Private Extern snd_pcm_set_params(pcmP As Pointer, formatB As Byte, accessB As Byte, channels As Integer, rate As Integer, soft_resample As Integer, latency As Integer) As Integer

' snd_pcm_sframes_t snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
' Write interleaved frames to a PCM.
Private Extern snd_pcm_writei(pcmP As Pointer, buffP As Pointer, uframes As Long) As Integer

' int snd_pcm_recover (snd_pcm_t *pcm, int err, int silent)
' Recover the stream state From an Error or suspend
Private Extern snd_pcm_recover(pcmP As Pointer, err As Integer, silent As Integer) As Integer

' int snd_pcm_close(snd_pcm_t **pcm)
' Close PCM handle.
Private Extern snd_pcm_close(pcmP As Pointer)


Public Sub Main()
 
 Dim err, channels, encoding, size As Integer
 Dim somma, buffer_size, frames As Integer
 Dim frequenza As Long
 Dim mh, buffer, handle As Pointer
 
' Inizializza la liberia di "mpg123":
  err = mpg123_init()
  If err <> MPG123_OK Then Error.Raise("Configurazione di base errata ! " & mpg123_plain_strerror(err))
 
  mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0)
 
  mh = mpg123_new(Null, 0)

' Apre il file mpeg in lettura:
  err = mpg123_open(mh, "percorso/del/file.mp3")
  If err <> MPG123_OK Then Error.Raise("Attenzione ! Problemi con 'mpg123': " & mpg123_strerror(mh))
 
  mpg123_getformat(mh, VarPtr(frequenza), VarPtr(channels), VarPtr(encoding))

  mpg123_format_none(mh)

  mpg123_format(mh, frequenza, channels, encoding)
 
' Si inizializza il buffer:
  buffer_size = mpg123_outblock(mh)
  buffer = Alloc(buffer_size)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Qui inizia la parte relativa alle funzioni di Alsa:

  err = snd_pcm_open(VarPtr(handle), "default", SND_PCM_STREAM_PLAYBACK, 0)
  If err < 0 Then Error.Raise("Playback open error: " & snd_strerror(err))

  err = snd_pcm_set_params(handle, 2, SND_PCM_ACCESS_RW_INTERLEAVED, channels, frequenza, 1, 500000)
  If err < 0 Then Error.Raise("Playback open error: " & snd_strerror(err))

  If encoding And MPG123_ENC_SIGNED_16 Then
    size = SizeOf(gb.Integer)
  Else
    size = SizeOf(gb.Float)
  Endif
  
' Ha inizio il ciclo di lettura ed i invio dei dati audio:
  Do
' Si leggono i dati audio e si riempie il "buffer":
    err = mpg123_read(mh, buffer, buffer_size, VarPtr(buffer_size))
  
' I dati audio vengono inviati al sub-sistema "PCM" di Alsa:
    frames = snd_pcm_writei(handle, buffer, buffer_size / size)
    If (frames < 0) Then Error.Raise("Errore alla funzione 'snd_pcm_writei': " & snd_strerror(err))
    
    somma += frames
    Write "\r" & CStr(Date(0, 0, 0, 0, 0, 0, (somma / frequenza) * 1000))
      
  Loop While err = MPG123_OK


' Chiude l'handle ed esce dalla libreria di "mpg123":
  mpg123_close(mh)
  mpg123_delete(mh)
  mpg123_exit()
 
' Chiude infine anche l'handle della libreria Alsa:
  snd_pcm_close(handle)

End



Riferimenti