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

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.

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

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

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}.\nPress 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

c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)的更多相关文章

  1. 怎样将M4A音频格式转换成MP3格式

    因为MP3音频格式应用的广泛性,所以很多时候我们都需要将不同的音频格式转换成MP3格式的,那么如果我们需要将M4A音频格式转换成MP3格式,我们应该怎样进行实现呢?下面我们就一起来看一下吧. 操作步骤 ...

  2. 怎样将flac音频格式转换成MP3格式

    Flac音频格式怎样转换成MP3格式呢?随着现在音频格式的不断多样性,生活中很多时候我们都会遇到音频格式转换的问题,如flac音频转MP3的问题,那么我们应该如何去解决这个问题呢?下面我们就一起去来一 ...

  3. vs2010音频文件压缩 调用lame_enc.dll将WAV格式转换成MP3

    /* //My_lame.h */ #pragma once#include "stdafx.h"#include <windows.h>#include <st ...

  4. .net amr格式文件转换成mp3格式文件的方法

    前言:winform端对于音频文件的格式多有限制,大多数不支持amr格式的文件的播放.但是,手机端传过来的音频文件大多数是amr格式的文件,所以,要想在winform客户端支持音频文件的播放,可以通过 ...

  5. 如何将AAC音频转换成MP3格式

    我们应该怎样将AAC音频转换成MP3格式呢?AAC是一种专为声音数据设计的文件压缩格式,相对于MP3音频来说更加高效,性价比跟高.但是因为MP3音频格式的通用性,我们还是时常需要将AAC音频转换成MP ...

  6. CEF3 HTML5 audio标签为什么不能播放mp3格式的音频文件

    CEF3 HTML5 audio标签 为什么不能播放mp3格式的音频文件   原因略.   解决方法: 找一个最新版的chrome ,我用的是24版本.路径 C:\Documents and Sett ...

  7. jav音频格式转换 ffmpeg 微信录音amr转mp3

    项目背景: 之前公司开发了一个微信公众号,要求把js-sdk录音文件在web网页也能播放.众所周知,html的<audio>标签ogg,mp3,wav,也有所说苹果safari支持m4a格 ...

  8. ffmpeg mp4 mp3 wav flac webm aac ac3 ogg格式转换

    版权声明:本文为博主原创文章,未经允许不得转载. ffmpeg是Linux中转换音频视频文件的常用工具. mp4 to mp3: ffmpeg -i $ID.mp4 -acodec libmp3lam ...

  9. ImageMagick convert多张照片JPG转成pdf格式,pdfunite合并PDF文件

      在认识ImageMagick之前,我***的图像浏览软件是KuickShow,截图软件是KSnapShot,这两款软件都是KDE附带的软件,用起来也是蛮方便的.在一次偶然的机会中,我遇到了Imag ...

随机推荐

  1. golang 垃圾回收 gc

    http://ruizeng.net/golang-gc-internals/ 摘要 在实际使用go语言的过程中,碰到了一些看似奇怪的内存占用现象,于是决定对go语言的垃圾回收模型进行一些研究.本文对 ...

  2. 关于Cocos2d-x数据类型的使用

    常用的是三种数据类型,Value,Vector,Map,翻译成中文就是值,数组,字典.其中字典的意思就是拿着某个关键字去这个数据结构里面找相应的对应的数据. //Value数据类型 Value int ...

  3. e676. 把彩色图像转换为灰色

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorConvertOp op = new ColorConvertOp(c ...

  4. 严格控制GOTO语句

    注意事项 1,使用顺序.选择.循环等有限的基本结构表示程序逻辑. 2,选用的控制结构只准许有一个入口和一个出口 3,程序语句组成容易识别的块,每块只有一个入口和一个出口. 4,复杂结构应该用基本控制结 ...

  5. 转载: erlang连接mysql

    转自:http://blog.csdn.net/flyinmind/article/details/7740540 项目中用到erlang,同时也用到mysql.惯例,google. 但是,按照网上说 ...

  6. MySQL Date函数的正确用法

    以下的文章主要介绍的是MySQL Date函数的实际应用其中包括如何获取当前时间的具体操作,Unix时间的具体应用,时间前后.时间间隔与时间转换的实际内容描述,以下就是文章的主要内容. MySQL D ...

  7. Linux内核剖析 之 内存管理

    1. 内存管理区 为什么分成不同的内存管理区? ISA总线的DMA处理器有严格的限制:仅仅能对物理内存前16M寻址. 内核线性地址空间仅仅有1G,CPU不能直接訪问全部的物理内存. ZONE_DMA  ...

  8. 解决RaycastTarget勾选过多的烦恼

    看过UGUI源码的朋友一定都知道,UI事件会在EventSystem在Update的Process触发.UGUI会遍历屏幕中所有RaycastTarget是true的UI,接着就会发射线,并且排序找到 ...

  9. Java 9的日期时间格式化趋近Unicode区域设置标准

        1.JDK-8148947,DataTimeFormatter的模式字母“g”:正如在LDML中定义的,字母“g”指代一个“简化儒略日期”,简化儒略日期与正常儒略日期的差别在于:(1)简化儒略 ...

  10. ie8下修改input的type属性报错

    摘要: 现在有一个需求如图所示,当用户勾选显示明文复选框时,要以明文显示用户输入的密码,去掉勾选时要变回密文,刚开始想到的就是修改输入框的type来决定显示明文还是密文,使用jQuery的attr来做 ...