Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux ALSA lib please test it with your Linux distro.
#1
EDIT: one ptr on ptr changed!

The FBSOUND lib will support Windows and Linux the Win part use DirectX or MMSystem currently i try to switch from Linux OSS (emulation mode) to the ALSA lib.

Please post here your result from test1.bas if you get an error message please post your kernel version 2.4 or 2.6. and ALSA version too.
thank you all for help.

Joshy

ALSA Lib Docu.
http://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html

Joshy
alsa.bi
Code:
option explicit

#define __LITTLE_ENDIAN
#define EPIPE 32

' PCM access type
enum snd_pcm_access_t
  ' mmap access with simple interleaved channels
  SND_PCM_ACCESS_MMAP_INTERLEAVED = 0
  ' mmap access with simple non interleaved channels
  SND_PCM_ACCESS_MMAP_NONINTERLEAVED
  ' mmap access with complex placement
  SND_PCM_ACCESS_MMAP_COMPLEX
  ' snd_pcm_readi/snd_pcm_writei access
  SND_PCM_ACCESS_RW_INTERLEAVED
  ' snd_pcm_readn/snd_pcm_writen access
  SND_PCM_ACCESS_RW_NONINTERLEAVED
end enum

' PCM state
enum snd_pcm_state_t
  ' Open
  SND_PCM_STATE_OPEN = 0
  ' Setup installed
  SND_PCM_STATE_SETUP
  ' Ready to start
  SND_PCM_STATE_PREPARED
  ' Running
  SND_PCM_STATE_RUNNING
  ' Stopped: underrun (playback) or overrun (capture) detected
  SND_PCM_STATE_XRUN
  ' Draining: running (playback) or stopped (capture)
  SND_PCM_STATE_DRAINING
  ' Paused
  SND_PCM_STATE_PAUSED
  ' Hardware is suspended
  SND_PCM_STATE_SUSPENDED
  ' Hardware is disconnected
  SND_PCM_STATE_DISCONNECTED
end enum

' PCM sample format
enum  snd_pcm_format_t
  ' Unknown
  SND_PCM_FORMAT_UNKNOWN = -1
  ' Signed 8 bit */
  SND_PCM_FORMAT_S8 = 0
  ' Unsigned 8 bit
  SND_PCM_FORMAT_U8,
  ' Signed 16 bit Little Endian
  SND_PCM_FORMAT_S16_LE
  ' Signed 16 bit Big Endian
  SND_PCM_FORMAT_S16_BE
  ' Unsigned 16 bit Little Endian
  SND_PCM_FORMAT_U16_LE
  ' Unsigned 16 bit Big Endian
  SND_PCM_FORMAT_U16_BE
  ' Signed 24 bit Little Endian
  SND_PCM_FORMAT_S24_LE
  ' Signed 24 bit Big Endian
  SND_PCM_FORMAT_S24_BE
  ' Unsigned 24 bit Little Endian
  SND_PCM_FORMAT_U24_LE
  ' Unsigned 24 bit Big Endian
  SND_PCM_FORMAT_U24_BE
  ' Signed 32 bit Little Endian
  SND_PCM_FORMAT_S32_LE
  ' Signed 32 bit Big Endian
  SND_PCM_FORMAT_S32_BE
  ' Unsigned 32 bit Little Endian
  SND_PCM_FORMAT_U32_LE
  ' Unsigned 32 bit Big Endian
  SND_PCM_FORMAT_U32_BE
  ' Float 32 bit Little Endian, Range -1.0 to 1.0
  SND_PCM_FORMAT_FLOAT_LE
  ' Float 32 bit Big Endian, Range -1.0 to 1.0
  SND_PCM_FORMAT_FLOAT_BE
  ' Float 64 bit Little Endian, Range -1.0 to 1.0
  SND_PCM_FORMAT_FLOAT64_LE
  ' Float 64 bit Big Endian, Range -1.0 to 1.0
  SND_PCM_FORMAT_FLOAT64_BE
  ' IEC-958 Little Endian
  SND_PCM_FORMAT_IEC958_SUBFRAME_LE
  ' IEC-958 Big Endian
  SND_PCM_FORMAT_IEC958_SUBFRAME_BE
  ' Mu-Law
  SND_PCM_FORMAT_MU_LAW
  ' A-Law
  SND_PCM_FORMAT_A_LAW
  ' Ima-ADPCM
  SND_PCM_FORMAT_IMA_ADPCM
  ' MPEG
  SND_PCM_FORMAT_MPEG
  ' GSM
  SND_PCM_FORMAT_GSM
  ' Special
  SND_PCM_FORMAT_SPECIAL = 31
  ' Signed 24bit Little Endian in 3bytes format
  SND_PCM_FORMAT_S24_3LE = 32
  ' Signed 24bit Big Endian in 3bytes format
  SND_PCM_FORMAT_S24_3BE
  ' Unsigned 24bit Little Endian in 3bytes format
  SND_PCM_FORMAT_U24_3LE
  ' Unsigned 24bit Big Endian in 3bytes format
  SND_PCM_FORMAT_U24_3BE
  ' Signed 20bit Little Endian in 3bytes format
  SND_PCM_FORMAT_S20_3LE
  ' Signed 20bit Big Endian in 3bytes format
  SND_PCM_FORMAT_S20_3BE
  ' Unsigned 20bit Little Endian in 3bytes format
  SND_PCM_FORMAT_U20_3LE
  ' Unsigned 20bit Big Endian in 3bytes format
  SND_PCM_FORMAT_U20_3BE
  ' Signed 18bit Little Endian in 3bytes format
  SND_PCM_FORMAT_S18_3LE
  ' Signed 18bit Big Endian in 3bytes format
  SND_PCM_FORMAT_S18_3BE
  ' Unsigned 18bit Little Endian in 3bytes format
  SND_PCM_FORMAT_U18_3LE
  ' Unsigned 18bit Big Endian in 3bytes format
  SND_PCM_FORMAT_U18_3BE


