Playing sound file from a live audio feed - c#

I am looking to make a sound visualizer with Unity3D (mainly for the UI and graphics), and as I get ready to export the project, I need to know how to get a live audio feed from whatever is playing on the computer and translate that to the audio samples that my program uses.
Currently, I use preset .mp3 files and apply them to audiosources, and a script updates the samples live and those translate to the bars you see in audio visualizers. I would like to move this up a step, but I don't know how to do that. I've looked into NAudio and CSCore, but I haven't been able to make anything work.
All I need is for NAudio or CSCore to record to a file, and also have me continuously play that file. That seems like it would have problems though, so if I can just get a sound card stream and use that data to pass through to the program, that would be great.
If anyone has any experience, docs, or tips that can help me out, please send them over. Appreciate the help, thank you.

Related

How can I save a webcam-recorded mp4 with audio in Visual C#?

I'm two days into researching this so I thought I'd post here. It looks like video recording software is usually written in C++ but my project requires C#. It's a WPF application and I just need it to save an mp4 locally. I don't need to upload to a server, I don't need to let the user pick a file location, I don't even need it to display the webcam feed as it's recording. All I need it to do is start/stop recording audio and video from a webcam/microphone and save it in a location that's determined by the code. I understand this may be broken down into steps like connecting to a webcam, connecting to a microphone, synchronizing them, encoding a video file, and saving it on the hard drive.
Are there any .NET classes that can help me with this? If not, can anyone recommend some libraries/frameworks/etc. that handle this sort of thing? I appreciate any help regarding how to do this. Thank you.

How to reproduce audio from a stream source

Thank you for your answer, I really appreciate it, and I have to share with you that it was a useful example, but in the following link is explained how to use it on Windows Phone 8.1 and how to create your own implementation of MSS.
http://video.ch9.ms/sessions/build/2014/2-528.pptx
What you’re doing is impossible.
Wave files aren’t media streams. You can’t dynamically change a .wav file and expect the MediaElement to pick those changes.
If you’re trying to play audio that you’re receiving from the network, or audio that you're generating dynamically from something else, then you need to play from your custom media stream source, not from a file.
The sample code is available here. The sample plays mp3; you’ll need to change it, wave audio is supported, with some limitations.

encoding mp3 or other music files

a bit of background:
this summer, i set myself a programming project,
where i want to make an E-jay clone(a program with simple drag-drop blocks which are little bits of music and beat to make a track)
now i dont really know where to start on the whole encoding music bit.
for example:
how does a sound file work?
how do i transform a piece of a sound file into a universal piece of sound information
how do i create a sound file from scratch
how do i add pieces of sound to a previously newly created sound file
im sorry if i seem like one of those guys that cant use google n stuff. but i have no experience with sound or anything on that part.
programming language: C#
ultimate goal: be able to encode a new mp3 file using small sound files which are inserted on certain points in the file to ultimatly make music
any help is appreciated,
thank you for reading my question.
I think you should check NAudio site - it has lots of examples.
Do some research into the MPEG compression standard, as you'll need to know about decoding as well as encoding. If you are going to be ambitious, maybe look into other audio file types as well.
MP3 Links:
MP3 Format
ISO Draft on the MPEG standard

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.

Getting microphone data (probably for c#)

i know there are many topics like mine, but i would realy love if somebody could explain me the theory behind the code.... what is the format of data you get from microphone?? Do same sounds return the same data from microphone or do you realy have to go through fft process if you want to compare sound samples.... Where can you save microphone data and analize it in live stream?
have a look at the open source .NET Voice Recorder project which uses NAudio. There is an article on Coding4Fun explaining how it works.

Categories