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

Da Gambas-it.org - Wikipedia.
Riga 4: Riga 4:
  
 
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:
  '''Public''' Struct SF_INFO
+
  Public Struct SF_INFO
 
   frames As Long
 
   frames As Long
 
   samplerate As Integer
 
   samplerate As Integer
Riga 11: Riga 11:
 
   sections As Integer
 
   sections As Integer
 
   seekable As Integer
 
   seekable As Integer
  '''End''' Struct
+
  End Struct
 
   
 
   
  '''Private''' Const MPG123_OK As Byte = 0
+
  Private Const MPG123_OK As Byte = 0
  '''Private''' Const MPG123_ADD_FLAGS As Byte = 2
+
  Private Const MPG123_ADD_FLAGS As Byte = 2
  '''Private''' Const MPG123_FORCE_FLOAT As Short = 1024
+
  Private Const MPG123_FORCE_FLOAT As Short = 1024
  '''Private''' Const MPG123_ENC_SIGNED_16 As Byte = 208
+
  Private Const MPG123_ENC_SIGNED_16 As Byte = 208
 
   
 
   
 
  Library "<FONT color=blue>libmpg123:0.36.6</font>"
 
  Library "<FONT color=blue>libmpg123:0.36.6</font>"
 
   
 
   
  <FONT color=gray>' ''int mpg123_init (void)  --> Function to initialise the mpg123 library.''</font>
+
  <FONT color=gray>' ''int mpg123_init (void)''
  '''Private''' Extern mpg123_init() As Integer
+
  ' ''Function to initialise the mpg123 library.''</font>
 +
  Private Extern mpg123_init() As Integer
 
   
 
   
  <FONT color=gray>' ''const char* mpg123_plain_strerror   (int errcode)  --> Return a string describing that error errcode means.''</font>
+
  <FONT color=gray>' ''const char* mpg123_plain_strerror (int errcode)''
  '''Private''' Extern mpg123_plain_strerror(errcode As Integer) As String
+
  ' ''Return a string describing that error errcode means.''</font>
 +
  Private''' Extern mpg123_plain_strerror(errcode As Integer) As String
 
   
 
   
  <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>
+
  <FONT color=gray>' ''mpg123_handle *, mpg123_new (const char *decoder, int *error)''
  '''Private''' Extern mpg123_new(decoder As String, errorI As Integer) As Pointer
+
  ' ''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
 
   
 
   
  <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(mhP 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)  --> Open and prepare to decode the specified file by filesystem path. This does not open HTTP urls.''</font>
+
  <FONT color=gray>' ''int mpg123_open (mpg123_handle * mh, const char * path)''
  '''Private''' Extern mpg123_open(mhP As Pointer, pathFile As String) As Integer
+
  ' ''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
 
   
 
   
  <FONT color=gray>' ''const char* mpg123_strerror   (mpg123_handle * mh)  --> Give string describing what error has occured in the context of handle mh.''</font>
+
  <FONT color=gray>' ''const char* mpg123_strerror (mpg123_handle * mh)''
  '''Private''' Extern mpg123_strerror(mhP As Pointer) As String
+
  ' ''Give string describing what error has occured in the context of handle mh.''</font>
 +
  Private Extern mpg123_strerror(mhP As Pointer) As String
 
   
 
   
  <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>
+
  <FONT color=gray>' ''int mpg123_getformat (mpg123_handle * mh, long * rate, Int * channels, Int * encoding)''
  '''Private''' Extern mpg123_getformat(mhP As Pointer, ratP As Pointer, channP As Pointer, encodP As Pointer) As Integer
+
' ''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
 
   
 
   
  <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>
+
  <FONT color=gray>' ''int mpg123_format_none (mpg123_handle * mh)''
  '''Private''' Extern mpg123_format_none(mhP As Pointer) As Integer
+
  ' ''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
 
   
 
   
  <FONT color=gray>' ''size_t mpg123_outblock   (mpg123_handle * mh)  --> The max size of one frame's decoded output with current settings.''
+
  <FONT color=gray>' ''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.''</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(mhP As Pointer) As Integer
 
   
 
   
  <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>
+
  <FONT color=gray>' ''int mpg123_read  (mpg123_handle * mh, unsigned char * outmemory, size_t outmemsize, size_t * done)''
  '''Private''' Extern mpg123_read(mhP As Pointer, outmemory As Pointer, outmemsize As Integer, done As Pointer) As Integer
+
' ''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
 
   
 
   
  <FONT color=gray>' ''int mpg123_close  (mpg123_handle * mh)  --> Closes the source, if libmpg123 opened it''</font>
+
  <FONT color=gray>' ''int mpg123_close  (mpg123_handle * mh)''
  '''Private''' Extern mpg123_close(mhP As Pointer) As Integer
+
  ' ''Closes the source, if libmpg123 opened it''</font>
 +
  Private Extern mpg123_close(mhP As Pointer) As Integer
 
   
 
   
  <FONT color=gray>' ''void mpg123_delete  (mpg123_handle * mh)  --> Delete handle, mh is either a valid mpg123 handle or NULL''</font>
+
  <FONT color=gray>' ''void mpg123_delete  (mpg123_handle * mh)''
  '''Private''' Extern mpg123_delete(mhP As Pointer)
+
  ' ''Delete handle, mh is either a valid mpg123 handle or NULL''</font>
 +
  Private Extern mpg123_delete(mhP As Pointer)
 
   
 
   
  <FONT color=gray>' ''void mpg123_exit (void)  --> Function to close down the mpg123 library.''</font>
+
  <FONT color=gray>' ''void mpg123_exit (void)''
  '''Private''' Extern mpg123_exit()
+
  ' ''Function to close down the mpg123 library.''</font>
 +
  Private Extern mpg123_exit()
 
   
 
   
 
   
 
   

Versione delle 04:09, 27 ott 2015

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:

Public Struct SF_INFO
 frames As Long
 samplerate As Integer
 channels As Integer
 formatI As Integer
 sections As Integer
 seekable As Integer
End Struct

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

Library "libmpg123:0.36.6"

' 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_ACCESS_RW_INTERLEAVED As Byte = 3
Private Const SND_PCM_FORMAT_U8 As Byte = 1
Private Const nomen As String = "default"


' int   snd_pcm_open(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode)
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 aPrivate Const MPG123_ADD_FLAGS As Byte = 2 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)
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)
Private Extern snd_pcm_close(pcmP As Pointer)



Public Sub Form_Open()

End


Public Sub Button1_Click()
 
 Dim err, channels, encoding, buffer_size, frames As Integer
 Dim rate 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(rate), VarPtr(channels), VarPtr(encoding))

  mpg123_format_none(mh)

  mpg123_format(mh, rate, 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), nomen, 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, rate, 1, 500000)
     If err < 0 Then Error.Raise("Playback open error: " & snd_strerror(err))

' 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:
     If encoding And MPG123_ENC_SIGNED_16 Then
       frames = snd_pcm_writei(handle, buffer, buffer_size / SizeOf(gb.Integer))
       If (frames < 0) Then Error.Raise("Errore alla funzione 'snd_pcm_writei': " & snd_strerror(err))
     Else
       frames = snd_pcm_writei(handle, buffer, buffer_size / SizeOf(gb.Float))
       If (frames < 0) Then Error.Raise("Errore alla funzione 'snd_pcm_writei': " & snd_strerror(err))
     Endif
   
' Si pone un brevissimo "Wait" per impedire che l'esecuzione audio blocchi l'uso di eventuali oggetti posti sul "Form":
     Wait 0.005

  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