I have played with the Naudio examples, and am able to amplify a WAV file that is opened in using the "WaveFileStream" function. (Code Example: AudioPlaybackPanel) This works fine:
I add a variable declaration, so I can access the channel later:
SampleChannel waveFromFile;
And in the existing function, I set it:
private ISampleProvider CreateInputStream(string fileName)
{
...
this.fileWaveStream = plugin.CreateWaveStream(fileName);
var waveChannel = new SampleChannel(this.fileWaveStream, true);
waveFromFile = waveChannel;
...
}
Then I add an AMPLIFY button, and this works just as I expect:
float ampFactor = 1.0f;
private void ampButton_Click(object sender, EventArgs e)
{
ampFactor += 2;
if (ampFactor >= 9.0f)
ampFactor = 1.0f;
waveFromFile.Volume = ampFactor;
}
But how can I do this when the input is not a WAV file, but instead is a microphone?
If I am looking at the NAudio examples, and try to add this code to the "RecordingPanel" demo, and it is being ignored -- meaning I put the value in the Volume, but there is no change.
Is it possible to amplify the audio coming in from the microphone? And, if so, can someone show a sample code snippet? I've looked online, but I can't seem to find it.
To take advantage of SampleChannel's ability to modify samples, you'd actually need to pull the audio through the SampleChannel. To do that, you could put the recorded audio into a BufferedWaveProvider and then put that into SampleChannel. Then you'd need to make sure you pulled enough audio back out of the SampleChannel and into the WaveFileWriter so it doesn't fill up. You may also want to use a SampleToWaveProvider16 if you wanted a 16 bit WAV file.
Related
I'm making a game music player in C# based on NAudio. The original PCM-16 four-channel audio file (Wii AST) is brought into the application, and the channels are split into two separate stereo wav files. I have a slider in my application to allow you to fade between the two files, because each pair of channels from the original AST file contains a different part/version of the song.
I'm using two instances of WaveOut and AudioFileReader for the two wave files to play them back, and I have a trackbar in the application to adjust the "channel bias" (in other words, the volume of the WaveOut instances). My problem is that a lot of the time (especially when seeking using the trackbar), the two audio files end up playing out-of-sync. I can't seem to get them to play at the exact same time. Does anyone know how to get the files to play in sync?
The only solution I can think of is to convert the two split files into a single 4-channel wave file so you could just adjust the volume of the channel pairs and not have to worry about syncing, but NAudio doesn't seem to have a way to do that.
Please keep in mind that I am new to asking questions on StackOverflow, so if I need to provide more detail or if there was something I failed to explain, please let me know. And please, be constructive.
Thanks!
So, as it turns out, I completely overlooked a class in NAudio that mixes multiple audio streams together. What I ended up doing was creating an instance of MixingWaveProvider32 at the beginning of the program and initializing it when the audio files were created by the decoder to prepare for playback. Here was the final code I came up with that ended up working:
MixingWaveProvider32 mixingWaveProvider32;
WaveOut masterOut;
AudioFileReader audioFileReader;
AudioFileReader audioFileReader2;
...
void loadAstFile(string path)
{
string stream1_Path = path + "_c1.wav";
string stream2_Path = path + "_c2.wav";
DisposeAudioFileReaders();
AstReader astReader = new AstReader(path);
if(astReader.Channels == 4)
{
astReader.Export(stream1_Path, 1.0f);
astReader.Export(stream2_Path, 0.0f);
audioFileReader = new AudioFileReader(stream1_Path);
audioFileReader2 = new AudioFileReader(stream2_Path);
mixingWaveProvider32 = new MixingWaveProvider32(new IWaveProvider[] { audioFileReader, audioFileReader2 });
}
else
{
astReader.Export(stream1_Path, 1.0f);
audioFileReader = new AudioFileReader(stream1_Path);
audioFileReader.Volume = 1.0f;
mixingWaveProvider32 = new MixingWaveProvider32(new IWaveProvider[] { audioFileReader});
}
masterOut = new WaveOut();
masterOut.Init(mixingWaveProvider32);
masterOut.Volume = 1.0f;
RefreshComponents();
Play();
}
Hopefully this helps somebody out there!
I have the following constructor, which successfully plays pink noise directly to my audio output device:
public WAVEManager(string inputFileName, string outputFileName)
{
IWavePlayer outputDevice;
outputDevice = new WaveOutEvent();
SignalGenerator pinkNoiseGenerator = new SignalGenerator();
pinkNoiseGenerator.Type = SignalGeneratorType.Pink;
outputDevice.Init(pinkNoiseGenerator);
outputDevice.Play();
// Wait for 10 seconds
System.Threading.Thread.Sleep(10000);
}
This all works fine. I understand that now if I want to write to a .wav, I have to initialise a WaveFileWriter like so:
WaveFileWriter writer = new WaveFileWriter(outputFileName, pinkNoiseGenerator.WaveFormat);
And then write to the created WAVE file:
writer.WriteData(buffer, 0, numSamples);
The rub being that I have no idea how to populate the buffer directly from pinkNoiseGenerator. I have searched through the documentation and examples and can't find anything to do with this - I imagine it must involve the .Read() method of the SignalGenerator class, but as the generator plays indefinitely, it has no defined length. To me, this means that the buffer can't be populated in the same way it could if we were, say, reading directly from an input WAVE file (as far as I can tell).
Could someone please point me in the right direction?
Thanks.
Here's how you can create a WAV file containing 10 seconds of pink noise:
var pinkNoiseGenerator = new SignalGenerator();
pinkNoiseGenerator.Type = SignalGeneratorType.Pink;
WaveFileWriter.CreateWaveFile16("pinkNoise.wav", pinkNoiseGenerator.Take(TimeSpan.FromSeconds(10)));
I am recording soundbytes using nAudio and I would like to store these directly into variables inside an object instead of writing them to a file.
At the moment, I am recording the data as follows:
internal void start_recording(int device_number, string recording_file)
{
recording_file_name = recording_file;
mic_source_stream = new WaveIn();
mic_source_stream.DeviceNumber = device_number;
mic_source_stream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(device_number).Channels);
mic_source_stream.DataAvailable += new EventHandler<WaveInEventArgs>(mic_source_stream_DataAvailable);
wave_writer = new WaveFileWriter(recording_file, mic_source_stream.WaveFormat);
mic_source_stream.StartRecording();
}
internal void mic_source_stream_DataAvailable(object sender, WaveInEventArgs e)
{
if (wave_writer == null) return;
wave_writer.Write(e.Buffer, 0, e.BytesRecorded);
wave_writer.Flush();
}
This creates a *.wav file containing the recording.
Instead, I would like keep the audio data in variables, to keep all related recordings into a single object instead of multiple *.wav files in the file system, but nAudio seems to be geared towards recording directly to a file and playing from a file.
If there an easy way to record audio to a variable and play it back from that variable or should I go the silly but simple route of recording to a *.wav file, reading the file to a byte array, then writing the array back to a file before loading it for playback?
Recordings will be very small, so performance is not an issue, it's just that it's grating to write to disk only to read it right back in memory, twice.
I'd recommend simply writing the recorded audio to a MemoryStream. Then when you want to play back, use a RawSourceWavestream passing in the MemoryStream and the WaveFormat you are recording in. No need to involve the WAV file format at all.
WaveFileWriter has constructor which accepts arbitrary Stream - just pass MemoryStream there. Name of this writer is a bit confusing of course.
I'm trying to resample the WasapiLoopbackCapture's output from my soundcards 44100Hz, 16bit, 2 channel waveformat to a 16000Hz, 16bit, 1 channel format for later use in a System.Net.Sockets.NetworkStream (I want to write the converted bytes to the network stream)
But I have no idea how to start even!
I'm really new to signal processing and I've tried searching for tutorials but I just can't wrap my head around how to do this.
Here's what I got so far:
void StartRecording()
{
capture = new WasapiLoopbackCapture(device); // device is an audiodevice picked from the user. speaker, headphones etc
capture.ShareMode = NAudio.CoreAudioApi.AudioClientShareMode.Shared;
capture.DataAvailable += capture_DataAvailable;
capture.RecordingStopped += capture_RecordingStopped;
capture.StartRecording();
}
void capture_DataAvailable(object sender, WaveInEventArgs e)
{
outputStream.Write(e.Buffer, 0, e.BytesRecorded); // here I want to output audio to the NetworkStream.
// But I have to resample it first, which brings me to my question.
}
What I basically want to know is how I can get a byte-array which has been resampled and ready to be shipped off to the other side of the networkstream!
Any suggestions are really appreciated! Thank you in advance.
NAudio includes several different resamplers - one that uses ACM (WaveFormatConversionStream), one that uses Media Foundation (MediaFoundationResampler) and one written entirely in managed code (WdlResamplingSampleProvider). I discuss each of these in this post.
For your case, you want to do "input driven" resampling, where you know how many input samples you have and just want to pass them into the resampler. This can be a bit trickier than output driven resampling in NAudio. I've written another post about how to do input driven resampling using AcmStream. A similar technique can be used with the Media Foundation resampler transform or the WDL Resampling Provider but I'm afraid I don't have a code sample of that available yet.
My objective is this: to allow users of my .NET program to choose their own .wav files for sound effects. These effects may be played simultaneously. NAudio seemed like my best bet.
I decided to use WaveMixerStream32. One early challenge was that my users had .wav files of different formats, so to be able to mix them together with WaveMixerStream32, I needed to "normalize" them to a common format. I wasn't able to find a good example of this to follow so I suspect my problem is a result of my doing this part wrong.
My problem is that when some sounds are played, there are very noticeable "clicking" sounds at their end. I can reproduce this myself.
Also, my users have complained that sometimes, sounds aren't played at all, or are "scratchy" all the way through. I haven't been able to reproduce this in development but I have heard this for myself in our production environment.
I've played the user's wav files myself using Windows Media and VLC, so I know the files aren't corrupt. It must be a problem with how I'm using them with NAudio.
My NAudio version is v1.4.0.0.
Here's the code I used. To set up the mixer:
_mixer = new WaveMixerStream32 { AutoStop = false, };
_waveOutDevice = new WaveOut(WaveCallbackInfo.NewWindow())
{
DeviceNumber = -1,
DesiredLatency = 300,
NumberOfBuffers = 3,
};
_waveOutDevice.Init(_mixer);
_waveOutDevice.Play();
Surprisingly, if I set "NumberOfBuffers" to 2 here I found that sound quality was awful, with audible "ticks" occurring several times a second.
To initialize a sound file, I did this:
var sample = new AudioSample(fileName);
sample.Position = sample.Length; // To prevent the sample from playing right away
_mixer.AddInputStream(sample);
AudioSample is my class. Its constructor is responsible for the "normalization" of the wav file format. It looks like this:
private class AudioSample : WaveStream
{
private readonly WaveChannel32 _channelStream;
public AudioSample(string fileName)
{
MemoryStream memStream;
using (var fileStream = File.OpenRead(fileName))
{
memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}
WaveStream originalStream = new WaveFileReader(memStream);
var pcmStream = WaveFormatConversionStream.CreatePcmStream(originalStream);
var blockAlignReductionStream = new BlockAlignReductionStream(pcmStream);
var waveFormatConversionStream = new WaveFormatConversionStream(
new WaveFormat(44100, blockAlignReductionStream.WaveFormat.BitsPerSample, 2), blockAlignReductionStream);
var waveOffsetStream = new WaveOffsetStream(waveFormatConversionStream);
_channelStream = new WaveChannel32(waveOffsetStream);
}
Basically, the AudioSample delegates to its _channelStream object. To play an AudioSample, my code sets its "Position" to 0. This code that does this is marshalled onto the UI thread.
This almost works great. I can play multiple sounds simultaneously. Unfortunately the sound quality is bad as described above. Can anyone help me figure out why?
Some points in response to your question:
Yes, you have to have all inputs at the same sample rate before you feed them into a mixer. This is simply how digital audio works. The ACM sample rate conversion provided by WaveFormatConversion stream isn't brilliant (has no aliasing protection). What sample rates are your input files typically at?
You are passing every input through two WaveFormatConversionStreams. Only do this if it is absolutely necessary.
I'm surprised that you are getting bad sound with NumberOfBuffers=2, which is now the default in NAudio. Have you been pausing and resuming, because there was a bug where a buffer could get dropped (fixed in the latest and will be fixed for NAudio 1.4 final)
A click at the end of a file can just mean it doesn't end on a zero sample. You would have to add a fade out to eliminate this (a lot of media players do this automatically)
Whenever you are troubleshooting a bad sound issue, I always recommend using WaveFileWriter to convert your WaveStream into a WAV file (taking care not to produce a never ending file!), so you can listen to it in another media player. This allows you to quickly determine whether it is your audio processing that is causing the problem, or the playback itself.