1

I'm new to C# and having a ball learning. I've built a text to speech console app for myself that i'm really happy with.

Because I'm so green, but getting a massive buzz from learning, I can't go past the code in this post by wjdavis5 without learning how to use it.

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

from this post:

change format from wav to mp3 in memory stream in NAudio

I understand the syntax, mostly, but I'm so green that I'm struggling to understand how to get the wavFile into the method.

Its basic C# stuff I'm obviously struggling with, and this is the first time I've been compelled to ask, but i just have to know!

Just look at that beautiful code!

Cheers Andrew

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Andrew Haas
  • 51
  • 10
  • 1
    what are you trying to do exactly. I just built an MP3 player in WPF and the Media Element plays wav files without any third party reference. Are you trying to convert to wav or what are you trying to do? – Halonic Aug 04 '18 at 12:28
  • At the moment, i am recording the wav output of the soundcard to a file. The reason i did that is i have two instances of Speech synthesizer running with different languages. (I am also using SSML...its a long story). What I'm learning is how to set the output to both the default audio, and also create an mp3 at the same time. It creates a wav just fine, im currently converting that with vlc afterwards, what im hoping for is to learn to create it in C# as the program runs. – Andrew Haas Aug 04 '18 at 15:21

1 Answers1

3
using System;
using System.Text;
using System.IO;
using System.Speech.Synthesis;
using System.Speech.AudioFormat;
using NAudio.Wave;
using NAudio.Lame;

namespace Lame_Mp3_Test
{
   class Program
   {

    public static void ConvertWavStreamToMp3File(ref MemoryStream ms, string 
savetofilename)
    {
        //rewind to beginning of stream
        ms.Seek(0, SeekOrigin.Begin);

        using (var retMs = new MemoryStream())
        using (var rdr = new WaveFileReader(ms))
        using (var wtr = new LameMP3FileWriter(savetofilename, rdr.WaveFormat, 
             LAMEPreset.VBR_90))
        {
            rdr.CopyTo(wtr);
        }

     }



    static void Main(string[] args)
      {



        using (SpeechSynthesizer reader = new SpeechSynthesizer())
        {
            //set some settings
            reader.Volume = 100;
            reader.Rate = 0; //medium

            //save to memory stream
            MemoryStream ms = new MemoryStream();
            reader.SetOutputToWaveStream(ms);

            //do speaking
            reader.Speak("This is a test mp3");

            //now convert to mp3 using LameEncoder or shell out to audiograbber
            ConvertWavStreamToMp3File(ref ms, "c:\\test\\mytest.mp3");
        }
     }
  }

}

Andrew Haas
  • 51
  • 10
  • Shameless plug: https://github.com/skittleson/SynthesizerAudio uses media encoder or lame depending on the OS. a few useful configurations as well. – Spencer Oct 16 '21 at 19:56