• c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)


    Have you been in need of converting mp3 audios to wav audios?  If so, the skill in this article provides you a chance to manage the mp3-to-wav conversion by yourself.

    The wave audio files are useful because they retain the first-generation archived file with high quality and simple file structures. But without a proper tool, it can still be difficult for us to so the conversion. And now I will introduce you to NAudio, an opensource library written in C#, which provide API to meet our needs.

    NAudio Overview

    NAudio is a .NET library with dozens of useful facilities to speed up the development of audio-related project in .NET. The file formats it concerns include .wav, .mp3 and .aiff and the functionalities on these format have undergone a restrict and long-standing testing because the project had been under development since 2001.

    Get Started with NAudio

    NAudio is distributed as two .dll files, along with an XML file to facilitate intenllisense documentation. We can download the latest version of NAudio from here and uncompress the .zip file into any directory, which looks like the following

    Unzip the archive and files are released. Fig-1: Unzip the archive and release two DLLs and one XML file.

    We use Visual C# 2010 Express to develop our application and Visual C# Express const nothing since it is free online. Then we set up a NAudio environment following the steps below:

    1. create a solution with arbitrary name.
    2. right click the ‘References’ on in the Solution Explorer.
    3. click the ‘Add Reference’, select ‘Browse’ tab.
    4. traverse to directory where we keep our DLLs, select two DLLs and press ‘OK’.

    Hence we finishing adding NAudio DLLs into our environment and we can start coding.

    naudio-024 Fig-2: Right click the ‘Reference’ and open the dialog.

    naudio-03 Fig-3: Traverse to the directory keeping two DLLs.

    naudio-04 Fig-4: The NAudio program sets are included in the reference.

    Convert MP3 to WAV with NAudio

    In NAudio, we are offered many facilities. However, what we need in this example is just one class, NAudio.Wave. So add the following two line beneath the other using commands.

    using NAudio;
    using NAudio.Wave;

    To complete conversion from MP3 to Wav, we only need to follow three steps:

    1. read in the mp3 file with Mp3FileReader class.
    2. get wave stream from the MP3 stream via CreatePcmStream interface.
    3. write the wave stream into a file with WaveFileWriter class.

    All the three steps above can be easily summarized into the following C# code with comments.

    using (Mp3FileReader reader = new Mp3FileReader(mp3file))
              {
                  using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                  {
                      WaveFileWriter.CreateWaveFile(wavfile, pcmStream);
                  }
              }

    Until now have we finished the work of converting a MP3 file to a Wave file. Surprisingly simple, isn’t it?

    Complete Code in C# with NAudio

    In this section, I simply produce the complete source code that can be built and run.

    using System;
    using System.Collections.Generic;
    using System.Text;

    using NAudio; using NAudio.Wave;

    namespace Mp3ToWav { class Program { static void Main(string[] args) { string mp3file;

    //Try to read a mp3 file path until it gets valid one. do { do { Console.Out.Write("Please enter the mp3 path:"); mp3file = Console.In.ReadLine(); } while(!System.IO.File.Exists(mp3file)); } while (!mp3file.EndsWith(".mp3"));

    //Generate the wav file path for output. string wavfile = mp3file.Replace(".mp3", ".wav"); string wavpath = wavfile;

    //Get audio file name for display in console. int index = wavfile.LastIndexOf("\"); string wavname = wavfile.Substring(index+1, wavfile.Length-index-1); index = mp3file.LastIndexOf("\"); string mp3name = mp3file.Substring(index+1, mp3file.Length-index-1);

    //Display message. Console.Out.WriteLine("Converting {0} to {1}", mp3name, wavname);

    //step 1: read in the MP3 file with Mp3FileReader. using (Mp3FileReader reader = new Mp3FileReader(mp3file)) {

    //step 2: get wave stream with CreatePcmStream method. using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)) {

    //step 3: write wave data into file with WaveFileWriter. WaveFileWriter.CreateWaveFile(wavfile, pcmStream); } } Console.Out.WriteLine("Conversion finish and wav is saved at {0}. Press any key to finish.", wavpath); Console.In.ReadLine(); } } }

    Conclusion

    In this article, I introduce a simplified and self-aided method to convert a mp3 audio file to a wave audio file with .NET library NAudio. To guide you through all the steps, I also show you the steps to include the DLLs into the reference, in which we can access the classes encapsulated in the DLL. And I hope you can find your way to complete this work.

    If you like this article, please share it with your friends on facebook, myspace, twitter or any other SNS or media by clicking the retweet buttons. If you have any problem about this article, please contact the author without hesitance.

    转:http://www.assembleforce.com/2012-07/use-naudio-library-to-convert-mp3-audio-into-wav-audio.h

  • 相关阅读:
    uni-app引入自己ui写的icon
    2020.07.17
    读uni-app
    图片上传,配置php以及vue简单实现
    vue子给父组件传值
    vue父子传值以及监听这个值,把值拿到子组件使用不报警告
    vue局部跳转,实现一个类似选项卡tab功能
    vuex的一个demo,数据拿到界面上
    vue-day06&day07----路由、路由常用的配置项、vue路由的内置组件、vue路由跳转的方式、路由传值、路由解耦(路由传值的一种方式)、编程式导航、嵌套路由children、路由元信息meta、路由生命周期(路由守卫)、路由懒加载、vue/cli、webpack配置alias、vue/cli中配置alias、vue-router底层原理、单页面开发的缺点、路由报错
    vue-day06----过渡&动画(transition)、axios、axios以post方式请求数据、axios常用的配置项、axios拦截器、qs处理数据、vue中async和await的使用
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3296422.html
Copyright © 2020-2023  润新知