0

I have to convert spx audio file (in ogg format) to mp3 file. I've tried a couple of thing and so far nothing is working.

I've tried using the LameMP3FileWriter from the Naudio.Lame library.

private void WriteOggStreamToMp3File(Stream oggStream, string mp3FileName)
{
    var format = new WaveFormat(8000, 1);
    using (var mp3 = new LameMP3FileWriter(mp3FileName, format, LAMEPreset.ABR_128))
    {
        oggStream.Position = 0;
        oggStream.CopyTo(mp3);
    }
}

Doesn't work great as the outputted mp3 file is nothing but static noise.

I've also found this sample from the NSpeex codeplex page (https://nspeex.codeplex.com/discussions/359730) :

private void WriteOggStreamToMp3File(Stream oggStream, string mp3FileName)
{
    SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow);
    Mp3WriterConfig config = new Mp3WriterConfig();

    using (Mp3Writer mp3 = new Mp3Writer(new FileStream(mp3FileName, FileMode.Create), config))
    {
        int i = 0;
        int bytesRead = 0;
        while (i < speexMsg.SpeexData.Length)
        {
            short[] outData = new short[160];
            bytesRead = decoder.Decode(speexMsg.SpeexData, i, speexMsg.FrameSize, outData, 0, false);

            for (int x = 0; x < bytesRead; x++)
                mp3.Write(BitConverter.GetBytes(outData[x]));

            i += speexMsg.FrameSize;
        }

        mp3.Flush();
    }
}

Unfortunately, Mp3WriterConfig and Mp3Writer are not part of the current library (NSpeex). And I have no idea what "speexMsg" is supposed to be.

So my question is : how can I convert a spx (in a ogg file) to mp3 using c#?

Kinetic
  • 2,640
  • 17
  • 35

1 Answers1

0

Conversions like this need to be done in two stages. First decode from ogg to PCM. Then encode from PCM to WAV. So if there are problems, a great way to debug is to create a WAV file from the decoded ogg first of all. And that allows you to listen to the decoded audio and check it is all OK. Then you can tackle the second stage of encoding to MP3. You can use the NAudio WaveFileWriter class to create your WAV file.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • I assume that is possible with NSpeex. It would be best to ask at the project site. – Mark Heath Sep 01 '16 at 10:42
  • Yeah, i've done that. The NSpeex codeplex page doesn't seem to be active at all, so i'm not keeping my hope up. In the meantime, our users will be using VLC to manually do the conversion. I'll post my complete solution if I ever find it. – Kinetic Sep 01 '16 at 12:24