#ifdef __LITTLE_ENDIAN
  ' Signed 16 bit CPU endian
  SND_PCM_FORMAT_S16 = SND_PCM_FORMAT_S16_LE
  ' Unsigned 16 bit CPU endian
  SND_PCM_FORMAT_U16 = SND_PCM_FORMAT_U16_LE
  ' Signed 24 bit CPU endian
  SND_PCM_FORMAT_S24 = SND_PCM_FORMAT_S24_LE
  ' Unsigned 24 bit CPU endian
  SND_PCM_FORMAT_U24 = SND_PCM_FORMAT_U24_LE
  ' Signed 32 bit CPU endian
  SND_PCM_FORMAT_S32 = SND_PCM_FORMAT_S32_LE
  ' Unsigned 32 bit CPU endian
  SND_PCM_FORMAT_U32 = SND_PCM_FORMAT_U32_LE
  ' Float 32 bit CPU endian
  SND_PCM_FORMAT_FLOAT = SND_PCM_FORMAT_FLOAT_LE
  ' Float 64 bit CPU endian
  SND_PCM_FORMAT_FLOAT64 = SND_PCM_FORMAT_FLOAT64_LE
  ' IEC-958 CPU Endian
  SND_PCM_FORMAT_IEC958_SUBFRAME = SND_PCM_FORMAT_IEC958_SUBFRAME_LE
#else
  ' Signed 16 bit CPU endian
  SND_PCM_FORMAT_S16 = SND_PCM_FORMAT_S16_BE
  ' Unsigned 16 bit CPU endian
  SND_PCM_FORMAT_U16 = SND_PCM_FORMAT_U16_BE
  ' Signed 24 bit CPU endian
  SND_PCM_FORMAT_S24 = SND_PCM_FORMAT_S24_BE
  ' Unsigned 24 bit CPU endian
  SND_PCM_FORMAT_U24 = SND_PCM_FORMAT_U24_BE
  ' Signed 32 bit CPU endian
  SND_PCM_FORMAT_S32 = SND_PCM_FORMAT_S32_BE
  ' Unsigned 32 bit CPU endian
  SND_PCM_FORMAT_U32 = SND_PCM_FORMAT_U32_BE
  ' Float 32 bit CPU endian
  SND_PCM_FORMAT_FLOAT = SND_PCM_FORMAT_FLOAT_BE
  ' Float 64 bit CPU endian
  SND_PCM_FORMAT_FLOAT64 = SND_PCM_FORMAT_FLOAT64_BE
  ' IEC-958 CPU Endian
  SND_PCM_FORMAT_IEC958_SUBFRAME = SND_PCM_FORMAT_IEC958_SUBFRAME_BE
#endif
end enum

enum snd_pcm_stream_t
  ' Playback stream
  SND_PCM_STREAM_PLAYBACK = 0
  ' Capture stream
  SND_PCM_STREAM_CAPTURE
end enum

' Non blocking mode (flag for open mode)
#define SND_PCM_NONBLOCK  &H0001
' Async notification (flag for open mode)
#define SND_PCM_ASYNC     &H0002

type snd_pcm_t           as any ptr
type snd_pcm_hw_params_t as any ptr
type snd_pcm_sw_params_t as any ptr

' hardware
declare function snd_pcm_hw_params_malloc cdecl alias "snd_pcm_hw_params_malloc" ( _
byval lp_hw_params as snd_pcm_hw_params_t ptr) as integer

declare sub snd_pcm_hw_params_free   cdecl alias "snd_pcm_hw_params_free" ( _
byval hw_params    as snd_pcm_hw_params_t)

declare function snd_pcm_hw_params_any    cdecl alias "snd_pcm_hw_params_any" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t) as integer

declare function snd_pcm_hw_params_set_access cdecl alias "snd_pcm_hw_params_set_access" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval access       as snd_pcm_access_t) as integer

declare function snd_pcm_hw_params_set_format cdecl alias "snd_pcm_hw_params_set_format" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval format       as snd_pcm_format_t) as integer

declare function snd_pcm_hw_params_get_rate cdecl alias "snd_pcm_hw_params_get_rate" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_get_rate_min cdecl alias "snd_pcm_hw_params_get_rate_min" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_get_rate_max cdecl alias "function snd_pcm_hw_params_get_rate_max" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_test_rate cdecl alias "snd_pcm_hw_params_test_rate" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger, _
byval direction    as  integer) as integer

declare function snd_pcm_hw_params_set_rate cdecl alias "snd_pcm_hw_params_set_rate" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger, _
byval direction    as  integer) as integer

declare function snd_pcm_hw_params_set_rate_min cdecl alias "snd_pcm_hw_params_set_rate_min" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_max cdecl alias "snd_pcm_hw_params_set_rate_max" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_minmax cdecl alias "snd_pcm_hw_params_set_rate_minmax" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval min as uinteger ptr,byval mindir as integer ptr, _
byval max as uinteger ptr,byval maxdir as integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_near cdecl alias "snd_pcm_hw_params_set_rate_near" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_first cdecl alias "snd_pcm_hw_params_set_rate_first" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_last cdecl alias "snd_pcm_hw_params_set_rate_last" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr, _
byval direction    as  integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_resample cdecl alias "snd_pcm_hw_params_set_rate_resample" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger) as integer

declare function snd_pcm_hw_params_get_rate_resample cdecl alias "snd_pcm_hw_params_get_rate_resample" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval rate         as uinteger ptr) as integer

declare function snd_pcm_hw_params_get_channels  cdecl alias "snd_pcm_hw_params_get_channels" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_get_channels_min cdecl alias "snd_pcm_hw_params_get_channels_min" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_get_channels_max cdecl alias "snd_pcm_hw_params_get_channels_max" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_test_channels cdecl alias "snd_pcm_hw_params_test_channels" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger) as integer

declare function snd_pcm_hw_params_set_channels cdecl alias "snd_pcm_hw_params_set_channels" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger) as integer

declare function snd_pcm_hw_params_set_channels_min cdecl alias "snd_pcm_hw_params_set_channels_min" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_set_channels_max cdecl alias "snd_pcm_hw_params_set_channels_max" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_set_channels_minmax cdecl alias "snd_pcm_hw_params_set_channels_minmax" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval min   as uinteger ptr, _
byval max   as uinteger ptr) as integer

declare function snd_pcm_hw_params_set_channels_near cdecl alias "snd_pcm_hw_params_set_channels_near" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_set_channels_firstcdecl alias "snd_pcm_hw_params_set_channels_firstcdecl" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_set_channels_last cdecl alias "snd_pcm_hw_params_set_channels_last" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t, _
byval n_channels   as uinteger ptr) as integer

declare function snd_pcm_hw_params_current cdecl alias "snd_pcm_hw_params_current" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t) as integer

declare function snd_pcm_hw_params cdecl alias "snd_pcm_hw_params" ( _
byval pcm          as snd_pcm_t, _
byval hw_params    as snd_pcm_hw_params_t) as integer

' PCM timestamp mode
enum snd_pcm_tstamp_t
  ' No timestamp
  SND_PCM_TSTAMP_NONE = 0
  ' Update mmap'ed timestamp
  SND_PCM_TSTAMP_MMAP
end enum

