c# - Split audio file into pieces -
i´m trying split audio file in pieces.
the fact is: have byte array , split wav file random pieces (3 example).
of course, know can´t this. have idea on how it?
byte[] result = stream.toarray(); byte[] testing = new byte[44]; (int ix = 0; ix < testing.length; ix++) { testing[ix] = result[ix]; } system.io.file.writeallbytes("yourfilepath_" + system.guid.newguid() + ".wav", testing);
i build solution in c# heard there lib called sox , can split silence gap this:
sox in.wav out.wav silence 1 0.5 1% 1 5.0 1% : newfile : restart
but everytime run command, 1 file generated. (audio file lasts 5 seconds, , each splitted file must have aroung 1 second).
what best way this?
thank much!
edit
with sox:
string sox = @"c:\program files (x86)\sox-14-4-1\sox.exe"; string inputfile = @"d:\brothers vibe - rainforest.mp3"; string outputdirectory = @"d:\splittest"; string outputprefix = "split"; int[] segments = { 10, 15, 30 }; ienumerable<string> enumerable = segments.select(s => "trim 0 " + s.tostring(cultureinfo.invariantculture)); string @join = string.join(" : newfile : ", enumerable); string cmdline = string.format("\"{0}\" \"{1}%1n.wav" + "\" {2}", inputfile, path.combine(outputdirectory, outputprefix), @join); var processstartinfo = new processstartinfo(sox, cmdline); process start = system.diagnostics.process.start(processstartinfo);
if sox complains libmad (for mp3) : copy dlls next it, see here
alternatively can use ffmpeg in same manner :
ffmpeg -ss 0 -t 30 -i "brothers vibe - rainforest.mp3" "brothers vibe - rainforest.wav"
(see docs details)
you can bass.net :
for code below pass in :
- input file name
- desired duration each segment
- output directory
- prefix use each segment file
the method check whether file long enough specified segments, if yes cut file wavs same sample rate, channels, bit depth.
using system; using system.collections.generic; using system.io; using system.linq; using system.windows.forms; using un4seen.bass; using un4seen.bass.misc; namespace windowsformsapplication2 { public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { if (!bass.bass_init(-1, 44100, bassinit.bass_device_default, intptr.zero)) throw new invalidoperationexception("couldn't initialize bass"); string filename = @"d:\brothers vibe - rainforest.mp3"; var segments = new double[] {30, 15, 20}; string[] splitaudio = splitaudio(filename, segments, "output", @"d:\split"); } private static string[] splitaudio(string filename, double[] segments, string prefix, string outputdirectory) { if (filename == null) throw new argumentnullexception("filename"); if (segments == null) throw new argumentnullexception("segments"); if (prefix == null) throw new argumentnullexception("prefix"); if (outputdirectory == null) throw new argumentnullexception("outputdirectory"); int = bass.bass_streamcreatefile(filename, 0, 0, bassflag.bass_stream_prescan | bassflag.bass_stream_decode); if (i == 0) throw new invalidoperationexception("couldn't create stream"); double sum = segments.sum(); long length = bass.bass_channelgetlength(i); double seconds = bass.bass_channelbytes2seconds(i, length); if (sum > seconds) throw new argumentoutofrangeexception("segments", "required segments exceed file duration"); bass_channelinfo info = bass.bass_channelgetinfo(i); if (!directory.exists(outputdirectory)) directory.createdirectory(outputdirectory); int index = 0; var list = new list<string>(); foreach (double segment in segments) { double d = segment; long seconds2bytes = bass.bass_channelseconds2bytes(i, d); var buffer = new byte[seconds2bytes]; int getdata = bass.bass_channelgetdata(i, buffer, buffer.length); string name = string.format("{0}_{1}.wav", prefix, index); string combine = path.combine(outputdirectory, name); int bitspersample = info.is8bit ? 8 : info.is32bit ? 32 : 16; var wavewriter = new wavewriter(combine, info.chans, info.freq, bitspersample, true); wavewriter.writenoconvert(buffer, buffer.length); wavewriter.close(); list.add(combine); index++; } bool free = bass.bass_streamfree(i); return list.toarray(); } } }
todo
the extraction not optimized, if concerned memory usage, function should enhanced grab parts of segments , write them progressively wavewriter
.
notes
bass.net has nag screen, can request free registration serial @ website.
note, install bass.net make sure copy bass.dll base package next exe. also, can use pretty audio formats, see website formats plugins , how load them (bass_pluginload).
Comments
Post a Comment