'converting wave file to mp3
'locate a wav file & build mp3 file name
filedialog "Locate wav file ...","*.wav",wavfile$
if wavfile$<>"" then
wpath$=getPath$(wavfile$)
wfile$=getFilename$(wavfile$)
mp3file$=left$(wfile$,len(wfile$)-3)+"mp3"
'set up conversion params
'-b 96,112,128,160,192 see documentation for full bitrate range & other settings.
quality$="-b 160"
paramStr$=quality$+chr$(32)+wfile$+chr$(32)+mp3file$ '-h a.wav a.mp3
lamefile$="lame.exe"

struct s,_
cbSize as long,fMask as long,hwnd as ulong,_
lpVerb$ as ptr,lpFile$ as ptr,lpParameters$ as ptr ,_
lpDirectory$ as ptr,nShow as long,hInstApp as long,_
lpIDList as long,lpClass as long,hkeyClass as long,_
dwHotKey as long,hIcon as ulong,hProcess as ulong
'end struct

SEEMASKNOCLOSEPROCESS = 64
s.cbSize.struct=len(s.struct)
s.fMask.struct=SEEMASKNOCLOSEPROCESS
s.hwnd.struct=0
s.lpVerb$.struct="open"
s.lpFile$.struct=lamefile$
s.lpParameters$.struct=paramStr$
s.lpDirectory$.struct=wpath$
s.nShow.struct=_SW_NORMAL
print "Converting wav to mp3"
print wavfile$;" @start time: ";time$()
print "Please wait ....."
calldll #shell32 , "ShellExecuteExA",_
s as struct,r as long

if r<>0 then
    hProcess=s.hProcess.struct
    else
    print "Error - return=";r
    end
end if

waitResult=-1
while waitResult<>0
calldll #kernel32, "WaitForSingleObject",_
hProcess as ulong,0 as long,_
waitResult as long
scan
'reduce processor load
calldll #kernel32,"Sleep",50 as ulong, res as void
wend

print "File conversion finished"
print wpath$;mp3file$;" @end time: ";time$()
print "End"
end if
end

function getFilename$(filespec$)
        fileindex=len(filespec$)
        filelength=len(filespec$)
          while mid$(filespec$, fileindex,1)<>"\"
            fileindex=fileindex-1
            if fileindex=0 then exit while
            scan
          wend
        getFilename$=right$(filespec$,filelength-fileindex)
end function


function getPath$(filespec$)
        fileindex=len(filespec$)
        filelength=len(filespec$)
          while mid$(filespec$, fileindex,1)<>"\"
            fileindex=fileindex-1
            if fileindex=0 then exit while
            scan
          wend
        getPath$=left$(filespec$,fileindex)
end function