' Unsigned frames quantity
type snd_pcm_uframes_t as uinteger
' Signed frames quantity
type snd_pcm_sframes_t as integer
type snd_pcm_status_t as any ptr '!!!

' software
declare function snd_pcm_sw_params_sizeof cdecl alias "snd_pcm_sw_params_sizeof" () as integer

declare function snd_pcm_sw_params_malloc cdecl alias "snd_pcm_sw_params_malloc" ( _
byval lp_sw_params as snd_pcm_sw_params_t ptr) as integer

declare sub snd_pcm_sw_params_free cdecl alias "snd_pcm_sw_params_free" ( _
byval sw_params    as snd_pcm_sw_params_t)

declare sub snd_pcm_sw_params_copy cdecl alias "snd_pcm_sw_params_copy" ( _
byval des_swparams as snd_pcm_sw_params_t, _
byval src_swparams as snd_pcm_sw_params_t)

declare function snd_pcm_sw_params_get_boundary cdecl alias "snd_pcm_sw_params_get_boundary" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_set_tstamp_mode cdecl alias "snd_pcm_sw_params_set_tstamp_mode" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_tstamp_t) as integer

declare function snd_pcm_sw_params_get_tstamp_mode  cdecl alias "snd_pcm_sw_params_get_tstamp_mode" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_tstamp_t ptr) as integer

declare function snd_pcm_sw_params_set_sleep_min cdecl alias "snd_pcm_sw_params_set_sleep_min" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as uinteger) as integer

declare function snd_pcm_sw_params_get_sleep_min cdecl alias "snd_pcm_sw_params_get_sleep_min" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as uinteger ptr) as integer

declare function snd_pcm_sw_params_set_avail_min cdecl alias "snd_pcm_sw_params_set_avail_min" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t) as integer

declare function snd_pcm_sw_params_get_avail_min cdecl alias "snd_pcm_sw_params_get_avail_min" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_set_xfer_align cdecl alias "snd_pcm_sw_params_set_xfer_align" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t) as integer

declare function snd_pcm_sw_params_get_xfer_align cdecl alias "snd_pcm_sw_params_get_xfer_align" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_set_start_threshold cdecl alias "snd_pcm_sw_params_set_start_threshold" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t) as integer

declare function snd_pcm_sw_params_get_start_threshold cdecl alias "snd_pcm_sw_params_get_start_threshold" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_set_stop_threshold cdecl alias "snd_pcm_sw_params_set_stop_threshold" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t) as integer

declare function snd_pcm_sw_params_get_stop_threshold cdecl alias "snd_pcm_sw_params_get_stop_threshold" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_set_silence_threshold cdecl alias "snd_pcm_sw_params_set_silence_threshold" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t) as integer

declare function snd_pcm_sw_params_get_silence_threshold cdecl alias "snd_pcm_sw_params_get_silence_threshold" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_set_silence_size cdecl alias "snd_pcm_sw_params_set_silence_size" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t) as integer

declare function snd_pcm_sw_params_get_silence_size cdecl alias "snd_pcm_sw_params_get_silence_size" ( _
byval sw_params    as snd_pcm_sw_params_t, _
byval value        as snd_pcm_uframes_t ptr) as integer

declare function snd_pcm_sw_params_current cdecl alias "snd_pcm_sw_params_current" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t) as integer

declare function snd_pcm_sw_params cdecl alias "snd_pcm_sw_params" ( _
byval pcm          as snd_pcm_t, _
byval sw_params    as snd_pcm_sw_params_t) as integer

' pcm device
declare function snd_pcm_open cdecl alias "snd_pcm_open"  ( _
byval pcm          as snd_pcm_t ptr,_
byval device       as string,_
byval stream       as snd_pcm_stream_t,_
byval mode         as integer) as integer

declare function snd_pcm_close cdecl alias "snd_pcm_close" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_prepare cdecl alias "snd_pcm_prepare" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_reset cdecl alias "snd_pcm_reset" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_status cdecl alias "snd_pcm_status" ( _
byval pcm          as snd_pcm_t, _
byval status       as snd_pcm_status_t) as integer

declare function snd_pcm_start cdecl alias "snd_pcm_start" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_drop cdecl alias "snd_pcm_drop" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_drain cdecl alias "snd_pcm_drain" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_pause cdecl alias "snd_pcm_pause" ( _
byval pcm          as snd_pcm_t, _
byval enable       as integer) as integer

declare function snd_pcm_state cdecl alias "snd_pcm_state" ( _
byval pcm          as snd_pcm_t) as snd_pcm_state_t

declare function snd_pcm_hwsync cdecl alias "snd_pcm_hwsync" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_delay cdecl alias "snd_pcm_delay" ( _
byval pcm          as snd_pcm_t, _
byval delayp       as snd_pcm_sframes_t ptr) as integer

declare function snd_pcm_resume cdecl alias "snd_pcm_resume" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_pcm_avail_update cdecl alias "snd_pcm_avail_update" ( _
byval pcm          as snd_pcm_t) as snd_pcm_sframes_t

declare function snd_pcm_rewind cdecl alias "snd_pcm_rewind" ( _
byval pcm          as snd_pcm_t, _
byval frames as snd_pcm_uframes_t) as snd_pcm_sframes_t

declare function  snd_pcm_forward cdecl alias "snd_pcm_forward" ( _
byval pcm          as snd_pcm_t, _
byval frames       as snd_pcm_uframes_t) as snd_pcm_sframes_t

declare function snd_pcm_writei cdecl alias "snd_pcm_writei" ( _
byval pcm          as snd_pcm_t, _
byval buffer       as any ptr, _
byval size         as snd_pcm_uframes_t) as snd_pcm_sframes_t

declare function snd_pcm_readi cdecl alias "snd_pcm_readi" ( _
byval pcm          as snd_pcm_t, _
byval buffer       as any ptr, _
byval size         as snd_pcm_uframes_t) as snd_pcm_sframes_t

declare function snd_pcm_writen cdecl alias "snd_pcm_writen" ( _
byval pcm          as snd_pcm_t, _
byval bufs         as any ptr ptr, _
byval size         as snd_pcm_uframes_t) as snd_pcm_sframes_t

declare function snd_pcm_readn cdecl alias "snd_pcm_readn" ( _
byval pcm          as snd_pcm_t, _
byval bufs         as any ptr ptr, _
byval size         as snd_pcm_uframes_t) as snd_pcm_sframes_t

declare function snd_pcm_wait cdecl alias "snd_pcm_wait" ( _
byval pcm          as snd_pcm_t, _
byval timeout      as integer) as integer

declare function snd_pcm_link cdecl alias "snd_pcm_link" ( _
byval pcm1         as snd_pcm_t, _
byval pcm2         as snd_pcm_t) as integer

declare function snd_pcm_unlink cdecl alias "snd_pcm_unlink" ( _
byval pcm          as snd_pcm_t) as integer

declare function snd_strerror cdecl alias "snd_strerror" ( _
byval errnum       as integer) as zstring ptr

declare function snd_asoundlib_version cdecl alias "snd_asoundlib_version" () as zstring ptr

function strError(errnum) as string
  dim as zstring ptr cstr
  dim as string ret
  cstr=snd_strerror(errnum)
  ret=*cstr
  return ret
end function

function strVersion() as string
  dim as zstring ptr cstr
  dim as string ret
  cstr=snd_asoundlib_version()
  ret=*cstr
  return ret
end function

type snd_async_handler_t  as any ptr
type snd_async_callback_t as sub (byval ahandler as snd_async_handler_t)

declare function snd_async_add_handler cdecl alias "snd_async_add_handler" ( _
byval handler      as snd_async_handler_t ptr, _
byval fd           as integer, _
byval callback     as snd_async_callback_t, _
byval private_data as any ptr) as integer

declare function snd_async_add_pcm_handler cdecl alias "snd_async_add_pcm_handler" ( _
byval handler      as snd_async_handler_t ptr, _
byval fd           as integer, _
byval callback     as snd_async_callback_t, _
byval private_data as any ptr) as integer

declare function snd_async_handler_get_pcm cdecl alias "snd_async_handler_get_pcm" ( _
byval handler      as snd_async_handler_t) as snd_pcm_t

declare function snd_async_del_handler cdecl alias "snd_async_del_handler" ( _
byval handler      as snd_async_handler_t) as integer

declare function snd_async_handler_get_callback_private cdecl alias "snd_async_handler_get_callback_private" ( _
byval handler      as snd_async_handler_t) as any ptr

declare function snd_async_handler_get_fd cdecl alias "snd_async_handler_get_fd" ( _
byval handler      as snd_async_handler_t) as integer

declare function snd_async_handler_get_signo cdecl alias "snd_async_handler_get_signo" ( _
byval handler      as snd_async_handler_t) as integer

#inclib "asound"
sorry about my english
Reply
#2
Try #1
Quote:leo@HERCULES:~/source$ fbc test1.bas
alsa.bi(377) : error 7: Expected ')'

declare function snd_pcm_sw_params_set_tstamp_mode cdecl alias "snd_pcm_sw_params_set_tstamp_mode" ( _
^

Solved by deleting row #377 (empty) in alsa.bi and save.

Try #2
Quote:leo@HERCULES:~/source$ ./test1
can't set channel count

Ubuntu 5.10 Breezy badger. Standard integrated soundcard (AC97?)
/post]
Reply
#3
Quote:
Quote:leo@HERCULES:~/source$ ./test1
can't set channel count

Hello red_Marvin thanx for the test
use the new testcode test1.bas and change nChannels 1 to 2
seams to be your card won't run in mono mode
and try it again.

thx Joshy
sorry about my english
Reply
#4
this version of "terst1.bas" you can change nSamples,nFRQ and nChannels.
example:
#define nSamples 2048
#define nFRQ 22025
#define nChannels 2

Joshy
Code:
#include "alsa.bi"

#define nSamples  8192
#define nFRQ      44100
#define nChannels 1

dim shared buf(nSamples) as short

function playback_callback(byval hPCM as snd_pcm_t,byval nFrames as integer) as integer
  static as single w1,w2,a
  dim as integer ret,i

  ? "playback callback called with " & str(nFrames) & " frames"
  ' ... fill buf with data
  w2+=0.1
  for i=0 to nSamples-1
    buf(i)=sin(w1)*5000:w1+=sin(w2)
  next
  ret=snd_pcm_writei(hPCM,@buf(0),nframes)
  if ret < 0 then ? "write failed "
  return ret
end function


dim as integer     ret
dim as string      device
dim as snd_pcm_t   hPlay
dim as snd_pcm_hw_params_t hwparams
dim as snd_pcm_sw_params_t swparams
dim as uinteger    channels,rate,frames_to_deliver

'device = "plughw:0,0"
device="hw:0,0"
if snd_pcm_open(@hPlay, device, SND_PCM_STREAM_PLAYBACK,SND_PCM_NONBLOCK)<0 then '
  ? "can't open device"
  sleep:end 1
end if

if snd_pcm_hw_params_malloc(@hwparams)<0 then
  snd_pcm_close(hPlay)
  ? "can't alloc hwparams"
  sleep:end 1
end if

if snd_pcm_hw_params_any(hPlay, hwparams) < 0 then
  snd_pcm_hw_params_free hwparams
  snd_pcm_close          hPlay
  ? "can't initialize hwparams"
  sleep:end 1
end if

