C
C#17mo ago
molioron

❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)

I have this code (using NAudio) for converting a wav byte array to an mp3 byte array:
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, 256))
{
rdr.CopyTo(wtr);
return retMs.ToArray();
}

}
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, 256))
{
rdr.CopyTo(wtr);
return retMs.ToArray();
}

}
Not sure why, but using the function the end of a wav file just gets cut off (examples attached) How can I fix this or are there any alternative ways of conversion?
14 Replies
basically, i am little cat
Pretty sure all variables in using blocks get disposed before return
molioron
molioronOP17mo ago
Should I move the return statement outside of the using statement?
basically, i am little cat
No, you probably just want to store array before return in some variable and then return it
molioron
molioronOP17mo ago
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;
}

}
basically, i am little cat
Ok then idk how to help, sorry
Kouhai
Kouhai17mo ago
Call flush before return Because Dispose get's called after ToArray, so wtr isn't flushed
molioron
molioronOP17mo ago
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
Kouhai
Kouhai17mo ago
Before ToArray not after ThumbsUpSmile
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, 256))
{
rdr.CopyTo(wtr);
wtr.Flush();
return retMs.ToArray();
}
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, 256))
{
rdr.CopyTo(wtr);
wtr.Flush();
return retMs.ToArray();
}
You could also do
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, 256))
{
rdr.CopyTo(wtr);
}
return retMs.ToArray();
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, 256))
{
rdr.CopyTo(wtr);
}
return retMs.ToArray();
molioron
molioronOP17mo ago
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();
}
}
}
Kouhai
Kouhai17mo ago
Yeah pretty much, using statement calls Dispose method after we leave the scope, so retMs isn't getting the latest data from pcmStream Typically Flush is called in the Dispose method, and Naudio does follow that, but because ToArray comes before the Dispose call, flush isn't called
molioron
molioronOP17mo ago
so pcmStream.Flush(); should be called before ToArray()?
Kouhai
Kouhai17mo ago
Yeah, or you could do it like this as well
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, 256))
{
rdr.CopyTo(wtr);
}
return retMs.ToArray();
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, 256))
{
rdr.CopyTo(wtr);
}
return retMs.ToArray();
Calling ToArray after wtr is Disposed
molioron
molioronOP17mo ago
I see, thanks!
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server