Concatenating SoundEffects (wav) and saving to isolated storage - c#

I am having an awful time trying to come up with a good solution to this problem of mine.
I have 3 sound effects that are marked as content on the phone, all the same bitrate.
sound1.wav
sound2.wav
sound3.wav
I want the user to be able to be able to select in any order, or however many times they like, the play order of the wav files, and then generate a new wav file based on their choices which can be stored and brought back from isolated storage.
So Thank You Walt Ritscher with the help so far, but as you will eventually see my coding skills are fragmented at best. What I'm trying to convey in the following code is that a user will be prompted to select any/all of the sounds with a tap event, and his selections will determine what the new soundeffect will sound like (it's order, etc) However there is still a lot I don't know about and here is the code I came up with (while not on my coding computer);
//SO I have this master list of sounds to use, indicated in this block of code:
//sound1.wav, sound2.wav, sound3.wav
// my wav files are marked as resources
// get the wav file from the DLL
var streamInfo1 = Application.GetResourceStream(
new Uri(sound1.wav, UriKind.Relative));
var streamInfo2 = Application.GetResourceStream(
new Uri(sound2.wav, UriKind.Relative));
var streamInfo3 = Application.GetResourceStream(
new Uri(sound3.wav, UriKind.Relative));
//With the above declarations made, I run each one as a stream as a variable.
var stream1 = streamInfo1.Stream as UnmanagedMemoryStream;
var stream2 = streamInfo2.Stream as UnmanagedMemoryStream;
var stream3 = streamInfo3.Stream as UnmanagedMemoryStream;
//The user can choose the sounds in any order, and repeat if desired.
//Let us assume the user chose in this order:
//sound1.wav + sound1.wav + sound2.wav +sound3.wav
for (int i = 0; i < 10; i++)
{
var bytesA = new byte[stream1.Length];
var bytesB = new byte[stream1.Length];
var bytesC = new byte[stream2.Length];
}
// read the bytes from the stream into the array
stream1.Read(bytesA, 0, (int)stream1.Length);
stream2.Read(bytesB, 0, (int)stream1.Length);
stream3.Read(bytesC, 0, (int)stream2.Length);
var combined = new byte[bytesA.Length + bytesA.Length + bytesB.Length] + bytesC.Length]];
// copy the bytes into the combined array
System.Buffer.BlockCopy(bytesA, 0, combined, 0, bytesA.Length);
System.Buffer.BlockCopy(bytesB, 0, combined, bytesA.Length, bytesB.Length);
var outputStream = new MemoryStream();
outputStream.Write(combined, 0, combined.Length);
// substitute your own sample rate
var effect = new SoundEffect(
buffer: outputStream.ToArray(),
sampleRate: 48000,
channels: AudioChannels.Mono);
var instance = effect.CreateInstance();
instance.Play();
// save stream to IsolatedStorage

Basically you have to get the bytes from the stream and combine into a new byte array. Then store that array into an UnmanagedMemoryStream.
// my wav files are marked as resources
// get the wav file from the DLL
var streamInfo1 = Application.GetResourceStream(
new Uri(loopWav, UriKind.Relative));
var streamInfo2 = Application.GetResourceStream(
new Uri(beatWav, UriKind.Relative));
var stream1 = streamInfo1.Stream as UnmanagedMemoryStream;
var stream2 = streamInfo2.Stream as UnmanagedMemoryStream;
var bytesA = new byte[stream1.Length];
var bytesB = new byte[stream2.Length];
// read the bytes from the stream into the array
stream1.Read(bytesA, 0, (int)stream1.Length);
stream2.Read(bytesB, 0, (int)stream2.Length);
var combined = new byte[bytesA.Length + bytesB.Length];
// copy the bytes into the combined array
System.Buffer.BlockCopy(bytesA, 0, combined, 0, bytesA.Length);
System.Buffer.BlockCopy(bytesB, 0, combined, bytesA.Length, bytesB.Length);
var outputStream = new MemoryStream();
outputStream.Write(combined, 0, combined.Length);
// substitute your own sample rate
var effect = new SoundEffect(
buffer: outputStream.ToArray(),
sampleRate: 48000,
channels: AudioChannels.Mono);
var instance = effect.CreateInstance();
instance.Play();
// save stream to IsolatedStorage
I've written more about WP7 audio for CoDe Magazine.
http://www.code-magazine.com/Article.aspx?quickid=1109071

Related

C# HttpClient Abruptly stops download

