• 合并多个声音文件


      上次,在项目中遇到了要求合并多个声音文件的需求,在网上搜索了一下,发现有一老外写的东西。现在发在首页。dudu要是觉得不好可以撤下阿

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Runtime.InteropServices;
    namespace MergeWaves
    {
        
    public class WaveIO
        
    {
            
    public int length;
            
    public short  channels;
            
    public int samplerate;
            
    public int DataLength;
            
    public short BitsPerSample;

            
    private  void WaveHeaderIN(string spath)
            
    {
                FileStream fs 
    = new FileStream(spath, FileMode.Open, FileAccess.Read);
             
                BinaryReader br 
    = new BinaryReader(fs);
                length 
    = (int)fs.Length - 8;
                fs.Position 
    = 22;
                channels 
    = br.ReadInt16();
                fs.Position 
    = 24;
                samplerate 
    = br.ReadInt32();
                fs.Position 
    = 34;

                BitsPerSample 
    = br.ReadInt16();
                DataLength 
    = (int)fs.Length - 44;
                br.Close ();
                fs.Close();

            }

      
            
    private  void WaveHeaderOUT(string sPath)
            
    {
                FileStream fs 
    = new FileStream(sPath, FileMode.Create, FileAccess.Write );

                BinaryWriter bw 
    = new BinaryWriter(fs);
                fs.Position 
    = 0;
                bw.Write(
    new char[4'R''I''F''F' });
           
                bw.Write(length);
              
                bw.Write(
    new char[8{'W','A','V','E','f','m','t',' '});
              
                bw.Write((
    int)16);
      
                bw.Write((
    short)1);
                bw.Write(channels);
            
                bw.Write(samplerate );
           
                bw.Write((
    int)(samplerate * ((BitsPerSample * channels) / 8)));
            
                bw.Write((
    short )((BitsPerSample * channels) / 8));
           
                bw.Write(BitsPerSample);
          
                bw.Write(
    new char[4{'d','a','t','a'});
                bw.Write(DataLength);
                bw.Close();
                fs.Close();
            }

            
    public void Merge(string[] files, string outfile)
            
    {
                WaveIO wa_IN 
    = new WaveIO();
                WaveIO wa_out 
    = new WaveIO();

                wa_out.DataLength 
    = 0;
                wa_out.length 
    = 0;


                
    //Gather header data
                foreach (string path in files)
                
    {
                    wa_IN.WaveHeaderIN(@path);
                    wa_out.DataLength 
    += wa_IN.DataLength;
                    wa_out.length 
    += wa_IN.length;

                }


                
    //Recontruct new header
                wa_out.BitsPerSample = wa_IN.BitsPerSample;
                wa_out.channels 
    = wa_IN.channels;
                wa_out.samplerate 
    = wa_IN.samplerate;
                wa_out.WaveHeaderOUT(@outfile);

                
    foreach (string path in files)
                
    {
                    FileStream fs 
    = new FileStream(@path, FileMode.Open, FileAccess.Read);
                    
    byte[] arrfile = new byte[fs.Length - 44];
                    fs.Position 
    = 44;
                    fs.Read(arrfile, 
    0, arrfile.Length);
                    fs.Close();

                    FileStream fo 
    = new FileStream(@outfile, FileMode.Append, FileAccess.Write);
                    BinaryWriter bw 
    = new BinaryWriter(fo);
                    bw.Write(arrfile);
                    bw.Close();
                    fo.Close();
                }

              }

        }

    }


    直接调用Merge方法就可以了,第一个参数是要合并的声音数组,第二个是输出的路径

  • 相关阅读:
    常用的文件查看命令
    Linux常用快捷按键
    寒冬储粮
    创建型模式:抽象工厂
    创建型模式:工厂方法
    创建型模式:单例模式
    开闭原则
    迪米特法则
    接口隔离原则
    依赖倒置原则
  • 原文地址:https://www.cnblogs.com/zsbfree/p/841802.html
Copyright © 2020-2023  润新知