Example #1
0
			public static void StripPadding(string WaveFile, int Seconds)
			{
				var reader = new WaveFileReader(WaveFile);
				var strTarget = new BinaryWriter(new MemoryStream());
				WaveFormat fmt = reader.WaveFormat;

				string temp1name = Path.GetDirectoryName(WaveFile) + Path.DirectorySeparatorChar +
								   Path.GetFileNameWithoutExtension(WaveFile) + "-temp." + Path.GetExtension(WaveFile);
				string temp2name = Path.GetDirectoryName(WaveFile) + Path.DirectorySeparatorChar +
								   Path.GetFileNameWithoutExtension(WaveFile) + "-old." + Path.GetExtension(WaveFile);
				int samples = fmt.SampleRate * Seconds;
				int sampleSize = (fmt.BitsPerSample / 8) * fmt.Channels;
				int padBytes = samples * sampleSize;
				var bufsize = (int)(reader.Length - (padBytes * 2));
				var buf = new byte[bufsize];
				int newSampleCount = bufsize / sampleSize;

				reader.Seek(padBytes, SeekOrigin.Begin);
				strTarget.Write(reader.Read(buf, 0, buf.Length));
				reader.Close();
				reader.Dispose();

				var file = new RiffFile(WaveFile, false);
				file.GetChunk<CkData>().Data = buf;

				// adjust marker positions, remove if outside
				foreach (Chunk ch in file.Chunks)
				{
					if (ch.GetType() == typeof(CkCue))
					{
						var me = ch as CkCue;
						//foreach (CuePoint cp in me.CuePoints)
						if (me != null)
							for (int i = me.CuePoints.Count - 1; i >= 0; i--)
							{
								CuePoint cp = me.CuePoints[i];
								cp.Position -= (uint)samples;
								cp.SampleOffset -= (uint)samples;
								if (cp.Position > newSampleCount)
									me.CuePoints.Remove(cp);
							}
					}

						// trim down region lengths (ugly, but necessary due to Vegas rounding rather than truncating)
					else if (ch is CkList)
					{
						var me = ch as CkList;
						foreach (ListChunk lch in me.Chunks)
						{
							if (lch.GetType() == typeof(LiCkLtxt))
							{
								var lt = lch as LiCkLtxt;
								if (lt != null && lt.SampleLength > newSampleCount)
								{
									lt.SampleLength = (uint)newSampleCount;
								}
							}
							else if (lch.GetType() == typeof(LiCkInfoTCOD))
							{
								var tch = lch as LiCkInfoTCOD;
								if (tch != null) tch.Position += (uint)samples;
							}
							else if (lch.GetType() == typeof(LiCkInfoTCDO))
							{
								var tch = lch as LiCkInfoTCDO;
								if (tch != null) tch.Position -= (uint)samples;
							}
						}
					}
				}
				file.Save(temp1name);
				File.Move(WaveFile, temp2name);
				File.Move(temp1name, WaveFile);
				File.Delete(temp2name);
			}