c# 4.0 - Mixed wave stream to mp3 using Lame -
....... ....... var mixer = new wavemixerstream32(); mixer.autostop = true; var messageoffset = background.totaltime; var messageoffsetted = new waveoffsetstream(message, timespan.fromseconds(10), timespan.zero,message.totaltime.subtract(timespan.fromseconds(10))); var background32 = new wavechannel32(background); background32.padwithzeroes = false; background32.volume = 0.6f; mixer.addinputstream(background32); var message32 = new wavechannel32(messageoffsetted); message32.padwithzeroes = false; message32.volume = 0.3f; mixer.addinputstream(message32); var ws = new wave32to16stream(mixer);
we trying mix multiple mp3's , wave files create single mp3 finally.
example: 5 sources files (3 mp3's, 2 wave files)
we giving input of each stream wavemixerstream32 , converting using wave32to16stream
we need final stream converted mp3. using lame , passing stream encodemixedstreamasmp3 returning error "invalid file format".
after researching, found riff header missing in mixed wave stream.
how add riff mixed wave stream generated based on multiple sources (mp3 , wave)?
an naudio wavestream
stream of sample data specified format, not riff wav file expected lame. convert riff wav file need add riff headers , on. naudio.wave.wavefilewriter
class this.
if you're working smallish output files aren't going blow memory, can simple (assuming yeti's lame wrapper or similar):
(code updated 19-aug-2013):
public byte[] encodemp3(iwaveprovider ws, uint bitrate = 128) { // setup encoder configuration wavelib.waveformat fmt = new wavelib.waveformat(ws.waveformat.samplerate, ws.waveformat.bitspersample, ws.waveformat.channels); yeti.lame.be_config beconf = new yeti.lame.be_config(fmt, bitrate); // encode wav mp3 int blen = ws.waveformat.averagebytespersecond; byte[] buffer = new byte[blen]; byte[] mp3data = null; using (memorystream mp3strm = new memorystream()) using (mp3writer mp3wri = new mp3writer(mp3strm, fmt, beconf)) { int rc; while ((rc = ws.read(buffer, 0, blen)) > 0) mp3wri.write(buffer, 0, rc); mp3data = mp3strm.toarray(); } return mp3data; }
-- update: setting mp3 encoding parameters
using yeti
lame wrapper, can specify mp3 encoding parameters passing in be_config
structure so:
wavelib.waveformat fmt = new wavelib.waveformat(ws.waveformat.samplerate, ws.waveformat.bitspersample, ws.waveformat.channels); yeti.lame.be_config beconf = new yeti.lame.be_config(fmt, 64); using (memorystream mp3strm = new memorystream()) using (mp3writer mp3wri = new mp3writer(mp3strm, fmt, beconf)) { ... }
the be_config
constructor takes wavelib.waveformat
, optional cbr bit rate in kb/s. if don't specify bit rate defaults 128kb/s output. if don't supply configuration mp3writer
creates default 1 waveformat
.
the code above creates be_config
output rate set 64kb/s. if want different bit rate, specify in place of 64
in new yeti.lame.be_config(fmt, 64);
be_config
contains lot of other options, of won't ever use. check out (slightly sketchy) documentation here.
Comments
Post a Comment