I have a file .ogg and i need to convert it to .wav. I'm tryng to use Opus.Net (that use NAudio) but i get this exception in OpusDecoder.Decode():
"System.Exception: 'Decoding failed - InvalidPacket'"
This is the code:
byte[] audioBynary = File.ReadAllBytes($"{filePath}{fileOgg}");
_decoder = OpusDecoder.Create(48000, 1);
var pcmBinary = _decoder.Decode(audioBynary, audioBynary.Length, out int decodedLenght);
WaveFileWriter.CreateWaveFile($"{filePath}{fileWav}", new WaveFileReader(new MemoryStream(pcmBinary)));
This works for me, I read the ogg/opus file with Concentus, copy it's bytes to a memoryStream and then use it to create a RawSourceWaveStream.
I can get an ISampleProvider from the RawSourceWaveStream, which is what you need to feed
WaveFileWriter.CreateWaveFile16.
Voilá
var filePath = $#"C:\Users\blabla\foo\bar\";
var fileOgg = "testAudio.ogg";
var fileWav = "testAudio.wav";
using (FileStream fileIn = new FileStream($"{filePath}{fileOgg}", FileMode.Open))
using (MemoryStream pcmStream = new MemoryStream())
{
OpusDecoder decoder = OpusDecoder.Create(48000, 1);
OpusOggReadStream oggIn = new OpusOggReadStream(decoder, fileIn);
while (oggIn.HasNextPacket)
{
short[] packet = oggIn.DecodeNextPacket();
if (packet != null)
{
for (int i = 0; i < packet.Length; i++)
{
var bytes = BitConverter.GetBytes(packet[i]);
pcmStream.Write(bytes, 0, bytes.Length);
}
}
}
pcmStream.Position = 0;
var wavStream = new RawSourceWaveStream(pcmStream, new WaveFormat(48000, 1));
var sampleProvider = wavStream.ToSampleProvider();
WaveFileWriter.CreateWaveFile16($"{filePath}{fileWav}", sampleProvider);
Related
I am sending a compressed byte array to a IIS server.
var byteXML = Encoding.UTF8.GetBytes(aufXML.ToString());
using (var result = new MemoryStream())
{
using (var compressionStream = new GZipStream(result, CompressionMode.Compress))
{
compressionStream.Write(byteXML, 0, byteXML.Length);
compressionStream.Flush();
}
byteXML = result.ToArray();
}
When I receive the data over the webserver I am decompressing the data:
using (var source = new MemoryStream(trafficAnhangXml))
{
byte[] lengthBytes = new byte[trafficAnhangXml.Length];
source.Read(lengthBytes, 0, trafficAnhangXml.Length);
var length = BitConverter.ToInt32(lengthBytes, 0);
using (var decompressionStream = new GZipStream(source, CompressionMode.Decompress))
{
var result = new byte[length];
decompressionStream.Read(result, 0, length);
trafficAnhangXml = result;
}
}
I try to get the InnerXML like this:
appPath = SaveFileAsync("D3.XML", trafficVorgangXml, systemUser).Result;
xDoc.Load(appPath);
receive.VorgangsXML = xDoc.InnerXml;
And then I get the following error:
Error: root element is missing
I used this code to decode bytes sent from the server
packets = SimpleBSON.Load(ReceivedBytes);
for (int i = 0; i < packets["mc"]; i++)
{
BSONObject packet = packets["m" + i] as BSONObject;
//here i can use the received packet
packet["hey"] = "hello";
}
But I am struggling in encoding it back again
I am using Kernys.BSON
I tried this
var obj = new BSONObject();
obj["m" + 0] = new BSONObject();
obj["m" + 0]["hey"] = "hi";
But for some reason this is not working
this is how I fixed it
var GPd = new BSONObject();
GPd["m" + 0] = new BSONObject();
GPd["m" + 0]["hey"] = "hi";
GPd["mc"] = 1;
byte[] mainsend = SimpleBSON.Dump(GPd);
MemoryStream memoryStream = new MemoryStream();
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
byte[] bsonDump = SimpleBSON.Dump(GPd);
binaryWriter.Write(bsonDump.Length + 4);
binaryWriter.Write(bsonDump);
}
//memorystream.ToArray is the encoded bytes
}
I am compressing a json using deflate compression technique and saving to sql server database. The json contains values from any culture ie. th-TH, zh-TW. The compressed string is getting saved successfully in database.
Json includes data like {"#id":"2113","description":"อาหารเช้าคอนติเนนทัล"}
Now when i read the same data from db, i convert it to bytes as
Encoding encoding = Encoding.UTF8;
encoding.GetBytes(data ?? string.Empty)
The compression like this
public static string Compress(this string data, CompressionTypeOptions compressionType)
{
var bytes = Compress(Encoding.UTF-8.GetBytes(data ?? string.Empty), compressionType);
return Encoding.UTF-8.GetString(bytes);
}
}
private static byte[] Compress(byte[] data, CompressionTypeOptions compressionType)
{
using (var memoryStream1 = new MemoryStream(data))
{
using (var memoryStream2 = new MemoryStream())
{
using (var compressionStream = CreateCompressionStream(compressionType, (Stream)memoryStream2,
CompressionMode.Compress))
{
CopyTo((Stream)memoryStream1, compressionStream);
compressionStream.Close();
return memoryStream2.ToArray();
}
}
}
}
Then decompressing like this
using (var memoryStream = new MemoryStream(data))
{
using (var compressionStream = CreateCompressionStream(compressionType, (Stream)memoryStream,
CompressionMode.Decompress))
return ReadAllBytesFromStream(compressionStream);
}
Here is ReadAllBytesFromStream definition
private static byte[] ReadAllBytesFromStream(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
var buffer1 = new byte[1];
while (true)
{
int count = stream.Read(buffer1, 0, 1);
if (count != 0)
memoryStream.Write(buffer1, 0, count);
else
break;
}
var length = memoryStream.Length;
var buffer2 = new byte[length];
memoryStream.Position = 0L;
memoryStream.Read(buffer2, 0, (int)length);
return buffer2;
}
}
Getting error at int count = stream.Read(buffer1, 0, 1); as
'System.IO.InvalidDataException'
'Unknown block type. Stream might be corrupted.'
Any help is appreaciated
Here I have some codes for encoding wma audio files..Its works perfectly.but uploading out put file to server ,some error happend.Its shows output file should be meet the
requirements like rate should be in 16000
public void ConvertToWMA(string tempFilePath, string tempFileName, string audioType)
{
WaveFormat form = new WaveFormat(16000, 16, 1);
using (WmaStream str = new WmaStream(tempFilePath + tempFileName, form))
{
string profileData;
using (StreamReader reader = new StreamReader(File.OpenRead("audio.prx")))
{
profileData = reader.ReadToEnd();
}
IWMProfileManager profileManager;
IWMProfile wmProfile = null;
profileManager = WM.CreateProfileManager();
profileManager.LoadProfileByData(profileData, out wmProfile);
WMProfile wmp = new WMProfile(str.Profile);
NAudio.WindowsMediaFormat.WmaWriter ww = new NAudio.WindowsMediaFormat.WmaWriter(new FileStream(#"D:\wma\conv\test.wma", FileMode.Create), form, wmProfile);
byte[] buff = null;
int read = 0;
buff = new byte[form.AverageBytesPerSecond];
read = str.Read(buff, 0, buff.Length);
while ((read > 0))
{
ww.Write(buff, 0, read);
read = str.Read(buff, 0, buff.Length);
}
}
}
how can get rid of this issue.someone please help me..
{
var temp = tempFilePath + tempFileName;
using (var reader = new MediaFoundationReader(temp))
{
// Create a wave format for 16-bit pcm at 8000 samples per second.
int channels = reader.WaveFormat.Channels;
int rate = 8000;
int rawsize = 2;
int blockalign = rawsize * channels; // this is the size of one sample.
int bytespersecond = rate * blockalign;
//MediaFoundationEncoder.enc(reader, "test.mp3");
var midformat =
WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm,
rate,
channels,
bytespersecond,
blockalign,
rawsize * 8);
// And a conversion stream to turn input into 16-bit PCM.
//var midstream = new MediaFoundationResampler(reader, midformat);
// var outstream = new PcmToALawConversionStream(midstream);
// var outstream = new PcmToALawConversionStream(midstream);
//var converted16Bit = new SampleToWaveProvider16(mixer);
//
// now for MP3, we need to upsample to 44.1kHz. Use MediaFoundationResampler
using (var resampled = new MediaFoundationResampler(
reader, midformat))
{
var outstream = new PcmToALawConversionStream(resampled);
// var desiredBitRate = 16000; // ask for lowest available bitrate
//MediaFoundationEncoder.EncodeToWma(outstream,
// "mixedtets10.wma", desiredBitRate);
WaveFileWriter.CreateWaveFile("mixedtets10.wma", outstream);
//NAudio.WindowsMediaFormat.WmaWriter ww = new NAudio.WindowsMediaFormat.WmaWriter(new FileStream(#"D:\wma\conv\test1.wma", FileMode.Create), midformat, outstream);
}
// NAudio.WindowsMediaFormat.WmaWriter ww = new NAudio.WindowsMediaFormat.WmaWriter(new FileStream(#"D:\wma\conv\test1.wma", FileMode.Create), midformat, outstream);
//NAudio.WindowsMediaFormat.WmaWriter Ww=
// The output stream is our custom stream.
//var outstream = new PcmToALawConversionStream(midstream);
}
}
Background: I am consuming a service which returns data with a MIME type of audio/wav. I need to provide a playback mechanism for this audio (currently built as an MVC application). As an example, my endpoint looks something like https://audio.fooservice.com/GetAudio?audioId=123
The audio is 8kHz, 1-channel u-law.
Due to varying format support across browsers when using the HTML5 <audio> tag, I am unable to use the original u-law wav because Internet Explorer will not play it.
My proposed solution is to do a real-time conversion from the source format to mp3.
I've cobbled together a partially working solution from various other questions here and in the NAudio forums, but it throws an exception as noted in the comments below:
private void NAudioTest(string url)
{
Stream outStream = new MemoryStream();
var format = WaveFormat.CreateMuLawFormat(8000, 1);
using (Stream ms = new MemoryStream())
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
using (Stream stream = request.GetResponse().GetResponseStream())
{
using (var reader = new RawSourceWaveStream(stream, format))
{
// reader is not seekable; we need to convert to a byte array to seek
var bytes = reader.ToByteArray();
// create a new stream from the byte aray
var seekableStream = new MemoryStream(bytes);
// instantiating a WaveFileReader as follows will throw an exception:
// "System.FormatException: Not a WAVE file - no RIFF header"
using (var waveReader = new WaveFileReader(seekableStream))
{
using (var pcmStream = WaveFormatConversionStream.CreatePcmStream(waveReader))
{
var pcmBytes = pcmStream.ToByteArray();
var mp3 = pcmBytes.ToMp3();
}
}
}
}
}
}
public static class StreamExtensions
{
public static byte[] ToByteArray(this Stream stream)
{
var ms = new MemoryStream();
var buffer = new byte[1024];
int bytes = 0;
while ((bytes = stream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, bytes);
return ms.ToArray();
}
}
public static class ByteExtensions
{
public static byte[] ToMp3(this byte[] bytes)
{
using (var outStream = new MemoryStream())
{
using (var ms = new MemoryStream(bytes))
{
using (var reader = new WaveFileReader(ms))
{
using (var writer = new LameMP3FileWriter(outStream, reader.WaveFormat, 64))
{
reader.CopyTo(writer);
return outStream.ToArray();
}
}
}
}
}
}
I've been poking around at this for most of the day and I feel like I'm introducing unnecessary complexity into something that seems like it should be fairly straightforward.
Any help would be much appreciated.
Note: I cannot change the source format and supporting IE is a requirement.
EDIT: I resolved the RIFF exception and am able to produce a stream of the MP3, but it's nothing but white noise. Hopefully I can resolve that as well. My new code is as follows:
[HttpGet]
public ActionResult GetMp3(string url)
{
if (String.IsNullOrWhiteSpace(url))
return null;
var muLawFormat = WaveFormat.CreateMuLawFormat(8000, 1);
var compressedStream = new MemoryStream();
using (var ms = new MemoryStream())
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
using (Stream webStream = request.GetResponse().GetResponseStream())
{
var buffer = new byte[4096];
int read;
while (webStream != null && (read = webStream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
}
ms.Position = 0;
using (WaveStream wav = WaveFormatConversionStream.CreatePcmStream(new RawSourceWaveStream(ms, muLawFormat)))
using (var mp3 = new LameMP3FileWriter(compressedStream, new WaveFormat(), LAMEPreset.MEDIUM_FAST))
wav.CopyTo(mp3);
}
compressedStream.Seek(0, 0);
return new FileStreamResult(compressedStream, "audio/mpeg");
}
This works for me (and I needed to do exactly what you wanted to do). Hope this helps someone else as well. I used NAudio with LAME.
You have to make sure that you copy the libmp3lamexx.dll files to your webserver's BIN location or to some folder in the %PATH% variable, else it won't work.
string sq = /* URL of WAV file (http://foo.com/blah.wav) */
Response.ContentType = "audio/mpeg";
using (WebClient wc = new WebClient())
{
if (!sq.ToLower().EndsWith(".wav"))
{
byte[] rawFile = wc.DownloadData(sq.Trim());
Response.OutputStream.Write(rawFile, 0, rawFile.Length);
}
else
{
using (var wavReader = new WaveFileReader(new MemoryStream(wc.DownloadData(sq.Trim()))))
{
try
{
using (var wavWriter = new LameMP3FileWriter(Response.OutputStream, wavReader.WaveFormat, LAMEPreset.ABR_128))
{
wavReader.CopyTo(wavWriter);
}
}
catch (ArgumentException)
{
var newFormat = new WaveFormat(wavReader.WaveFormat.SampleRate, 16, 2);
using (var pcmStream = new WaveFormatConversionStream(newFormat, wavReader))
{
using (var wavWriter = new LameMP3FileWriter(Response.OutputStream, pcmStream.WaveFormat, LAMEPreset.ABR_128))
{
pcmStream.CopyTo(wavWriter);
}
}
}
}
}
Response.Flush();
Response.End();
}