Why is there no support for SetVolume, SetPan,...
I tried to implement that but I get E_NOINTERFACE HRESULT. So I tried to implement the IUnknown Interface but it does not work. Do you have any ideas?
Why is there no support for SetVolume, SetPan,...
I tried to implement that but I get E_NOINTERFACE HRESULT. So I tried to implement the IUnknown Interface but it does not work. Do you have any ideas?
Hi,
similar to this(http://naudio.codeplex.com/discussions/356641) problem, I'm looking for a clever solution to mixdown n files (mono) to a single file (stereo or mono doesn't matter).
I run into WaveMixerStream32 but I don't know how to handle it correctly.
Also I'm looking for a way to amplify/normalize the mixdown. Do some of you have any ideas how to do that in NAudio?
Thanks!
Hi Hfuy,
Thank you for the posting, is there any easy way to convert that InBuf to .wav file. If I have given the filename the inBuf should be saved to the filename.wav file. Please help me in this regard. Any sample code would be helpful.
Thanks in advance.
Sorry, I can't really help you - I used the data in other parts of my software, and never needed to send it to a file. Presumably there is a way to do this in naudio; I found a few promising hits with a simple google search:
http://www.google.co.uk/#hl=en&sa=X&ei=eh38T_eHJYWL8gOqlcmRBw&ved=0CEIQvwUoAQ&q=naudio+save+wav+file&spell=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=42bc37e02999c64c&biw=960&bih=569
Hello all,
I was recently looking at a piece of code Mark posted on stackoverflow regarding fade in/fade out.
The directions are, "Just call BeginFadeIn
or BeginFadeOut
with the appropriate fade duration."
So I tried to implement it like this:
// fade it out over 2 seconds var afr = new AudioFileReader(filename); var fader = new FadeInOutSampleProvider(afr); fader.BeginFadeOut(2000);
But I am not exactly sure what to do next, because there is more to it than this. I am not sure what to do with the Read function. My goal is to be able to pass in a file that has a static volume throughout, and get a new file that fades to zero over the last 2 seconds of the file.
Any pointers would be appreciated.
//class
public class FadeInOutSampleProvider : ISampleProvider { enum FadeState { Silence, FadingIn, FullVolume, FadingOut, } private readonly object lockObject = new object(); private readonly ISampleProvider source; private int fadeSamplePosition; private int fadeSampleCount; private FadeState fadeState; public FadeInOutSampleProvider(ISampleProvider source) { this.source = source; this.fadeState = FadeState.FullVolume; } public void BeginFadeIn(double fadeDurationInMilliseconds) { lock (lockObject) { fadeSamplePosition = 0; fadeSampleCount = (int)((fadeDurationInMilliseconds * source.WaveFormat.SampleRate) / 1000); fadeState = FadeState.FadingIn; } } public void BeginFadeOut(double fadeDurationInMilliseconds) { lock (lockObject) { fadeSamplePosition = 0; fadeSampleCount = (int)((fadeDurationInMilliseconds * source.WaveFormat.SampleRate) / 1000); fadeState = FadeState.FadingOut; } } public int Read(float[] buffer, int offset, int count) { int sourceSamplesRead = source.Read(buffer, offset, count); lock (lockObject) { if (fadeState == FadeState.FadingIn) { FadeIn(buffer, offset, sourceSamplesRead); } else if (fadeState == FadeState.FadingOut) { FadeOut(buffer, offset, sourceSamplesRead); } else if (fadeState == FadeState.Silence) { ClearBuffer(buffer, offset, count); } } return sourceSamplesRead; } private static void ClearBuffer(float[] buffer, int offset, int count) { for (int n = 0; n < count; n++) { buffer[n + offset] = 0; } } private void FadeOut(float[] buffer, int offset, int sourceSamplesRead) { int sample = 0; while (sample < sourceSamplesRead) { float multiplier = 1.0f - (fadeSamplePosition / (float)fadeSampleCount); for (int ch = 0; ch < source.WaveFormat.Channels; ch++) { buffer[offset + sample++] *= multiplier; } fadeSamplePosition++; if (fadeSamplePosition > fadeSampleCount) { fadeState = FadeState.Silence; // clear out the end ClearBuffer(buffer, sample + offset, sourceSamplesRead - sample); break; } } } private void FadeIn(float[] buffer, int offset, int sourceSamplesRead) { int sample = 0; while (sample < sourceSamplesRead) { float multiplier = (fadeSamplePosition / (float)fadeSampleCount); for (int ch = 0; ch < source.WaveFormat.Channels; ch++) { buffer[offset + sample++] *= multiplier; } fadeSamplePosition++; if (fadeSamplePosition > fadeSampleCount) { fadeState = FadeState.FullVolume; // no need to multiply any more break; } } } public WaveFormat WaveFormat { get { return source.WaveFormat; } } }
I figured it out almost immediately after posting:
var afr = new AudioFileReader(filename); var fader = new FadeInOutSampleProvider(afr); fader.BeginFadeOut(2000); var stwp = new NAudio.Wave.SampleProviders.SampleToWaveProvider(fader); WaveFileWriter.CreateWaveFile("faded"+filename, stwp);
private WaveFileWriter writer; private List<WaveFileReader> inputs = new List<WaveFileReader>(); ... ... private void button1_Click(object sender, RoutedEventArgs e) { foreach (string s in blah.audiofilenames) inputs.Add(new WaveFileReader(s)); var waveProvider = new MultiplexingWaveProvider(inputs, inputs.Count); writer = new WaveFileWriter(@"D:\temp\mixdown.wav", waveProvider.WaveFormat); int bytesRead = 0; int buffer_size = 4096; while (true) { byte[] bytes = new byte[buffer_size]; try { bytesRead = waveProvider.Read(bytes, 0, buffer_size); } catch (Exception ec) { // throw ec; } if (bytesRead == 0) { Console.WriteLine("end"); if (writer != null) { writer.Flush(); writer.Close(); writer.Dispose(); } break; } writer.Write(bytes, 0, buffer_size); } }
This is the code that solved my first problem. It is generating a multitrack wave file (8+ tracks in one file in my case). Of course you could put this in its own thread.
Did I use the FFT in the right way? But it was so slow that all the GUI stucked
half way. Nota that I put DoFFT() in the wi_DataAvailable() handler. And actually,
I only had 64 samples for FFT, but it was really slow.
I wonder if there is someone kind enough to provide me some usage tips and examples.
void DoFFT(Int32[] samples) { fft = new List<Complex>(); for (int i = 0; i < samples.Length; ++i ) { Complex c = new Complex(); c.X = sample_lst[i]; c.Y = 0; fft.Add(c); } AdjustFFT(); ffts = fft.ToArray(); FastFourierTransform.FFT(true, ffts.Length, ffts); } // Make FFT have power-of-2 elements.
// And apply windowing to samples
void AdjustFFT() { int i = 0; double c = 1; for (; c < fft.Count; ++i) { c = Math.Pow(2.0,i*1.0); } int d = (int)c; for (int k = fft.Count; k < d; ++k) { Complex p = new Complex(); p.X = 0; p.Y = 0; fft.Add(p); } for (int v = 0; v < fft.Count; ++v) { Complex g = new Complex(); g.X = (float)(fft[v].X * FastFourierTransform.HannWindow(v, fft.Count)); g.Y = (float)fft[v].Y; fft[v] = g; } }
for an explanation of FFT with NAudio please see this article
http://channel9.msdn.com/coding4fun/articles/AutotuneNET
Hello, I discover a problem today, I sending midi trough MIDI loopbe1 free apzz, a virtual wire. Have this error:
NAudio.MmException was unhandled
HResult=-2146233088
Message=NoDriver calling midiOutShortMsg
Source=NAudio
StackTrace:
at NAudio.MmException.Try(MmResult result, String function)
at NAudio.Midi.MidiOut.Send(Int32 message)
This happen only when I run my code, and then lock windows , after unlock MS windows when send MIDI NOTES, this error happen, but no if send CC, only with MIDI NOTES, I repeat not when send midi CC.
Hoppe can help to me Mark, 100% stuck on this, if window sis locked when my software is running then when unlock the software cash due that NAudio.MmException.
midiOut.Send(
MidiMessage.StartNote(1, 127, 1).RawData);
You ought to be able to play and record at the same time unless you are using a soundcard that doesn't support it (which is very rare but I have heard of hem). What driver callback mode are you using?
DirectSound was contributed to NAudio by someone else, and possibly they did not implement all the features.
you can either build the code yourself, or use Nuget to pick up a pre-release build
http://nuget.org/packages/naudio
Mark
Yes, is very clear is related to driver, I try closing and open again if system is locked, but have to surf deep on the windows api for detect when that happen.
Thank you!
hi,
I am grabbing voice from microphone using waveInEvent _DataAvailable. As the microphone is not high sensitivity (can't record voice from long distance), I'd like to amplify each grabbed byte in waveInEvent _DataAvailable, so the sound is going to be increased double to triple times,
so how can I do that using NAudio?
thanks
check out the calculator on this page
Hello,
I sucessfully am playing the same wav file over two sound cards. This is not the issue however as the same problem ocurrs with a single sound card.
I am playing back a wav file. When I close the program, an error message is thrown that says the WavOut device was not closed. Also, the file crackles after it finished playing.
There probably is a method lurking around in NAudio that I am not calling. I have a feeling I need to dispose of the WaveOut object after the files are done. I called the dispose method, but the program does not wait for the wav file finished playing.
How do you rectify this situation?
Thanks,
Ben (kingneb)
Is this possible to convert Existing Audio of type Mp3 to Wav , and Wma to Wav Format
markheath wrote:
You ought to be able to play and record at the same time unless you are using a soundcard that doesn't support it (which is very rare but I have heard of hem). What driver callback mode are you using?
Thank you for answering.
Meanwhile I solved the problem (but I have to study if I'm using best solution).
It seems there were some threading relative problems:
1- gui freezed just trying to acquire signal inside the main (gui) thread;
2- it isn't possible to acquire a signal in a separate thread just using default "new WaveIn()"
To solve 1. Now I'm using a thread for output and a thread for input (both started by gui thread).
To solve 2. To be able to acquire signal I used workaround described here: http://naudio.codeplex.com/workitem/16131
while (isRecording) {
Thread.Sleep(100);
System.Windows.Forms.Application.DoEvents();
}
When naudio next version is released, I'll try to use directly WaveInEvent.
PS: I'm using a PCM2902 based chip (I assembled) to work.