Differenze tra le versioni di "Eseguire un file mp3 usando le API di mpg123 e di Alsa"

Da Gambas-it.org - Wikipedia.
 
(20 versioni intermedie di uno stesso utente non sono mostrate)
Riga 2: Riga 2:
 
<BR>Le funzioni di ''mpg123'' effettueranno la decodifica del file mp3 ed l'assegnazione dei dati ad un ''[http://it.wikipedia.org/wiki/Buffer buffer]''. Tale ''buffer'' sarà, poi, passato all'apposita funzione esterna di Alsa per la scrittura dei dati nel suo sub-sistema ''PCM''.
 
<BR>Le funzioni di ''mpg123'' effettueranno la decodifica del file mp3 ed l'assegnazione dei dati ad un ''[http://it.wikipedia.org/wiki/Buffer 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.1'' " 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:
 
Vediamo, di seguito, un semplice esempio di quanto detto. Distingueremo la libreria di ''mpg123'' colorandola di bluda quella di Alsa colorata di rosso:
  Library "<FONT color=blue>libmpg123:0.38.4</font>"
+
  Library "<FONT color=blue>libmpg123:0.48.1</font>"
 
+
  Private Const MPG123_OK As Byte = 0
+
Public Struct mpg123_frameinfo
  Private Const MPG123_ADD_FLAGS As Byte = 2
+
  version As Integer
  Private Const MPG123_FORCE_FLOAT As Short = 1024
+
  layer As Integer
  Private Const MPG123_ENC_SIGNED_16 As Byte = 208
+
  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
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_init (void)''
 
  <FONT color=gray>' ''int mpg123_init (void)''
Riga 21: Riga 36:
 
  <FONT color=gray>' ''mpg123_handle * mpg123_new (const char *decoder, int *error)''
 
  <FONT color=gray>' ''mpg123_handle * mpg123_new (const char *decoder, int *error)''
 
  ' ''Create a handle with optional choice of decoder (named by a string)''</font>
 
  ' ''Create a handle with optional choice of decoder (named by a string)''</font>
  Private Extern mpg123_new(decoder As String, errorI As Integer) As Pointer
+
  Private Extern mpg123_new(decoder As String, perror As Pointer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_param (mpg123_handle * mh, Enum mpg123_parms type, long value, double fvalue)
 
  <FONT color=gray>' ''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.''</font>
 
  ' ''Set a specific parameter, For a specific mpg123_handle, using a parameter type key chosen From the mpg123_parms enumeration, To the specified value.''</font>
  Private Extern mpg123_param(mhP As Pointer, type As Integer, valueL As Long, fvalue As Float) As Integer
+
  Private Extern mpg123_param(mh As Pointer, type As Integer, valueL As Long, fvalue As Float) As Integer
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_open (mpg123_handle * mh, const char * path)''
 
  <FONT color=gray>' ''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.''</font>
 
  ' ''Open and prepare to decode the specified file by filesystem path. This does not open HTTP urls.''</font>
  Private Extern mpg123_open(mhP As Pointer, pathFile As String) As Integer
+
  Private Extern mpg123_open(mh As Pointer, pathFile As String) As Integer
 
   
 
   
 
  <FONT color=gray>' ''const char* mpg123_strerror (mpg123_handle * mh)''
 
  <FONT color=gray>' ''const char* mpg123_strerror (mpg123_handle * mh)''
 
  ' ''Give string describing what error has occured in the context of handle mh.''</font>
 
  ' ''Give string describing what error has occured in the context of handle mh.''</font>
  Private Extern mpg123_strerror(mhP As Pointer) As String
+
  Private Extern mpg123_strerror(mh As Pointer) As String
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_getformat (mpg123_handle * mh, long * rate, int * channels, int * encoding)''
 
  <FONT color=gray>' ''int mpg123_getformat (mpg123_handle * mh, long * rate, int * channels, int * encoding)''
 
  ' ''Get the current output format written to the addresses given.''</font>
 
  ' ''Get the current output format written to the addresses given.''</font>
  Private Extern mpg123_getformat(mhP As Pointer, ratP As Pointer, channP As Pointer, encodP As Pointer) As Integer
+
  Private Extern mpg123_getformat(mh As Pointer, ratP As Pointer, channP As Pointer, encodP As Pointer) As Integer
 +
 +
<FONT color=gray>' ''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.''</font>
 +
Private Extern mpg123_info(mh As Pointer, mi As Mpg123_frameinfo) As Integer
 +
 +
<FONT color=gray>' ''off_t mpg123_length (mpg123_handle * mh)''
 +
' ''Return, if possible, the full (expected) length of current track in samples.''</font>
 +
Private Extern mpg123_length(mh As Pointer) As Long
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_format_none (mpg123_handle * mh)''
 
  <FONT color=gray>' ''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.''</font>
 
  ' ''Configure a mpg123 handle to accept no output format at all, use before specifying supported formats with mpg123_format.''</font>
  Private Extern mpg123_format_none(mhP As Pointer) As Integer
+
  Private Extern mpg123_format_none(mh As Pointer) As Integer
 +
 +
<FONT color=gray>' ''int mpg123_format (mpg123_handle *mh, long rate, int channels, int encodings)''
 +
' ''Set the audio format support of a mpg123_handle in detail.''</font>
 +
  Private Extern mpg123_format(mh As Pointer, rate As Long, channels As Integer, encodings As Integer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''size_t mpg123_outblock (mpg123_handle * mh)''
 
  <FONT color=gray>' ''size_t mpg123_outblock (mpg123_handle * mh)''
 
  ' ''The max size of one frame's decoded output with current settings.''
 
  ' ''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.''</font>
 
  ' ''Use that to determine an appropriate minimum buffer size for decoding one frame.''</font>
  Private Extern mpg123_outblock(mhP As Pointer) As Integer
+
  Private Extern mpg123_outblock(mh As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_read (mpg123_handle * mh, unsigned char * outmemory, size_t outmemsize, size_t * done)''
 
  <FONT color=gray>' ''int mpg123_read (mpg123_handle * mh, unsigned char * outmemory, size_t outmemsize, size_t * done)''
 
  ' ''Read from stream and decode up to outmemsize bytes.''</font>
 
  ' ''Read from stream and decode up to outmemsize bytes.''</font>
  Private Extern mpg123_read(mhP As Pointer, outmemory As Pointer, outmemsize As Integer, done As Pointer) As Integer
+
  Private Extern mpg123_read(mh As Pointer, outmemory As Pointer, outmemsize As Integer, done As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''int mpg123_close (mpg123_handle * mh)''
 
  <FONT color=gray>' ''int mpg123_close (mpg123_handle * mh)''
 
  ' ''Closes the source, if libmpg123 opened it''</font>
 
  ' ''Closes the source, if libmpg123 opened it''</font>
  Private Extern mpg123_close(mhP As Pointer) As Integer
+
  Private Extern mpg123_close(mh As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''void mpg123_delete (mpg123_handle * mh)''
 
  <FONT color=gray>' ''void mpg123_delete (mpg123_handle * mh)''
 
  ' ''Delete handle, mh is either a valid mpg123 handle or NULL''</font>
 
  ' ''Delete handle, mh is either a valid mpg123 handle or NULL''</font>
  Private Extern mpg123_delete(mhP As Pointer)
+
  Private Extern mpg123_delete(mh As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''void mpg123_exit (void)''
 
  <FONT color=gray>' ''void mpg123_exit (void)''
Riga 96: Riga 123:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
 +
  Dim filemp3 As String
 
   Dim err, channels, encoding, size As Integer
 
   Dim err, channels, encoding, size As Integer
 
   Dim somma, buffer_size, frames As Integer
 
   Dim somma, buffer_size, frames As Integer
 
   Dim frequenza As Long
 
   Dim frequenza As Long
   Dim mh, buffer, handle As Pointer
+
   Dim inf As New Mpg123_frameinfo
 +
  Dim mp, buffer, handle As Pointer
 
    
 
    
 
  <FONT color=gray>' ''Inizializza la liberia di "mpg123":''</font>
 
  <FONT color=gray>' ''Inizializza la liberia di "mpg123":''</font>
  err = mpg123_init()
+
  err = mpg123_init()
  If err <> MPG123_OK Then Error.Raise("Configurazione di base errata ! " & mpg123_plain_strerror(err))
+
  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)
+
  mpg123_param(mp, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0)
 
    
 
    
  mh = mpg123_new(Null, 0)
+
  mp = mpg123_new(Null, 0)
 +
 
 +
  filemp3 = "<FONT color=darkgreen>''/percorso/del/file.mp3''</font>"
 
   
 
   
 
  <FONT color=gray>' ''Apre il file mpeg in lettura:''</font>
 
  <FONT color=gray>' ''Apre il file mpeg in lettura:''</font>
  err = mpg123_open(mh, "<FONT color=gray>''percorso/del/file.mp3''</font>")
+
  err = mpg123_open(mp, filemp3)
  If err <> MPG123_OK Then Error.Raise("Attenzione ! Problemi con 'mpg123': " & mpg123_strerror(mh))
+
  If err <> MPG123_OK Then Error.Raise("Attenzione ! Problemi con 'mpg123': " & mpg123_strerror(mp))
 
    
 
    
  mpg123_getformat(mh, VarPtr(frequenza), VarPtr(channels), VarPtr(encoding))
+
  mpg123_getformat(mp, VarPtr(frequenza), VarPtr(channels), VarPtr(encoding))
   
+
   
   mpg123_format_none(mh)
+
  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(mh, frequenza, channels, encoding)
+
  mpg123_format(mp, frequenza, channels, encoding)
 
    
 
    
 
  <FONT color=gray>' ''Si inizializza il buffer:''</font>
 
  <FONT color=gray>' ''Si inizializza il buffer:''</font>
  buffer_size = mpg123_outblock(mh)
+
  buffer_size = mpg123_outblock(mp)
  <FONT color=orange>'''buffer'''</font> = Alloc(buffer_size)
+
  <FONT color=orange>'''buffer'''</font> = Alloc(SizeOf(gb.Byte), buffer_size)
 
   
 
   
 
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
  <FONT color=gray>' ''Qui inizia la parte relativa alle funzioni di Alsa:''</font>
 
  <FONT color=gray>' ''Qui inizia la parte relativa alle funzioni di Alsa:''</font>
 
   
 
   
  err = snd_pcm_open(VarPtr(handle), "default", SND_PCM_STREAM_PLAYBACK, 0)
+
  err = snd_pcm_open(VarPtr(handle), "default", SND_PCM_STREAM_PLAYBACK, 0)
  If err < 0 Then Error.Raise("Playback open error: " & snd_strerror(err))
+
  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)
+
  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 err < 0 Then Error.Raise("Playback open error: " & snd_strerror(err))
 
   
 
   
  If encoding And MPG123_ENC_SIGNED_16 Then
+
  If encoding And MPG123_ENC_SIGNED_16 Then
    size = SizeOf(gb.Integer)
+
    size = SizeOf(gb.Integer)
  Else
+
  Else
    size = SizeOf(gb.Float)
+
    size = SizeOf(gb.Float)
  Endif
+
  Endif
 
    
 
    
 
  <FONT color=gray>' ''Ha inizio il ciclo di lettura ed i invio dei dati audio:''</font>
 
  <FONT color=gray>' ''Ha inizio il ciclo di lettura ed i invio dei dati audio:''</font>
  Do
+
  Repeat
 
  <FONT color=gray>' ''Si leggono i dati audio e si riempie il "buffer":''</font>
 
  <FONT color=gray>' ''Si leggono i dati audio e si riempie il "buffer":''</font>
    err = mpg123_read(mh, <FONT color=orange>'''buffer'''</font>, buffer_size, VarPtr(buffer_size))
+
    err = mpg123_read(mp, <FONT color=orange>'''buffer'''</font>, 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))
 
  <FONT color=gray>' ''I dati audio vengono inviati al sub-sistema "PCM" di Alsa:''</font>
 
  <FONT color=gray>' ''I dati audio vengono inviati al sub-sistema "PCM" di Alsa:''</font>
    frames = snd_pcm_writei(handle, <FONT color=orange>'''buffer'''</font>, buffer_size / size)
+
    frames = snd_pcm_writei(handle, <FONT color=orange>'''buffer'''</font>, buffer_size / size)
    If (frames < 0) Then Error.Raise("Errore alla funzione 'snd_pcm_writei': " & snd_strerror(err))
+
    If frames < 0 Then Error.Raise("Errore alla funzione 'snd_pcm_writei': " & snd_strerror(frames))
   
+
    somma += frames
    somma += frames
+
    Write "\r\e[0mTempo trascorso: \e[31m" & Str(Time(0, 0, 0, (somma / frequenza) * 1000))
    Write "\r" & CStr(Date(0, 0, 0, 0, 0, 0, (somma / frequenza) * 1000))
+
  Until err = MPG123_DONE
     
 
  Loop While err = MPG123_OK
 
 
   
 
   
 
   
 
   
  <FONT color=gray>' ''Chiude l'handle ed esce dalla libreria di "mpg123":''</font>
+
  <FONT color=gray>' ''Libera la memoria precedentemente occupata:''</font>
  mpg123_close(mh)
+
  snd_pcm_close(handle)
  mpg123_delete(mh)
+
  Free(buffer)
  mpg123_exit()
+
  mpg123_close(mp)
    
+
   mpg123_delete(mp)
<FONT color=gray>' ''Chiude infine anche l'handle della libreria Alsa:''</font>
+
  mpg123_exit()
  snd_pcm_close(handle)
+
 
+
  End
  '''End'''
 
 
 
  
  

Versione attuale delle 17:12, 13 gen 2024

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

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"

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