RetroCoders Community

FreeBasic Programming => FreeBasic Libraries => Topic started by: ron77_db on Apr 01, 2022, 11:56 AM

Title: Recording sound from mic with BASS lib (WAV format)
Post by: ron77_db on Apr 01, 2022, 11:56 AM
hello...

this code uses BASS library with freebasic to record sound from the webcam mic and save it as a file in WAV format.
it's just a small demo I made with a help of a friend. enjoy...

/' Feb 17 2022 a program to record sound from webcam mic and save it as wav file
using BASS audio library. started by ron77 and finished with help of mysoft
'/


#include "bass.bi"
#include "crt.bi"
 
if BASS_RecordInit(-1) = false then
    print "bass record init failed!"
EndIf
 
const sFILE = "test.wav"

type WaveFile
_RIFF       as long  = cvl("RIFF")
RiffSize    as long  = 36 'DataSize+sizeof(WaveFile)
_WAVE       as long  = cvl("WAVE")
_FMT        as long  = cvl("fmt ")
_FmtSz      as long  = 16
_Format     as short = 1 'PCM
Channels    as short '> Channels (1=mono,2=stereo)
Rate        as LONG  '> Sample rate (44100)
ByteRate    As Long  'Align*Rate
Align       as short '(Bits*Channels)\8
Bits        as short '> Bits per sample (16)
_Data       as long = cvl("data")
DataSize    as long = 0
End Type
Type RecordStruct
pWaveFile as WaveFile ptr
iFile     as long
End Type

screenres 640,480

 
function MyRecordProc cdecl ( RECORD as HRECORD, BUFFER AS const ANY PTR , LENGTH AS DWORD, pParms AS any ptr ) as bool
with *cptr(RecordStruct ptr, pParms)   
put #.iFile,,*cptr(ubyte ptr,BUFFER),LENGTH
.pWaveFile->DataSize += LENGTH
end with
dim as const short ptr pBuff  = BUFFER
    screenlock   
    cls
    for N as long = 0 to (LENGTH\2)
    var iSam = pBuff[N]\((32768\240)+1)
    line( N , 240 )-step(0,iSam),10   
    Next
    screenunlock
    return true
End Function

var f = freefile()

'clear file
open sFILE for binary access write as #f
close #f
'open file to both read/write
open sFILE for binary as #f

dim as WaveFile tFile

with tFile
'wave attributes
.Rate = 44100 :  .Bits = 16 : .Channels = 1
.Align = (.Bits*.Channels)\8
.ByteRate = .Align*.Rate

'store header on file and start recording
put #f,,tFile
var record = BASS_RecordStart(.Rate, .Channels, 0, @MyRecordProc, @type<RecordStruct>(@tFile,f) )
 
WindowTitle("Recording - Any Key to stop")
sleep

'update wave file header and close
BASS_ChannelStop(record)

.RiffSize += .DataSize
put #f,1,tFile
close #f

end with

'unintialize
BASS_Recordfree()