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.

E' necessario avere installata nel sistema e richiamare in Gambas le librerie condivise: "libmpg123.so.0.48.2 " e "libasound.so.2.0.0 ".

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.48.2"

Public Struct mpg123_frameinfo
  version As Integer
  layer As Integer
  rate As Long
  mode As Integer
  mode_ext As Integer
  famesize As Integer
  flags As Integer
  emphasis As Integer
  bitrate As Integer
  abr_rate As Integer
  vbr As Integer
End Struct

Private Enum MPG123_DONE = -12, MPG123_OK = 0
Private Const MPG123_ADD_FLAGS As Integer = 2
Private Const MPG123_FORCE_FLOAT As Long = 1024
Private Const MPG123_ENC_SIGNED_16 As Integer = 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, perror As Pointer) 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(mh 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(mh 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(mh 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(mh As Pointer, ratP As Pointer, channP As Pointer, encodP As Pointer) As Integer

' int mpg123_info (mpg123_handle * mh, struct mpg123_frameinfo * mi)
' Get frame information about the MPEG audio bitstream and store it in a mpg123_frameinfo structure.
Private Extern mpg123_info(mh As Pointer, mi As Mpg123_frameinfo) As Integer

' off_t mpg123_length (mpg123_handle * mh)
' Return, if possible, the full (expected) length of current track in samples.
Private Extern mpg123_length(mh As Pointer) As Long

' 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(mh As Pointer) As Integer

' int mpg123_format (mpg123_handle *mh, long rate, int channels, int encodings)
' Set the audio format support of a mpg123_handle in detail.
 Private Extern mpg123_format(mh As Pointer, rate As Long, channels As Integer, encodings As Integer) 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(mh 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(mh 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(mh 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(mh As Pointer)

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


Library "libasound:2.0.0"

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 filemp3 As String
 Dim err, channels, encoding, size As Integer
 Dim somma, buffer_size, frames As Integer
 Dim frequenza As Long
 Dim inf As New Mpg123_frameinfo
 Dim mp, 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(mp, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0)
 
 mp = mpg123_new(Null, 0)
  
 filemp3 = "/percorso/del/file.mp3"

' Apre il file mpeg in lettura:
 err = mpg123_open(mp, filemp3)
 If err <> MPG123_OK Then Error.Raise("Attenzione ! Problemi con 'mpg123': " & mpg123_strerror(mp))
 
 mpg123_getformat(mp, VarPtr(frequenza), VarPtr(channels), VarPtr(encoding))
   
 mpg123_info(mp, inf)
 Print "File audio: "; filemp3
 Print "Frequenza:  "; frequenza; " hertz"
 Print "Canali:     "; channels
 Print "Bitrate:    "; inf.bitrate
 Print "MPEG:       "; inf.mode
 Print "Layer:      "; inf.layer
 Print "Durata:     "; Time(0, 0, 0, (mpg123_length(mp) / frequenza) * 1000)
 Print
  
 mpg123_format_none(mp)

 mpg123_format(mp, frequenza, channels, encoding)
 
' Si inizializza il buffer:
 buffer_size = mpg123_outblock(mp)
 buffer = Alloc(SizeOf(gb.Byte), 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:
 Repeat
' Si leggono i dati audio e si riempie il "buffer":
   err = mpg123_read(mp, buffer, buffer_size, VarPtr(buffer_size))
   If (err > MPG123_DONE) And (err <> MPG123_OK) Then Error.Raise("Errore nella lettura dei dati audio: " & mpg123_plain_strerror(err))
' 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(frames))
   somma += frames
   Write "\r\e[0mTempo trascorso: \e[31m" & Str(Time(0, 0, 0, (somma / frequenza) * 1000))
 Until err = MPG123_DONE


' Libera la memoria precedentemente occupata:
 snd_pcm_close(handle)
 Free(buffer)
 mpg123_close(mp)
 mpg123_delete(mp)
 mpg123_exit()
  
End


Riferimenti