Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change format from wav to mp3 in memory stream in NAudio

Hi there iam trying to convert text to speech (wav) in the memorystream convert it to mp3 and then play it on the users page.so need i help what to do next?

here is my asmx code :

[WebMethod]
public byte[] StartSpeak(string Word)
{
    MemoryStream ms = new MemoryStream();
    using (System.Speech.Synthesis.SpeechSynthesizer synhesizer = new System.Speech.Synthesis.SpeechSynthesizer())
    {
        synhesizer.SelectVoiceByHints(System.Speech.Synthesis.VoiceGender.NotSet, System.Speech.Synthesis.VoiceAge.NotSet, 0, new System.Globalization.CultureInfo("en-US"));
        synhesizer.SetOutputToWaveStream(ms);
        synhesizer.Speak(Word);
    }
    return ms.ToArray();

    }

Thanks.

like image 644
user2431114 Avatar asked Sep 27 '13 19:09

user2431114


People also ask

How do I change a file from WAV to MP3?

To convert a particular WAV file into the MP3 format, go to the "Options" menu., select the "Rip Settings" tab and move the cursor to the "Format" tab. When the Format menu expands, select MP3 from the menu.

How do I convert WAV to bitrate?

In the General Preferences tab, click on Import Settings, located towards the bottom. Click on the menu next to Import Using > WAV Encoder. Then click to change Setting > Custom and a new window will open. In the WAV Encoder window, change the Sample Rate to 44.100 kHz and Sample Size to 16-bit.

Can I export MP3 to WAV?

You can convert an MP3 file into a WAV file on any Windows or Mac computer by using Audacity or iTunes, both of which are free programs. You can also use a free online converter if you don't have access to Audacity or iTunes.

Can Naudio convert MP3 to WAV?

One of the more common support requests with NAudio is how to convert MP3 files to WAV. Here’s a simple function that will do just that: ...and that’s all there is to it.

How much space does a WAV file take up?

WAV files are uncompressed lossless audio, which can take up quite a bit of space, coming in around 10 MB per minute. WAV file formats use containers to contain the audio in “chunks” using the Resource Interchange File Format.

Is it time to convert your WAV to MP3?

WAV audio files are a great way to preserve the complete and accurate quality of a recording in a truly lossless format on your computer. However, if you’re not an audiophile and are concerned about storage space, it might be time to convert them to a more manageable format like MP3. What Is a WAV File?

What is a WAV file?

What Is a WAV File? A Waveform Audio File Format (WAV, pronounced “Wave”) is a raw audio format created by Microsoft and IBM. WAV files are uncompressed lossless audio, which can take up quite a bit of space, coming in around 10 MB per minute.


2 Answers

Just wanted to post my example too using NAudio.Lame:

NuGet:

Install-Package NAudio.Lame

Code Snip: Mine obviously returns a byte[] - I have a separate save to disk method b/c I think it makes unit testing easier.

public static byte[] ConvertWavToMp3(byte[] wavFile)
        {

            using(var retMs = new MemoryStream())
            using (var ms = new MemoryStream(wavFile))
            using(var rdr = new WaveFileReader(ms))
            using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 128))
            {
                rdr.CopyTo(wtr);
                return retMs.ToArray();
            }


        }
like image 181
Wjdavis5 Avatar answered Nov 15 '22 21:11

Wjdavis5


You need an MP3 compressor library. I use Lame via the Yeti Lame wrapper. You can find code and a sample project here.

Steps to get this working:

  1. Copy the following files from MP3Compressor to your project:

    • AudioWriters.cs
    • Lame.cs
    • Lame_enc.dll
    • Mp3Writer.cs
    • Mp3WriterConfig.cs
    • WaveNative.cs
    • WriterConfig.cs

  2. In the project properties for Lame_enc.dll set the Copy to Output property to Copy if newer or Copy always.

  3. Edit Lame.cs and replace all instances of:

    [DllImport("Lame_enc.dll")]
    

    with:

    [DllImport("Lame_enc.dll", CallingConvention = CallingConvention.Cdecl)]
    
  4. Add the following code to your project:

    public static Byte[] WavToMP3(byte[] wavFile)
    {
         using (MemoryStream source = new MemoryStream(wavFile))
         using (NAudio.Wave.WaveFileReader rdr = new NAudio.Wave.WaveFileReader(source))
         {
             WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.BitsPerSample, rdr.WaveFormat.Channels);
    
             // convert to MP3 at 96kbit/sec...
             Yeti.Lame.BE_CONFIG conf = new Yeti.Lame.BE_CONFIG(fmt, 96);
    
             // Allocate a 1-second buffer
             int blen = rdr.WaveFormat.AverageBytesPerSecond;
             byte[] buffer = new byte[blen];
    
             // Do conversion
             using (MemoryStream output = new MemoryStream())
             { 
                 Yeti.MMedia.Mp3.Mp3Writer mp3 = new Yeti.MMedia.Mp3.Mp3Writer(output, fmt, conf);
    
                 int readCount;
                 while ((readCount = rdr.Read(buffer, 0, blen)) > 0)
                     mp3.Write(buffer, 0, readCount);
                 mp3.Close();
    
                 return output.ToArray();
             }
         }
     }
    
  5. Either add a reference to System.Windows.Forms to your project (if it's not there already), or edit AudioWriter.cs and WriterConfig.cs to remove the references. Both of these have a using System.Windows.Forms; that you can remove, and WriterConfig.cs has a ConfigControl declaration that needs to be removed/commented out.

Once all of that is done you should have a functional in-memory wave-file to MP3 converter that you can use to convert the WAV file that you are getting from the SpeechSynthesizer into an MP3.

like image 41
Corey Avatar answered Nov 15 '22 22:11

Corey