zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    How to render client report definition files (.rdlc) directly to the Response stream without preview(转)
    dedecms 5.7后台拿WEBSHELL
    sql注入语句大全
    常用putty命令汇总
    sql学习笔记(要够长)
    Ubuntu自动运行脚本
    backtrack安装优化过程
    原创【bt5非官网版本】 在虚拟机下无法连接网络的解决方案
    第五章——循环结构5.1
    Kali Linux 1.0
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3296422.html
Copyright © 2011-2022 走看看