Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to cut Wav Audio File based on NAudio

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "C# based on NAudio how to achieve Wav audio file cutting", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "C# based on NAudio how to achieve Wav audio file cutting" it!

Preface

C# cuts Wav audio files based on NAudio tools, cutting one audio file into multiple audio files

Note: before calling the method, you need to import NAudio.dll or search the NuGet program manager for NAudio and install it.

This article is cut by time.

Implementation code using NAudio.Wave;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace XXX.util {public static class WavFileUtils {/ cut Wav audio files based on NAudio tool (PCM format only) / object file / output file / start time / end time public static void TrimWavFile (string inPath, string outPath, TimeSpan cutFromStart) TimeSpan cutFromEnd) {using (WaveFileReader reader = new WaveFileReader (inPath)) {int fileLength = (int) reader.Length Using (WaveFileWriter writer = new WaveFileWriter (outPath, reader.WaveFormat)) {float bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000F; int startPos = (int) Math.Round (cutFromStart.TotalMilliseconds * bytesPerMillisecond); startPos = startPos-startPos% reader.WaveFormat.BlockAlign; int endPos = (int) Math.Round (cutFromEnd.TotalMilliseconds * bytesPerMillisecond) EndPos = endPos-endPos% reader.WaveFormat.BlockAlign; / / determine whether the end position is out of bounds endPos = endPos > fileLength? FileLength: endPos; TrimWavFile (reader, writer, startPos, endPos) } / remerge wav files / read stream / / write stream / / start stream / end stream private static void TrimWavFile (WaveFileReader reader, WaveFileWriter writer, int startPos Int endPos) {reader.Position = startPos Byte [] buffer = new byte [1024]; while (reader.Position

< endPos) { int bytesRequired = (int)(endPos - reader.Position); if (bytesRequired >

0) {int bytesToRead = Math.Min (bytesRequired, buffer.Length); int bytesRead = reader.Read (buffer, 0, bytesToRead); if (bytesRead > 0) {writer.Write (buffer, 0, bytesRead) }}}

Call:

String filePath = "D:\\ wav\\ test.wav"; / / the path to the file to be cut int cutTimeSpan = 20ram / the time segment time to be cut (seconds) FileInfo fi = new FileInfo (filePath); / / the length of time to get the recording file (seconds) int fileTime = (int) Util.Cover (Util.GetVoiceTime (filePath)) / 1000X hand / calculate the number of equal pieces of the file to be cut decimal fileNum = Math.Ceiling ((decimal) fileTime / cutTimeSpan); int I = 0 While (I < fileNum) {string nowTime = Util.GetTimeStamp (); / / current timestamp / / absolute address of files saved after cutting var outputPath = System.IO.Path.Combine (fi.Directory.FullName, string.Format ("{0} _ {1} {2}", fi.Name.Replace (fi.Extension, "), nowTime, fi.Extension); / / start time of cutting TimeSpan cutFromStart = TimeSpan.FromSeconds (I * cutTimeSpan) / / end time of cutting TimeSpan cutFromEnd = cutFromStart + TimeSpan.FromSeconds (cutTimeSpan); / / Audio cutting WavFileUtils.TrimWavFile (recordFile.FilePath, outputPath, cutFromStart, cutFromEnd); iTunes;}

Util class:

Using Shell32;using System;using System.Diagnostics;using System.IO;using System.Net;using System.Net.Sockets;using System.Text.RegularExpressions;using System.Threading;using System.Windows.Forms Namespace XXX.util {class Util {/ public static string GetTimeStamp () {TimeSpan ts = DateTime.UtcNow-new DateTime (1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64 (ts.TotalMilliseconds). ToString () } / return audio duration / public static string GetVoiceTime file path / public static string GetVoiceTime (string SongPath) {string dirName = Path.GetDirectoryName (SongPath); string SongName = Path.GetFileName (SongPath); ShellClass sh = new ShellClass () Folder dir = sh.NameSpace (dirName); FolderItem item = dir.ParseName (SongName); string SongTime = Regex.Match (dir.GetDetailsOf (item,-1), "\\ d:\\ d {2}:\\ d {2}"). Value;// returns audio duration return SongTime } / time format to millisecond value / public static long Cover (string time) {string [] a = time.Split (':') If (long.Parse (a [0]) = = 0 & & long.Parse (a [1]) = = 0) {return long.Parse (a [2]) * 1000 } else if (long.Parse (a [0]) = = 0 & & long.Parse (a [1])! = 0) {return (long.Parse (a [1]) * 60 + long.Parse (a [2])) * 1000 } else if (long.Parse (a [0])! = 0 & & long.Parse (a [1]) = 0) {return ((long.Parse (a [0]) * 60 * 60) + long.Parse (a [2])) * 1000 } else if (long.Parse (a [0])! = 0 & & long.Parse (a [1])! = 0) {return (long.Parse (a [0]) * 60) + long.Parse (a [1])) * 60) * 1000;} return 0;} effect

Thank you for reading, the above is "C# based on NAudio how to achieve Wav audio file clipping" content, after the study of this article, I believe you on C# based on NAudio how to achieve Wav audio file clipping this problem has a deeper understanding, the specific use of the need for everyone to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report