I have this C# Script that is supposed to download a zip archive from GitHub, unpack it and put it in a specific folder:
using (var client = new HttpClient())
{
var filePath = Path.GetFullPath("runtime");#"https://github.com/BlackBirdTV/tank/releases/latest/download/runtime.zip?raw=true";
ConsoleUtilities.UpdateProgress("Downloading Runtime...", 0);
var request = await client.GetStreamAsync(url);
var buffer = new byte[(int)bufferSize];
var totalBytesRead = 0;
int bytes = 0;
while ((bytes = await request.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
totalBytesRead += bytes;
ConsoleUtilities.UpdateProgress($"Downloading Runtime... ({totalBytesRead} of {bufferSize} bytes read) ", (int)(totalBytesRead / bufferSize * 100));
}
}
Decompress(buffer, filePath);
When I now run this, the download starts and it seems like it finishes, yet at a sporadic place it just stops. Somehow, It downloads the bytes as my Console shows, but they are zeroed out. It seems like either my computer receives zeros (which I doubt) or the bytes don't get written to the buffer.
Weirdly enough, downloading the file over the browser works just fine.
Any help is greatly appreciated
As I state in the comments, your problem is that each iteration of your while loop is overwriting your buffer, and you are not accumulating the data anywhere. So your last iteration doesn't completely fill the buffer and all you're left with is whatever data you got in the last iteration.
You could fix that bug by storing the accumulated buffer somewhere, but a far better solution is to not fuss with buffers and such and just use the built-in CopyToAsync method of Stream:
using var client = new HttpClient();
using var stream = await client.GetStreamAsync("https://github.com/BlackBirdTV/tank/releases/latest/download/runtime.zip?raw=true");
using var file = new FileStream(#"c:\temp\runtime.zip", FileMode.Create);
await stream.CopyToAsync(file);
Here I'm saving it to a local file at c:\temp\runtime.zip, but obviously change that to suit your needs. I suppose you're avoiding this method so you can track progress, which is fair enough. So if that's really important to you, read on for a fix to your original solution.
For completeness, here's your original code fixed up to work by writing the buffer to a FileStream:
var bufferSize = 1024 * 10;
var url = #"https://github.com/BlackBirdTV/tank/releases/latest/download/runtime.zip?raw=true";
using var client = new HttpClient();
using var stream = await client.GetStreamAsync(url);
using var file = new FileStream(#"c:\temp\runtime.zip", FileMode.Create);
var filePath = Path.GetFullPath("runtime");
var request = await client.GetStreamAsync(url);
var buffer = new byte[(int)bufferSize];
var totalBytesRead = 0;
int bytes = 0;
while ((bytes = await request.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
await file.WriteAsync(buffer, 0, bytes);
totalBytesRead += bytes;
ConsoleUtilities.UpdateProgress($"Downloading Runtime... ({totalBytesRead} of {bufferSize} bytes read) ", (int)(totalBytesRead / bufferSize * 100));
}

Buffer stream from microphone iterating results

What i would like to do is, constantly listen the microphone, have the microphone writer that writes into a stream, when the stream has x lenght, yield result, clear the stream and keep doing it it in a loop.
Then from the caller analyse all bytes received, i am a bit new to microphone recording, i don't event know if the code below register something, I am using NAudio library:
this is the caller:
var buffers = service.StreamHearing();
foreach (var buffer in buffers)
{
//analyse
}
and then is the actual service:
public IEnumerable<byte[]> StreamHearing()
{
var buffer = new byte[512];
using(var stream = new MemoryStream(buffer))
using(var writer = new WaveFileWriter(new IgnoreDisposeStream(stream), new WaveFormat(44100, 1)))
{
var recorder = new WaveInEvent
{
WaveFormat = new WaveFormat(44100, 1),
BufferMilliseconds = 100
};
recorder.StartRecording();
while (true)
{
yield return buffer;
buffer = new byte[512];
stream.SetLength(0);
}
}
}
this doesn't work, please give me a hand on how to do it.
thanks
You need to understand a couple of things which you miss.
First of all, WaveInEvent will write the data from the microphone asynchronously. Here is a correct snippet for writing the data from the microphone to some stream:
var buffer = new byte[512];
var recorder = new WaveInEvent
{
WaveFormat = new WaveFormat(44100, 1),
BufferMilliseconds = 100
};
var stream = new MemoryStream(buffer);
var writer = new WaveFileWriter(new IgnoreDisposeStream(stream), recorder.WaveFormat);
recorder.DataAvailable += (source, eventArgs) =>
{
var data = eventArgs.Buffer;
var bytesRead = eventArgs.BytesRecorded;
//here is a place where data from the microphone will be available
//you can add your processing right here in case you don't need to record and save the data
writer.Write(data, 0, bytesRead);
};
waveIn.StartRecording();
There are multiple different ways on how to proceed with the data you receive, but I don't know your main goal, so it is hard to say what is better way you choose the processing.

How use ImageSharp(Web) to compress / mutate stream with images(IFormFile)

I am trying to compress image(usually around 5-30) quality / size with ImageSharp.Web() library, and I cant really understand how can I do that or what I am missing here.
Can I reuse the same memory stream / IFormFile object to save the mutated image? Or do I need to create a new Image from current image object?
To work with a memory Stream do I also need to use specific JpegDecoder() ?
Not sure if this line is correct item.SaveAsJpeg(memoryStream);.
Maybe someone can help me out with the logic or any tips or tricks would be really helpful. Thanks!
Simple code example:
private byte[] ConvertImageToByteArray(IFormFile inputImage)
{
byte[] result = null;
// filestream
using (var fileStream = inputImage.OpenReadStream()) // IFormFile inputImage
// memory stream
using (var memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0; // The position needs to be reset.
var before = memoryStream.Length;
using (var item = Image.Load(memoryStream)) // do I need to use here JpegDecoder?
{
var beforeMutations = item.Size();
// dummy resize options
int width = 50;
int height = 100;
IResampler sampler = KnownResamplers.Lanczos3;
bool compand = true;
ResizeMode mode = ResizeMode.Stretch;
// init resize object
var resizeOptions = new ResizeOptions
{
Size = new Size(width, height),
Sampler = sampler,
Compand = compand,
Mode = mode
};
// mutate image
item.Mutate(x => x
.Resize(resizeOptions)
.Rotate(35));
var afterMutations = item.Size();
// try to save mutated image back to memory stream / overwrite
// this is not overwriting memory stream
item.SaveAsJpeg(memoryStream);
// prepare result to byte[]
result = memoryStream.ToArray();
}
var after = fileStream.Length; // kind of not needed.
}
I know this post is a bit old, and I'm sure that you have fixed your issue, but hopefully, it'll help some else out in the future.
Can I reuse the same memory stream / IFormFile object to save the mutated image? Or do I need to create a new Image from current image object?
You have two streams going on here, you can use one for the image from IFormFile and you can manipulate everything here, and then use your MemoryStream to save to
To work with a memory Stream do I also need to use specific JpegDecoder()?
You can save your image to the Memory Stream with .SaveAsJpeg(memoryStream)
Not sure if this line is correct item.SaveAsJpeg(memoryStream);.
You are on the right track
Maybe someone can help me out with the logic or any tips or tricks would be really helpful. Thanks!
Here is my rewrite based on what you have: Hope this helps. I'm sure there are some things that can be simplified, I tried to keep it close to OPs format
private byte[] ConvertImageToByteArray(IFormFile inputImage)
{
byte[] result = null;
// memory stream
using (var memoryStream = new MemoryStream())
// filestream
using (var image = Image.Load(inputImage.OpenReadStream())) // IFormFile inputImage
{
//var before = memoryStream.Length; Removed this, assuming you are using for debugging?
var beforeMutations = image.Size();
// dummy resize options
int width = 50;
int height = 100;
IResampler sampler = KnownResamplers.Lanczos3;
bool compand = true;
ResizeMode mode = ResizeMode.Stretch;
// init resize object
var resizeOptions = new ResizeOptions
{
Size = new Size(width, height),
Sampler = sampler,
Compand = compand,
Mode = mode
};
// mutate image
image.Mutate(x => x
.Resize(resizeOptions)
.Rotate(35));
var afterMutations = image.Size();
//Encode here for quality
var encoder = new JpegEncoder()
{
Quality = 30 //Use variable to set between 5-30 based on your requirements
};
//This saves to the memoryStream with encoder
image.Save(memoryStream, encoder);
memoryStream.Position = 0; // The position needs to be reset.
// prepare result to byte[]
result = memoryStream.ToArray();
var after = memoryStream.Length; // kind of not needed.
}
}

Decode LZMA2 file with .xz extension using 7zip SDK

I need to decode .xz file created with 7zip. Compression alghorithm is LZMA2. How should I use 7zip SDK for C# to solve this problem?
I already tried with this code, but it didn't work:
var coder = new SevenZip.Compression.LZMA.Decoder();
var input = new MemoryStream(); // filled with byte array of .xz file
var output = new MemoryStream();
// Read the decoder properties
byte[] properties = new byte[5];
zipFile.Read(properties, 0, 5);
// Read in the decompress file size.
byte[] fileLengthBytes = new byte[8];
zipFile.Read(fileLengthBytes, 0, 8);
long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
coder.SetDecoderProperties(properties); // this throws exception
coder.Code(zipFile, output, zipFile.Length, fileLength, null);
output.Flush();
output.Close();
I marked a line which causes exception.

Alternative way of using System.IO.File.OpenWrite() in Using(Stream fileStream = System.IO.File.OpenWrite("D:\sample.svg"))

using (Stream fileStream = System.IO.File.OpenWrite("D:\sample.svg"))
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = SvgStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, len);
}
}
I have this code for converting a PPT to SVG. SvgStream contains the PPT Slides. I don't want to store the converted SVG file on a physical path like D:\sample.svg. Is it possible to store it on an object that is not a physical path?
Is it possible to store it on an object that is not a physical path?
Sure. Create a MemoryStream instead of a FileStream if you want to stream into memory rather than the file system.
And while I'm here: you might want to use the CopyTo method rather than this tedious business of buffering it over one page at a time.
I saved it to a string:
SvgStream = new MemoryStream();
sld.WriteAsSvg(SvgStream);
SvgStream.Position = 0;
string[] svgArray = new string[MAX];
//instead of using(var fileStream = File.IO.......
//I came up with this:
var sr = new StreamReader(SvgStream);
svgArray[i] = sr.ReadToEnd();

Categories

Resources