if snd_pcm_hw_params_set_access(hPlay, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0 then
  snd_pcm_hw_params_free hwparams
  snd_pcm_close          hPlay
  ? "can't set hwparams access type"
  sleep:end 1
end if

if snd_pcm_hw_params_set_format(hPlay, hwparams, SND_PCM_FORMAT_S16) < 0 then
  snd_pcm_hw_params_free hwparams
  snd_pcm_close          hPlay
  ? "can't set sample format"
  sleep:end 1
end if

rate=nFRQ
if snd_pcm_hw_params_set_rate_near(hPlay, hwparams,@rate, 0) < 0 then
  snd_pcm_hw_params_free hwparams
  snd_pcm_close          hPlay
  ? "can't set sample rate"
  sleep:end 1
else
  ? rate
end if

channels=nChannels
if snd_pcm_hw_params_set_channels(hPlay, hwparams, channels) < 0 then 'mono
  snd_pcm_hw_params_free hwparams
  snd_pcm_close          hPlay
  ? "can't set channel count"
  sleep:end 1
else
  ? channels
end if

if snd_pcm_hw_params (hPlay, hwparams) < 0 then
  snd_pcm_hw_params_free hwparams
  snd_pcm_close          hPlay
  ? "can't set hwparams"
end if

snd_pcm_hw_params_free hwparams

' tell ALSA to wake us up whenever 4096 or more frames
' of playback data can be delivered. Also, tell
' ALSA that we'll start the device ourselves.
if snd_pcm_sw_params_malloc(@swparams) < 0 then
  ? "can't allocate swparams"
  snd_pcm_close          hPlay
  sleep:end 1
end if

if snd_pcm_sw_params_current(hPlay, swparams)< 0 then
  ? "can't initialize swparams"
  snd_pcm_close          hPlay
  sleep:end 1
end if

if snd_pcm_sw_params_set_avail_min(hPlay, swparams, nsamples)< 0 then
  ? "can't set minimum available count"
  snd_pcm_close          hPlay
  sleep:end 1
else
  ? nSamples
end if

if snd_pcm_sw_params_set_start_threshold(hPlay, swparams, 0) < 0 then
  ? "can't set start mode"
  snd_pcm_close          hPlay
  sleep:end 1
end if

if snd_pcm_sw_params(hPlay,swparams) < 0 then
  ? "can't set software parameters"
  snd_pcm_close          hPlay
  sleep:end 1
end if

' the interface will interrupt the kernel every nSamples frames, and ALSA
' will wake up this program very soon after that.
if snd_pcm_prepare(hPlay) < 0 then
  ? "can't prepare audio interface"
  snd_pcm_close          hPlay
  sleep:end 1
end if

while len(inkey)=0
  ' wait till the interface is ready for data, or 1 second has elapsed.
  if snd_pcm_wait(hPlay, 1000) < 0 then
    ? "poll failed "
    exit while
  end if
  ' find out how much space is available for playback data
  frames_to_deliver = snd_pcm_avail_update(hPlay)
  if frames_to_deliver < 0 then
    if (frames_to_deliver = -EPIPE) then
      ? "an xrun occured"
      exit while
    else
      ? "unknown ALSA avail update return value ";frames_to_deliver
      exit while
    end if
  end if

  if frames_to_deliver >nSamples then frames_to_deliver=nSamples
  ' deliver the data
  if playback_callback(hPlay,frames_to_deliver)<>frames_to_deliver then
    ? "playback callback failed"
    exit while
  end if
wend

snd_pcm_close hPlay

sleep
end
sorry about my english
Reply
#5
When using the original setup but changing nChannels to 2
Quote:#define nSamples 8192
#define nFRQ 44100
#define nChannels 2
the program mande a short beep.
mp3
[Image: 17008-t.png]
Waveshape


[Image: 17009-t.png]
Spectrum


[Image: 17010-t.png]
Toneheight



When using the other settings it made an occilating sound.
mp3
Quote:#define nSamples 2048
#define nFRQ 22025
#define nChannels 2
[Image: 17007-t.png]
Waveshape


[Image: 17009-t.png]
Spectrum


[Image: 17010-t.png]
Toneheight


There seem to be a clicking sound present especially in example #2,
I have no idea why though...
/post]
Reply
#6
hello red_Marvin again thx for testing.
i have setup 3 difrent boxes with linux and every box produce other results i don't know why i must read the whole ALSA doc's now.
I have compared my last translation of the C headers and can't find any problem.

Joshy
I will post my next work and hope you can help again.
sorry about my english
Reply
#7
I'll provide you with system info as far as I can, the program for I use for recording the sound is called audacity (and that's where the screenshots come from).
/post]
Reply
#8
Hello red_Marvin,
can you test this code too?

thx Joshy
Code:
option explicit

const EAGAIN                       = 11 ' Try again
const EPIPE                        = 32 ' Broken pipe
const ESTRPIPE                     = 86 ' Streams pipe error

const BLOCK                        = 0
const NONBLOCK                     = 1
const ASYNC                        = 2
const SND_PCM_STREAM_PLAYBACK      = 0
const SND_PCM_STREAM_CAPTURE       = 1
const SND_PCM_FORMAT_S16_LE        = 2
const SND_PCM_ACCESS_RW_INTERLEAVED= 3

type snd_pcm_t           as _snd_pcm
type snd_pcm_hw_params_t as _snd_pcm_hw_params

' PCM
declare function snd_pcm_open cdecl alias "snd_pcm_open" ( _
byval pcm          as snd_pcm_t ptr ptr, _
byval device       as string, _
byval direction    as integer, _
byval mode         as integer) as integer

declare function snd_pcm_close cdecl alias "snd_pcm_close" ( _
byval pcm          as snd_pcm_t ptr) as integer

declare function snd_pcm_hw_free cdecl alias "snd_pcm_hw_free" ( _
byval pcm          as snd_pcm_t ptr) as integer

declare function snd_pcm_nonblock cdecl alias "snd_pcm_nonblock" ( _
byval pcm          as snd_pcm_t ptr, _
byval nonblock     as integer) as integer

declare function snd_pcm_prepare cdecl alias "snd_pcm_prepare" ( _
byval pcm as snd_pcm_t ptr) as integer

declare function snd_pcm_writei cdecl alias "snd_pcm_writei" ( _
byval pcm          as snd_pcm_t ptr, _
byval buffer       as any ptr, _
byval size         as integer) as integer

declare function snd_pcm_resume cdecl alias "snd_pcm_resume" ( _
byval pcm as snd_pcm_t ptr) as integer

'hardware
declare function snd_pcm_hw_params_malloc cdecl alias "snd_pcm_hw_params_malloc" ( _
byval hw           as snd_pcm_hw_params_t ptr ptr) as integer

declare function snd_pcm_hw_params_any cdecl alias "snd_pcm_hw_params_any" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr) as integer

declare function snd_pcm_hw_params_set_access cdecl alias "snd_pcm_hw_params_set_access" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval mode         as integer) as integer

declare function snd_pcm_hw_params_set_format cdecl alias "snd_pcm_hw_params_set_format" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval fmt          as integer) as integer

declare function snd_pcm_hw_params_set_channels cdecl alias "snd_pcm_hw_params_set_channels" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval Channels     as integer) as integer

declare function snd_pcm_hw_params_get_channels cdecl alias "snd_pcm_hw_params_get_channels" ( _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpChannels   as integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_near cdecl alias "snd_pcm_hw_params_set_rate_near" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpRate         as integer ptr, _
byval lpDir        as integer ptr) as integer

declare function snd_pcm_hw_params_set_periods_near cdecl alias "snd_pcm_hw_params_set_periods_near" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpValue      as integer ptr, _
byval lpDir        as integer ptr) as integer

declare function snd_pcm_hw_params_get_period_size cdecl alias "snd_pcm_hw_params_get_period_size" ( _
byval params       as snd_pcm_hw_params_t ptr, _
byval lpFrames     as integer ptr, _
byval lpDir        as integer ptr) as integer

declare function snd_pcm_hw_params_set_buffer_size_near cdecl alias "snd_pcm_hw_params_set_buffer_size_near" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpFrames     as integer ptr) as integer

declare function snd_pcm_hw_params_get_buffer_size cdecl alias "snd_pcm_hw_params_get_buffer_size" ( _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpFrames     as integer ptr) as integer

