You must Sign In to post a response.
  • Category: .NET

    How to convert audio from any format to .wav format

    I need to develop a web application to convert any kind of audio to .wav and able to play the converted wav files in the browser.But this should be done programmatically without using any purchased licenses.How can i do this.Though i am new to this but i still tried the below code but it's not working.can anyone help me out.

    My code in .aspx page

    <h4>Select a file to upload:</h4>

    <asp:FileUpload ID="FileUpload1" AllowMultiple="true"
    runat="server"></asp:FileUpload>

    <br />
    <br />

    <asp:Button ID="UploadBtn"
    Text="Save Converted file"
    OnClick="UploadBtn_Click"
    runat="server"></asp:Button>

    <hr />

    <asp:Label ID="UploadStatusLabel"
    runat="server">
    </asp:Label>
    My code in code behind-

    string fileName = string.Empty;
    string savePath = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void UploadBtn_Click(object sender, EventArgs e)
    {

    savePath = Server.MapPath(".") + "\\TempFiles\\";
    if (FileUpload1.HasFile)
    {
    fileName = Server.HtmlEncode(FileUpload1.FileName);
    string extension = System.IO.Path.GetExtension(fileName);

    if ((extension == ".DSS") || (extension == ".ds2") || (extension == ".dss") || (extension == ".WAV"))
    {
    ToWav(fileName, savePath);
    savePath += fileName;
    FileUpload1.SaveAs(savePath);
    UploadStatusLabel.Text = "Your file was uploaded successfully.";
    }
    else
    {
    UploadStatusLabel.Text = "Your file was not uploaded because " +
    "it does not have a .dss or .ds2 or .wav extension.";
    }

    }

    }

    static void ToWav(string filename, string path)
    {
    using (DsReader dr = new DsReader(filename))
    {
    IntPtr format = dr.ReadFormat();
    using (WaveWriter ww = new WaveWriter(File.Create(path + ".wav"), AudioCompressionManager.FormatBytes(format)))
    {
    byte[] data = dr.ReadData();
    ww.WriteData(data);
    }
    }
    }
    With the above code it creates a wav file in my application folder but it asks to purchase it.Here i am attaching the screen shots to show where i got strucked.
  • #768302
    Hi,

    You are using third party namespaces but it is asking for purchase option.

    using (DsReader dr = new DsReader(filename))
    {
    IntPtr format = dr.ReadFormat();
    using (WaveWriter ww = new WaveWriter(File.Create(path + ".wav"), AudioCompressionManager.FormatBytes(format)))
    {
    byte[] data = dr.ReadData();
    ww.WriteData(data);
    }

  • #768305
    Only C# is unable to help you more, as there is no direct support to media classes from c#, I think you need to take support of third party DLL for it, There is this NuGet package called NAudio, download it from following path
    https://www.nuget.org/packages/NAudio/
    This provides awesome functionality for what you want to do, see below snippet

    using NAudio.Wave;

    private static void ConvertMp3ToWav(string _inPath_, string _outPath_)
    {
    using (Mp3FileReader mp3 = new Mp3FileReader(_inPath_))
    {
    using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
    {
    WaveFileWriter.CreateWaveFile(_outPath_, pcm);
    }
    }
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments