How to convert .spx to .mp3?

Jun 15, 2012 at 10:54 AM

Hi,balistof

Can you give me a demo with "converting .spx to .mp3" please?

I don't know any thing about Media,~,,,,:(

Oct 2, 2012 at 8:56 PM

Have you found a solution?

Oct 5, 2012 at 6:19 PM

                SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow, (int)speexMsg.SampleRate, true);
                Mp3WriterConfig config = new Mp3WriterConfig();

                using (MemoryStream outStream = new MemoryStream())
                {
                    using (Mp3Writer mp3 = new Mp3Writer(outStream, config))
                    {
                        //Now we need to convert the speex to PCM and write it out to the MP3
                        using (MemoryStream PCM = new MemoryStream(speexMsg.SpeexData))
                        {
                            int i = 0;
                            int bytesRead = 0;
                            while (i < speexMsg.SpeexData.Length)
                            {
                                short[] outData = new short[320];
                                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();
                        return outStream.ToArray();
                    }
                }
Oct 24, 2012 at 7:22 PM

The above was using silverlight nspeex. Here is Speex to Mp3 and Mp3 to Speex using Nspeex and Naudio.

Using a SpeexMsg as speexMsg

 

SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow);
Mp3WriterConfig config = new Mp3WriterConfig();
string fileName = string.Format("C:\\message_{0}.mp3", messageId);

using (Mp3Writer mp3 = new Mp3Writer(new FileStream(fileName, 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();
}

Next is mp3 bytes to speex:



private SpeexMsg GetSpeexFromMp3Bytes(byte[] mp3Bytes)
{
    //First get the pcm bytes then encode them to speex bytes.
    using (MemoryStream ms = new MemoryStream(mp3Bytes))
    {
        using (NAudio.Wave.Mp3FileReader reader = new NAudio.Wave.Mp3FileReader(ms))
        {
            using (NAudio.Wave.WaveStream pcmStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(reader))
            {
                byte[] pcmBytes = new byte[(int)pcmStream.Length];
                pcmStream.Read(pcmBytes, 0, (int)pcmStream.Length);

                SpeexMsg speexMsgFromMp3 = GetSpeexFromRaw(pcmBytes);
                speexMsgFromMp3.Duration = (byte)pcmStream.TotalTime.Duration().TotalSeconds;
                return speexMsgFromMp3;
            }
        }
    }
}

private SpeexMsg GetSpeexFromRaw(byte[] pcmBytes)
{
    SpeexEncoder encoder = new SpeexEncoder(BandMode.Narrow);
    int testSize = encoder.FrameSize;

    //Convert to short.
    var inDataSize = pcmBytes.Length / 2;
    inDataSize = inDataSize - inDataSize % encoder.FrameSize;
    short[] inData = new short[inDataSize];
    int index = 0;
    for (index = 0; index < inDataSize; index++)
        inData[index] = BitConverter.ToInt16(pcmBytes, index * 2);

    int bytesEncoded = 20;
    using (MemoryStream ms = new MemoryStream())
    {
        int i = 0;
        while (i < inData.Length)
        {
            byte[] outData = new byte[testSize];
            bytesEncoded = encoder.Encode(inData, i, encoder.FrameSize, outData, 0, testSize);

            ms.Write(outData, 0, bytesEncoded);

            i += encoder.FrameSize;
        }
        ms.Flush();

        var newMsg = new SpeexMsg(SpeexMsg.SpeexMsgSR.SR8000, ms.ToArray());
        newMsg.FrameSize = bytesEncoded;
        return newMsg;
    }
}
May 17, 2013 at 3:12 AM
allencf1 wrote:
SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow, (int)speexMsg.SampleRate, true); Mp3WriterConfig config = new Mp3WriterConfig(); using (MemoryStream outStream = new MemoryStream()) { using (Mp3Writer mp3 = new Mp3Writer(outStream, config)) { //Now we need to convert the speex to PCM and write it out to the MP3 using (MemoryStream PCM = new MemoryStream(speexMsg.SpeexData)) { int i = 0; int bytesRead = 0; while (i < speexMsg.SpeexData.Length) { short[] outData = new short[320]; 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(); return outStream.ToArray(); } }
Where are Mp3WriterConfig, Mp3Writer come from ? which reference shall use in silverlight project? thank you !