多WAV文件合成

  1 public class WAVSyn
  2     {
  3         /// <summary>
  4         /// 合成WAV文件
  5         /// </summary>
  6         /// <param name="fromWAVFile">合成WAV文件的源地址字符串组</param>
  7         /// <param name="toWAVFile">合成的WAV文件输出位置,toFileAdd如果存在,必须确定不在使用,如果被其他程序使用,合成时报错</param>
  8         /// <returns></returns>
  9          public int GetSoundFile(string[] fromWAVFile,string toWAVFile)
10         {
11             //源地址字符串组的长度
12             int fromFileCount = 0;
13
14             foreach (string str in fromWAVFile)
15             {
16                 if (ISWAVFile(str, "wav"))
17                 {
18                     fromFileCount++;
19                 }
20             }
21
22
23             //传入wav集合小于2,退出,返回-1
24             if (fromFileCount < 2)
25                 return -1;
26
27             //创建不为空的个数
28             System.IO.FileStream[] fs = new System.IO.FileStream[fromFileCount];
29
30             //把wav文件加载到fs数组中
31             int x = 0;
32             foreach (string temStr in fromWAVFile)
33             {
34                 if(ISWAVFile(temStr,"wav"))
35                     fs[x++] = new System.IO.FileStream(temStr,System.IO.FileMode.Open,System.IO.FileAccess.Read);
36             }
37
38
39             //WAV文件头大小为56个字节,包含文件头和数据头
40             //其中文件大小存在与文件头第五个字节开始的四个字节
41             //数据大小存在与文件头的第52个字节开始的四个字节
42             byte[][] bInfo = new byte[fromFileCount][];
43             for (int i = 0; i < fromFileCount; i++)
44             {
45                 bInfo[i] = new byte[56];
46                 fs[i].Read(bInfo[i],0,56);
47             }
48
49
50             //得到合成wav文件的大小
51             int wavFileSum = 0;
52             int[] fileSize = new int[fromFileCount];
53
54             for (int j = 0; j < fromFileCount; j++)
55             {
56                 fileSize[j] = System.BitConverter.ToInt32(bInfo[j],4);
57                 wavFileSum += fileSize[j];
58             }
59
60             byte[] newFileSize = System.BitConverter.GetBytes(wavFileSum);
61
62             //得到合成wav文件的数据大小
63             int dateSizeSum = 0;
64             for (int k = 0; k < fromFileCount; k++)
65             {        
66                 dateSizeSum += System.BitConverter.ToInt32(bInfo[k],52);
67             }
68
69             //???为什么要加上48???
70             dateSizeSum += 48;
71
72             byte[] newDateSize = System.BitConverter.GetBytes(dateSizeSum);
73
74             System.IO.FileStream toFile = new System.IO.FileStream(toWAVFile,System.IO.FileMode.Create);
75             System.IO.BinaryWriter bw = new System.IO.BinaryWriter(toFile);
76
77             byte[][] dushu = new byte[fromFileCount][];
78             for (int l = 0; l < fromFileCount; l++)
79             {
80                 dushu[l] = new byte[fileSize[l]];
81             }
82
83
84             for (int i = 4, j = 0; i < 8; i++, j++)
85             {
86                 bInfo[0][i] = newFileSize[j];
87             }
88
89             for (int i = 52, j = 0; i < 56; i++, j++)
90             {
91                 bInfo[0][i] = newDateSize[j];
92             }
93
94             bw.Write(bInfo[0]);
95
96             for (int i = 0; i < fromFileCount; i++)
97             {
98                 fs[i].Read(dushu[i],0,fileSize[i]);
99                 bw.Write(dushu[i],0,fileSize[i]);
100                 fs[i].Close();
101             }
102
103             bw.Flush();
104             bw.Close();
105
106             toFile.Close();
107
108             //正常结束
109             return 0;
110         }
111
112
113         //函数重载1
114         public int GetSoundFile(string fromFileAdd1, string fromFileAdd2, string toFileAdd)
115         {
116             string[] fromFileAdd = new string[] { fromFileAdd1, fromFileAdd2 };
117             return GetSoundFile(fromFileAdd, toFileAdd);
118         }
119
120         //函数重载2
121         public int GetSoundFile(string fromFileAdd1, string fromFileAdd2, string fromFileAdd3, string toFileAdd)
122         {
123             string[] fromFileAdd = new string[] { fromFileAdd1, fromFileAdd2, fromFileAdd3 };
124             return GetSoundFile(fromFileAdd, toFileAdd);
125         }
126
127         private bool ISWAVFile(string filePath, string type)
128         {
129             string strType = System.IO.Path.GetExtension(filePath);
130
131             if (filePath == string.Empty || strType == string.Empty)
132                 return false;
133
134             //不需要比较扩展名,直接返回TRUE
135             if (type == string.Empty)
136                 return true;
137
138             //需要比较扩展名
139             if (strType.IndexOf(type.ToLower() ) > -1)
140                 return true;
141
142             return false;
143            
144         }
145     }

posted @ 2010-04-23 18:37  MNight  阅读(1076)  评论(3编辑  收藏  举报