C#实现Wav PCM格式文件,截取与淡入淡出处理
因工具需要 对 Wav 文件做处理 包含转格 截取 淡入淡出处理 ,此处将代码记下 以备后用。
View Code
1 /// <summary>
2 /// 文件截取处理
3 /// </summary>
4 public class WavScissorHelper
5 {
6 public static bool GetWavFileScissor(string OriginalAudioFilePath, string DestinedAudioFilePath, int beginCutTime, int endCutTime, ref string errorInfo)
7 {
8 if (!File.Exists(OriginalAudioFilePath))
9 {
10 errorInfo = string.Format("源文件:[{0}]不存在!", OriginalAudioFilePath);
11 return false;
12 }
13 if (!Directory.Exists(Path.GetDirectoryName(DestinedAudioFilePath)))
14 {
15 errorInfo = string.Format("文件夹:[{0}]不存在!", Path.GetDirectoryName(DestinedAudioFilePath));
16 return false;
17 }
18 if ((beginCutTime < 0) || (endCutTime < 0)
19 || (beginCutTime > endCutTime))
20 {
21 errorInfo = string.Format("截取时间[{0}][{1}]有误!", beginCutTime, endCutTime);
22 return false;
23 }
24 try
25 {
26 WavFile oldFile = new WavFile(OriginalAudioFilePath);
27 if (endCutTime > oldFile.PlayTime)
28 {
29 errorInfo = string.Format("结束截取时间[{0}]大于音频文件播放时间[{1}]!", endCutTime, oldFile.PlayTime);
30 return false;
31 }
32 int timeSpan = endCutTime - beginCutTime;
33
34 WavFile newFile = new WavFile(DestinedAudioFilePath);
35 newFile.WavFormat = oldFile.WavFormat;
36
37 byte[] newAudioBytes = new byte[oldFile.WavFormat.ByteRate * timeSpan];
38 Array.Copy(oldFile.AudioDataBytes, oldFile.WavFormat.ByteRate * beginCutTime, newAudioBytes, 0, newAudioBytes.Length);
39
40 newFile.WriteWavFile(newAudioBytes);
41 return true;
42 }
43 catch (Exception e)
44 {
45 return false;
46
47 }
48 }
49
50 }
51
52 /// <summary>
53 /// 淡入淡出处理
54 /// </summary>
55 public class WavFadeHelper
56 {
57 private WavFile _wavFile;
58
59 public WavFadeHelper(string wavFilePath)
60 {
61 _wavFile = new WavFile(wavFilePath);
62 }
63
64
65 public bool FadeAtFirstSec(FadeMode fadeMode, long fadeLength)
66 {
67 return FadeAtSec(fadeMode, 0, fadeLength);
68 }
69
70 public bool FadeAtEndSec(FadeMode fadeMode, long fadeLength)
71 {
72 long totalTime = _wavFile.PlayTime;
73 if (totalTime < fadeLength)
74 fadeLength = totalTime;
75 long startTime = totalTime - fadeLength;
76
77 return FadeAtSec(fadeMode, startTime, startTime + fadeLength);
78 }
79
80 public bool FadeAtSec(FadeMode fadeMode, lo
补充:软件开发 , C# ,