declare function snd_pcm_hw_params cdecl alias "snd_pcm_hw_params" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr) as integer

declare sub snd_pcm_hw_params_free cdecl alias "snd_pcm_hw_params_free" ( _
byval hw as snd_pcm_hw_params_t ptr)

#inclib "asound"


'#define device "plughw:0,0"
#define device "hw:0,0"

type player_driver_t
  handle     as snd_pcm_t ptr 'NULL = Error
  Speed      as integer  ' 11025 22050 44100
  nChannels  as integer  ' 1 or 2
  nBits      as integer  ' 8 or 16
  SampleSize as integer  ' (nBits\8) * nChannels
  Buffer     as short ptr ' curently 16Bit
  BufferSize as integer  ' SampleSize * 8192
  nChunks    as integer  ' 2
  ChunkSize  as integer  ' BufferSize / nChunks
end type

function Alsa_Init(byval strDevice as string,byval Speed as integer) as player_driver_t ptr
  dim as snd_pcm_t ptr           htmp
  dim as snd_pcm_hw_params_t ptr hw
  dim as integer                 ret, direction
  dim as player_driver_t ptr     tmp
  dim as zstring ptr             strRet
  ' open device in non blocking mode
  ret = snd_pcm_open(@htmp, strDevice, SND_PCM_STREAM_PLAYBACK, NONBLOCK)
  if (ret < 0) then ? "failed open":goto Alsa_Init_Error

  ' switch to blocking mode
  ret=snd_pcm_nonblock(htmp,BLOCK)
  if (ret < 0) then ? "failed block":goto Alsa_Init_Error

  'alloc the player
  tmp=callocate(sizeof(player_driver_t))
  tmp->Speed=Speed
  tmp->nChannels  = 2
  tmp->nBits      = 16
  tmp->SampleSize = tmp->nBits\8 * tmp->nChannels
  tmp->BufferSize = 8192 * tmp->SampleSize
  tmp->nChunks    = 2
  tmp->ChunkSize  = tmp->BufferSize \ tmp->nChunks

  ' qwery param
  snd_pcm_hw_params_malloc(@hw)
  ret = snd_pcm_hw_params_any(htmp,hw)
  if (ret < 0) then ? "failed hw_param_any":goto Alsa_Init_Error

  ' set read write access
  ret = snd_pcm_hw_params_set_access(htmp, hw, SND_PCM_ACCESS_RW_INTERLEAVED)
  if (ret < 0) then ? "failed set_access":goto Alsa_Init_Error

  ' set 16bit signed
  ret = snd_pcm_hw_params_set_format(htmp,hw,2)
  if (ret < 0) then ? "failed set_format":goto Alsa_Init_Error

  'set stero
  ret = snd_pcm_hw_params_set_channels(htmp,hw, tmp->nChannels)
  if (ret < 0) then
    snd_pcm_hw_params_get_channels(hw, @ret)
    if (ret<>tmp->nChannels) then ? "failed set_channels":goto Alsa_Init_Error
  end if

  'set speed
  ret = snd_pcm_hw_params_set_rate_near(htmp,hw,@tmp->Speed,0)
  if (ret < 0) then
    ? "failed set_rate_near":goto Alsa_Init_Error
  elseif (ret > 0) then
    tmp->Speed=ret
  end if
  if tmp->Speed<>Speed then ? "warning: use speed";tmp->Speed

  ' number of Chunks
  ret = snd_pcm_hw_params_set_periods_near(htmp, hw,@tmp->nChunks, 0)
  if (ret < 0) then ? "failed set_periods_near":goto Alsa_Init_Error

  ' set ChunkSize
  ret = snd_pcm_hw_params_set_buffer_size_near(htmp, hw, @tmp->BufferSize)
  if (ret < 0) then ? "failed set_buffer_size_near":goto Alsa_Init_Error

  ' set the config
  ret = snd_pcm_hw_params(htmp,hw)
  if (ret < 0) then ? "failed hw_params":goto Alsa_Init_Error

  ret = snd_pcm_prepare(htmp)
  if (ret < 0) then ? "failed prepare":goto Alsa_Init_Error

Alsa_Init_Ok:
  tmp->Handle=htmp
  snd_pcm_hw_params_free hw

  return tmp

Alsa_Init_Error:
    if hw  <>0 then snd_pcm_hw_params_free hw
    if htmp<>0 then snd_pcm_close          htmp
    if tmp <>0 then deallocate             tmp
    return 0

end function

sub Alsa_Exit(byval pcm as snd_pcm_t ptr)
  if pcm=0 then exit sub
  ? snd_pcm_close(pcm)
end sub

sub Alsa_Send(byval player as player_driver_t ptr,byval nBytes as integer)

  dim as integer ret,nFrames
  dim as any ptr lpStart,lpEnd

  if player        =0 then exit sub
  if player->Handle=0 then exit sub
  if player->Buffer=0 then exit sub
  if nBytes        <1 then exit sub

  nFrames  = nBytes \ Player->SampleSize
  lpStart=Player->Buffer
  lpEnd  =Player->Buffer+nBytes

  while(lpStart<lpEnd)
    ret = snd_pcm_writei(Player->Handle,lpStart,nFrames)

    if (ret=-EAGAIN) then
      ' device busy
    elseif (ret=-ESTRPIPE) then
      ret=-EAGAIN
      while ret=-EAGAIN:ret=snd_pcm_resume(Player->Handle):wend
    elseif (ret=-EPIPE) then

    else
      if (ret<0) then  ? "failed send audiodata"
      if snd_pcm_prepare(Player->Handle) < 0 then exit sub
      nFrames-=ret:lpStart+=ret*Player->SampleSize
    endif
  wend
end sub


'
' main
'
dim as player_driver_t ptr player
dim as integer i,l,r
player=Alsa_Init(device,44100)

if player=0 then goto test_exist

player->Buffer=allocate(8192*player->SampleSize)

for i=0 to 8192*player->nChannels-1 step 2
  player->Buffer[i]=l:l+=100:if l>5000 then l=-5000
  player->Buffer[i+1]=r:r+=30:if r>5000 then r=-5000
next

for i=1 to 10
  Alsa_Send player,8192*player->SampleSize
next



test_exist:
sleep

end
sorry about my english
Reply
#9
please help the windows part of the fbsound lib is ready
but i nead alsa driver tests on Linux boxes too

this code with all configs mono/stereo 8/16bits 10000-48000 kHz works on my 3 linux boxes but i can test it only with SLACKWARE.

thx for your help
Joshy
Code:
option explicit

const EAGAIN                       = 11 ' Try again
const EPIPE                        = 32 ' Broken pipe
const ESTRPIPE                     = 86 ' Streams pipe error

const BLOCK                        = 0
const NONBLOCK                     = 1
const ASYNC                        = 2
const SND_PCM_STREAM_PLAYBACK      = 0
const SND_PCM_STREAM_CAPTURE       = 1
const SND_PCM_FORMAT_S16_LE        = 2
const SND_PCM_ACCESS_RW_INTERLEAVED= 3

type snd_pcm_t           as _snd_pcm
type snd_pcm_hw_params_t as _snd_pcm_hw_params

' PCM
declare function snd_pcm_open cdecl alias "snd_pcm_open" ( _
byval pcm          as snd_pcm_t ptr ptr, _
byval device       as string, _
byval direction    as integer, _
byval mode         as integer) as integer

declare function snd_pcm_close cdecl alias "snd_pcm_close" ( _
byval pcm          as snd_pcm_t ptr) as integer

declare function snd_pcm_hw_free cdecl alias "snd_pcm_hw_free" ( _
byval pcm          as snd_pcm_t ptr) as integer

declare function snd_pcm_nonblock cdecl alias "snd_pcm_nonblock" ( _
byval pcm          as snd_pcm_t ptr, _
byval nonblock     as integer) as integer

declare function snd_pcm_prepare cdecl alias "snd_pcm_prepare" ( _
byval pcm as snd_pcm_t ptr) as integer

declare function snd_pcm_writei cdecl alias "snd_pcm_writei" ( _
byval pcm          as snd_pcm_t ptr, _
byval buffer       as any ptr, _
byval size         as integer) as integer

declare function snd_pcm_resume cdecl alias "snd_pcm_resume" ( _
byval pcm as snd_pcm_t ptr) as integer

'hardware
declare function snd_pcm_hw_params_malloc cdecl alias "snd_pcm_hw_params_malloc" ( _
byval hw           as snd_pcm_hw_params_t ptr ptr) as integer

declare function snd_pcm_hw_params_any cdecl alias "snd_pcm_hw_params_any" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr) as integer

declare function snd_pcm_hw_params_set_access cdecl alias "snd_pcm_hw_params_set_access" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval mode         as integer) as integer

declare function snd_pcm_hw_params_set_format cdecl alias "snd_pcm_hw_params_set_format" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval fmt          as integer) as integer

declare function snd_pcm_hw_params_set_channels cdecl alias "snd_pcm_hw_params_set_channels" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval Channels     as integer) as integer

declare function snd_pcm_hw_params_get_channels cdecl alias "snd_pcm_hw_params_get_channels" ( _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpChannels   as integer ptr) as integer

declare function snd_pcm_hw_params_set_rate_near cdecl alias "snd_pcm_hw_params_set_rate_near" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpRate         as integer ptr, _
byval lpDir        as integer ptr) as integer

declare function snd_pcm_hw_params_set_periods_near cdecl alias "snd_pcm_hw_params_set_periods_near" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpValue      as integer ptr, _
byval lpDir        as integer ptr) as integer

declare function snd_pcm_hw_params_get_period_size cdecl alias "snd_pcm_hw_params_get_period_size" ( _
byval params       as snd_pcm_hw_params_t ptr, _
byval lpFrames     as integer ptr, _
byval lpDir        as integer ptr) as integer

declare function snd_pcm_hw_params_set_buffer_size_near cdecl alias "snd_pcm_hw_params_set_buffer_size_near" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpFrames     as integer ptr) as integer

declare function snd_pcm_hw_params_get_buffer_size cdecl alias "snd_pcm_hw_params_get_buffer_size" ( _
byval hw           as snd_pcm_hw_params_t ptr, _
byval lpFrames     as integer ptr) as integer

declare function snd_pcm_hw_params cdecl alias "snd_pcm_hw_params" ( _
byval pcm          as snd_pcm_t ptr, _
byval hw           as snd_pcm_hw_params_t ptr) as integer

declare sub snd_pcm_hw_params_free cdecl alias "snd_pcm_hw_params_free" ( _
byval hw as snd_pcm_hw_params_t ptr)

#inclib "asound"


'#define device "plughw:0,0"
#define device "hw:0,0"

union uniptr
  ptr8       as byte  ptr '  -128 to   +127
  ptr16      as short ptr '-32768 to +32767
end union
type player_driver_t
  handle     as snd_pcm_t ptr 'NULL = Error
  Speed      as integer  ' 11025 22050 44100
  nChannels  as integer  ' 1 or 2
  nBits      as integer  ' 8 or 16
  nFrames    as integer  ' 512,1024,2048,4096 and so on
  FrameSize  as integer  ' (nBits\8) * nChannels
  nPeriods   as integer  ' try 2
  PeriodSize as integer  ' BufferSize / FrameSize
  Buffer     as uniptr
end type

