Ejemplo n.º 1
0
        /// <summary>
        /// 合成Wave格式音频文件
        /// </summary>
        /// <param name="outputFile">要合成的文件名称</param>
        /// <param name="sourceFiles">源文件名称集合</param>
        public static void Merge(string outputFile, IEnumerable <string> sourceFiles)
        {
            var waIn  = new WaveFileMerge();
            var waOut = new WaveFileMerge
            {
                _dataLength = 0,
                _length     = 0
            };

            //聚合文件头
            var enumerable = sourceFiles as string[] ?? sourceFiles.ToArray();

            foreach (var path in enumerable)
            {
                if (!File.Exists(path))
                {
                    continue;
                }
                waIn.WaveHeaderSourceFile(path);
                waOut._dataLength += waIn._dataLength;
                waOut._length     += waIn._length;
            }

            //重构文件头
            waOut._bitsPerSample = waIn._bitsPerSample;
            waOut._channels      = waIn._channels;
            waOut._samplerate    = waIn._samplerate;
            waOut.WaveHeaderOutputFile(outputFile);

            foreach (var path in enumerable)
            {
                if (!File.Exists(path))
                {
                    continue;
                }
                using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    var arrfile = new byte[fs.Length - 44];
                    fs.Position = 44;
                    fs.Read(arrfile, 0, arrfile.Length);

                    using (var fo = new FileStream(outputFile, FileMode.Append, FileAccess.Write))
                    {
                        using (var bw = new BinaryWriter(fo))
                        {
                            bw.Write(arrfile);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 拼接 Wave 格式音频文件
 /// </summary>
 /// <param name="outputFile">要合成的文件名称</param>
 /// <param name="sourceFiles">源文件名称集合</param>
 public static void MergeWave(string outputFile, IEnumerable <string> sourceFiles)
 {
     WaveFileMerge.Merge(outputFile, sourceFiles);
 }