简体   繁体   English

在 NAudio 的内存流中将格式从 wav 更改为 mp3

[英]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?嗨,我正在尝试将内存流中的文本转换为语音 (wav) 将其转换为 mp3,然后在用户页面上播放。所以需要我帮助下一步做什么?

here is my asmx code :这是我的 asmx 代码:

[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.谢谢。

Just wanted to post my example too using NAudio.Lame:只是想使用 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.代码片段:我的显然返回一个字节 [] - 我有一个单独的保存到磁盘方法 b/c 我认为它使单元测试更容易。

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


        }

You need an MP3 compressor library.您需要一个 MP3 压缩器库。 I use Lame via the Yeti Lame wrapper.我通过 Yeti Lame 包装器使用 Lame。 You can find code and a sample project here .您可以在此处找到代码和示例项目。

Steps to get this working:使这个工作的步骤:

  1. Copy the following files from MP3Compressor to your project:将以下文件从MP3Compressor复制到您的项目中:

    • 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 .Lame_enc.dll的项目属性中,将Copy to Output属性设置为Copy if newerCopy always

  3. Edit Lame.cs and replace all instances of:编辑Lame.cs并替换以下所有实例:

     [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.System.Windows.Forms的引用添加到您的项目中(如果它还没有),或者编辑AudioWriter.csWriterConfig.cs以删除引用。 Both of these have a using System.Windows.Forms;这两个都有一个using System.Windows.Forms; that you can remove, and WriterConfig.cs has a ConfigControl declaration that needs to be removed/commented out.您可以删除,并且WriterConfig.cs有一个需要删除/注释掉的ConfigControl声明。

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.完成所有这些后,您应该有一个功能齐全的内存波形文件到 MP3 转换器,您可以使用它将从SpeechSynthesizer获得的 WAV 文件转换为 MP3。

This is a bit old now, but since you haven't accepted the answer I previously provided...这现在有点旧了,但是由于您还没有接受我之前提供的答案...

I have recently built an extension for NAudio that encapsulates the LAME library to provide simplified MP3 encoding.我最近为 NAudio 构建了一个扩展,它封装了 LAME 库以提供简化的 MP3 编码。

Use the NuGet package manager to find NAudio.Lame .使用 NuGet 包管理器查找NAudio.Lame Basic example for using it available here . here提供使用它的基本示例。

Assuming you're trying to convert the output into MP3, you need something that can handle transcoding the audio.假设您正在尝试将输出转换为 MP3,您需要一些可以处理音频转码的东西。 There are a number of tools available, but my personal preference is FFmpeg .有许多可用的工具,但我个人更喜欢FFmpeg It's a command line tool so you will need to take that into account, but otherwise it's very easy to use.它是一个命令行工具,因此您需要考虑到这一点,但除此之外它非常易于使用。

There's lots of information online, but you can start by checking out their documentation here .网上有很多信息,但您可以先在这里查看他们的文档。

I had a similar requirement in .net4.0 to convert 8bit 8Khz mono wav and used the following code我在 .net4.0 中有类似的要求来转换 8bit 8Khz mono wav 并使用以下代码

public void WavToMp3(string wavPath, string fileId)
    {
        var tempMp3Path = TempPath + "tempFiles\\" + fileId + ".mp3";
        var mp3strm = new FileStream(tempMp3Path, FileMode.Create);
        try
        {
            using (var reader = new WaveFileReader(wavPath))
            {
                var blen = 65536;
                var buffer = new byte[blen];
                int rc;
                var bit16WaveFormat = new WaveFormat(16000, 16, 1);
                using (var conversionStream = new WaveFormatConversionStream(bit16WaveFormat, reader))
                {
                    var targetMp3Format = new WaveLib.WaveFormat(16000, 16, 1);
                    using (var mp3Wri = new Mp3Writer(mp3strm, new Mp3WriterConfig(targetMp3Format, new BE_CONFIG(targetMp3Format,64))))
                    {
                        while ((rc = conversionStream.Read(buffer, 0, blen)) > 0) mp3Wri.Write(buffer, 0, rc);
                        mp3strm.Flush();
                        conversionStream.Close();
                    }
                }
                reader.Close();
            }
            File.Move(tempMp3Path, TempPath + fileId + ".mp3");
        }
        finally
        {
            mp3strm.Close();
        }
    }

Prerequists:先决条件:

  1. .net 4 compiled yeti library (to obtain it download this older one ( http://www.codeproject.com/KB/audio-video/MP3Compressor/MP3Compressor.zip ) and convert it to .net4.0 then build the solution to obtain the new version dlls) .net 4 编译的 Yeti 库(要获得它,请下载这个较旧的库( http://www.codeproject.com/KB/audio-video/MP3Compressor/MP3Compressor.zip )并将其转换为 .net4.0 然后构建解决方案获取新版本的dll)
  2. download the NAudio libraries (as Lame support 16bit wav sample only i had to first convert it from 8bit to 16bit wav)下载 NAudio 库(因为 Lame 仅支持 16 位 wav 样本,所以我必须先将其从 8 位转换为 16 位 wav)

I have used a buffer size of 64kpbs (my custom requirement)我使用了 64kpbs 的缓冲区大小(我的自定义要求)

have a try:试一试:

using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new     
Mp3FileReader(inputStream))) 
using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat)) 
{ 
    byte[] bytes = new byte[waveStream.Length]; 
    waveStream.Position = 0;
    waveStream.Read(bytes, 0, waveStream.Length); 
    waveFileWriter.WriteData(bytes, 0, bytes.Length); 
    waveFileWriter.Flush(); 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM