C# audio output without a stream or buffer - c#

I'm working on a program that I want to have some audio output. I would use System.Media.SoundPlayer, except that the data is generated dynamically and in real time. I really just want to set the speaker to a single byte value, and change that value when needed. Any buffers or streams would make this overly complicated.

You can't. System.Media.SoundPlayer works on streams, either dynamically generated like you want or generated from a file.
Learn to work with streams. They're not complicated. Certainly less complicated then dynamically generating audio.

Related

Splitting and Saving Mp4 In Unity/C#

So I want to make an application that will be able to split an mp4 into various segments and save them as separate videos, but I am having trouble with the approach to something like this. I can convert the mp4 into a byte array, but I am unable to properly trim or cut the bytes without the saved video being either corrupted or just not working. Is there a way to achieve this, either by using a byte array or by using another solution?
I know C# is not the best way to go about doing something like this, but it's my preferred coding language and I am trying to make an app only for myself, so I don't mind if the code is a bit messy or slow.
If you are trying to make an audio editor, it might be just better to use one like audacity (it is open source) or any other.

How to encode and decode a text file to binary in c#?

For my Unity Game, I want to save player data by using text files. Text files can be easily modified and so can the data in them be modified. So, I would like to convert the text files to 0's and 1's. So that when you open it you should see 0101011 instead of readable and editable data. I know that I can use Read Bytes of File and replace the text in the file with this data, but how in the world do I make it data again? I need help with that.
Text files can be easily modified and so can the data in them be modified.
Is this a problem? games are made to have fun, so why do you want to stop the user from gaming how he wants? If it is a competitive multiplayer game, or any type of game involving actual money, you should not rely on any data on the client.
So, I would like to convert the text files to 0's and 1's. So that when you open it you should see 0101011 instead of readable and editable data
Please do not do this, it will just reduce performance for no benefit. If you do not want trivial modification of data, just use a binary serialization format. Or use the Unity provided storage options. Using binary serialization will likely be faster and produce smaller files, at the cost of making modifications more difficult. But you do not seem concerned about the last point.
Converting objects to serialized data and back again is a common operation, and there are great libraries for it. Json is common for textual data, I have used Protobuf .net for binary data, and find it quite fast and easy to use.

How to convert stored MP3 file data (byte[]) into a float[] Unity can use as AudioSource-Data?

I did tons of research during the last days and nothing helped me out for my special problem. I am writing my own music engine for a Unity3d game and for this I created custom files containing the mp3 data and other information.
What I'm trying to do now is to take these split up mp3-byte-arrays (which can be played when I store them individually, I tested it - so the audio data seems to be fine) and convert them to Unity's AudioSource(s) somehow. I think converting the byte[] into a float[] containing the needed sample data of my audio would be enough, because audioClip.setData( ... ); should do the trick then (I hope).
But I continuously fail at decompressing and/or converting my raw mp3 buffer[] to anything like float[] - and even if I somehow succeed, the only thing I hear is nasty whitenoise-like nonsense.
Any ideas? I would love to hear from you and solve this problem!
Implementation of a decompression algorithm that will take a compressed MP3 stream and output it as uncompressed data is not a trivial task. You can either find a library that does this (I am not aware of any), or work around the issue in some way.
You can use untiy engine to decompress (aka play back) through an AudioSource, and sample it then, or execute some FFMPEG command line to get the wav. Most ways of doing it are cumbersome and ideally you should find a way of not doing it at all.
byte[] or float[] is not really a signifficant difference here - the difference is encoded vs raw

C# and Audio Generation/Playback

I’m making an audio synthesizer and I’m having issues figuring out what to use for audio playback. I’m using physics and math to calculate the source waveforms and then need to feed that waveform to something which can play it as sound. I need something that can 1) play the waveforms I calculate and 2) play multiple sounds simultaneously (like holding one key down on a piano while pressing other keys). I’ve done a fair bit of research into this and I can’t find something that does both of those things. As far as I know, I have 5 potential options:
DirectSound. It can take a waveform (a short[]) as a parameter and play it as sound, and can play multiple sounds simultaneously. But it won’t work with .NET 4.5.
System.Media.SoundPlayer. It works with .NET 4.5 and has better quality audio than Direct Sound, but it has to play sound from a .wav file and cannot play multiple sounds at once (nor can multiple instances of SoundPlayer). I ‘trick’ SoundPlayer into working by translating my waveform into .wav format in memory and then send SoundPlayer a MemoryStream of the in-memory .wav file. Could I potentially achieve control over the playback by altering the stream? I cannot append bytes to the stream (I tried) but I could potentially make the stream an arbitrary size and just re-write all the bytes in the stream with the next segment of audio data every time the end of the stream is reached.
System.Windows.Controls.MediaElement. I have not experimented with this yet, but from MSDNs documentation I don’t see a way to send it a waveform in memory without saving it to disk first and then reading it; I don’t think I can send it a stream.
System.Windows.Controls.MediaPlayer. I have not experimented with this either, but the documentation says it’s meant to be used as a companion to some kind of animation. I could potentially use this without doing any real (user-perceivable) animation to achieve my desired effect.
An open source solution. I’m hesitant to use an open source solution as I find they are typically poorly documented and not very maintainable, but I am open to ideas if there is one out there that is well documented and can do what I need.
Can anyone offer me any guidance on this or how to create flexible audio playback?
http://naudio.codeplex.com , without a doubt. Mark is a regular here on SO, the product is well alive, there are good code examples.
It works. We built some great stuff with it.

WAV file auto repeat in C#

I have a 10 second sound effect wave file. What I would like to do is take that file and repeat it n number of times and then save the longer WAV file to disk. This way I can create a much longer background effect rather than auto-repeat on the media player which is a bit stuttered between repeats. I am trying to do this in C#.
That's reasonably easy to do, given the WAV file format - assuming it's uncompressed audio (most WAV files are - or at least, they were last time I had anything to do with them).
There may well be audio APIs you can use to do this without getting stuck into the binary format, but I suspect they'd take as long to learn as just doing it yourself as you're not doing anything particularly complicated.
If you only need to do this with a small number of files, you might as well do it by hand with Audacity.
If you're using .Net 2.0 or higher then you can use the System.Media.SoundPlayer class and specifically its PlayLooping method to achieve what you want with no stuttering. This is preferable to creating a new wav file - it means less disk space for the file and less memory needed to render the sound. In general you should always use buffered techniques like this for audio playback if the sound will be looped or is longer than a few seconds.
You can do this easily in C# using the NAudio .NET audio library. You would need to use the WaveFileReader and WaveFileWriter classes. You can also use it to create a custom WaveStream that will loop your audio as many times as you want without the need to create a long WAV file.

Categories