从WAV格式改变为MP3在n音讯内存流 [英] change format from wav to mp3 in memory stream in NAudio

查看:208
本文介绍了从WAV格式改变为MP3在n音讯内存流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好IAM试图文字转换为语音(WAV)中的MemoryStream它转换成MP3,然后发挥它的用户page.so需要我帮助下一步做什么?

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?

这是我的ASMX code:

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();

    }

感谢。

推荐答案

您需要一个MP3 COM pressor库。我通过雪人拉梅包装使用跛脚。你可以在这里找到 code和示例项目

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

步骤得到这个工作:


  1. MP3Com pressor ​​将以下文件复制到您的项目:

  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


Lame_enc.dll 设置复制到输出属性<$ C项目属性$ C>复制如果新或复制总是

修改 Lame.cs 和替换的所有实例:

Edit Lame.cs and replace all instances of:

[DllImport("Lame_enc.dll")]

[DllImport("Lame_enc.dll", CallingConvention = CallingConvention.Cdecl)]`


  • 添加以下code到项目:

  • 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();
            }
        }
    }
    


  • 无论是添加到 System.Windows.Forms的引用到您的项目(如果它不存在的话),或编辑 AudioWriter。 CS WriterConfig.cs 来删除引用。这两个有一个使用System.Windows.Forms的; 您可以删除,而 WriterConfig.cs 有一个 ConfigControl 声明,需要删除/注释掉。

  • 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.

    一旦所有做到这一点,你应该有一个功能在内存波文件,您可以使用到您从 SpeechSynthesizer 成MP3。

    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.

    这篇关于从WAV格式改变为MP3在n音讯内存流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