molioron
molioron
CC#
Created by molioron on 7/16/2023 in #help
❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)
I see, thanks!
20 replies
CC#
Created by molioron on 7/16/2023 in #help
❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)
so pcmStream.Flush(); should be called before ToArray()?
20 replies
CC#
Created by molioron on 7/16/2023 in #help
❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)
Thanks! It worked. I have a similar function for the other way arround, should it also get the same treatment?
public static byte[] ConvertMp3ToWav(byte[] mp3File)
{
using (var retMs = new MemoryStream())
using (var ms = new MemoryStream(mp3File))
using (Mp3FileReader reader = new Mp3FileReader(ms))
{
var rs = new RawSourceWaveStream(reader, new WaveFormat(31000, 1));
using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(rs))
{
WaveFileWriter.WriteWavFileToStream(retMs,pcmStream);
return retMs.ToArray();
}
}
}
public static byte[] ConvertMp3ToWav(byte[] mp3File)
{
using (var retMs = new MemoryStream())
using (var ms = new MemoryStream(mp3File))
using (Mp3FileReader reader = new Mp3FileReader(ms))
{
var rs = new RawSourceWaveStream(reader, new WaveFormat(31000, 1));
using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(rs))
{
WaveFileWriter.WriteWavFileToStream(retMs,pcmStream);
return retMs.ToArray();
}
}
}
20 replies
CC#
Created by molioron on 7/16/2023 in #help
❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using(var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using(var rdr = new WaveFileReader(ms))
// originally 128 (as in 128K, but value itself should be 128)
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 256))
{
rdr.CopyTo(wtr);
byte[] result = retMs.ToArray();
wtr.Flush();
return result;
}

}
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using(var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using(var rdr = new WaveFileReader(ms))
// originally 128 (as in 128K, but value itself should be 128)
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 256))
{
rdr.CopyTo(wtr);
byte[] result = retMs.ToArray();
wtr.Flush();
return result;
}

}
Still seems like audio gets cut off
20 replies
CC#
Created by molioron on 7/16/2023 in #help
❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)
done but audio still gets cut off
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using(var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using(var rdr = new WaveFileReader(ms))
// originally 128 (as in 128K, but value itself should be 128)
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 256))
{
rdr.CopyTo(wtr);
byte[] result = retMs.ToArray();
return result;
}

}
public static byte[] ConvertWavToMp3(byte[] wavFile)
{
using(var retMs = new MemoryStream())
using (var ms = new MemoryStream(wavFile))
using(var rdr = new WaveFileReader(ms))
// originally 128 (as in 128K, but value itself should be 128)
using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 256))
{
rdr.CopyTo(wtr);
byte[] result = retMs.ToArray();
return result;
}

}
20 replies
CC#
Created by molioron on 7/16/2023 in #help
❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)
Should I move the return statement outside of the using statement?
20 replies