public static void UseWaveFile() { try { try { string directory = @"C:\Users\Dor Shaar\Desktop\Boiling Water"; WavFilesCreator wavFilesCreator = new WavFilesCreator(directory); foreach (string wavFile in Directory.GetFiles(directory)) { WavFile wav = new WavFile(wavFile); wavFilesCreator.SplitWaveFileByInterval(wav, 7); } } catch (FileNotFoundException e) { throw e; } catch (FormatException e) { Console.WriteLine(e.Message); } } catch (FileNotFoundException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } }
public static void UseWaveFile() { try { foreach (string filePath in Directory.GetFiles(@"Resources\")) { try { WavFile waveFile = new WavFile(filePath); //waveFile.CutFromEnd(waveFile.WaveFilePath, 7); //waveFile.AddNoise(70); waveFile.SplitWaveFileByInterval(10f); } catch (FileNotFoundException e) { throw e; } catch (FormatException e) { Console.WriteLine(e.Message); } } } catch (FileNotFoundException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } }
public void CutFromEnd(WavFile wavFile, FilePath outputFilePath, int secondsToCut) { CutFromWaveFile( wavFile, outputFilePath, wavFile.DurationInSeconds - secondsToCut, wavFile.DurationInSeconds); }
/// <summary> /// Creates a folder with the same BARCODE_DIRECTORY_NAME wave file name, moves the wave file to that folder /// and creates another folder with the splitted wave files. /// </summary> /// <param name="intervalInSeconds"></param> public void SplitWaveFileByInterval(WavFile wavFile, float intervalInSeconds) { // Creates directory "waveFilesDirectory". FilePath waveFilesDirectory = FilePath.CreateFilePath( wavFile.FilePath.DirectoryPath, wavFile.FilePath.NameWithoutExtension); Directory.CreateDirectory(waveFilesDirectory.FileFullPath); mLogger.WriteLine($"New directory created:{waveFilesDirectory.FileFullPath}"); // Moves wave file into directory. FilePath newWaveFilePath = FilePath.CreateFilePath( waveFilesDirectory.FileFullPath, wavFile.FilePath.Name); File.Move(wavFile.FilePath.FileFullPath, newWaveFilePath.FileFullPath); wavFile.FilePath = newWaveFilePath; mLogger.WriteLine($"Wave file renamed to {newWaveFilePath.FileFullPath}"); // Creates directory inside "waveFilesDirectory" for the splitted wave files. FilePath splittedWaveFilesDirectory = FilePath.CreateFilePathWithPrefix( waveFilesDirectory.FileFullPath, "splitted", wavFile.FilePath.NameWithoutExtension); Directory.CreateDirectory(splittedWaveFilesDirectory.FileFullPath); mLogger.WriteLine($"New directory created:{splittedWaveFilesDirectory.FileFullPath}"); int bytesNumberPerSplit = (int)(intervalInSeconds * wavFile.SampleRate * wavFile.BytesPerSample); int splitsNumber = (int)Math.Ceiling( Convert.ToDecimal(wavFile.DataSizeInBytes / bytesNumberPerSplit)); int bytesReaderIndex = 0; mLogger.WriteLine( $"{wavFile.FilePath.Name} will be splited into interval of {intervalInSeconds} seconds"); for (int splitNum = 0; splitNum < splitsNumber; ++splitNum) { byte[] samplesGroup = new byte[bytesNumberPerSplit]; for (int bytesWriterIndex = 0; (bytesWriterIndex < bytesNumberPerSplit) && (bytesReaderIndex < wavFile.DataSizeInBytes); ++bytesWriterIndex, ++bytesReaderIndex) { samplesGroup[bytesWriterIndex] = wavFile.SoundData[bytesReaderIndex]; } FilePath splittedWaveFilePath = FilePath.CreateFilePathWithPrefix( splittedWaveFilesDirectory.FileFullPath, splitNum.ToString(), $"split{WavFile.WavFileExtension}"); using (Stream outputSteam = new FileStream( splittedWaveFilePath.FileFullPath, FileMode.Create, FileAccess.Write)) { wavFile.WriteWaveFileHeaderToStream(outputSteam, bytesNumberPerSplit); outputSteam.Write(samplesGroup, 0, bytesNumberPerSplit); } mLogger.WriteLine($"{splittedWaveFilePath.FileFullPath} was saved"); } }
public void CutFromWaveFile(WavFile waveFile, FilePath outputFilePath, int cutBeginInSeconds, int cutEndInSeconds) { int secondsToCut = cutEndInSeconds - cutBeginInSeconds; if (secondsToCut < 0) { mLogger.WriteLine($"{nameof(cutBeginInSeconds)} is greater then {nameof(cutEndInSeconds)}"); return; } else if ((cutBeginInSeconds < 0) || (cutEndInSeconds > waveFile.DurationInSeconds)) { mLogger.WriteLine($"Illegel value of {nameof(cutBeginInSeconds)} or {nameof(cutEndInSeconds)}"); return; } else { // The method: Copies from start. When reaches endCopyPosition stop to copy and return to // copy when reaches startCopyAgainPosition. int endCopyPosition = cutBeginInSeconds * waveFile.ByteRate; int startCopyAgainPosition = cutEndInSeconds * waveFile.ByteRate; int writer = 0; byte[] cutSoundData = new byte[waveFile.SoundData.Length - (startCopyAgainPosition - endCopyPosition)]; for (int reader = 0; reader < endCopyPosition; ++reader, ++writer) { cutSoundData[writer] = waveFile.SoundData[reader]; } for (int reader = startCopyAgainPosition; reader < waveFile.SoundData.Length; ++reader, ++writer) { cutSoundData[writer] = waveFile.SoundData[reader]; } using (Stream outputSteam = new FileStream( outputFilePath.FileFullPath, FileMode.Create, FileAccess.Write)) { waveFile.WriteWaveFileHeaderToStream(outputSteam, cutSoundData.Length); outputSteam.Write(cutSoundData, 0, cutSoundData.Length); } mLogger.WriteLine($"{outputFilePath.FileFullPath} was saved"); } }
public void CutFromStart(WavFile wavFile, FilePath outputFilePath, int secondsToCut) { CutFromWaveFile(wavFile, outputFilePath, 0, secondsToCut); }