In this situation, I'm trying to get the URI of a .WAV audio that is stored in Azure Storage. Here is the service code snippet:
var tempAudioUrl = _blobStorareService.UploadFileToBlob(file.FileName, file.ReadFully(), file.ContentType);
var audioFile = await AudioConverter.ConvertToMp3Async(new Uri(string.Format(tempAudioUrl)));
var audioUrl = _blobStorareService.UploadFileToBlob(Guid.NewGuid().ToString(), audioFile, "audio/mp3");
_blobStorareService.DeleteBlobData(tempAudioUrl);
The code of the AudioConverter class:
public static class AudioConverter
{
public static async Task<byte[]> ConvertToMp3Async(Uri uri)
{
using (var client = new WebClient())
{
var file = await client.DownloadDataTaskAsync(uri);
var target = new WaveFormat(8000, 16, 1);
using (var outPutStream = new MemoryStream())
using (var waveStream = new WaveFileReader(new MemoryStream(file)))
using (var conversionStream = new WaveFormatConversionStream(target, waveStream))
using (var writer = new LameMP3FileWriter(outPutStream, conversionStream.WaveFormat, 32, null))
{
conversionStream.CopyTo(writer);
return outPutStream.ToArray();
}
}
}
}
If anyone knows the answer I would be very grateful.
Before you use WaveFormatConversionStream
, you should use RawSourceWaveStream
first. It can help you solve the issue.
Reason: if you have a 16kHz stereo file, you can't go to an 8kHz mono file in one go.
Related Post: