[英]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:使这个工作的步骤:
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
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 newer
或Copy always
。
Edit Lame.cs
and replace all instances of:编辑
Lame.cs
并替换以下所有实例:
[DllImport("Lame_enc.dll")]
with:与:
[DllImport("Lame_enc.dll", CallingConvention = CallingConvention.Cdecl)]
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(); } } }
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.cs
和WriterConfig.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:先决条件:
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.