Code: Select all
Dim shared As Any Ptr alsa
dim shared as integer OSS
'ALSA declarations
alsa = DyLibLoad("asound")
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
#ifndef NULL
#define NULL 0
#endif
Type snd_pcm_t As Any Ptr
Type snd_pcm_hw_params_t As Any Ptr
Type snd_output_t As Any Ptr
' PCM
Dim Shared snd_strerror as Function ( _
Byval ecode As Integer) As Zstring Ptr
snd_strerror= DyLibSymbol(alsa, "snd_strerror")
Dim Shared snd_pcm_open as Function ( _
Byval pcm As snd_pcm_t Ptr, _
Byval device As Zstring Ptr, _
Byval direction As Integer, _
Byval mode As Integer) As Integer
snd_pcm_open= DyLibSymbol(alsa, "snd_pcm_open")
Dim Shared snd_pcm_close as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_close= DyLibSymbol(alsa, "snd_pcm_close")
Dim Shared snd_pcm_start as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_start= DyLibSymbol(alsa, "snd_pcm_start")
Dim Shared snd_pcm_drain as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_drain= DyLibSymbol(alsa, "snd_pcm_drain")
Dim Shared snd_pcm_hw_free as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_hw_free= DyLibSymbol(alsa, "snd_pcm_hw_free")
Dim Shared snd_pcm_nonblock as Function ( _
Byval pcm As snd_pcm_t, _
Byval nonblock As Integer) As Integer
snd_pcm_nonblock= DyLibSymbol(alsa, "snd_pcm_nonblock")
Dim Shared snd_pcm_prepare as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_prepare= DyLibSymbol(alsa, "snd_pcm_prepare")
Dim Shared snd_pcm_writei as Function ( _
Byval pcm As snd_pcm_t, _
Byval buffer As Any Ptr, _
Byval size As Integer) As Integer
snd_pcm_writei= DyLibSymbol(alsa, "snd_pcm_writei")
Dim Shared snd_pcm_recover as Function ( _
Byval pcm As snd_pcm_t, _
Byval err As Integer, _
Byval silent As Integer) As Integer
snd_pcm_recover= DyLibSymbol(alsa, "snd_pcm_recover")
Dim Shared snd_pcm_avail_update as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_avail_update= DyLibSymbol(alsa, "snd_pcm_avail_update")
Dim Shared snd_pcm_wait as Function ( _
Byval pcm As snd_pcm_t, _
Byval msec As Integer) As Integer
snd_pcm_wait= DyLibSymbol(alsa, "snd_pcm_wait")
Dim Shared snd_pcm_resume as Function ( _
Byval pcm As snd_pcm_t) As Integer
snd_pcm_resume= DyLibSymbol(alsa, "snd_pcm_resume")
'hardware
Dim Shared snd_pcm_hw_params_malloc as Function ( _
Byval hw As snd_pcm_hw_params_t Ptr) As Integer
snd_pcm_hw_params_malloc= DyLibSymbol(alsa, "snd_pcm_hw_params_malloc")
Dim Shared snd_pcm_hw_params_any as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t) As Integer
snd_pcm_hw_params_any= DyLibSymbol(alsa, "snd_pcm_hw_params_any")
Dim Shared snd_pcm_hw_params_set_access as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t, _
Byval mode As Integer) As Integer
snd_pcm_hw_params_set_access= DyLibSymbol(alsa, "snd_pcm_hw_params_set_access")
Dim Shared snd_pcm_hw_params_set_format as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t, _
Byval fmt As Integer) As Integer
snd_pcm_hw_params_set_format= DyLibSymbol(alsa, "snd_pcm_hw_params_set_format")
Dim Shared snd_pcm_hw_params_set_channels as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t, _
Byval Channels As Integer) As Integer
snd_pcm_hw_params_set_channels= DyLibSymbol(alsa, "snd_pcm_hw_params_set_channels")
Dim Shared snd_pcm_hw_params_get_channels as Function ( _
Byval hw As snd_pcm_hw_params_t, _
Byval lpChannels As Integer Ptr) As Integer
snd_pcm_hw_params_get_channels= DyLibSymbol(alsa, "snd_pcm_hw_params_get_channels")
Dim Shared snd_pcm_hw_params_set_rate_near as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t, _
Byval lpRate As Integer Ptr, _
Byval lpDir As Integer Ptr) As Integer
snd_pcm_hw_params_set_rate_near= DyLibSymbol(alsa, "snd_pcm_hw_params_set_rate_near")
Dim Shared snd_pcm_hw_params_get_periods as Function ( _
Byval hw As snd_pcm_hw_params_t, _
Byval lpValue As Integer Ptr, _
Byval lpDir As Integer Ptr) As Integer
snd_pcm_hw_params_get_periods= DyLibSymbol(alsa, "snd_pcm_hw_params_get_periods")
Dim Shared snd_pcm_hw_params_set_periods_near as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t, _
Byval lpValue As Integer Ptr, _
Byval lpDir As Integer Ptr) As Integer
snd_pcm_hw_params_set_periods_near= DyLibSymbol(alsa, "snd_pcm_hw_params_set_periods_near")
Dim Shared snd_pcm_hw_params_get_period_size as Function ( _
Byval params As snd_pcm_hw_params_t, _
Byval lpFrames As Integer Ptr, _
Byval lpDir As Integer Ptr) As Integer
snd_pcm_hw_params_get_period_size= DyLibSymbol(alsa, "snd_pcm_hw_params_get_period_size")
Dim Shared snd_pcm_hw_params_set_period_size_near as Function ( _
Byval pcm As snd_pcm_t Ptr, _
Byval hw As snd_pcm_hw_params_t, _
Byval lpValue As Integer Ptr, _
Byval lpDir As Integer Ptr) As Integer
snd_pcm_hw_params_set_period_size_near= DyLibSymbol(alsa, "snd_pcm_hw_params_set_period_size_near")
Dim Shared snd_pcm_hw_params_set_buffer_size_near as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t, _
Byval lpFrames As Integer Ptr) As Integer
snd_pcm_hw_params_set_buffer_size_near= DyLibSymbol(alsa, "snd_pcm_hw_params_set_buffer_size_near")
Dim Shared snd_pcm_hw_params_get_buffer_size as Function ( _
Byval hw As snd_pcm_hw_params_t, _
Byval lpFrames As Integer Ptr) As Integer
snd_pcm_hw_params_get_buffer_size= DyLibSymbol(alsa, "snd_pcm_hw_params_get_buffer_size")
Dim Shared snd_pcm_hw_params as Function ( _
Byval pcm As snd_pcm_t, _
Byval hw As snd_pcm_hw_params_t) As Integer
snd_pcm_hw_params= DyLibSymbol(alsa, "snd_pcm_hw_params")
Dim Shared snd_pcm_hw_params_free as Sub ( _
Byval hw As snd_pcm_hw_params_t)
snd_pcm_hw_params_free= DyLibSymbol(alsa, "snd_pcm_hw_params_free")
Dim Shared As snd_pcm_t Ptr hDevice
Dim Shared As snd_pcm_hw_params_t Ptr hw
Dim Shared As Integer ret,value,direction,buffersize,nFrames,Periodsize
Dim Shared As Zstring Ptr strRet
'OSS declarations
#include "file.bi"
OSS=FileExists("/dev/dsp")
declare function fileno cdecl alias "fileno" (byval as any ptr) as integer
declare function ioctl cdecl alias "ioctl" (byval hDevice as integer,byval io_cmd as integer,byval lpArg as integer ptr) as integer
const SNDCTL_DSP_RESET = &H00005000
const SNDCTL_DSP_SYNC = &H00005001
const SNDCTL_DSP_POST = &H00005008
const SNDCTL_DSP_NONBLOCK = &H0000500e
const SNDCTL_DSP_SPEED = &Hc0045002
const SNDCTL_DSP_STEREO = &Hc0045003 '0=mono 1=stereo
const SNDCTL_DSP_GETBLKSIZE = &Hc0045004
const SNDCTL_DSP_SETFMT = &Hc0045005
const SNDCTL_DSP_SAMPLESIZE = &Hc0045005 'same as _SETFMT
const SNDCTL_DSP_CHANNELS = &Hc0045006 '1=mono 2=stereo
const SOUND_PCM_WRITE_FILTER= &Hc0045007
const SNDCTL_DSP_SUBDIVIDE = &Hc0045009
const SNDCTL_DSP_SETFRAGMENT =&Hc004500A
'arg for SNDCTL_DSP_SETFMT cmd
const AFMT_MU_LAW = &H00000001
const AFMT_A_LAW = &H00000002
const AFMT_IMA_ADPCM = &H00000004
const AFMT_U8 = &H00000008
const AFMT_S16_LE = &H00000010 ' Little endian signed
const AFMT_S16_BE = &H00000020 ' Big endian signed 16
const AFMT_S8 = &H00000040
const AFMT_U16_LE = &H00000080 ' Little endian U16
const AFMT_U16_BE = &H00000100 ' Big endian U16
const AFMT_MPEG = &H00000200 ' MPEG (2) audio
const AFMT_AC3 = &H00000400 ' Dolby Digital AC3
sub SoundSet(frequency as integer, channels as integer, bits as integer)
if alsa then
if bits=16 then PeriodSize=2 else Periodsize=1
Periodsize*=channels
ret = snd_pcm_open(@hDevice,"default", SND_PCM_STREAM_PLAYBACK, BLOCK)
snd_pcm_hw_params_malloc(@hw)
ret = snd_pcm_hw_params_any(hDevice,hw)
ret = snd_pcm_hw_params_set_access(hDevice, hw, SND_PCM_ACCESS_RW_INTERLEAVED)
ret = snd_pcm_hw_params_set_format(hDevice,hw,IIF(bits=16,2,1))
ret = snd_pcm_hw_params_set_channels(hDevice,hw,channels)
value=frequency 'set speed
ret = snd_pcm_hw_params_set_rate_near(hDevice,hw,@value,@direction)
ret = snd_pcm_hw_params(hDevice,hw)
snd_pcm_hw_params_free (hw)
snd_pcm_prepare(hDevice)
elseif OSS then
if OSS>0 then close OSS
OSS=FreeFile
open "/dev/dsp" for output as #1
dim as integer arg
arg=IIF(bits=16, AFMT_S16_LE, AFMT_U8)
ioctl fileno(cast (any ptr,fileattr(1,2))), SNDCTL_DSP_SETFMT, @arg
arg=IIF(channels<>0, 1, 0)
ioctl fileno(cast (any ptr,fileattr(1,2))), SNDCTL_DSP_STEREO, @arg
arg=channels
ioctl fileno(cast (any ptr,fileattr(1,2))), SNDCTL_DSP_CHANNELS, @arg
arg=frequency
ioctl fileno(cast (any ptr,fileattr(1,2))), SNDCTL_DSP_SPEED, @arg
end if
end sub
sub playbuffer (soundBuffer as any ptr, buffersize as integer)
if alsa then
buffersize/=Periodsize
ret=snd_pcm_writei(hDevice, soundBuffer, buffersize)
if ret=EPIPE then
ret=snd_pcm_recover(hDevice, ret,1)
if ret=0 then ret=snd_pcm_writei(hDevice, soundBuffer, buffersize)
end if
elseif OSS>0 then
put #OSS, ,*cast(ubyte ptr, soundBuffer), buffersize
end if
end sub
so a program using this can be compiler both under windows and linux
The same example works in both systems:
Code: Select all
Dim MyBuffer(200000) as short
SoundSet 44100,2,16
dim i2 as integer
for a as integer=0 to 80
for i as integer=1 to 512*4 step 2
i2=i2+1
MyBuffer(i)= (i2 mod 20)*i2/10
MyBuffer(i+1)= (i2 mod 20)*i2/10
next
playbuffer @MyBuffer(0),1024*4
next
sleep