/// <summary> /// Ensure any given wave file path that the file matches with default or base format [16bit 8kHz Mono] /// </summary> /// <param name="strPath">Wave file path</param> /// <returns>True/False</returns> public bool Validate(string strPath) { if (strPath == null) { strPath = ""; } if (strPath == "") { return(false); } clsWaveProcessor wa_val = new clsWaveProcessor(); wa_val.WaveHeaderIN(strPath); if (wa_val.BitsPerSample != BIT_PER_SMPL) { return(false); } if (wa_val.Channels != CHNL) { return(false); } if (wa_val.SampleRate != SMPL_RATE) { return(false); } return(true); }
/// <summary> /// Adapted from - Concatenation Wave Files using C# 2005 by By Ehab Mohamed Essa /// URL - http://www.codeproject.com/useritems/Concatenation_Wave_Files.asp /// </summary> /// <param name="StartFile">Wave file to stay in the begining</param> /// <param name="EndFile">Wave file to stay at the end</param> /// <param name="OutFile">Merged output to wave file</param> /// <returns>True/False</returns> public bool Merge(string strStartFile, string strEndFile, string strOutFile) { if ((strStartFile == strEndFile) && (strStartFile == null)) { return(false); } if ((strStartFile == strEndFile) && (strStartFile == "")) { return(false); } if ((strStartFile == strOutFile) || (strEndFile == strOutFile)) { return(false); } clsWaveProcessor wa_IN_Start = new clsWaveProcessor(); clsWaveProcessor wa_IN_End = new clsWaveProcessor(); clsWaveProcessor wa_out = new clsWaveProcessor(); wa_out.DataLength = 0; wa_out.Length = 0; try { //Gather header data if (!wa_IN_Start.WaveHeaderIN(@strStartFile)) { return(false); } if (!wa_IN_End.WaveHeaderIN(@strEndFile)) { return(false); } if (!Compare(ref wa_IN_Start, ref wa_IN_End)) { return(false); } wa_out.DataLength = wa_IN_Start.DataLength + wa_IN_End.DataLength; wa_out.Length = wa_IN_Start.Length + wa_IN_End.Length; //Recontruct new header wa_out.BitsPerSample = wa_IN_Start.BitsPerSample; wa_out.Channels = wa_IN_Start.Channels; wa_out.SampleRate = wa_IN_Start.SampleRate; wa_out.WaveHeaderOUT(@strOutFile); //Write data - modified code byte[] arrfileStart = GetWAVEData(strStartFile); byte[] arrfileEnd = GetWAVEData(strEndFile); int intLngthofStart = arrfileStart.Length; Array.Resize(ref arrfileStart, (arrfileStart.Length + arrfileEnd.Length)); Array.Copy(arrfileEnd, 0, arrfileStart, intLngthofStart, arrfileEnd.Length); WriteWAVEData(strOutFile, ref arrfileStart); } catch (Exception ex) { throw ex; return(false); } return(true); }
/// <summary> /// Filter out sielence or noise from wave file. The noise or silence frequencies are set in filter constants - /// FILTER_FREQ_HIGH and FILTER_FREQ_LOW. For a given application, some experimentation may be required in /// begining to decide the HIGH and LOW filter frequencies (alternate suggestion are most welcome). /// </summary> /// <param name="strPath">Path for wave file</param> /// <returns>True/False</returns> public bool StripSilence(string strPath) { if (strPath == null) { strPath = ""; } if (strPath == "") { return(false); } clsWaveProcessor wain = new clsWaveProcessor(); clsWaveProcessor waout = new clsWaveProcessor(); waout.DataLength = waout.Length = 0; if (!wain.WaveHeaderIN(@strPath)) { return(false); } waout.DataLength = wain.DataLength; waout.Length = wain.Length; waout.BitsPerSample = wain.BitsPerSample; waout.Channels = wain.Channels; waout.SampleRate = wain.SampleRate; byte[] arrfile = GetWAVEData(strPath); //check for silence int startpos = 0; int endpos = arrfile.Length - 1; //At start try { for (int j = 0; j < arrfile.Length; j += 2) { short snd = ComplementToSigned(ref arrfile, j); if (snd > FILTER_FREQ_LOW && snd < FILTER_FREQ_HIGH) { startpos = j; } else { break; } } } catch (Exception ex) { Console.Write(ex.Message); } //At end for (int k = arrfile.Length - 1; k >= 0; k -= 2) { short snd = ComplementToSigned(ref arrfile, k - 1); if (snd > FILTER_FREQ_LOW && snd < FILTER_FREQ_HIGH) { endpos = k; } else { break; } } if (startpos == endpos) { return(false); } if ((endpos - startpos) < 1) { return(false); } byte[] newarr = new byte[(endpos - startpos) + 1]; for (int ni = 0, m = startpos; m <= endpos; m++, ni++) { newarr[ni] = arrfile[m]; } //write file waout.DataLength = newarr.Length; waout.WaveHeaderOUT(@strPath); WriteWAVEData(strPath, ref newarr); return(true); }
/// <summary> /// Mix two wave files. The mixed data will be written back to the main wave file. /// </summary> /// <param name="strPath">Path for source or main wave file.</param> /// <param name="strMixPath">Path for wave file to be mixed with source.</param> /// <returns>True/False</returns> public bool WaveMix(string strPath, string strMixPath) { if (strPath == null) { strPath = ""; } if (strPath == "") { return(false); } if (strMixPath == null) { strMixPath = ""; } if (strMixPath == "") { return(false); } clsWaveProcessor wain = new clsWaveProcessor(); clsWaveProcessor wamix = new clsWaveProcessor(); clsWaveProcessor waout = new clsWaveProcessor(); wain.DataLength = wamix.Length = 0; if (!wain.WaveHeaderIN(strPath)) { return(false); } if (!wamix.WaveHeaderIN(strMixPath)) { return(false); } waout.DataLength = wain.DataLength; waout.Length = wain.Length; waout.BitsPerSample = wain.BitsPerSample; waout.Channels = wain.Channels; waout.SampleRate = wain.SampleRate; byte[] arrfile = GetWAVEData(strPath); byte[] arrmix = GetWAVEData(strMixPath); for (int j = 0, k = 0; j < arrfile.Length; j += 2, k += 2) { if (k >= arrmix.Length) { k = 0; } short snd1 = ComplementToSigned(ref arrfile, j); short snd2 = ComplementToSigned(ref arrmix, k); short o = 0; // ensure the value is within range of signed short if ((snd1 + snd2) >= -32768 && (snd1 + snd2) <= 32767) { o = Convert.ToInt16(snd1 + snd2); } byte[] b = SignedToComplement(o); arrfile[j] = b[0]; arrfile[j + 1] = b[1]; } //write mixed file waout.WaveHeaderOUT(@strPath); WriteWAVEData(strPath, ref arrfile); return(true); }
/// <summary> /// Increase or decrease volume of a wave file by percentage /// </summary> /// <param name="strPath">Source wave</param> /// <param name="booIncrease">True - Increase, False - Decrease</param> /// <param name="shtPcnt">1-100 in %-age</param> /// <returns>True/False</returns> public bool ChangeVolume(string strPath, bool booIncrease, short shtPcnt) { if (strPath == null) { strPath = ""; } if (strPath == "") { return(false); } if (shtPcnt > 100) { return(false); } clsWaveProcessor wain = new clsWaveProcessor(); clsWaveProcessor waout = new clsWaveProcessor(); waout.DataLength = waout.Length = 0; if (!wain.WaveHeaderIN(@strPath)) { return(false); } waout.DataLength = wain.DataLength; waout.Length = wain.Length; waout.BitsPerSample = wain.BitsPerSample; waout.Channels = wain.Channels; waout.SampleRate = wain.SampleRate; byte[] arrfile = GetWAVEData(strPath); //change volume for (int j = 0; j < arrfile.Length; j += 2) { short snd = ComplementToSigned(ref arrfile, j); try { short p = Convert.ToInt16((snd * shtPcnt) / 100); if (booIncrease) { snd += p; } else { snd -= p; } } catch { snd = ComplementToSigned(ref arrfile, j); } byte[] newval = SignedToComplement(snd); if ((newval[0] != null) && (newval[1] != null)) { arrfile[j] = newval[0]; arrfile[j + 1] = newval[1]; } } //write back to the file waout.DataLength = arrfile.Length; waout.WaveHeaderOUT(@strPath); WriteWAVEData(strPath, ref arrfile); return(true); }