guest (78.150.200.144)
Apr 10, 2009
==Converting wav files to MP3== by [[user: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 ==Other LAME Options to re-encode existing mp3 files.== Apart from wav to mp3 conversion, lame.exe is also able to modify existing mp3 files, for example changing the bitrate or resampling or applying highpass/lowpass filters, etc. Switches.html, found inside the zip file, documents all the command line switches with examples of how to apply them. As an example, to change the bitrate (and subsequently reduce the file size) of an already encoded file which has say a bitrate of 160kb/s, to 80kb/s, run lame.exe with parameters ' -b 80 source.mp3 dest.mp3 ' . If you have mp3 files which you would like to play in your LB programs, using native playwave command, lame.exe can convert them to wav too! The 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! [[code format="vb"]] '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 [[code]] Source file: [[file:lb-lame.bas]]