woodcutter woodcutter Apr 9, 2009 - "Text editing and source file added."

Converting wav files to MP3

by - mike_ukmid mike_ukmid
There are many examples of LB sound recorders which record in wav format, a format which consumes huge hard disk space. There are also many freeware wav to MP3 utilities but it is possible to include wav to mp3 conversion in your LB programs using a free command line executable, lame.exe.

Lame.exe can be obtained from http://www.rarewares.org/mp3-lame-bundle.php by downloading Lame3.98.2.zip (549kB) and the zip file also contains all the information required to make use of lame.exe

See the various html files within the zip for details on setting sample rates, bitrates, etc. The demo is set to a bitrate of 160kB/s.

  • Create a new folder and from the zip file extract only lame.exe to that folder.
  • Download the source file from the link at the bottom of the page or start LB, copy/paste the code below and save as lb-lame.bas to the same folder.
  • Run lb-lame.bas, locate a wav file, the newly generated mp3 file will be saved to the same folder as the wav file.
  • Lame.exe is run 'hidden' but if you want to observe the program, change s.nShow.struct=_SW_HIDE to
    s.nShow.struct=_SW_NORMAL

This code (below) could be added to most of the LB wave recorder demos, record sound as wav, convert to mp3 then immediately delete the wave file.

Enjoy!
'converting wave file to mp3 (M.Bradbury April 2009)
'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_HIDE
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
 
 
 
 
Source file: