C# Save text to speech to MP3 file

15,029

Solution 1

There are multiple options such as saving to an existing stream. If you want to create a new WAV file, you can use the SetOutputToWaveFile method.

reader.SetOutputToWaveFile(@"C:\MyWavFile.wav");

Solution 2

Not my answer, copy paste from How do can I use LAME to encode an wav to an mp3 c#


Easiest way in .Net 4.0:

Use the visual studio Nuget Package manager console:

Install-Package NAudio.Lame

Code Snip: Send speech to a memory stream, then save as mp3:

//reference System.Speech
using System.Speech.Synthesis; 
using System.Speech.AudioFormat;

//reference Nuget Package NAudio.Lame
using NAudio.Wave;
using NAudio.Lame; 


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, "mytest.mp3");
}

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

Solution 3

Often, if something works on a dev workstation but not on a production server, its a permissions issue. two thoughts: Does Lame create temporary files somewhere? If so the IIS process needs write permissions there. When writing the output file, again the IIS process needs permission to write that. ConvertWavStreamToMp3File(ref ms, "mytest.mp3"); "mytest.mp3" will probably need to be a full path, using Server.MapPath()

Share:
15,029

Related videos on Youtube

CodeMonkeyAlx
Author by

CodeMonkeyAlx

I'm just a noob looking to learn... Unlike most newbs I run on an honor system. Yes I might grub for answers but I am also not just going to slap those answers into my code. I will look at them and I will see what they do. Right now my coding level as I like to call it is about 3 on a scale of 10. I know basics if you give me code I can understand what it does, I just cant code it myself just yet. Thats is all and may we all enjoy ourselves here.

Updated on July 14, 2022

Comments

  • CodeMonkeyAlx
    CodeMonkeyAlx almost 2 years

    I am wondering if there is a way to save text to speech data to an mp3 or Wav file format to be played back at a later time?

    SpeechSynthesizer reader = new SpeechSynthesizer();
    reader.Rate = (int)-2;
    reader.Speak("Hello this is an example expression from the computers TTS engine in C-Sharp);
    

    I am trying to get that saved externally so I can play it back later. What is the best way to do this?

  • ankit sharma
    ankit sharma over 7 years
    I had tried same code of yours , works perfect at local but unable to create mp3 file on server. Do you have any idea what configuration we need on server?
  • Cel
    Cel over 7 years
    you need to deploy some dlls on the server for naudio to work e.g. did you put libmp3lame.32.dll and libmp3lame.64.dll in the same folder as your exe?
  • ankit sharma
    ankit sharma over 7 years
    Yes i do have them, i have them in the Bin folder and the root directory as well.But When I run this application on server it create a mp3 file with the size of 2kb always independent of the very large text you enter and the mp3 doesn't play. I think due to some issues on server it cannot create mp3 properly.
  • Cel
    Cel over 7 years
    sounds like you need to do more digging, I do not know what the issue is, at least..