I am trying to create a WAV object, from a WMA byte array, I've tried the following code, but the software exits, and in the dump file I found the following exception when I try to create fileReader
"The thread tried to read from or write to a virtual address for which it does not have the appropriate access."
System.IO.File.WriteAllBytes("wmatemp.wma", data);
WMAFileReader fileReader = new WMAFileReader("wmatemp.wma");
WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(fileReader);
WAV wav = new WAV(AudioMemStream(waveStream).ToArray());
I know I should not save the .wma to the HDD, but I don't know how to proceed differently, any help ?
Related
In my app I am recording voice using AudioRecorder as given in the following site, Audio Recorder it is working but it produce large size WAV file.
For example : If I record audio for 1 minute it takes 4MB to 5MB. So that I want to convert the wave file into MP3 file to reduce the size of the file. Please help me to compress the wav file ,give some example. Thanks in advance.
I never tried converting files before so i looked up on
some threads that might be helpful to you.
One is converting wav to mp3 which require file conversion into a byte[]
public byte[] ConvertToMp3(Uri uri)
{
using (var client = new WebClient())
{
var file = client.DownloadData(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();
}
}
}
however on this method he is using a third party service which downloads the
wav file and then to be called on that method but this does not guaranty if the file size will be reduced.
however i have check that you can compress wav files using a library called zlib.
just decompress it whenever u need it.
Please check the link below:
How to convert wav file to mp3 in memory?
Reducing WAV sound file size, without losing quality
I have a project. It is with Raspberry Pi Camera V2. One PC is used for encoding the captured video in MJPEG format and sending it with the serial port.
My PC is used for receiving the data, saving it in a .mjpeg formatted file and playing it with an MJPEG to MP4 converter.
I am trying to save the data in these lines:
byte[] data= new byte[100];
serialPort.Read(data,0,100);
BinaryWriter videoFile = new BinaryWriter(File.Open("video.mjpeg",FileMode.Create));
string dataAscii;
dataAscii = System.Text.Encoding.UTF8.GetString(data); //bytearray to string
videoFile.Write(dataAscii); // which is received
It works, it creates a .mjpeg file. However, I couldn't make it play with the converter. Maybe I should save the data frame by frame or try to save in a different way. I have no idea about what I am doing wrong.
Any ideas, many thanks!
Kane
Why are you converting the byte array into a string before writing it? That's your problem. Just write the byte array directly to the file stream.
I'm trying to convert a mp3 into a wave stream using NAudio. Unfortunately I receive the error Not a WAVE file - no RIFF header on the third line when creating the wave reader.
var mp3Reader = new Mp3FileReader(mp3FileLocation);
var pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
var waveReader = new WaveFileReader(pcmStream)
Shouldn't these streams work together properly? My goal is to combine several mp3s and wavs into a single stream for both playing and saving to disc( as a wav).
I'm going to preface this with a note that I've never used NAudio. Having said that, there's a guide to concatenating audio on their Github site.
Having looked at the API, you can't use Mp3FileReader directly as it doesn't implement ISampleProvider. However, you can use AudioFileReader instead.
Assuming you have an IEnumerable<string> (aka List or array) of the filenames you want to join named files:
var sampleList = new List<ISampleProvider>();
foreach(string file in files)
{
sampleList.add(new AudioFileReader(file));
}
WaveFileWriter.CreateWaveFile16("outfilenamegoeshere.wav", new ConcatenatingSampleProvider(sampleList));
you don't need the second and third lines. Mp3FileReader will convert to PCM for you and you can play it directly with a player like WaveOutEvent. To actually produce a WAV file on disk, pass it into WaveFileWriter.CreateWaveFile
using(var reader = new Mp3FileReader(mp3FileLocation))
{
WaveFileWriter.CreateWaveFile(reader);
}
in My application, i read .DSS format audio Files into Byte Array,with following code
byte[] bt = File.ReadAllBytes(Filepath);
but i am unable to get data into Byte's. but In the Audio player it is playing ,
here how can i read the files into Byte Array.
Here i am attaching Snap, what bt have, it show's 255 for all bytes.
TIA
To ensure this is not the issue with File.ReadAllBytes, try to read file using stream, like this:
using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int) fileStream.Length);
// use buffer;
}
UPDATE: as it's not working too, there should be issue with your file. Try to find any process that may be blocking and using it at the moment. Also, try to open the file with any HEX editor and see if there really any meaningful data present. I'd also create clean testing app/sandbox to test if it's working.
Well, the Dss format is copyrighted, and you'll likely not find a lot of information about it.
255 or 0xFF is commonly used in Dss files to indicate that a byte is not in use. You will see many of them in the header of the Dss file, later in the audio part they will be more sparse.
That means: a value of 255 in the region of bytes 83-97 which you show does NOT mean that something went wrong.
I am trying to output a text-to-speech wav file and play it with the HTML5 <audio> tag. The text-to-speech method is outputting the bytes, but the html5 control isn't playing it.
If instead of streaming the bytes directly to the control, i save it as a file first, then convert the file to bytes with filestream and output them, it starts to play, but i don't want to have to save the file every time. I'm using MVC 4.
// in a class library
public byte[] GenerateAudio(string randomText)
{
MemoryStream wavAudioStream = new MemoryStream();
SpeechSynthesizer speechEngine = new SpeechSynthesizer();
speechEngine.SetOutputToWaveStream(wavAudioStream);
speechEngine.Speak(randomText);
wavAudioStream.Flush();
Byte[] wavBytes = wavAudioStream.GetBuffer();
return wavBytes;
}
// in my controller
public ActionResult Listen()
{
return new FileContentResult(c.GenerateAudio(Session["RandomText"].ToString()), "audio/wav");
}
// in my view
<audio controls autoplay>
<source src="#Url.Content("~/Captcha/Listen")" type="audio/wav" />
Your browser does not support the <audio> element.
</audio>
I am also playing back a wav file to an audio element, and your code has the same logic as mine. I just noticed that you are flushing your stream before you return the byte array, which will seem to be empty.
Also, you can use file as return type and pass the byte array to its constructor. The content type is just the same as in your code. I would like to mention (maybe it could be a help also) that I used 2 streams: an outside scope stream and the actual stream where the data will be saved. After I have populated the actual stream I copied its contents to the outside stream using stream.CopyTo() and the instance of that outside stream is the one I used in my return statement. This avoids the error "Cannot Access Closed stream" (not the exact the error statement).