function Alsa_Init(byval strDevice as string, _
                   byval Speed    as integer, _
                   byval Bits     as integer, _
                   byval Channels as integer, _
                   byval Frames   as integer=4096) as player_driver_t ptr
  Bits \=8
  Frames\=256

  ' range of Speed
  if Speed < 11025 then
    Speed = 11025
  elseif Speed>48000 then
    Speed = 48000
  end if
  ' range of Bits
  if Bits<1 then
    Bits=1
  elseif Bits>2 then
    Bits=2
  end if
  ' range of Channels
  if Channels<1 then
    Channels=1
  elseif Channels>2 then
    Channels=2
  end if
  ' range of frames
  if Frames<2 then
    Frames=2
  elseif Frames>32 then
    Frames=32
  end if
  Bits   = Bits   shl 3  ' *   8
  Frames = Frames shl 7  ' * 256

  dim as snd_pcm_t ptr           htmp
  dim as snd_pcm_hw_params_t ptr hw
  dim as integer                 ret, direction,PeriodSize
  dim as player_driver_t ptr     tmp
  dim as zstring ptr             strRet
  ' open device in non blocking mode
  ret = snd_pcm_open(@htmp, strDevice, SND_PCM_STREAM_PLAYBACK, NONBLOCK)
  if (ret < 0) then ? "failed open":goto Alsa_Init_Error

  ' switch to blocking mode
  ret=snd_pcm_nonblock(htmp,BLOCK)
  if (ret < 0) then ? "failed block":goto Alsa_Init_Error

  'alloc the player
  tmp=callocate(sizeof(player_driver_t))
  tmp->Speed      = Speed
  tmp->nChannels  = Channels
  tmp->nBits      = Bits
  tmp->FrameSize  = (Bits \ 8) * Channels
  tmp->nFrames    = Frames
  tmp->nPeriods   = 2
  tmp->PeriodSize = 0

  ' qwery param
  snd_pcm_hw_params_malloc(@hw)
  ret = snd_pcm_hw_params_any(htmp,hw)
  if (ret < 0) then ? "failed hw_param_any":goto Alsa_Init_Error

  ' set read write access
  ret = snd_pcm_hw_params_set_access(htmp, hw, SND_PCM_ACCESS_RW_INTERLEAVED)
  if (ret < 0) then ? "failed set_access":goto Alsa_Init_Error

  if tmp->nBits=8 then
     ret = snd_pcm_hw_params_set_format(htmp,hw,0) 'set 8bit signed
  else
     ret = snd_pcm_hw_params_set_format(htmp,hw,2) ' set 16bit signed
  end if
  if (ret < 0) then ? "failed set_format":goto Alsa_Init_Error

  'set mono or stero
  ret = snd_pcm_hw_params_set_channels(htmp,hw, tmp->nChannels)
  if (ret < 0) then
    snd_pcm_hw_params_get_channels(hw,@tmp->nChannels)
    if (tmp->nChannels<>Channels) then ? "warning: use channels=";tmp->nChannels
  end if

  'set speed
  ret = snd_pcm_hw_params_set_rate_near(htmp,hw,@tmp->Speed,0)
  if (ret < 0) then
    ? "failed set_rate_near":goto Alsa_Init_Error
  elseif (ret > 0) then
    tmp->Speed=ret
  end if
  if tmp->Speed<>Speed then ? "warning: use speed";tmp->Speed

  ' set number of Frames
  ret = snd_pcm_hw_params_set_buffer_size_near(htmp, hw, @tmp->nFrames)
  if (ret < 0) then ? "failed set_buffer_size_near":goto Alsa_Init_Error

  ' Chunks of Frames
  ret = snd_pcm_hw_params_set_periods_near(htmp, hw,@tmp->nPeriods, 0)
  if (ret < 0) then ? "failed set_periods_near":goto Alsa_Init_Error
  snd_pcm_hw_params_get_period_size(hw,@tmp->PeriodSize,@Direction)

  ' set the config
  ret = snd_pcm_hw_params(htmp,hw)
  if (ret < 0) then ? "failed hw_params":goto Alsa_Init_Error

  ret = snd_pcm_prepare(htmp)
  if (ret < 0) then ? "failed prepare":goto Alsa_Init_Error

Alsa_Init_Ok:
  tmp->Handle=htmp
  snd_pcm_hw_params_free hw
  return tmp

Alsa_Init_Error:
  if hw  <>0 then snd_pcm_hw_params_free hw
  if htmp<>0 then snd_pcm_close          htmp
  if tmp <>0 then deallocate             tmp
  return 0

end function

sub Alsa_Exit(byval pcm as snd_pcm_t ptr)
  if pcm=0 then exit sub
  ? snd_pcm_close(pcm)
end sub

sub Alsa_Info(byval player  as player_driver_t ptr)
  ? "Alsa_Info"
  ? "-------------------"
  if player=0 then ? "not init":exit sub
  ? "speed     :" ; player->Speed
  ? "bits      :" ; player->nBits
  ? "channels  :" ; player->nChannels
  ? "frames    :" ; player->nFrames
  ? "framesize :" ; player->FrameSize
  ? "chunks    :" ; player->nPeriods
  ? "chunksize :" ; player->PeriodSize
end sub

sub Alsa_Send(byval player as player_driver_t ptr,byval nBytes as integer)

  dim as integer ret,nFrames
  dim as any ptr lpStart,lpEnd

  if player             =0 then exit sub
  if player->Handle     =0 then exit sub
  if player->Buffer.ptr8=0 then exit sub
  if nBytes             <1 then exit sub

  nFrames  = nBytes \ Player->FrameSize
  lpStart=Player->Buffer.ptr8
  lpEnd  =Player->Buffer.ptr8+nBytes

  while(lpStart<lpEnd)
    ret = snd_pcm_writei(Player->Handle,lpStart,nFrames)

    if (ret=-EAGAIN) then
      ' device busy
    elseif (ret=-ESTRPIPE) then
      ret=-EAGAIN
      while ret=-EAGAIN:ret=snd_pcm_resume(Player->Handle):wend
    elseif (ret=-EPIPE) then

    else
      if (ret<0) then  ? "failed send audiodata"
      if snd_pcm_prepare(Player->Handle) < 0 then exit sub
      nFrames-=ret:lpStart+=ret*Player->FrameSize
    endif
  wend
end sub


'
' main
'
dim as player_driver_t ptr player
dim as integer i,l,r



' !!!! please test all this configs !!!!

'Player=Alsa_Init(device,12000, 8,1,2048)
'Player=Alsa_Init(device,24000, 8,2,2048)
'Player=Alsa_Init(device,44100,16,1,2048)
Player=Alsa_Init(device,44100,16,2,2048)



if Player=0 then goto test_exist

Alsa_Info Player

player->Buffer.ptr8=allocate(Player->nFrames*Player->FrameSize)

? "Buffer_Init"
if Player->nBits=8 then
  ? "8Bit"
  if Player->nChannels=1 then
    ? "mono"
    for i=0 to Player->nFrames-1
      player->Buffer.ptr8[i  ]=l:l+=1:if l>64 then l=-64
    next
  else
    ? "stereo"
    for i=0 to Player->nFrames*Player->nChannels - 1 step 2
      player->Buffer.ptr8[i  ]=l:l+=1:if l>64 then l=-64
      player->Buffer.ptr8[i+1]=r:r+=3:if r>64 then r=-64
    next
  end if
else
  ? "16Bit"
  if Player->nChannels=1 then
    ? "mono"
    for i=0 to Player->nFrames - 1
      player->Buffer.ptr16[i  ]=l:l+=100:if l>5000 then l=-5000
    next
  else
    ? "stereo"
    for i=0 to Player->nFrames*Player->nChannels - 1 step 2
      player->Buffer.ptr16[i  ]=l:l+=100:if l>5000 then l=-5000
      player->Buffer.ptr16[i+1]=r:r+=30:if r>5000 then r=-5000
    next
  end if
end if

? "Alsa_Send"
for i=1 to 10
  Alsa_Send player,Player->nFrames*Player->FrameSize
next



test_exist:
sleep

end
sorry about my english
Reply
#10
Test #1 (Sounded BzzBzzBzz):
Download 1.mp3
[Image: 17227-t.png]

Test #2 (A short Bzz)
Download 2.mp3
[Image: 17229-t.png]
/post]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)